View Full Version : Rotation question

Run-E

05-15-2001, 05:52 AM

Hi!

I'm just starting to learn OpenGl and I thought I would do so by making some kinde of a pool or snooker game. My problem is that I want the ball to rotate about an axis perpendicular to the ball's velocity vector... As it would i real life if you don't take into consideration it might have some other kind of spin. I just want to be able to roll the ball forward in any direction.

I serched this forum, but I'm not sure I could find the answer I'm looking for... I would be very glad if someone here could help me, since I'm kind of stuck right now.

Thanks in advance! http://www.opengl.org/discussion_boards/ubb/smile.gif

If you know the direction the ball is rolling, and you know where up is, you can calculate the sidevector by taking the crossproduct of the other two vectors. side = front cross up

There you have the axis you need to rotate about. Just pass this axis along with the angle to glRotatef. The angel at which you need to rotate is depending on the balls radius and speed. I'm sure you can calculate the proper angle if you really want to. Just take a paper and make some nifty drawings.

Run-E

05-15-2001, 12:58 PM

Hmm... So I have to use Glrotate only once then?! And precalculate what to put in there using the method you described..? Have I understood this correctly?

Thanks, by the way! http://www.opengl.org/discussion_boards/ubb/smile.gif

Let's say the directon of the ball is V, and up is U, then the sidevector S = V x U (x = crossproduct). S is the vector you pass to glRotate, like this: glRotatef(angle, S.x, S.y, S.z); That will rotate the object angle degrees about the vector S, which is the same as rotating the ball as it rolls on the table. So yes, I guess you got it right.

I haven't bothered calculating the formula for the angle, let's say I left it as an exercise for the reader http://www.opengl.org/discussion_boards/ubb/smile.gif If you really can't calculate the angle, I can give it a try, just tell me.

Run-E

05-16-2001, 07:08 AM

Thank you!

I'll see if I can get this to work... http://www.opengl.org/discussion_boards/ubb/smile.gif

Run-E

07-16-2001, 05:52 AM

Bringing this old topic back to life... http://www.opengl.org/discussion_boards/ubb/smile.gif

I haven't had time to try your suggestions until now and... Well.. It kind of works but not 100%.

When the ball gets a sudden change in direction (for example if it hits a wall) I sometimes get a strange result. The ball seems to flip or rotate very much in one single frame and then everything is fine until it gets another sudden change in velocity. The strange thing is that it happens most of the times but not EVERY time.

I would appreciate any help I could get on this.

Thanks!

RedZen

07-16-2001, 06:17 AM

Hmmmm, my opinion?, well considering that it's a snooker game and not pool. You won't have numbers on the balls. This means you don't really need to rotate the balls. You can give the effect with lighting. Otherwise the only way I can see it is with very high precision. I can't think up a math formula now for ya, gotto almost go home http://www.opengl.org/discussion_boards/ubb/smile.gif

Run-E

07-16-2001, 06:26 AM

http://www.opengl.org/discussion_boards/ubb/smile.gif

The thing is that my game has changed a bit sice I first posted this question and now the ball is supposed to roll on an arbitrary shaped landscape and it looks kind of boring if the ball is only sliding around instead of rolling... http://www.opengl.org/discussion_boards/ubb/smile.gif

Well... if someone has solved this problem please help me out!

Thanks in advance!

Run-E

07-18-2001, 06:30 AM

C'mon... Someone must know the answer to this one... http://www.opengl.org/discussion_boards/ubb/smile.gif

Kilam Malik

07-18-2001, 07:49 AM

First to the problem: You count up the rotation in glRotate(rot, s.x, s.y, s.z). The s is the same, as long as the direction stays. But after a direction change, the s-vector changes. No you apply the rotation to an other vector. But you have to keep the old rotation, otherwise you get this flip-back.

Solution: Make a matrix, make it identity. Now in each frame you multiply this matrix with the rotation matrix (rot, sx, sy, sz). This matrix is now applied to the ball.

The bad news: You have to do your own matrix calculations ;-) You could use the matrix functions from gl, but that would slow down...

Kilam.

Run-E

07-18-2001, 11:10 AM

Thanks!

I think I know what to do now! http://www.opengl.org/discussion_boards/ubb/smile.gif

Powered by vBulletin® Version 4.2.3 Copyright © 2017 vBulletin Solutions, Inc. All rights reserved.