Jump to content
  • Advertisement
Sign in to follow this  
toto1919

OpenGL glRotatef and billiard balls

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
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.
Read about Vectors if you don't know them already.


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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!