[Solved] Code for a rolling ball

Started by
15 comments, last by Roming22 20 years, 5 months ago
I've found something that can be interesting. glRotated(angle, x, y, z) is performing the rotation angle around the vector (x,y,z). That's why glRotated(90, .5, 0, 0) did not rotate the ball at 45 degrees.

I guess I need to do some math to calculate the rotation that is the sum of the rotation on each axis. Then I would have the angle of the rotation and the vector of the rotation, and would just have to call glRotate accordingly.

So, is there any way to do that (I have a vector class with dot and cross product, calcul of an angle between 2 vectors).

[edited by - Roming22 on November 12, 2003 3:11:15 PM]
Advertisement
I am talking to myself ?

I don''t care, I''ve found the solution.

First I needed a matrix in my class:

double rotationMatrix[16];

Then I needed to initialize it as the Identity (once, at the very beginning of the game):

glPushMatrix();
glLoadIdentity();
glGetDoubled(GL_MODELVIEW__MATRIX, rotationMatrix);
glPopMatrix();

Afterwards, each time the ball rotates of an angle alphe around the axis (x,y,z) I have to do the following:

glPushMatrix();
glLoadIdentity();
glRotated(alpha, x, y, z)
glMultMatrixd(rotationMatrix);
glGetDoubled(GL_MODELVIEW__MATRIX, rotationMatrix);
glPopMatrix();

Finally when drawing the ball I do:

glPushMatrix();
glTranslate(position.x, position.y, position.z);
glMultMatrixd(rotationMatrix);
gluSphere(quadric,radius,sections*2,sections);
glPopMatrix();

And that makes my ball(s) roll and pivot the way it should.
I could find it out thanks to the code of BilliardGL. It wasn''t easy to figure everything out as the code is in german, and I don''t speak a word of it, which made it difficult to find the right classes and functions (and for your information, a ball is a "kugel" in german).
Yay !
There are a couple other solutions that are pretty similar :
- use your own matrix product function, so you don''t have to modify the OpenGL matrix stacks (this one is easy).
- use quaternions instead of matrices : a quaternion is used to hold a rotation ; you can use the quaternion product to compose rotations, and it''s quite easy to extract the axis and the angle of a quaternion. You''ll probably find a bunch of quaternion classes using a search engine.
Hmmm who said "if it''s not broken, don''t fix it" ?

SaM3d!, a cross-platform API for 3d based on SDL and OpenGL.
The trouble is that things never get better, they just stay the same, only more so. -- (Terry Pratchett, Eric)
SaM3d!, a cross-platform API for 3d based on SDL and OpenGL.The trouble is that things never get better, they just stay the same, only more so. -- (Terry Pratchett, Eric)
Yep, I won''t touch it.

I guess the OpenGL calls are very fast and therefore I think that adding a quaternion class or doing my own matrix multiplication will result in a performance decrease.

But that''s my guess...
Don't you have to use radians with glRotatef()?

*sorry I didn't see you were using glRotated() not that I know if it makes any difference. Does d stand for "degrees" as opposed to "double"?

[edited by - strider44 on November 13, 2003 8:13:39 AM]
The Love Of Trees
d stands for double.
You have to use degrees in glRotate functions.
The conversion from radians to degrees is simple:
radians = degrees*PI/180

Hope I helped,

Roming
quote:Original post by Roming22
I guess the OpenGL calls are very fast and therefore I think that adding a quaternion class or doing my own matrix multiplication will result in a performance decrease.


Not sure about that... Your hand-coded rotation functions could be inlined, so it might be a bit faster...
But I guess you don''t rotate your ball 1000 times per frame, so this is not relevant...


SaM3d!, a cross-platform API for 3d based on SDL and OpenGL.
The trouble is that things never get better, they just stay the same, only more so. -- (Terry Pratchett, Eric)
SaM3d!, a cross-platform API for 3d based on SDL and OpenGL.The trouble is that things never get better, they just stay the same, only more so. -- (Terry Pratchett, Eric)

This topic is closed to new replies.

Advertisement