[SOLVED!] Ball rolling problem

Started by
15 comments, last by ch1haya 13 years, 1 month ago
note that this is Direct3D 9 code

My code here:

D3DXMATRIX matRotZ;
D3DXMATRIX matRotX;
D3DXMATRIX matRotation;
D3DXMATRIX matTranslate;
D3DXMATRIX matBall;

D3DXMatrixTranslation( &matTranslate, m_fBallTransX, m_fBallRadius, m_fBallTransZ );
D3DXMatrixRotationX( &matRotX, m_fBallRotX );
D3DXMatrixRotationZ( &matRotZ, m_fBallRotZ );
D3DXMatrixMultiply( &matRotation, &matRotX, &matRotZ );
D3DXMatrixMultiply( &matBall, &matRotation, &matTranslate );
m_pd3dDevice->SetTransform( D3DTS_WORLD, &matBall );

ok, I have another code for controlling ball movements. I only need to focus on this. The translation of the ball has no problem at ball, the problem is rotation.

If I only rotate the ball at one axis, ok, no problem at all. But, the problem is described here:

After the ball move the along X-axis and rotated along Z-axis 180 degrees, and then I move the ball along Z-axis and the ball will rotate along X-axis at reverse direction.
After the ball move the along X-axis and rotated along Z-axis 90 degrees, and then I move the ball along Z-axis and the ball will rotate along Y-axis instead.

I don't know if you will understand my problem, but I think I need some help from either DirectX experts or Math experts.
Advertisement
I'm not sure if I understand the question, but generally to create the effect of a rolling object you'll need to use incremental rotations rather than Euler angles. (This has been covered quite a few times in the past on the forums, so you might try searching for 'ball roll' or 'rolling ball' and see what you can find.)
You understand the question well. But there is a problem, I can't really find a matrix for doing the rolling effect.
Does quaternion help? Can anyone give example, or just give a code for doing real rolling effect?
Oh my god, no experts here to help? I thought there would be many experts that are able to help here...

Oh my god, no experts here to help? I thought there would be many experts that are able to help here...

There are plenty of people here who can help. Your posts were only about an hour and a half apart though - a little early to be bemoaning the lack of experts on the forum, IMO ;)

But there is a problem, I can't really find a matrix for doing the rolling effect.
Does quaternion help? Can anyone give example, or just give a code for doing real rolling effect?
For this it doesn't really matter if you use matrices or quaternions; the concepts will be the same either way.

In short:

1. Store the orientation of the ball as a quaternion or matrix
2. For each update, if the speed of the ball is greater than a specified epsilon:
3. The axis of rotation is the normalized cross product of the ball's velocity vector and the current 'up' vector
4. The angle of rotation (in radians) is the distance traveled by the ball during that update divided by the ball's radius
5. Apply this axis-angle rotation to the ball's orientation
6. Re-normalize the orientation to prevent drift (normalization for quaternions, orthogonalization for matrices)
I can't understand the concept well. Can anyone show me a full example for rolling ball?
Or give the full algorithm, I spent a few days thinking about it and I don't even get the answer.
I'm not quite familiar with 3D mathematics. Can someone at least give me the method to compute the up vector?

I can't understand the concept well. Can anyone show me a full example for rolling ball?
Or give the full algorithm, I spent a few days thinking about it and I don't even get the answer.
I'm not quite familiar with 3D mathematics. Can someone at least give me the method to compute the up vector?

I'm not going to try to write up a full example right now (although maybe someone else will), but I can try to provide some additional information if needed.

First, let me ask about this:

Can someone at least give me the method to compute the up vector?
Can you clarify what you mean by this?

Specifically, what type of environment do you have? Is it just a flat plane? Or is there more complex geometry? And how are you handling collision detection and response? Are you using a physics engine?

(The reason I ask these questions is that the answers will be relevant to the question of where to get the 'up' vector, assuming that's what you're asking.)

'Chihaya' said:

I can't understand the concept well. Can anyone show me a full example for rolling ball?
Or give the full algorithm, I spent a few days thinking about it and I don't even get the answer.
I'm not quite familiar with 3D mathematics. Can someone at least give me the method to compute the up vector?

I'm not going to try to write up a full example right now (although maybe someone else will), but I can try to provide some additional information if needed.

First, let me ask about this:

Can someone at least give me the method to compute the up vector?

Can you clarify what you mean by this?

Specifically, what type of environment do you have? Is it just a flat plane? Or is there more complex geometry? And how are you handling collision detection and response? Are you using a physics engine?

(The reason I ask these questions is that the answers will be relevant to the question of where to get the 'up' vector, assuming that's what you're asking.)


Ask JYK has eluded to, this question has been asked many many many times before. I've answered it myself 2 or 3 times and I, and I assume all the "experts" are probably tired of repeating the same old explanations. But for you I'll make an example.

Stand Facing North.
Lay down on your back (+/- 90 degree rotation on X)
Roll Onto your stomach (+/- 180 rotation on Z)

Stand on your head Facing north (+/- 180 rotation on Z)
Fall over.


As you can now feel, rotation is not commutative. So trying to build a rotation matrix(orientation) from scratch each frame, using the accumulated rotations will not do what you want it to. Instead you need to store the orientation each frame, and apply the incremental rotations as they occur. If this doesn't make sense, repeat the excercise. When it does, search the forums. If you look for posts from me, I distinctly remember posting example code for someone a bit before Christmas. Best of luck.
Will matrix stack be useful for this? I'm not really sure
Also, are there a method to rotate the object without rotating axis?
Does matrix stack help about this? Are there any method to rotate an object without rotating the axes?

This topic is closed to new replies.

Advertisement