[source langg = "cpp"]
atan2(mouse_y- object.y, mouse_x- object.x);
Calculating rotation angel
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.
object.x/y is the center coordinates for the (square) object.
Thanks in adivce!
-Mads
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 faceface_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! :)
Hmm still not getting it right.. Im working solely with radians.
hmm its not working..
Clickyyyyyyyy
The red dot would be the mouse pointer.
[Edited by - MadsGustaf on March 3, 2008 1:03:08 PM]
[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]
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).
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
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.
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.
A vector A - B points from B to A.
Also,
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?
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?
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]
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!
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!
Those look to me like exactly the results of not correcting for the two things mentioned in my previous post.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement