How to get object to rotate to face something

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
TheKingSparta
Prole
Posts: 2
Joined: Wed Feb 15, 2017 1:41 am

How to get object to rotate to face something

Post 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. :)
User avatar
ken.athomos
Citizen
Posts: 77
Joined: Fri Aug 05, 2016 10:13 am
Location: Philippines

Re: How to get object to rotate to face something

Post 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).
TheKingSparta
Prole
Posts: 2
Joined: Wed Feb 15, 2017 1:41 am

Re: How to get object to rotate to face something

Post by TheKingSparta »

Thank you very much. Your answer is exactly what I needed!! :)
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 5 guests