First of all Hi,
I am new in the community but have been using Chipmunk Physics for quite some time and I really like it.
I have kind of a math problem I guess (or a Trigonometry one) so here is the problem:
I got 2 bodies: a pineapple and a shuriken, upon collision of the two bodies they are merged into one the shuriken's body gets deleted and its shapes are built on the pineapples body.
So as you can see in the image below I store the last angle of the shuriken before it got deleted, and i have the angle of the pineapple which is constantly changes due to the simulation.
I want to know how can I rotate the shuriken by the amount that the pineapple is being rotated, to make it simpler think of it as if the shuriken is flying at the pineapple and when it hits it penetrates it and then rotates with the pineapple in sync.
Hope you guys can help me because I've looked at the documentation tried the rotation functions but couldn't get what I wanted.
so thanks in advance,
Anton.
A tricky rotation question

 Posts: 9
 Joined: Sun Jul 24, 2011 1:46 pm
 Contact:
 slembcke
 Site Admin
 Posts: 4166
 Joined: Tue Aug 14, 2007 7:13 pm
 Contact:
Re: A tricky rotation question
If you just want the relative angle between the two bodies you have two options: cyclic subtraction of the angles (annoying and confusing), or using rotation vectors (much easier).
Basically the cpBody.rot vector stores a rotation as a complex number. You don't really need to know what that means other than there are a number of neat properties of complex numbers that can be used to efficiently rotate 2D vectors. (Quaternions used for rotations in 3D are an extension of complex numbers) Basically though, you can think of a rotation vector as being a vector that points in the direction of the angle, and has a length of 1.0 (a unit vector).
Anyway! There are two functions for working with rotation vectors: cpvrotate() and cpvunrotate(). One adds two rotations, and the other subtracts them. So if you do this:
You could also calculate the absolute difference between the two rotations using a dot product and acos(), then use the cross product to figure out if it's a negative or positive rotation. So there are a number of ways you could approach this. I'd use the solution above though, it's the simplest.
Probably off topic:
In my own graphics engines I use the cpBody.rot vector directly instead of an angle in degrees or radians for rotating things. You can easily use the rotation vector to make a transformation matrix, and you don't have to fiddle around with trigonometry. http://chipmunkphysics.net/release/Chi ... #Transform That's not really an option though if you are using an existing sprite engines if they don't let you use transformation matrices. (Which is most of them, *sigh*.)
Basically the cpBody.rot vector stores a rotation as a complex number. You don't really need to know what that means other than there are a number of neat properties of complex numbers that can be used to efficiently rotate 2D vectors. (Quaternions used for rotations in 3D are an extension of complex numbers) Basically though, you can think of a rotation vector as being a vector that points in the direction of the angle, and has a length of 1.0 (a unit vector).
Anyway! There are two functions for working with rotation vectors: cpvrotate() and cpvunrotate(). One adds two rotations, and the other subtracts them. So if you do this:
Code: Select all
// (shuriken rotation  pineapple rotation) as a rotation vector
cpVect rotDelta = cpvunrotate(shirikenBody>rot, pineappleBody>rot);
// Get the angle of the rotation vector:
cpFloat angleDelta = cpvtoangle(rotDelta);
Probably off topic:
In my own graphics engines I use the cpBody.rot vector directly instead of an angle in degrees or radians for rotating things. You can easily use the rotation vector to make a transformation matrix, and you don't have to fiddle around with trigonometry. http://chipmunkphysics.net/release/Chi ... #Transform That's not really an option though if you are using an existing sprite engines if they don't let you use transformation matrices. (Which is most of them, *sigh*.)
Can't sleep... Chipmunks will eat me...
Check out our latest projects! > http://howlingmoonsoftware.com/wordpress/
Check out our latest projects! > http://howlingmoonsoftware.com/wordpress/

 Posts: 9
 Joined: Sun Jul 24, 2011 1:46 pm
 Contact:
Re: A tricky rotation question
Thanks for the quick reply and in depth answer.
Generally I can use a matrix but its not really needed for my use all I need is to get a correct angle for the shuriken to feed it into Marmalades floating matrix.
So again thanks for the answer, its just that I am new to programming and this is my first project, And there are some things that just give me headaches.
At last I made it! Yey.
For all the poor souls that math has its merciless grip on them I'll post my solution to the problem I had,It actually wasn't that hard just annoying and time consuming at least for me. but... learning is good for you, so here it is:
pineappleRot gets its first value at the collision handler between the shuriken and the pineapple same goes about the shurikenAngle.
essentially what happens in these lines is that we get the pineapples rot value and with unrotate it with its previous value the one before the update this gives us the delta of the pineapples rotation which then we convert into an angle in pineappleDeltaAngle and the we take the last angle that our shuriken had and subtract the delta angle of the pineapple from it.
Thanks again to slembcke for the insight on the matter.
Generally I can use a matrix but its not really needed for my use all I need is to get a correct angle for the shuriken to feed it into Marmalades floating matrix.
So again thanks for the answer, its just that I am new to programming and this is my first project, And there are some things that just give me headaches.
At last I made it! Yey.
For all the poor souls that math has its merciless grip on them I'll post my solution to the problem I had,It actually wasn't that hard just annoying and time consuming at least for me. but... learning is good for you, so here it is:
Code: Select all
pineappleDeltaRot = cpvunrotate(pineappleRot,pineappleBody>rot);
pineappleRot = pineappleBody>rot;
pineappleDeltaAngle = cpvtoangle(pineappleDeltaRot);
shurikenAngle = shurikenAngle  pineappleDeltaAngle;
essentially what happens in these lines is that we get the pineapples rot value and with unrotate it with its previous value the one before the update this gives us the delta of the pineapples rotation which then we convert into an angle in pineappleDeltaAngle and the we take the last angle that our shuriken had and subtract the delta angle of the pineapple from it.
Thanks again to slembcke for the insight on the matter.
Who is online
Users browsing this forum: No registered users and 5 guests