Page 1 of 1

cpvrotate explained ?

Posted: Tue Sep 13, 2011 2:46 pm
by The Oddler
Hi,

In the documentation I found the following operation:
cpVect cpvrotate(const cpVect v1, const cpVect v2) – Uses complex multiplication to rotate v1 by v2. Scaling will occur if v1 is not a unit vector.

I'm not entirely sure how to rotate a vector by another vector but, if I understand correctly, to rotate it by an angle I have to use cpvforangle(const cpFloat a) first to make a vector of my angle.
Now, the last part says v1 will be scaled if it's length is not one (1). So to just rotate v1 without scaling I first have to calculate it's length, then use cpvrotate and then multiply the vector again with the length ?

Am I correct in this, and is there an easier way ? :P

Thanks !
-The Oddler

Re: cpvrotate explained ?

Posted: Tue Sep 13, 2011 3:39 pm
by slembcke
It's basically the 2D equivalent of quaternions, only much simpler simpler. (both are spinors)

Think of zero degrees as (1,0). Whatever the angle between your rotation vector and (1, 0) will be the rotation applied to v1. cpvforangle() gives you exactly that:
cpvforangle(0) -> (1,0)
cpvforangle(M_PI/2) -> (0, 1)
cpvforangle(M_PI) -> (-1,0)
etc...

So to rotate a vector by angle radians you would do this:
cpvrotate(vector, cpvforangle(angle));

If you wanted to rotate a big list of vectors you could do this:

Code: Select all

cpVect rot = cpvforangle(angle);
for(i=0; i<A_LOT; i++){
  cpVect rotated = cpvrotate(list[i], rot);
}
This is good because you only need to do the trig calculations once instead of a bajillion times. Chipmunk exploits this internally all over the place.

Re: cpvrotate explained ?

Posted: Tue Sep 13, 2011 3:42 pm
by The Oddler
Aha, shmart ! :D I get it now :P I knew about vector*matrix stuff, and this is similar :P
Thanks !

Though what about the length ?

Re: cpvrotate explained ?

Posted: Tue Sep 13, 2011 5:19 pm
by slembcke
Oh right. So with complex multiplication you are adding the angles and multiplying their lengths.

So if you have a vector of length 4 at 35 degrees and a vector of length 0.5 at -15 degrees, you would end up with a vector of length 2 at 20 degrees.

In the normal case, you really only want the rotation part so you'd give it a vector that has a length of 1. That way you get the rotation, but the length stays the same. Make sense?

Re: cpvrotate explained ?

Posted: Tue Sep 13, 2011 6:45 pm
by The Oddler
Yes, I get it I think, but then v2's length would be changed to 1 right, not v1's length. Right ?

Thanks !

Re: cpvrotate explained ?

Posted: Tue Sep 13, 2011 8:08 pm
by slembcke
Neither vector is modified, it returns a new one.

Re: cpvrotate explained ?

Posted: Wed Sep 14, 2011 4:35 am
by The Oddler
Yes, but the new one, will have a length equal to that of v1 right ? I got a little confused by the last part "Scaling will occur if v1 is not a unit vector." Shouldn't that be "Scaling will occur if v2 is not a unit vector." ?

Re: cpvrotate explained ?

Posted: Wed Sep 14, 2011 9:24 am
by slembcke
No not exactly, it will have a length equal to the length of v1 * the length of v2. When you use a rotation vector with a length of 1, you get 1.0*(length of other vector) thus preserving the length of the vector you wanted to rotate.