Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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:

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?

share|cite|improve this question

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.

share|cite|improve this answer

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_y*sin(angle) +

// translate the ball center back around the box

    ball.Center() = new_center + box.Center();
share|cite|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.