Jump to content
  • Advertisement
Sign in to follow this  
ch1haya

[SOLVED!] Ball rolling problem

This topic is 2652 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

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.

Share this post


Link to post
Share on other sites
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.)

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites

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?[/quote]
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)

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites

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?[/quote]
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.)

Share this post


Link to post
Share on other sites

[quote name='Chihaya' timestamp='1295276513' post='4760108']
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?[/quote]
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.)
[/quote]

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.

Share this post


Link to post
Share on other sites
Hidden
Will matrix stack be useful for this? I'm not really sure
Also, are there a method to rotate the object without rotating axis?

Share this post


Link to post
Sign in to follow this  

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!