Question

I've a 3D box: center point = (a,b,c), width = w, height = h, depth = d.

the center point isn't the origin.

I have a ball on the box(touch each other), its center and radius.

I can rotate the box(around the X axis but its center STAYS the same..) and I want the ball to stay stuck to the box. so the ball needs to be rotated WITH the box.

the angle of the rotation is 45 degrees.

I tried to do this:

I defined the Rotation Matrix around the X axis:

mat[3][3]
1,    0   ,    0 
0, cos(45), -sin(45) 
0, sin(45), cos(45)

and multiply it by the ball center vector:

(ball.Center().m_x , ball.Center().m_y, ball.Center().m_z) * mat

so I got:

Point3D new_center(ball.Center().m_x, 
                   ball.Center().m_y*cos(45) + ball.Center().m_z*sin(45), 
                   -(ball.Center().m_y)*sin(45) + ball.Center().m_z*cos(45));
ball.Center() = new_center;

the ball is really rotated when the box is rotated but too far. How can I fix it?

Was it helpful?

Solution

Have you tried to translate it to the origin of coordinates, rotate and then translate back?

And I think that the coordinates should be multiplied on the right by the transformation matrix, i.e.:

Point3D new_center(ball.Center().m_x, 
                   ball.Center().m_y*cos(45) - ball.Center().m_z*sin(45), 
                   ball.Center().m_y*sin(45) + ball.Center().m_z*cos(45);
ball.Center() = new_center;

OTHER TIPS

thanks to Alexander Mihailov, here's the final answer:

// correcting the ball center to the origin according the box.Center

    Point3D ball_center_corrected = ball.Center() - box.Center();

// rotation_matrix(of X axis) * ball_center_corrected // so the rotation is around the X axis

    Point3D new_center(ball_center_corrected.m_x,
                       ball_center_corrected.m_y*cos(angle) -
                       ball_center_corrected.m_z*sin(angle),
                       ball_center_corrected.m_y*sin(angle) +
                       ball_center_corrected.m_z*cos(angle));

// translate the ball center back around the box

    ball.Center() = new_center + box.Center();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top