
It is currently Sun Feb 25, 2018 8:46 pm





Author 
Message 
codeslasher

Post subject: Rotating about an axis Posted: Sun Feb 01, 2009 1:03 pm 
Joined: Sat Jun 23, 2007 7:56 pm Posts: 145

Hi Guys,
Am working on a game and I need to clarify my thoughts.
I have a ball which I want to make rotate (roll) around its local X axis while it moves forward in world space. Basically, make is look like a normal ball would behave.
If I have a forward axis of (0, 0, 1) and x axis of (1,0,0), everything works fine.
However, once I rotate the ball's forward vector around the y axis (0, 1, 0), the ball stops rotating in the forwards direction. (it look like its rotating sideways)
I think I need to apply the X axis rotation in local space before applying the world's rotation to its forward vector.
How can I do this in opengl?
This is what I have 
I think I have to do the following
1. get the ball's forward vector
2. cross forward vector with the ball's up vector to get the right vector
3. use glRotate (rollAngle, rightVector.x, rightVector.y, rightVector.z)
4. then apply the normal Transform Node transformations
 translate to final world positon
 offset by center
apply world rotation about fixed axis
glRotatef(m_rotation.m_x, 1.0f, 0.0f, 0.0f);
glRotatef(m_rotation.m_y, 0.0f, 1.0f, 0.0f);
glRotatef(m_rotation.m_z, 0.0f, 0.0f, 1.0f);
scale
undo offset to center
Is this the correct way to do something like this?





Marek

Post subject: Posted: Mon Feb 02, 2009 7:42 am 
Joined: Sun Feb 11, 2007 8:59 am Posts: 1106 Location: Ontario Canada

This is what I would do:
1) Get the direction that you want to roll in
2) Determine how far (how many radians/degrees) you want to roll
3) Convert 1 & 2 into a quaternion value
4) Get the current ball's orientation as a quaternion
5) Multiply 3 and 4 together to get the resulting quaternion
6) convert the quaternion back to an axis and angle representation, use this to display the results in OpenGL
7) don't forget to translate the ball in the desired direction also so it looks like it actually "rolled"
See http://www.flipcode.com/documents/matrfaq.html for quaternion math equations
Last edited by Marek on Tue Feb 03, 2009 11:55 am, edited 1 time in total.





codeslasher

Post subject: Posted: Mon Feb 02, 2009 8:55 am 
Joined: Sat Jun 23, 2007 7:56 pm Posts: 145

Thanks Marek, I'll give it a go.
The current orientation I get as the balls transformation node values(its modelViewMatrix). I can convert the modelViewMatrix (excluding the translation column) to a Quaternion. So I have that quaternion.
Then I calculate the Roll direction as (0, 0, 1) * rotationMatix(Yangle), where Yangle is rotation of the forward vector around the up vector.
Am I doing this the wrong way?
Also after reading your post, I don't see a rotation around the Right vector of the object?
Have I missed something?
Am at work, so I'll have to try your advice out later tonight before getting back with my results.
Thanks once again. I'll be back





codeslasher

Post subject: Posted: Tue Feb 03, 2009 5:39 am 
Joined: Sat Jun 23, 2007 7:56 pm Posts: 145

Hi Marek,
I spent the night implementing my Quarternion class and trying to perform the steps you suggested.
Below is an image of the ball before I try to roll 
And this is what happens after roll is performed 
Am using the following piece of code 
void Object::roll(float deltaTime) { CVector3 forward(0.0f, 0.0f, 1.0f); CVector3 rot; //Get the rotation of the object around the Y axis m_orientation.getRotationYangle(rot); //Rotate its direction vector into the right direction forward.rotateY(rot); forward.normalise();
//increment/decrement the roll angle m_rollAngle += (RADIANS(m_rotationSpeed) * deltaTime);
//make sure its in range 0  360 degrees radian while(m_rollAngle < 0.0f) { m_rollAngle = RADIANS(360.0f) + m_rollAngle; }
while(m_rollAngle > 360.0f) { m_rollAngle = m_rollAngle  RADIANS(360.0f); }
Matrix matrix; Matrix currentOrient; CVector3 pos = getPosition();
Quarternion Q2; currentOrient.set(m_orientation.m_matrix);
//Get the objects current orientation as a quarternion Q2.fromMatrix(currentOrient); Q2.normalise(); //Create the Roll quarternion > Roll angle amount in the direction vector m_tempQuart.setRotation (m_rollAngle, forward); m_tempQuart = m_tempQuart * Q2; m_tempQuart.normalise();
//Convert the quarternion into a matrix m_tempQuart.toMatrix(&matrix);
//Store the calculated quarternion as a matrix into the object'ss orientation matrix.getEntries(m_orientation.m_matrix); m_orientation>m_matrix[12] = pos.m_x; m_orientation>m_matrix[13] = pos.m_y; m_orientation>m_matrix[14] = pos.m_z; m_orientation>m_matrix[15] = 1.0f; }
What am I doing wrong?





codeslasher

Post subject: Posted: Wed Feb 04, 2009 4:34 am 
Joined: Sat Jun 23, 2007 7:56 pm Posts: 145

Hello,
After a long evening, I finally got the rotation to work using quarternions.
I made this video Rotating ball to show how it looks.
Does it look correct?
This is the code am using for the roll
void Object::roll(float deltaTime) { CVector3 forward(0.0f, 0.0f, 1.0f); CVector3 up(0.0f, 1.0f, 0.0f); CVector3 right; CVector3 rot; Matrix mat; CVector3 pos;
m_orientation>getRotate(rot); forward.rotateY(rot.m_y); forward.normalise();
m_rollAngle += (RADIANS(m_rotationSpeed) * deltaTime);
while(m_rollAngle < 0.0f) { m_rollAngle = RADIANS(360.0f) + m_rollAngle; }
while(m_rollAngle > RADIANS(360.0f)) { m_rollAngle = m_rollAngle  RADIANS(360.0f); }
pos = getPosition();
right.cross(forward, up); right.normalise();
m_tempQuart.setRotation (m_rollAngle, right); m_tempQuart.normalise();
m_tempQuart.toMatrix(&mat);
mat.getEntries(m_orientation>m_matrix); m_orientation>m_matrix[12] = pos.m_x; m_orientation>m_matrix[13] = pos.m_y; m_orientation>m_matrix[14] = pos.m_z; m_orientation>m_matrix[15] = 1.0f; }
Steps
1. Get forward vector
2. Rotate to put in direction using Y axis rotation
3. Get the Right vector  > rotated forward vector (direction) x Up vectors
4. Create the rotation Quarternion using the Right vector as the rotation axis
5. Convert to Quarternion to matrix form
6. Set the objects orientation to this matrix and add its new position
Thanks to Marek, I corrected my Quarternion class code which had some errors in its calculations.
It helps to have someone else look at code you've written in a hurry.





Marek

Post subject: Posted: Fri Feb 06, 2009 1:18 pm 
Joined: Sun Feb 11, 2007 8:59 am Posts: 1106 Location: Ontario Canada

I created a small demo of a rolling ball program using quaternions which you can download here. If you're interested in knowing the details, just let me know and I'll make a VMK
Last edited by Marek on Sat Dec 11, 2010 7:20 am, edited 1 time in total.





codeslasher

Post subject: Posted: Fri Feb 06, 2009 5:04 pm 
Joined: Sat Jun 23, 2007 7:56 pm Posts: 145

Sweet , :)
Your ball behaves different from mine!
Am guessing that you used the Up and Down arrows to control the forward velocity and the Left and Right for the side velocity.
I'd like to see how you converted you inputs from the arrow keys into velocity/angle values for its motion.
It would be very helpful.
Can we ask for a VMK please.
Thanks Marek.





Marek

Post subject: Posted: Sat Feb 07, 2009 3:52 pm 
Joined: Sun Feb 11, 2007 8:59 am Posts: 1106 Location: Ontario Canada

Acutally I'm using the arrow keys to control the balls acceleration. You'll notice that if you press and hold down on a key, the ball will start moving faster and faster, you'll need to follow the ball with the camera using the WSAD keys but eventually the ball will move faster then the camera and you'll loose it out of your view.
I also added some damping into the rolling so that when you stop pressing, the ball will come to a stop on its own.
The math equations that I used are identical to the ones in the physics VMK series where I control the space ship on the screen.





BugHunter

Post subject: Posted: Sun Feb 08, 2009 5:19 am 
Joined: Wed Aug 06, 2008 7:53 pm Posts: 182 Location: Russia

codeslasher wrote: It would be very helpful. Can we ask for a VMK please.
Indeed :!:











You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum

