How to rotate an object in Java 3D?

I have a cone that I painted in Java 3D with the following code:

Cone cone = new Cone(2f, 3f);

Transform3D t3d = new Transform3D();
TransformGroup coneTransform = new TransformGroup(t3d);
coneTransform.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);

t3d.setTranslation(new Vector3f(0f,0f,0f);
coneTransform.setTransform(t3d);
coneTransform.addChild(cone);

this.addChild(coneTransform);

Suppose I have a cone sitting at (1,1,1), and I want the tip of the cone to point to an imaginary line passing through (0,0,0) and (1,1,1) .. . How can i do this?

Here is an example of what I tried:

Transform3D t3d = new Transform3D();  

Vector3f direction = new Vector3f(1,2,1);    

final double angleX = direction.angle(new Vector3f(1,0,0));
final double angleY = direction.angle(new Vector3f(0,1,0));
final double angleZ = direction.angle(new Vector3f(0,0,1));

t3d.rotX(angleX);
t3d.rotY(angleY);
t3d.rotZ(angleZ);

t3d.setTranslation(direction);

coneTransform.setTransform(t3d);

Thanks in advance for your help!

+3
source share
5 answers

I finally understood what I would like to do using the Quaternions, which I learned about here: http://www.cs.uic.edu/~jbell/Courses/Eng591_F1999/outline_2.html Here is my solution.

Creating a cone:

 private void attachCone(float size) {
        Cone cone = new Cone(size, size* 2);

        // The group for rotation
        arrowheadRotationGroup = new TransformGroup();
        arrowheadRotationGroup.
             setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        arrowheadRotationGroup.addChild(cone);

        // The group for positioning the cone
        arrowheadPositionGroup = new TransformGroup();
        arrowheadPositionGroup. 
              setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        arrowheadPositionGroup.addChild(arrowheadRotationGroup);

        super.addChild(arrowheadPositionGroup);
    }

, , (0,0,0) (direction.x, direction.y, direction.z), :

private final Vector3f yAxis = new Vector3f(0f, 1f, 0f);
private Vector3f direction; 

private void rotateCone() {
        // Get the normalized axis perpendicular to the direction 
        Vector3f axis = new Vector3f();
        axis.cross(yAxis, direction);
        axis.normalize();

        // When the intended direction is a point on the yAxis, rotate on x
        if (Float.isNaN(axis.x) && Float.isNaN(axis.y) && Float.isNaN(axis.z)) 
        {
            axis.x = 1f;
            axis.y = 0f;
            axis.z = 0f;
        }
        // Compute the quaternion transformations
        final float angleX = yAxis.angle(direction);
        final float a = axis.x * (float) Math.sin(angleX / 2f);
        final float b = axis.y * (float) Math.sin(angleX / 2f);
        final float c = axis.z * (float) Math.sin(angleX / 2f);
        final float d = (float) Math.cos(angleX / 2f);

        Transform3D t3d = new Transform3D();
        Quat4f quat = new Quat4f(a, b, c, d);
        t3d.set(quat);
        arrowheadRotationGroup.setTransform(t3d);

        Transform3D translateToTarget = new Transform3D();
        translateToTarget.setTranslation(this.direction);
        arrowheadPositionGroup.setTransform(translateToTarget);
    }
+1

Java 3D , . , , Transform3D.
:

Transform3D rotation = new Transform3D();
Transform3D temp = new Transform3D();

rotation.rotX(Math.PI/2);
temp.rotZ(Math.PI/2);
rotation.mul(temp); // multiply the 2 transformation matrices together.

Math.PI, , , Math.PI 180 .

- Vector3fs angle(). , - . , . , , . [, -, API]

+3

java3D.

, 4 , .

1) ( )
2) 3) 4)

4x4.

, ( , , , - , 2 col 3 3 col 2 ).

1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1

. - . .

, (1, 0, 0, 0) x (, , ). (0, 1, 0, 0) y (, , ). (0, 0, 1, 0) Z (, , ). (0, 0, 0, 1) , .

, X.

, , (1, 0, 0, 0) . Up - (0, 1, 0, 0) - 0, 0, 0 1. , ? , z. ? , , . , , . ? . ,

 0 0 1 0
 0 1 0 0
-1 0 0 0
 0 0 0 1

. , , ( vPos ​​vFocus). vPos vFocus, vPos vFocus (vFocus.x - vPos.x, vFocus.y - vPos.y, vFocus.z - vPos.z, vFocus.w - vPos.w), , "1" w, "0". , , 1 ws 0. , , vFocus, vDir. , vPos ​​vFocus. vDir (vDir.x/length, vDir.y/length, vDir.z/length, vDir.w/length), , 1.

ponit 3- 4- . assuem - (0, 1, 0, 0) vUp. , vUp , ( ) , vDir vUp. vLat. .. , . , - vLat vDir, 4 .

:

vLat.x vUp.x vDir.x vPos.x
vLat.y vUp.y vDir.y vPos.y
vLat.z vUp.z vDir.z vPos.z
vLat.w vUp.w vDir.w vPos.w

, , (0, 1, 0, 0) , .

+2

, :

coneTransform.rotX(Math.PI / 4);
coneTransform.rotY(Math.PI / 4);
0
source

You can pass Transform3D rotation matrix. you can get the rotation matrix using the rotation matrix calculator online: http://toolserver.org/~dschwen/tools/rotationmatrix.html here is my example:

    Matrix3f mat = new Matrix3f(0.492403876506104f, 0.586824088833465f,
            -0.642787609686539f, 0.413175911166535f, 0.492403876506104f,
            0.766044443118978f, 0.766044443118978f, -0.642787609686539f, 0f);

    Transform3D trans = new Transform3D();

    trans.set(mat);
0
source

Source: https://habr.com/ru/post/1716040/


All Articles