Search code examples
javamatrix3drotationjava-3d

Rotate 3D object with Direction and Axis


I am working on showing a 3D model (IFC) in Java3D. I need to rotate the object with 2 vectors, a direction vector and a axis vector that are given by the IFC model.

But I can't figure out how to get the right angle.

Code:

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

The axisVector is always (0.0, 0.0, 1.0), so it needs to be rotated on the Z-axis

But when I calculate the angle it seems always 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)

I know through testing that -1.0 means inverted so 180°.

Can some one help me understanding what I am doing wrong?

Edit

Documentation for the placement object (Direction and axis)

Screenshots of the results:

  • Orange: are the floors
  • Green: is the roof
  • Red: is de rotation point
  • Left: Side perspective
  • Right: Top perspective
  • The group of 3 slabs have direction direction (0.0, 1.0, 0.0)
  • The group of 2 slabs have direction direction (1.0, 0.0, 0.0)
  • Roof has direction: (-1.0, 0.0, 0.0)

I did the *2 test to simulate the 180° of -1.0. as you can see in the last example the roof is correctly drawn.

enter image description here

enter image description here


Solution

  • You don't need the angle between the direction vector and the axis vector. Since your axis vector is always (0.0, 0.0, 1.0), you can define a constant vector of (1.0, 0.0, 0.0) to which you can compare the direction vector, i.e.:

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