Make an object face the mouse cursor?
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Make an object face the mouse cursor?
I'm new to using love2d and I was confused on how to go about making a player face towards the mouse. I don't need exact code, I would just like to be led into the direction I should be headed to make this happen. Thanks
- zorg
- Party member
- Posts: 3465
- Joined: Thu Dec 13, 2012 2:55 pm
- Location: Absurdistan, Hungary
- Contact:
Re: Make an object face the mouse cursor?
Also not strictly specific to löve either; it's just math.
Me and my stuff True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
- NotARaptor
- Citizen
- Posts: 59
- Joined: Thu Feb 22, 2018 3:15 pm
Re: Make an object face the mouse cursor?
"math.atan2" is indeed the right answer
But to be more specific, but still with zero code -
Simplify and generalise. You have an object at point A which you want to rotate to face point B. It doesn't matter if B is the mouse position, the centre of the screen, the end effector of a robot arm, or the centroid of a complex polygon - it's just a point.
If we assume that the object (player) initially faces right (+X), and rotates anticlockwise from there, it's a really simple trig problem.
Simply apply SOHCAHTOA. The tangent of α is the opposite divided by the adjacent, or (yB-yA)/(xB-xA). So α=atan((yB-yA)/(xB-xA))
This only deals with one quadrant though. The point B can be in the top-right (where it is) but also top-left, bottom-left and bottom-right. The arctangent only returns value between -pi/2 and pi/2, so clearly we don't have the full picture. Another "gotcha" is the division - if A is directly above or below B, (xB-xA) will be zero, so the division won't be possible to do.
It might be worth working out each of these special cases and making your own function to handle them, just to understand how it works. However you don't need to - virtually all programming languages these days have atan2 built in, which handles them all for you.
α=atan2(yB-yA,xB-xA)
But to be more specific, but still with zero code -
The "player" isn't special. The "mouse" isn't special. If you think about it in those terms you'll be looking for a "PlayerLookAtMouseComponent" on the Unity store.I was confused on how to go about making a player face towards the mouse
Simplify and generalise. You have an object at point A which you want to rotate to face point B. It doesn't matter if B is the mouse position, the centre of the screen, the end effector of a robot arm, or the centroid of a complex polygon - it's just a point.
If we assume that the object (player) initially faces right (+X), and rotates anticlockwise from there, it's a really simple trig problem.
Simply apply SOHCAHTOA. The tangent of α is the opposite divided by the adjacent, or (yB-yA)/(xB-xA). So α=atan((yB-yA)/(xB-xA))
This only deals with one quadrant though. The point B can be in the top-right (where it is) but also top-left, bottom-left and bottom-right. The arctangent only returns value between -pi/2 and pi/2, so clearly we don't have the full picture. Another "gotcha" is the division - if A is directly above or below B, (xB-xA) will be zero, so the division won't be possible to do.
It might be worth working out each of these special cases and making your own function to handle them, just to understand how it works. However you don't need to - virtually all programming languages these days have atan2 built in, which handles them all for you.
α=atan2(yB-yA,xB-xA)
Re: Make an object face the mouse cursor?
Worth noting that that formula applies when the sprite is drawn with the player pointing right, which is the value for 0°. If it is not, you may need to subtract an offset from the angle, to match the sprite. Remember angle is returned in radians.
- NotARaptor
- Citizen
- Posts: 59
- Joined: Thu Feb 22, 2018 3:15 pm
Re: Make an object face the mouse cursor?
Good point on the radians there, that might not be obvious to everyone.
Another thing I didn't actually mention is that I'm assuming the position of A (the player) is also the centre of rotation for it - if you draw the player at that position without an offset, it would naturally rotate around the upper-left corner rather than the centre, which is probably what you want.
To get a little bit more code-y this time, if we assume you're rendering the player with love.graphics.draw(), and the player sprite (image, quad, whatever) is PLAYER_WIDTH pixels wide and PLAYER_HEIGHT pixels tall, and you want it to rotate and be placed about its centre, you should call it like this:
Another thing I didn't actually mention is that I'm assuming the position of A (the player) is also the centre of rotation for it - if you draw the player at that position without an offset, it would naturally rotate around the upper-left corner rather than the centre, which is probably what you want.
To get a little bit more code-y this time, if we assume you're rendering the player with love.graphics.draw(), and the player sprite (image, quad, whatever) is PLAYER_WIDTH pixels wide and PLAYER_HEIGHT pixels tall, and you want it to rotate and be placed about its centre, you should call it like this:
Code: Select all
love.graphics.draw(PLAYER_SPRITE, x, y, alpha, 1, 1, PLAYER_WIDTH/2, PLAYER_HEIGHT/2
Re: Make an object face the mouse cursor?
Thanks guys! Just got it to work!
Who is online
Users browsing this forum: Semrush [Bot] and 0 guests