OK, theres something wrong with my math here. (Rotation of x/y axis)

Started by
5 comments, last by alvaro 11 years, 4 months ago
I'm drawing two objects. A and B.

A is the base object and B is the dependent object.


I describe them this way because A and B are always drawn together and B is always at a certain slope to A. So wherever I draw A object B is , as an example, always up 3 and to the right 6 from wherever A is.

Now when I rotate the objects I want them to stay in the same relative position to eachother.

So the slope of B is calculated by

x' = x*cos(A) - y*sin(A)
y' = y*cos(A) + x*sin(A)

Where A is the angle I want to rotate it by. This is so I can rotate the objects and B will still be in the "correct place".

Somethings wrong.

I calculate the angle between the base object and the mouse(using atan2() so divide by zero not the problem). I take this angle and apply it to the slope of B described above. This allows the object to rotate as I move the mouse around and stay "in its right position".

It will stay in the right position until the angle nears 90 or 270 degrees. At these two points the two objects are pretty much on top of eachother/lined up (not the slope I originally wanted to preserve with the rotation). Then as I continue to move the mouse the object flips (like a mirrored reflection). I just want the slope to be preserved through the rotation. At 180 degrees the object should essentially be upside down. Not flipped/reflected.

Note* My Y axis is inverted. (0,1500) being the origin (bottom left). (1500, 0) being the top right.


I'm not on my home computer so I can't post the code right now, but I will later if needed.


Thanks guys.
Advertisement
I think it would be best if you could pick an example with concrete values for every variable, go through the computations and see where they go wrong. If you can't figure it out yourself that way, you can post the example with concrete values, describe what you are doing with them and then we are very likely to be able to help.

Also, when you say "slope" you must mean something else ("position"?).
When you say:

x' = x*cos(A) - y*sin(A)
y' = y*cos(A) + x*sin(A)

Does your code happen to 'actually' do:

x = x*cos(A) - y*sin(A)
y = y*cos(A) + x*sin(a)

aka, you update the value for x, then you are using the new value for x in the computation for y isntead of the old one?

When you say:

x' = x*cos(A) - y*sin(A)
y' = y*cos(A) + x*sin(A)

Does your code happen to 'actually' do:

x = x*cos(A) - y*sin(A)
y = y*cos(A) + x*sin(a)

aka, you update the value for x, then you are using the new value for x in the computation for y isntead of the old one?


No. I apply it to the original slope of Object B.

Like this:

glPushMatrix();
glTranslatef(BaseAX+(60.0*cos(AngleD)-10.0*sin(AngleD)),baseAY+(10.0*cos(AngleD)-60.0*sin(AngleD)),0.0f);
glBegin(GL_QUADS);
//Set color
glColor4f(blah);


Draw stuff


glEnd();
glPopMatrix();

Where 60 is the original "run" and 10 the original "rise". I changed the + to a - for the Y because my Y axis is inverted. Origin is (0,1500). I noticed that when it was a + the object rotated the opposite way as the mouse. When changed to a - it followed the mouse like its supposed to.

Image a gun with 3 parts rotating around the player. The gun base and two other parts of the gun A and B. A/B are at a slope to the base. They are always X over and Y up as an ex. Now if the gun rotates around the player you have to account for the rotation in the slope. Which led to the equations.

x = x*cos(A) - y*sin(A)
y = y*cos(A) + x*sin(a)

problem is that the slope isn't preserved like I want. These objects move on top of each other (Their x becomes 0) as I rotate ( at 90 degree intervals. So between o degrees and 90, 90-180, 180-270, 270-360) they flip in these intervals. In each one the parts all move to the center being on top of each other(having the same slope even though I use their own respective "rises"/"runs" when I translate using the equation) then they continue until they're flipped opposite if when they entered the 90 degree interval. Same as they go through each one. So at each axis the slope is right (but upside down at 180) but in between the go through this process of shifting to the opposite side, being on top of each other at 45 deg.

glTranslatef(BaseAX+(60.0*cos(AngleD)-10.0*sin(AngleD)),baseAY+(10.0*cos(AngleD)-60.0*sin(AngleD)),0.0f);


There is a sign error in there. It should be
glTranslatef(BaseAX+(60.0*cos(AngleD)-10.0*sin(AngleD)),baseAY+(10.0*cos(AngleD)+60.0*sin(AngleD)),0.0f);

[quote name='GDsnakes' timestamp='1354845113' post='5007960']
glTranslatef(BaseAX+(60.0*cos(AngleD)-10.0*sin(AngleD)),baseAY+(10.0*cos(AngleD)-60.0*sin(AngleD)),0.0f);


There is a sign error in there. It should be
glTranslatef(BaseAX+(60.0*cos(AngleD)-10.0*sin(AngleD)),baseAY+(10.0*cos(AngleD)+60.0*sin(AngleD)),0.0f);
[/quote]

Yes, as I said above
I changed the + to a - for the Y because my Y axis is inverted. Origin is (0,1500). I noticed that when it was a + the object rotated the opposite way as the mouse. When changed to a - it followed the mouse like its supposed to.

Do you think that is the problem?

Yes, as I said above
I changed the + to a - for the Y because my Y axis is inverted. Origin is (0,1500). I noticed that when it was a + the object rotated the opposite way as the mouse. When changed to a - it followed the mouse like its supposed to.

Do you think that is the problem?


Yes, that is a problem. What you obtain when you use the wrong sign is no longer a rotation. If your y axis is flipped, you may need to change the sign of the angle or, equivalently, change the sign of both sin(AngleD), but not just one of them.


Perhaps you would be less confused if you were to use the conventions from math (positive Y goes up, posotive X goes to the right) and then map your coordinates to pixels at the very end, when you have to make an API call that expects pixels.

This topic is closed to new replies.

Advertisement