Object always facing the player?
BlitzMax Forums/BlitzMax Beginners Area/Object always facing the player?
| ||
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? |
| ||
You can use angel# = ATan2(y1-y2,x1-x2) SetRotation angel# 1 = Cordinates from Tank 2 = " from Player Tim |
| ||
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? |
| ||
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. |
| ||
Hey thanks for the example, Klepto! It helped and worked out perfectly! |
| ||
Hey, I can use that in my current project. Thanks for posting that code. |