Moving Gradually from one angle to another

BlitzMax Forums/BlitzMax Programming/Moving Gradually from one angle to another

I know there is bound to be something like this laying around, but how does one move gradually from one angle to another ?

Use a Float variable?

I found this code

Function curveangle#( currentangle#,desiredangle#,curve# )
	

	Adist#=	Sqr((DesiredAngle#-CurrentAngle#)*(DesiredAngle#-CurrentAngle#))
    If DesiredAngle>CurrentAngle 
		If Adist#>180 
			CurrentAngle=CurrentAngle-(Adist#/curve)
			If CurrentAngle<-180 Then CurrentAngle=CurrentAngle+360
		Else
			Adist#=360-Adist#
			CurrentAngle=CurrentAngle+(Adist#/curve)
			If CurrentAngle>180 Then CurrentAngle=CurrentAngle-360
		EndIf
		
    Else
		If ADist#>180 
			CurrentAngle=CurrentAngle+(Adist#/curve)
			If CurrentAngle>180 Then CurrentAngle=CurrentAngle-360
		Else
			Adist#=360-Adist#
			CurrentAngle=CurrentAngle-(Adist#/curve)
			If CurrentAngle<-180 Then CurrentAngle=CurrentAngle+360
		EndIf
    EndIf
			
	Return CurrentAngle
	
End Function


ok guys I need some help here

I want the rect to move gradually from one point to another, not at the speed of the mouse, this will also stop the rect from jumping from one side to the other if you quickly move across.

here is the code.

Strict

graphics 640,480

Local angle:Float = 180 
Local destangle:Float=0  
Local oldangle:Float=0

Local px:Float = 0
Local py:Float = 0
Local mx:Float = 0
Local my:Float = 0
Local dx:Float = 0
Local dy:Float = 0

While not KeyDown(KEY_ESCAPE)
    
mx = MouseX()
my = MouseY()

	dx = mx - 320
	dy = my - 240

    angle = ATan2(dx,dy)

	angle = WrapAngle(angle)
	
	DrawText angle,10,10
	
 	'Local crvangle = curveangle(oldangle,360,10)
	
	px = 320 + Sin(angle) * 100
	py = 240 + Cos(angle) * 100
	
	
	DrawRect px,py,10,10
		
		
	Flip
	Cls
Wend

Function WrapAngle:Float(angle:Float)
	If angle > 360 Then angle :- 360
	If angle < 0 Then angle :+ 360 
	Return angle
End Function

Function CurveValue:Float(current:Float,destination:Float,amount)

	current=current+((destination-current)/amount)
	If current<0.01 and current>-0.01 Then current=0
	Return current

End Function


Function curveangle:Float(currentangle:Float,desiredangle:Float,curve:Float )
	

	Local Adist:Float=Sqr((DesiredAngle-CurrentAngle)*(DesiredAngle-CurrentAngle))
    If DesiredAngle>CurrentAngle 
		If Adist>180 
			CurrentAngle=CurrentAngle-(Adist/curve)
			If CurrentAngle<-180 Then CurrentAngle=CurrentAngle+360
		Else
			Adist=360-Adist
			CurrentAngle=CurrentAngle+(Adist/curve)
			If CurrentAngle>180 Then CurrentAngle=CurrentAngle-360
		EndIf
		
    Else
		If ADist>180 
			CurrentAngle=CurrentAngle+(Adist/curve)
			If CurrentAngle>180 Then CurrentAngle=CurrentAngle-360
		Else
			Adist=360-Adist
			CurrentAngle=CurrentAngle-(Adist/curve)
			If CurrentAngle<-180 Then CurrentAngle=CurrentAngle+360
		EndIf
    EndIf
			
	Return CurrentAngle
	
End Function


Do you mean like this ?

Strict

Graphics 640,480

Local angle:Float = 180 
Local destangle:Float=0  
Local oldangle:Float=0

Local px:Float = 0
Local py:Float = 0
Local mx:Float = 0
Local my:Float = 0
Local dx:Float = 0
Local dy:Float = 0

Local Speed:Float = 0.5
Local rectangle:Float = 180


While Not KeyDown(KEY_ESCAPE)
    
mx = MouseX()
my = MouseY()

	dx = mx - 320
	dy = my - 240

    angle = ATan2(dx,dy)

	angle = WrapAngle(angle)
	
	If angle > rectangle Then 
		rectangle:+Speed
	ElseIf angle < rectangle
		rectangle:-Speed
	EndIf
	
	
	DrawText angle,10,10
	DrawText rectangle,10,30
	
 	'Local crvangle = curveangle(oldangle,360,10)
	
	px = 320 + Sin(rectangle) * 100
	py = 240 + Cos(rectangle) * 100
	
	
	DrawRect px,py,10,10
		
		
	Flip
	Cls
Wend

Function WrapAngle:Float(angle:Float)
	If angle > 360 Then angle :- 360
	If angle < 0 Then angle :+ 360 
	Return angle
End Function

Function CurveValue:Float(current:Float,destination:Float,amount)

	current=current+((destination-current)/amount)
	If current<0.01 And current>-0.01 Then current=0
	Return current

End Function


Function curveangle:Float(currentangle:Float,desiredangle:Float,curve:Float )
	

	Local Adist:Float=Sqr((DesiredAngle-CurrentAngle)*(DesiredAngle-CurrentAngle))
    If DesiredAngle>CurrentAngle 
		If Adist>180 
			CurrentAngle=CurrentAngle-(Adist/curve)
			If CurrentAngle<-180 Then CurrentAngle=CurrentAngle+360
		Else
			Adist=360-Adist
			CurrentAngle=CurrentAngle+(Adist/curve)
			If CurrentAngle>180 Then CurrentAngle=CurrentAngle-360
		EndIf
		
    Else
		If ADist>180 
			CurrentAngle=CurrentAngle+(Adist/curve)
			If CurrentAngle>180 Then CurrentAngle=CurrentAngle-360
		Else
			Adist=360-Adist
			CurrentAngle=CurrentAngle-(Adist/curve)
			If CurrentAngle<-180 Then CurrentAngle=CurrentAngle+360
		EndIf
    EndIf
			
	Return CurrentAngle
	
End Function



I have been Using this...
         Method LookAt(X:Float,Y:Float,Turnspeed:Float=.001)
 		Local Target:Float=ATan2(Y-Position.Y,X-Position.X) +180
		Local X1:Float = Sin(Angle)
		Local Y1:Float = Cos(Angle)
		Local X2:Float = Sin(Target)
		Local Y2:Float = Cos(Target)
		Local Current:Float =ATan2(X1-(X1-X2)*Turnspeed,Y1-(Y1-Y2)*Turnspeed)+180
		Local DX:Float = Target-Current
	 	If Abs(DX)>180 Then DX=DX-Sgn(DX)*360
		
	End Method 


I Don't know if it will suit your needs. It works with in a Type. I hope it helps.

This method is part of an "Ememy" Type and this code creates an angular offset which turns the enemy to face the X,Y that I specify. It will choose the closest angle to turn.

DX Contains the Direction to turn.

Regards,
Eric

Thanks for the code, Eric, it will be very useful in future

but right now, I want the rectangle to have a gardual smooth movement to the mouse pointer.

Kleptos is close, but sometimes bounces back

Is there anybody that knows how CurveAngle really works ? I am really battling to get this useable, I know the math gurus here could solve this.

Use something called Slerp for interpolation between two complex rotations.