Rotating a Ball

Started by
11 comments, last by ph33r 20 years, 10 months ago
It''s been two days and no one has any idea what is wrong with my code? I''m really stuck thanks for any help
Advertisement
Do you guys not know why it doesn''t work or don''t really care?
I think you guys havn''t been helping me because I havn''t been supplying enough information so I''m going to go through a case scenerio. Ok so I''ve stepped through the debugger and I''m getting what seems to be the correct results but I''m going to post them here for everyone to see.

First I basically have a 2d game in 3d and the board is set up with
+y as up -y as down
+x as up -x as down
+z into screen -z out of screen

So my goal is to take a radian between 0 and 2pi and convert it into an axis I can rotate on. I also have a variable that represents the percentage the ball has turned.
0(0%) = start of rotation
1(100%) = full 360 rotation
So when I recieve a number like .345 I have to multiply it by pi * 2 to see how far it needs to be rotated on the rotation axis. Ok I don''t want to jump to far ahead so here is how my program works for when the ball is traveling right at
PI / 2 or as my debugger outputed 1.5707964f
So you guys know what I''m looking at on the screen the ball does move right, but it doesn''t rotate at all it just slides across the screen and when it hits different curves the ball starts rotating just not in the correct direction. So the ball is moving right on my game board like this (excuse the lame ascii art

BALL DIRECTION RIGHT ANGLE = 1.5707964f;
_____________
|_|_|_|_|_|_|
|_|_|_|_|_|_|
|_|o|--->_|_|
|_|_|_|_|_|_|

My goal is to find the arbitrary rotational axis and rotate the ball on it by ( PI*2 ) * PERCENT_OF_FULL_ROTATION. So the first thing I did was build a vector based on the direction the ball is head(to the right). To accomplish this I need to convert the ball''s angular direction to a vector by using sin and cos like this

// Determine direction ball is facing
vecDirection.x = ( float )sin( angle );
vecDirection.y = ( float )cos( angle );
vecDirection.z = 0.0f;

Now here is what my debugger gave for each of the variables
x = 1.0000000
y = -4.3711388e-008 (which is pretty close to 0)
z = 0.00000000

Now I''m pretty sure that is right because it is basic triginometry and hopefully I didn''t skrew this part up. Anyway the next thing I need to do is build a perpendicular vector to my direction vector and take the cross product so I can end up with my arbitrary rotational axis. So I filled my perpendicular vector with these values

// Perpendicular vector
vecPerp.x = 0.0f;
vecPerp.y = 0.0f;
vecPerp.z = 1.0f;

To my knowledge that is a perpendicular vector the my 2d board, since the board is based on x,y and not on z - the z is guarenteed to be perpendicular to the x or y axis. So now I take the cross product and finally get my axis of rotation with this code.

// Cross-product of Direction / Perpendicular to get axis of rotation
D3DXVec3Cross( &vecRotationAxis, &vecDirection, &vecPerp );

Which ends up giving me the values in the Rotation Axis of

x = -4.3711388e-008
y = -1
z = 0

This looks right to me even though I havn''t taken linear algebra yet from my research over the pass couple days I believe these to be correct results. So now that I got my rotational axis I just am left to rotate the ball on it well thats easy enough to do basically I take the percent the ball is at in its rotation and multiply it by 2pi using this code.
D3DXMatrixRotationAxis( &matRotA, &vecRotationAxis, ( D3DX_PI * 2.0f ) * m_drawInfo.ball_orientation.angle_turned );
The debugger told me the percent was .0077449246
Now this may be a problem because I have no idea what the values in matRotA should look like nor do I think you guys care but from looking at that small code it seems like it would work. Now I''m guessing that my problem must be in how I set up the world transformation here is the last snippet of code.

// 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 );

Then I set the world transformation and render the mesh to the screen and all it does is slide to the right not rotating at all. Thanks for reading this long post and taking the time to read it.

This topic is closed to new replies.

Advertisement