Jump to content
  • Advertisement
Sign in to follow this  
MadsGustaf

Calculating rotation angel

This topic is 3726 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am trying to make my object "point" towards the mouse, however i am having a little bt of trouble to do the calculation. From a previous project i did something like this: but it does not look right.
[source langg = "cpp"]
atan2(mouse_y- object.y, mouse_x- object.x);

object.x/y is the center coordinates for the (square) object. Thanks in adivce! -Mads

Share this post


Link to post
Share on other sites
Advertisement

double rotate = (Math.Atan2(mouseY-spriteY,mouseX-spriteX)/Math.PI)*180;

//if the sprite is above the mouse point (BASED ON SCREEN CO-ORDINATES, NOT CARTESIAN)
if (mouseY>spriteY)
rotate = 360 - rotate;

//the heading to face
face_direction(rotate);




First we calculate the angle to rotate using atan2 (important to use ATAN2 not ATAN).

Then we establish if the point is above or below us (if using screen co-ordinates then the y axis value is decreasing as we approach the top of the screen) hence if spriteY<mouseY (above the mouse point), in which case you subtract the result from 360 degrees.

Now the KEY thing to note here is that the sprite is always considered to be facing east (0 degrees), this allows you to get a perfect calculation (it's totally irrelevant that the sprite is facing 0 to start with as he always snaps to the correct mouse facing anyway - so don't get confused by this).

The FACT that you start at 0 and face accordingly either +clockwise or -counterclockwise.

Example result might be 315 degrees, so you rotate from 0, -45degrees to face the mouse.

Bingo! :)

Share this post


Link to post
Share on other sites
Hmm still not getting it right.. Im working solely with radians.

[source langg = "cpp"]

atan2(mouse_y- object.y, mouse_x- object.x);

if(mouse_y < object.y)
angle = (2*b2_pi) + angle;

object.angle = angle ;







hmm its not working..


Clickyyyyyyyy

The red dot would be the mouse pointer.

[Edited by - MadsGustaf on March 3, 2008 1:03:08 PM]

Share this post


Link to post
Share on other sites
If you're using atan2(), you shouldn't have to perform any additional calculations to get the correct angle (aside perhaps from modifying the results for use with a different coordinate system).

Share this post


Link to post
Share on other sites
Hmm you seem to be right. Currently i am getting the same result as the image i posted above, but that means i am back at the beginning with


atan2(mouse_y- object.y, mouse_x- object.x);


I do not quite see why it should be that different for another coordinate system. An angle is an angle. I am using SDL's coordinate system where (0,0) is located at top left of the screen and x increase going from left to right, and y increase by going from up to down.

Share this post


Link to post
Share on other sites
A vector A - B points from B to A.

Also,

Quote:

I do not quite see why it should be that different for another coordinate system. An angle is an angle. I am using SDL's coordinate system where (0,0) is located at top left of the screen and x increase going from left to right, and y increase by going from up to down.


Mathematical coordinates have y increasing upward instead of downward. That, in effect, flips the angle vertically: if you pass dy = 10, dx = 0 to the function, you'll get an upward angle (pi / 2 radians), when the desired direction is in fact downwards.

Once you have the angle value, what do you do with it?

Share this post


Link to post
Share on other sites
Quote:

if(mouse_y < object.y)
angle = (2*b2_pi) + angle;
??You would get angles > 360 here


If this is based on screen co-ordinates, then it's WRONG - I have already shown you how to calculate it properly.

Also I forgot to mention, treat the result as ABSOLUTE eg: rotate = Math.Abs(rotate). This is wise because you can get negative results if the mouse is at 0,0 and the point is "above it.. >0".


angle = atan2(mouse_y-object.y,mouse_x-object.x);

if(mouse_y > object.y)
angle = - ABSOLUTE(angle);
else
angle = ABSOLULTE(angle);


rotate_point_to(angle);





Learn to print out the results of the calculations and / or DEBUG and see WHY things are not correct. Atan2 is the correct way to do it.

[Edited by - Pickl3d on March 6, 2008 3:05:02 AM]

Share this post


Link to post
Share on other sites
I am still not getting it right.. i have drawn another image to try to explain what is happening.

The object seem to rotate the reverse direction than it is supposed to. For example, if i move the cursor in an arc of 90 degrees, the object will rotate -90 degrres, and will face towards the mouse at 45 degrees. Look at the picture for a clearer view.

The angle in the picture is the angle printed out in degrees. The red dot would be the mouse cursor. The square with the green cross is the object.

Click me!

Share this post


Link to post
Share on other sites
Those look to me like exactly the results of not correcting for the two things mentioned in my previous post.

Share this post


Link to post
Share on other sites
I understand the part about the y-axis being flipped. But not how you would solve it.. would you multiply dy by -1, to get the inverse? Sorry for being ignorant, but this is really something new to me. I did not quite get the vector part either. How would it have any use in this example?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!