How make the object.rotation be locked on to the mouse position

So my problem is that I cant find anything related about this on solar2d, and I’m new here. What I want to do is make the player rotation to be locked on where the mouse position is, like this:
Untitled-1
Can anybody help me and explain how can I do that?
Thanks!

Is that what you’re looking for?

not really i want the object only to rotate in the direction of the mouse position not to move to the mouse position

look something like this that is made in unity, I want to make it in solar2d:

This seems to be computing the angle of the straight line formed between the centre of the display object and the location of the mouse pointer.

This can be easily calculated by atan2 function in the math library when you know coordinates of both the display object and the mouse pointer.

Following that, it’s just a matter of setting the rotation of your display object to that angle and optionally adding a constant offset to it as desired.

yea i did something but doesnt really work the whay I want it to it follows the mouse from the right side, how can i fix it:

function rotationMove(event)
            local angle = math.atan2(event.y-player.y,event.x - player.x);
            player.rotation = angle * 180/ math.pi
            print(player.rotation)
end
Runtime:addEventListener("mouse", rotationMove)

Should be something minor as in theory this code shoudl work. I’d imagine the range in which the atan2 is returning the angle {-pi,pi} is causing the issue in getting the correct quadrant. You should try printing that angle and see if it might need further processing before you assert it on the display object.

Getting an unsigned angle in degrees in the 0-360 range will probably fix it

so im looking at the console if mouse in
right direction , angle is 0
left = -180
up = -90
down = 90

and like if mouse is on 0(right) then the player angle is -90(up)

nevermind i fixed, i just added +90 at the end and it now works great

player.rotation = (angle * (180/ math.pi)+90)

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.