Skip to main content
gamedev.net
🔒 Locked

Rotation

Started by ProblemBaby 2 replies 1.3k views
Original Post
ProblemBaby
ProblemBaby
I want to simulate a ball rolling on a floor in 3D but can't find out how to set up the rotation for this... can someone help me?
Problems every where
DraganO
DraganO
First of all you will need a function to create a rotationmatrix for rotating around an abitrary axis, like this:

void ArbitraryRotationMatrix(D3DXMATRIX* pOut, const D3DXVECTOR3* NormalizedAxis, float angle){	D3DXMatrixIdentity(pOut);	pOut->_11 = NormalizedAxis->x * NormalizedAxis->x * (1 - cosf(angle)) + cosf(angle);	pOut->_12 = NormalizedAxis->y * NormalizedAxis->x * (1 - cosf(angle)) - NormalizedAxis->z * sinf(angle);	pOut->_13 = NormalizedAxis->z * NormalizedAxis->x * (1 - cosf(angle)) + NormalizedAxis->y * sinf(angle);	pOut->_21 = NormalizedAxis->x * NormalizedAxis->y * (1 - cosf(angle)) + NormalizedAxis->z * sinf(angle);	pOut->_22 = NormalizedAxis->y * NormalizedAxis->y * (1 - cosf(angle)) + cosf(angle);	pOut->_23 = NormalizedAxis->z * NormalizedAxis->y * (1 - cosf(angle)) - NormalizedAxis->x * sinf(angle);	pOut->_31 = NormalizedAxis->x * NormalizedAxis->z * (1 - cosf(angle)) - NormalizedAxis->y * sinf(angle);	pOut->_32 = NormalizedAxis->y * NormalizedAxis->z * (1 - cosf(angle)) + NormalizedAxis->x * sinf(angle);	pOut->_33 = NormalizedAxis->z * NormalizedAxis->z * (1 - cosf(angle)) + cosf(angle);}


To make the ball roll on the floor, you could take the balls last frame position and its current position to determine a rotation axis using the crossproduct of the move vector and a up vector. Now create a rotation matrix for this frame depending on how much the ball moved since the last frame. Multiply this rotation matrix to your balls total rotationmatrix every frame. This way you get a convincing looking rotating ball, even if this is everything but physically correct.

[Edited by - DraganO on August 25, 2008 5:21:10 PM]
haegarr
haegarr
There are some levels of abstraction, dependend on how the motion of the ball is prescribed. Since a rolling ball is desired, sliding should play no role; that makes the following approach possible.

Assume that the track of the ball on the ground is prescribed. That means that the position p(t) of contact is given for any t. When a step is computed at time tn, the position of the contact p(tn) and hence the position of the ball's center is known. The position p(tn-1) of the previous step is also known. As ever, the simulation step must be sufficiently small.

The mean axis of rotation can then be chosen perpendicular to the difference vector of the both positions, and parallel to the ground. The angle of rotation has a counterpart of an arc length on the ball's surface, given by
a / ( 2 pi ) * 2 pi r == a r
where a denotes the angle and r the radius of the ball. The arc length must be the same as the path segment length from p(tn-1) to p(tn). The length can perhaps be approximated by
l := | p(tn) - p(tn-1) |
resulting in an angle
a = l / r

With the axis and the angle a rotation presentation is available that can be converted in any other rotation representation if needed.

Other approaches were necessary if gravity plays a role, since the path of motion become dependend on the ground's spatial gradient and the ball's mass (the OP says nothing in this sense). That has primarily an impact on the path, but the method of computing the rotation can often be similar to the one mentioned above.
ProblemBaby
ProblemBaby
Works perfect!

Thanks
Problems every where

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.