Image Rotation to Movement Direction
Monkey Forums/Monkey Programming/Image Rotation to Movement Direction
| ||
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 |
| ||
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. |
| ||
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 |
| ||
DrawImage(img, x, y, -ang, 1, 1) the -angle is wrong. It should not be negative. |
| ||
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. |
| ||
You are just great! Thank you very very much! |
| ||
@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 |
| ||
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).DirectionWhere 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 |