旋转矩阵-围绕旋转的盒子旋转一个球

Rotation matrix - rotate a ball around a rotating box

本文关键字:旋转 一个 盒子      更新时间:2023-10-16

我有一个3D盒子:center point = (a,b,c), width = w, height = h, depth = d

中心点不是原点

我有一个球在盒子上(彼此触摸),它的centerradius

我可以旋转盒子(围绕X轴,但其中心保持不变…),我希望球保持粘在盒子上。所以球需要和盒子一起旋转。

旋转角度为45度。

我试过这样做:

我定义了围绕X轴的旋转矩阵:

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

乘以球的中心矢量:

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

所以我得到:

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;

当盒子旋转太远时,球确实旋转了。我该怎么修理它?

您是否尝试将其转换为坐标原点,旋转然后再转换回来?

我认为坐标应该在右边乘以变换矩阵,即:

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;

感谢Alexander Mihailov,这里是最终答案:

//根据方框将球中心修正到原点。中心

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

//rotation_matrix(of X轴)* ball_center_corrected//所以旋转是围绕X轴

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

//将球的中心移回盒子周围

    ball.Center() = new_center + box.Center();