Missile Movement using Sin Cos

Blitz3D Forums/Blitz3D Beginners Area/Missile Movement using Sin Cos

If fully 3d best to use aligntovector as this uses quarternions internally and avoids the singularities of using euler which can result in gimbal lock. A quick example below.

Hit space to position target in a random place. Change the last param of aligntovector to effect the rate of turning.

Graphics3D 640,480,16,1

Global CAMERA = CreateCamera()
Global TARGET = CreateCube() : ScaleEntity TARGET, 2,2,2 : EntityColor TARGET, 255,0,0
Global MISSILE = CreateCone() : RotateMesh MISSILE, 90,0,0 : FitMesh MISSILE, -1,-1,0,2,2,6
FLAME = CreateCone( 8 , False, MISSILE ) : RotateMesh FLAME, -90,0,0 : FitMesh FLAME,-1,-1,-4,2,2,4 : EntityColor FLAME, 255,128,0

;init target
PositionEntity TARGET, Rand(-50,50 ), Rand( -50,50 ) , Rand( 50,100 )


While Not KeyDown(1)


	;target reached or spawn new target position
	If KeyDown( 57 ) Or ( EntityDistance( MISSILE , TARGET ) < 5 )
		PositionEntity TARGET, Rand(-50,50 ), Rand( -50,50 ) , Rand( 50,100 )
	EndIf
	
	;get vector to target
	Dx# = EntityX( TARGET ) - EntityX( Missile )
	Dy# = EntityY( TARGET ) - EntityY( Missile )
	Dz# = EntityZ( TARGET ) - EntityZ( Missile )
	;align missle to target vector
	AlignToVector MISSILE, Dx, Dy, Dz, 3, .05 
	;move missile forward
	MoveEntity MISSILE, 0,0,1
	;animate flame
	ScaleEntity FLAME, 1,1,Rnd( .5, 5 ), 1
	
	RenderWorld()
	Flip
	
Wend

End