#### Archived

This topic is now archived and is closed to further replies.

# Rotating a Ball

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

## Recommended Posts

Ok I''m not sure if this is suppose to go under game programming or graphic theory. Anyway my question is about transforms and a rolling marble mesh. Basically my game has a rolling marble that has to avoid obsticles and such. So to implement my rolling marble I decided to load in a mesh and rotate it on one of it''s axis but I can''t get it to look right. How do I know how to rotate it? I have these variables direction(in radians) percent rotate(between 0 and 1) If the ball is going to the right it would make sence to rotate it on the y axis, but if the ball was going up you would want to rotate it on the x axis. So I decided to rotate it first on the y axis with this line of code D3DXMatrixRotationY( &matRotY, ( D3DX_PI * 2 ) * m_drawInfo.ball_orientation.direction ); and then rotate it on it''s z axis for it''s direction with this line of code D3DXMatrixRotationZ( &matRotZ, m_drawInfo.ball_orientation.angle_turned ); yet its not rotating in the right direction...

##### Share on other sites
Here''s what I did:

I grabbed the ball''s velocity vector and the vector to the ground, took the cross product, and rotated along that vector.

(There''s a function where you rotate around a user-defined line)

it looked kinda goofy when I did it... (when I moved the ball in different directions the rotation looked too... immediate) I haven''t figured out a really good way to make it look real yet.

##### Share on other sites
A little bit of physics makes everything look more realistic Nypyren''s solution is dead on, but if you add an acceleration vector to the ball you will get much nicer results when the ball switches directions. A newly input direction means acceleration in that direction which means that you will have to calculate the resultant acceleration (from the current acceleration vector). Use this acceleration to calculate the new velocity and you will have a nice rolling effect. I''d offer some code but I''m stuck at work right now... Good luck!

insert witty signature here

##### Share on other sites
How do you rotate along an arbitrary axis?

##### Share on other sites
If you''re using DX you can use the function D3DXMatrixRotationAxis to calculate the rotation matrix.

insert witty signature here

##### Share on other sites
Is there anyway to rotate my ball without using vectors as the information? I'm in charge of all the graphics programming and my friend is in charge of the game logic programming. He is at camp right now for two weeks and I can't even begin to deciphere his code because it is such a mess.

What I have to work with is percetage rotated( 0.0 to 1.0 ) and I have the direction the ball is facing which is between 0 and 2pi. I need to somehow rotate the ball every frame can anyone help me out?

Here is the code I've been using so far(which doesn't work )

// Rotate the ball
D3DXMatrixRotationY( &matRotY, ( D3DX_PI * 2 ) * m_drawInfo.ball_orientation.percent_rotation );
D3DXMatrixRotationZ( &matRotZ, m_drawInfo.ball_orientation.angle_turned );

[edited by - ph33r on June 18, 2003 5:36:18 PM]

##### Share on other sites
Well, for a simple terrain test, you have all the information you need.

You can simply assume that the terrain is straight-down (vector: 0,-1,0 (where y points up)). Then all you need is the directional vector. You got the direction that the ball is facing. Simply trigonometry will get you the X and Z values for the vector. If I''m not wrong, X = cos(direction); Z = sin(direction). Could be the other way around though, not sure, you have to look into that

Then get the crossproduct and do as was explained before.

##### Share on other sites
Excuse my ignorance I'm taking linear algebra and physics next semester so I don't know much about vectors. I'm not even sure what class you learn them in I assume it would be linear algebra though I'm not too sure.

I'm not really sure I fully understand you say that I should build the vector using X and Z I Think I would want the vector to be Z 0 and solve for X and Y. Also how would you solve for X or Y you only know the angle you don't have enough variables to solve for the equation(like length) Or maybe I can use a length of 1 for the hypotonuse because the length doesn't really matter..hmm I think I'm on to something!

[edited by - ph33r on June 18, 2003 8:12:36 PM]

##### Share on other sites
Ok I've tried doing what you've said and it just doesn't work the ball doesn't even roll anymore it just slides along the board. Oh and my board is 2d meaning it's planes are x, y I just tilted the camera back to give it a 3d look.

Anyway here is the code it doesn't work for crap though

// Matrix structure	D3DXMATRIX matWorld, matTrans, matRotA;	// Calculate the ball's position	float x = ( float )m_drawInfo.ball_position.x + .5f;	float y = ( float )m_drawInfo.ball_position.y + .25f;	float z = ( float )m_drawInfo.ball_position.z;	// -- Rotate the ball --		// Vectors	D3DXVECTOR3 vecDirection;	D3DXVECTOR3 vecPerp;	D3DXVECTOR3 vecRotationAxis;	// Determine direction ball is facing	vecDirection.x = ( float )sin(m_drawInfo.ball_orientation.angle_turned);	vecDirection.y = ( float )cos(m_drawInfo.ball_orientation.angle_turned);	vecDirection.z = 0.0f;	// Perpendicular vector    vecPerp.x = 0.0f;	vecPerp.y = 0.0f;	vecPerp.z = 1.0f;	// Cross-product of Direction / Perpendicular to get axis of rotation	D3DXVec3Cross( &vecRotationAxis, &vecDirection, &vecPerp );    	// Transform the ball	D3DXMatrixTranslation( &matTrans, x, y, z );	D3DXMatrixRotationAxis( &matRotA, &vecRotationAxis, ( D3DX_PI * 2.0f ) * m_drawInfo.ball_orientation.angle_turned );		// Build the final matrix	D3DXMatrixIdentity( &matWorld );	D3DXMatrixMultiply( &matWorld, &matWorld, &matRotA );	D3DXMatrixMultiply( &matWorld, &matWorld, &matTrans );		// Set the transformation	m_Graphics.SetWorld( matWorld );		// Draw the ball mesh	m_Graphics.DrawMesh( 0 );

[edited by - ph33r on June 18, 2003 8:34:08 PM]

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5

• 10
• 9
• 9
• 11
• 11
• ### Forum Statistics

• Total Topics
633682
• Total Posts
3013311
×