0

我有一个 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;

当盒子旋转但太远时,球确实旋转了。我该如何解决?

4

2 回答 2

0

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

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

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;
于 2013-05-30T02:19:07.577 回答
0

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

// 根据 box.Center 将球中心校正到原点

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

// rotation_matrix(of X axis) * 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();
于 2013-05-30T10:03:19.160 回答