cpvrotate explained ?

Official forum for the Chipmunk2D Physics Library.
Post Reply
The Oddler
Posts: 6
Joined: Tue Sep 13, 2011 12:05 pm
Contact:

cpvrotate explained ?

Post 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
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: cpvrotate explained ?

Post 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.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
The Oddler
Posts: 6
Joined: Tue Sep 13, 2011 12:05 pm
Contact:

Re: cpvrotate explained ?

Post 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 ?
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: cpvrotate explained ?

Post 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?
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
The Oddler
Posts: 6
Joined: Tue Sep 13, 2011 12:05 pm
Contact:

Re: cpvrotate explained ?

Post 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 !
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: cpvrotate explained ?

Post by slembcke »

Neither vector is modified, it returns a new one.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
The Oddler
Posts: 6
Joined: Tue Sep 13, 2011 12:05 pm
Contact:

Re: cpvrotate explained ?

Post 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." ?
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: cpvrotate explained ?

Post 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.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Post Reply

Who is online

Users browsing this forum: No registered users and 10 guests