# Rotation matrix - rotate a ball around a rotating box

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?

-

You are rotating the ball center's vector with respect to the origin. If you just want to rotate the box with respect to the ball center. Then instead of computing ball center vector times the rotation matrix, compute (box.Corner() - ball.Center())*mat + ball.Center(), and you will get the new corner coordinates for the box.

-

thanks for trying! I tried something else and it works:

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

-