Skip to main content
GameDev.net gamedev.net
🔒 Locked

glRotatef and billiard balls

Started by toto1919 Sep 19, 2006 at 9:50 AM 8 replies 1.1k views
Original Post
toto1919
toto1919
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?
toto1919
toto1919
Thanx for the reply.
ade-the-heat
ade-the-heat
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.

Rasmadrak
Rasmadrak
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!

"Game Maker For Life, probably never professional thou." =)
Lord_Evil
Lord_Evil
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.
If I was helpful, feel free to rate me up ;)If I wasn't and you feel to rate me down, please let me know why!
toto1919
toto1919
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]
Lord_Evil
Lord_Evil
Try this article: http://www.gamedev.net/reference/articles/article1095.asp.

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.
If I was helpful, feel free to rate me up ;)If I wasn't and you feel to rate me down, please let me know why!
toto1919
toto1919
Wow! Thanx for the quick reply.

The webpage looks "easy" enough for me to grasp... Looking in to it now...
toto1919
toto1919
Works fine now!

Thanx for all your valuable input.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.