Missile Movement using Sin Cos

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

Mortiis(Posted 2007) [#1]
Hey,

Can you give me an example of homing missile movement with sin/cos, I really don't have a clue about these, I want it to move smoothly not directly to target.

Thanks in advance!


ingenium(Posted 2007) [#2]
global angle# = 0
global inc# = 0.1
global x#,y#,z# = 0

[...]

If angle > 1 Then inc = -inc
angle# = angle# + inc#
x# = 0
y# = sin(angle)
z# = z# + 1
positionentity mesh,x,y,z



is it????


Stevie G(Posted 2007) [#3]
Sorry ingenium but the above is nonsense.

@ Mortiis, are you using 2d or 3d?


ingenium(Posted 2007) [#4]
Graphics3D 1024,768,32
SetBuffer BackBuffer()

Global cube = CreateCube()
PositionEntity cube,0,0,0
ScaleEntity cube,.1,.1,.1

Global cam = CreateCamera()
PositionEntity cam,10,0,-5
RotateEntity cam,0,0,0

Global light = CreateLight()
PositionEntity light,0,10,0

Global angle# = 0
Global inc# = .5
Global x#,y#= 0

While Not KeyDown(1)

If angle > 1 Then inc = -inc
angle = angle + inc
x = x + .1
y = Sin(angle) * 10
PositionEntity cube,x,y,45

RenderWorld()
UpdateWorld()
Text 0,0,"x: " +x
Text 0,10,"y: "+y
Text 0,20,"z: "+z
Flip
Wend
End





This works for me.


Mortiis(Posted 2007) [#5]
@Stevie G:It is 3d.Sorry I didn't specified.

@ingenium:Nice sine wave, thanks for trying to help bro, but it's not exactly what I asked for.Thanks.


GfK(Posted 2007) [#6]
You can use DeltaPitch and DeltaYaw between the missile and its target to determine whether it needs to turn left/right/up/down.

I'll leave you to work it out from there - its very easy once you know about those two commands.


splinux(Posted 2007) [#7]
Just look at this:
http://www.blitzbasic.com/Community/posts.php?topic=57317

It is just 2d, but it won't be difficult to make it 3d.


Stevie G(Posted 2007) [#8]
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



Mortiis(Posted 2007) [#9]
@Gfk:I already use them, but they go to the target instantly in a direct route.Not so realistic, at least not what I want, thanks.

@splinux:Thanks

@Stevie G:Exactly what I wanted! Thank you so much for the example and thinking about technical issues :) Thanks brother.


Buggy(Posted 2007) [#10]
Stevie G - Are those real words? Quaternions? Euler? Gimbal block?


Stevie G(Posted 2007) [#11]
Nah .. just made them up ;)


Sir Gak(Posted 2007) [#12]
@Buggy (Almost typoed "Buffy").

Check out link:
http://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles

And, he meant Gimbal lock (not block):

http://en.wikipedia.org/wiki/Gimbal_lock


Buggy(Posted 2007) [#13]
I'm not falling for that scheme. Anyone can edit Wikipedia. It's easy to see that you two are in cahoots.

But thanks.


Blitzplotter(Posted 2007) [#14]
Yeah, everyone knows limbal gock is the future, gimbal lock is just so last season.

How inaccurate is your missile going to be ? It'd be nice to see your missile wavering it's way to the target, certainly better than aimming staright for it. (;-)


Subirenihil(Posted 2007) [#15]
Quaternions, Euler, Gimbal Lock are all real terms and were used correctly.

Quaternions and Euler are two different ways of expressing 3D rotation. Pitch, yaw, and roll are the Euler form of expressing rotation. Quaternions are more complex but avoid the problem called Gimbal lock associated with the Euler system. Gimble lock is where, due to certain rotation, 2 of the axises become identical.

It's college level math stuff, don't worry if you don't understand. They are real terms.