Image Rotation to Movement Direction

Monkey Forums/Monkey Programming/Image Rotation to Movement Direction

xzess(Posted 2011) [#1]
Hi, i have a problem,
i want rotate my image to the Movement Direction


I found a Tutorial and tried the following:

Self.Direction = ((-ATan2 (Position.Y - Movement.Y, Position.X - Movement.X) + 360) Mod 360) + 90

Its the best what worked so far, you can see the result in the webapp, i updated it..

Http://www.xzess.org/Webgames/Exhale/

To be honest im not sure what i am doing, and thats not good ^^

My Ships have a Position Vector and a Movement Vector so the position Vec moves to the Movement Vec which you set with the PlanetDrag

The Ship frontside faces to the Top so the image has to be rotated


Jesse(Posted 2011) [#2]
this is the basic correct formula for pointing to the target direction:
 atan2(destinationy-positiony,destinationx-positionx)

this is assuming your ship image was made pointing to the right.
this formula will produce an angle between -180 and 180. you add 360 and mod it by 360 to make the angle between 0 and 360.
Note that if your image was made pointing up and since 0 degrees is to the right it will always be off by 90 degrees when displayed and that is the reason for adding 90 to the angle. if you don't want to have to add the 90 degrees then you need to "make" your unrotated images point to the right.
You don't need to add the 360 and mod it by 360, that was just done to keep the angle positive. Negative numbers are valid and work just fine.


Dima(Posted 2011) [#3]
You could store the direction in angle, and use speed to move.

	Field x#, y#, ang#, speed# = 1.0/60.0 * 32.0
	Method FollowMouse()
		Local vx# = MouseX() - x
		Local vy# = MouseY() - y
		ang = ATan2(vy, vx)		
		x += Cos(ang)*speed
		y += Sin(ang)*speed
	End
	Method Draw()
		' negative angle because Y axis is flipped
		DrawImage(img, x, y, -ang, 1, 1)	
	End




Jesse(Posted 2011) [#4]
		DrawImage(img, x, y, -ang, 1, 1)

the -angle is wrong. It should not be negative.


Dima(Posted 2011) [#5]
The Y axis has to be flipped, so if you're not happy with -angle while drawing, you could try flipping the axis in ATan2(-Y, X) and -Sin(ang) and use positive angle to draw like so:
	Field x#, y#, ang#, speed# = 1.0/60.0 * 32.0
	Method FollowMouse()
		Local vx# = MouseX() - x
		Local vy# = MouseY() - y
		ang = ATan2(-vy, vx)		
		x += Cos(ang)*speed
		y += -Sin(ang)*speed
	End
	Method Draw()
		DrawImage(img, x, y, ang, 1, 1)	
	End



In the end, both codes produce same results.


xzess(Posted 2011) [#6]
You are just great!
Thank you very very much!


Jesse(Posted 2011) [#7]
@Dima you are right, I didn't know the y-axis was inversed. I had other languages in my mind.

anyway the correct way of doing it is like this:
	Method Draw:Void()
		Translate(x,y)
		DrawImage(img, 0,0,-ang,1,1)
	End Method



Tibit(Posted 2011) [#8]
Self.Direction = ((-ATan2 (Position.Y - Movement.Y, Position.X - Movement.X) + 360) Mod 360) + 90

Nothing wrong with the advice above, but if you are already using Vectors, maybe this would be a simpler solution?
Self.Direction = Result.MakeBetween( Position, Movement).Direction
Where Result is a private Vector in that class/module used only for calculations to get optimal performance

* Vector Class
Get a well tested and easy to use Vector class

* More info on use of Vectors
How to use Vectors mini-guide