# Rotational axis? for movement of ball

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

## Recommended Posts

I have a ball on a track and right now the ball moves side to side and I change the rotation based on which key the user has pressed. Problem is when the user presses fwd and left/right the ball doesn't say 45 degrees. So to make the ball look a bit more realistic in movements what am I needing to do to get this ball to rotate correctly when moving 2 directions at once vs. 1(fwd,right, left) Their isn't any backwards...
//used to calculate playerDir and is a scalar
//used later on with a playerTransformMatrix to determine playerPos.xyz
velocityClamped = velocity;
NX::MATH::Clamp<float>(velocityClamped, .5f, 1.0f);

if(playerRight)
playerDir += velocityClamped * PLAYERDIR_ACCEL_CONSTANT * DIR_CHANGE_RATE;
if(playerLeft)
playerDir -= velocityClamped * PLAYERDIR_ACCEL_CONSTANT * DIR_CHANGE_RATE;

//for the rotation of the ball
if(playerRight)
rotMatrix.Rotate(angle * velocity, false, false, true);//z axis
else if(playerLeft)
rotMatrix.Rotate(-angle * velocity, false, false, true);//z axis
else
rotMatrix.Rotate(angle * velocity, true, false, false);//x axis


rotMatrix is a 4x4 matrix thanks

##### Share on other sites
There's not much to go off of from that code, but if I understand correctly, you're trying to have the combination key-press LEFT + UP create a 45 degree rotation as opposed to a full 90 (or 0) degree rotation. The problem with your code, then, is the if/else if/else sequence. In the event of a LEFT + UP combination, only one of those if blocks is going to be executed; namely, the first one that evaluates to true.

Try changing the code to a series of simple ifs. This way, each of the statements that evaluates to true will be executed, not just the first one.

##### Share on other sites
Nah that doesn't work, the ball goes all nuts like you are stretching it out and contracting it...

##### Share on other sites
LOL. So much for listening to me [smile]

Out of curiosity, what does your code look like now?

##### Share on other sites
I used this

if(playerRight)
rotMatrix.Rotate(angle * velocityClamped * .1f, false, false, true);
if(playerLeft)
rotMatrix.Rotate(-angle * velocityClamped * .1f, false, false, true);
if(playerFwd)
rotMatrix.Rotate(angle * velocity * .00025f, true, false, false);

##### Share on other sites
Can you post your Rotate() function?

##### Share on other sites
Hi jky,

I changed my Rotate to sepearate functions but still does the same thing as before. I can only rotate one axis at a time with this rotate function.

What I am wondering is do I need to do some kind of rotational velocity or angular velocity? I have no idea I am not looking for realistic simulation here, but would like the ball to spin on the correct axis when the player moves in those directions?

Thanks

//Rotate a 3D point in Radians not Degrees	void RotateX(T angle)	{	    T sine   = sin(angle);		T cosine = cos(angle);		matrix[5]  = cosine;		matrix[6]  = sine;		matrix[9]  = -sine;		matrix[10] = cosine;	}	//Rotate a 3D point in Radians not Degrees	void RotateY(T angle)	{	    T sine   = sin(angle);		T cosine = cos(angle);        matrix[0]  = cosine;		matrix[2]  = -sine;		matrix[8]  = sine;		matrix[10] = cosine;	}	//Rotate a 3D point in Radians not Degrees	void RotateZ(T angle)	{	    T sine   = sin(angle);		T cosine = cos(angle);        matrix[0] = cosine;		matrix[1] = sine;		matrix[4] = -sine;		matrix[5] = cosine;	}

##### Share on other sites
What are the types of velocity and playerDir?

I notice that your rotation functions don't set the matrix to identity before setting the relevant elements. Is this intentional? This means that the results may be incorrect if the matrix is not already identity.

##### Share on other sites
both are scalar floats and I call identity before the rotate in a separate call

##### Share on other sites
Don't you need 3 rotation matrices, one for each axis, then rotMatrix would be the product of those 3 matrices (in the right order) ?
Your RotateX/Y/Z functions assume the matrix is identity, so you can't call those functions successively on the same matrix. It overrides part of the last rotation, stretching the ball.

1. 1
Rutin
25
2. 2
JoeJ
20
3. 3
4. 4
5. 5

• 9
• 9
• 46
• 41
• 23
• ### Forum Statistics

• Total Topics
631749
• Total Posts
3002051
×