使用Direction和Axis旋转3D对象

时间:2012-12-11 12:19:55

标签: java matrix 3d rotation java-3d

我正在努力在Java3D中展示3D模型(IFC)。我需要使用IFC模型给出的2个向量,方向向量和轴向量来旋转对象。

但我无法弄清楚如何获得正确的角度。

代码:

// Create vectors
Vector3f directionVector = new Vector3f(dx, dy, dz);
Vector3f axisVector = new Vector3f(ax, ay, az);

//Calculate angle
float angle = axisVector.angle(directionVector);

//create AxisAngle4f
AxisAngle4f axisAngle = new AxisAngle4f(axisVector, angle);

axisVector始终为(0.0,0.0,1.0),因此需要在Z轴上旋转

但是当我计算角度时,似乎总是1.5707964(90°):

Example 1:
dir:    (-1.0, 0.0, 0.0)
axis:   (0.0, 0.0, 1.0)
angle:  1.5707964 (90.00000250447816)
AA:     (0.0, 0.0, 1.0, 1.5707964)

Example 2:
dir:    (0.0, 1.0, 0.0)
axis:   (0.0, 0.0, 1.0)
angle:  1.5707964 (90.00000250447816)
AA:     (0.0, 0.0, 1.0, 1.5707964)

Example 3:
dir:    (1.0, 0.0, 0.0)
axis:   (0.0, 0.0, 1.0)
angle:  1.5707964 (90.00000250447816)
AA:     (0.0, 0.0, 1.0, 1.5707964)

我通过测试知道-1.0意味着反转180°。

有人可以帮我理解我做错了吗?

修改

Documentation表示展示位置对象(方向和轴)

结果截图:

  • 橙色:是地板
  • 绿色:是屋顶
  • 红色:是旋转点
  • 左:侧视角
  • 右:顶部视角
  • 3个板组具有方向(0.0,1.0,0.0)
  • 2块板的组具有方向(1.0,0.0,0.0)
  • 屋顶有方向:( - 1.0,0.0,0.0)

我做了* 2测试来模拟180°-1.0。正如你在上一个例子中看到的那样,正确绘制了屋顶。

enter image description here

enter image description here

3 个答案:

答案 0 :(得分:2)

您不需要方向矢量和轴矢量之间的角度。由于你的轴向量总是(0.0,0.0,1.0),你可以定义一个常数向量(1.0,0.0,0.0),你可以用它来比较方向向量,即:

// Create vectors
Vector3f directionVector = new Vector3f(dx, dy, dz);
Vector3f axisVector = new Vector3f(ax, ay, az);
Vector3f constantBaseVector = new Vector3f(1, 0, 0);

//Calculate angle
float angle = constantBaseVector.angle(directionVector);

//create AxisAngle4f
AxisAngle4f axisAngle = new AxisAngle4f(axisVector, angle);

答案 1 :(得分:1)

我猜测axisVector.angle(directionVector)会返回两个向量之间的角度。

在所有三个示例中,您将基轴上的单位矢量与z轴的单位矢量进行比较,这些矢量根据定义彼此垂直。你期待得到什么?

对我来说,-x和z之间的角度是90°,y和z之间的角度是90°,x和z之间的角度是90°。所以所有的数据都是正确的,你期望得到什么?

编辑:

示例一:(-1,0,0)和(0,0,1)之间的角度= 90° 示例二:(0,1,0)和(0,0,1)之间的角度= 90° 实施例3:(1,0,0)和(0,0,1)= 90°之间的角度

请在笛卡尔坐标系中绘制这些矢量,并且应该清楚。另请注意,两个向量之间的角度是这两个向量定义的平面上的最小角度。

答案 2 :(得分:1)

每个轴之间的角度应为90°,计算任意两个矢量之间的角度只会给出0° - 180°范围内的值,而不是所需的360°。

此外,如果zAxis不是(0,0,1),那么您需要计算旋转轴和角度。

相反,您可以从3轴方向创建旋转矩阵,并从中创建变换对象。 y轴是x轴和z轴的交叉积:

// Given xAxis and zAxis.
Vector3f xAxis = new Vector3f(1, 0, 0);
Vector3f zAxis = new Vector3f(0, 0, 1);

// Create yAxis.
Vector3f yAxis = new Vector3f();
yAxis.cross(zAxis, xAxis);

// Create rotation matrix from axes.
Matrix4f rotationMatrix = new Matrix4f();
rotationMatrix.setRow(0, new Vector4f(xAxis));
rotationMatrix.setRow(1, new Vector4f(yAxis));
rotationMatrix.setRow(2, new Vector4f(zAxis));
rotationMatrix.setRow(3, new Vector4f(0, 0, 0, 1));

// Create transform.
Transform3D transform = new Transform3D(rotationMatrix);

// Transform points.
Vector3f vector = new Vector3f(0, 1, 0);
transform.transform(vector);
System.out.println(vector);