Error in implementation of rotate() in class fxVector2D
by Bo Bindestreck · in Torque Game Builder · 07/06/2005 (2:03 pm) · 5 replies
I think the expression
mX = mX * mCos(angle) - mY * mSin(angle);
should be
mX = mX * mCos(angle) + mY * mSin(angle);
Am I right?
mX = mX * mCos(angle) - mY * mSin(angle);
should be
mX = mX * mCos(angle) + mY * mSin(angle);
Am I right?
About the author
#2
fxVector2D v(10, 10);
v.rotate(90);
printf("%f, %f\n", v.mX, v.mY);
output:
-10.000000, -10,000000
You mean this result is what T2D internally expects in some way?
Looks more like a 180 degree rotation to me, regardless of axis choices. Or do I just still don't get it?
07/07/2005 (6:05 am)
Try:fxVector2D v(10, 10);
v.rotate(90);
printf("%f, %f\n", v.mX, v.mY);
output:
-10.000000, -10,000000
You mean this result is what T2D internally expects in some way?
Looks more like a 180 degree rotation to me, regardless of axis choices. Or do I just still don't get it?
#3
However, (I'm pretty sure) the correct version should actually be:
mX = mX * mCos(angle) - mY * mSin(angle);
mY = mX * mSin(angle) + mY * mCos(angle);
This is the same transformation as for a system where the +ve x-axis goes to the right and the +ve y-axis is upwards but in this case the angle would be counterclockwise!
To transform from one to the other you flip the y-axis and negate the angle but it turns out that this has zero net effect (I'll spare the maths).
It also appears that rotate() isn't actually used anywhere yet though (except maybe by Bo!)
Surely for a rotate function this makes no sense?
If I rotate something by zero degrees should it not remain unchanged?
Not knockin' ya Melv, just trying to understand. Keep up the excellent work ;)
Bri
07/07/2005 (8:34 am)
Bo is not alone in thinking that this is not right!However, (I'm pretty sure) the correct version should actually be:
mX = mX * mCos(angle) - mY * mSin(angle);
mY = mX * mSin(angle) + mY * mCos(angle);
This is the same transformation as for a system where the +ve x-axis goes to the right and the +ve y-axis is upwards but in this case the angle would be counterclockwise!
To transform from one to the other you flip the y-axis and negate the angle but it turns out that this has zero net effect (I'll spare the maths).
It also appears that rotate() isn't actually used anywhere yet though (except maybe by Bo!)
Quote:
This arrangement means that zero-degrees lies along the negative Y-axii
Surely for a rotate function this makes no sense?
If I rotate something by zero degrees should it not remain unchanged?
Not knockin' ya Melv, just trying to understand. Keep up the excellent work ;)
Bri
#4
inline fxVector2D& rotate(F32 angle) { angle = mDegToRad(angle); F32 tempX = mX; mX = mX * mCos(angle) - mY * mSin(angle); mY = tempX * mSin(angle) + mY * mCos(angle); return *this; };
Yeah, I had just got into work and didn't have the code in-front of me and more importantly I hadn't even had a coffee!! The reference to using the { mSin, -mCos } format is true for all the other functions, especially the "polar" ones, although not relevant to this rotational case as Brian quite correctly pointed-out.
I'll make sure I get some coffee before I hit the forums in the future!
Thanks for the heads-up; that one could of caused untold misery!
- Melv.
07/07/2005 (10:04 am)
Okay, I just got in from work and had a look at the code and it is indeed foo-bar'd. Brians code is the correct version so the function becomes...inline fxVector2D& rotate(F32 angle) { angle = mDegToRad(angle); F32 tempX = mX; mX = mX * mCos(angle) - mY * mSin(angle); mY = tempX * mSin(angle) + mY * mCos(angle); return *this; };
Yeah, I had just got into work and didn't have the code in-front of me and more importantly I hadn't even had a coffee!! The reference to using the { mSin, -mCos } format is true for all the other functions, especially the "polar" ones, although not relevant to this rotational case as Brian quite correctly pointed-out.
I'll make sure I get some coffee before I hit the forums in the future!
Thanks for the heads-up; that one could of caused untold misery!
- Melv.
#5
Bo
07/11/2005 (7:49 am)
Thanks Brian, now I'll also get the direction right when rotating, actually didn't matter the way I happened to use it.Bo
Associate Melv May
This arrangement means that zero-degrees lies along the negative Y-axii and the code that follows it rotates such that positive rotation is clockwise by using {-sin,+cos }.
Your version would put 0-deg along the positive Y-axii which, while not incorrect, isn't what T2D uses.
Hope this clears this up,
- Melv.