Page 1 of 1
How to get object to rotate to face something
Posted: Wed Feb 15, 2017 1:47 am
by TheKingSparta
I'm making a game and I want the character's weapons to rotate to face the mouse, like in a 2D shooter where the character aims wherever the player's mouse is. I have absolutely no idea how to accomplish this, and I'd be grateful if someone could help me out.
Re: How to get object to rotate to face something
Posted: Wed Feb 15, 2017 9:39 am
by ken.athomos
Currently in class so I might not be able to explain the thing I'm going to provide you.
Please see this:
https://love2d.org/wiki/Tutorial:Fire_Toward_Mouse
EDIT: Okay I'm home and from what I understood, you want something to face where the mouse is right?
First you need to get the position of the mouse using
love.mouse.getPosition.
After that, you need to solve for the angle of the object. To compute the angle, you need to use Lua's
math.atan2. The computation for the angle can be seen in the link above (go to the "Firing Bullets" section). If you've done stuff properly (or are at least confident that the angle is solved), all you have to do now is to use the computed angle in stuff like
love.graphics.draw (see the 4th parameter) or other similar stuff).
Re: How to get object to rotate to face something
Posted: Wed Feb 15, 2017 5:53 pm
by TheKingSparta
Thank you very much. Your answer is exactly what I needed!!