Object always facing the player?

BlitzMax Forums/BlitzMax Beginners Area/Object always facing the player?

Neuro(Posted 2005) [#1]
This was easily done in Blitz3d, but does anyone have an example of how to make an object (tank, turret....etc) always face you where ever you move around in the screen in 2d with BlitzMax?


Gehhilfe(Posted 2005) [#2]
You can use

angel# = ATan2(y1-y2,x1-x2)
SetRotation angel#

1 = Cordinates from Tank
2 = " from Player

Tim


Neuro(Posted 2005) [#3]
Ok, thanks it sorta works. However, I notice the angle never increased over 180 degrees as apposed to 360.

So now that it sorta faces the player,how would I move the object towards the player after turning towards them?


klepto2(Posted 2005) [#4]
Graphics 800,600,0,-1
Cls

DrawRect 0,0,50,10

Flip

Tank:TImage = CreateImage(50,10,1)
GrabImage(Tank,0,0)
MidHandleImage Tank


Global TX#,TY#
Global PX,PY
Global SpeedX#
Global SpeedY#
Global TSpeed# = 1.5

TX = 200
Ty = 200

While Not KeyHit(Key_Escape)
Cls

PX = MouseX()
PY = MouseY()


Local angle:Int = ATan2(TY-PY,TX-PX)

SpeedX = Cos(angle)*TSpeed 
SpeedY = Sin(Angle)*-TSpeed
TX:-SpeedX
TY:+SpeedY


SetRotation Angle
DrawImage Tank,Tx,Ty
SetRotation 0

Flip
Wend


I hope this will help you.


Neuro(Posted 2005) [#5]
Hey thanks for the example, Klepto! It helped and worked out perfectly!


wildboar(Posted 2005) [#6]
Hey, I can use that in my current project. Thanks for posting that code.