# OpenGL glRotatef and billiard balls

This topic is 4435 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi. I've made a billiard ball with a number on it in OpenGL. Here's my problem. angle += 0.1; glRotatef(angle,normalized.x,normalized.y,0.0f); ... Works fine until I hit a wall and the ball changes direction. Is there a way to start from that position and rotate from there, like... if (hitwall) { angle = 0.0; newaxis(); } //and then... angle += 0.1; glRotatef(angle,normalized.x,normalized.y,0.0f); Or is there an easier way to do this?

##### Share on other sites
Congratulations
You've just discovered one of the most Important Truths about OpengGl graphics programming.
OpenGl is for graphics Only; it is cannot keep track of your gamelogic/physics for you.

The position of the ball, is best tracked as an actual coordinate that you update in your gameloop. Not by facking position/angle changes with offcenter glrotates.

Now, with that covered...
All glrotates take place about the origin, but you can rotate about an arbitrary location, by translating the object to the origin, rotating, then translating the rotated object back to where it was.
But really, what you're doing now is Not a good way of representing the ball's motion. fix that first
you want to have a gamePhysics representation that is totally self-contained. OpenGl should Only be for Displaying things after you retrieve the ball coords from the physics module...

##### Share on other sites
Well, the 'physics' is pretty simple.
Angle of reflection = angle of incidence.
eg get the angle of direction relative to the normal to the edge, then the reflected angle will be the negative of this.

let y axis = up, and x axis = right

eg vector of ball going to edge at right of screen = (x=1, y=1), then vector after hitting edge will be (-1, 1).

example 2: vector of ball (1, 4), and hits edge at top of screen then vector after hitting edge will be (1, -4).

example 3: vector of ball going to edge = (-1, 5), and hits edge on left of screen then vector after hitting edge will be (1, 5).

example 4: vector of ball going to edge = (1, -3), and hits edge on bottom of screen then vector after hitting edge will be (1, 3).

After hitting edge you'd also want to to decrease the magnitude of the velocity due to friction.

##### Share on other sites
1) Check out matrices in OpenGL... google, etc.

2) Each update, add a "rotate" matrix onto the balls' matrix.

3) Render each ball with their corresponding rotation-matrix.

Cheers!

##### Share on other sites
You could keep track of your rotations in a vector (or better a quaternion) and create a rotation matrix each frame (you would be doing that anyway). That would help you reduce the precision errors that come along with matrix multiplications.

##### Share on other sites
Thanx for all the replies. Now I have a lot of work ahead of me...

Have been reading a lot about Matrices and Quaternions on the Internet. Phew! I think I have a basic understanding of what they do now.

Tell me if I'm going in the wrong direction...

1. Set where to rotate in a matrix
2. Add that matrix to the quaternion
3. Show the result

Read about rotation matrices, but still don't understand how to get my normalised vector that I've used before for rotating, into a matrix. And how do I add that matrix to a quaternion? Any Rotating-For-Dummies webpage that explains that? ... In a very simple way ;)

[Edited by - toto1919 on September 21, 2006 3:26:52 AM]

##### Share on other sites

You should keep a quaternion Q for your ball, add the delta rotation (in form of a quaternion) to Q each frame, then convert Q into a matrix and add it to the modelview matrix (e.g. glMultMatrixf(Q->toMatrix()).

For Q->toMatrix() see the article.

##### Share on other sites
Wow! Thanx for the quick reply.

The webpage looks "easy" enough for me to grasp... Looking in to it now...

##### Share on other sites
Works fine now!

Thanx for all your valuable input.

1. 1
Rutin
26
2. 2
3. 3
4. 4
5. 5

• 11
• 9
• 9
• 9
• 14
• ### Forum Statistics

• Total Topics
633312
• Total Posts
3011312
• ### Who's Online (See full list)

There are no registered users currently online

×