Search code examples
javaopengl-esrotationminecrafteuler-angles

Cube 3D points rotations X & Z


Include details about your goal:

  • So first of all, my goal is to make the points rotations of a cube aroud X, Y, and Z axis to rotate the cube itself.

Describe expected:

  • I expect to have that after an 17.18° rotation around X axis:

enter image description here

  • I expect to have that after an 17.18° rotation around Z axis:

enter image description here

Actual results:

  • Black shape is the default cube with no rotations

  • White shape is the rotated cube.

  • My actual result after an 17.18° rotation around X axis with my formula:

enter image description here

  • My actual result after an 17.18° rotation around Z axis with my formula:

enter image description here

Show what you’ve tried and tell us what you found (on this site or elsewhere) and why it didn’t meet your needs. You can get better answers when you provide research.

I have saw a few site that describe exactly what I want:

Show some code

  • First of all i am using Java because my problem is my Minecraft virtually shape don't meet with the real one;

Rotation methods

public void rotateX() {
        for(int i = 0; i < points.size(); i++) {
            
            Vector vector = points.get(i);
            
          double sinTheta = Math.sin(eulerAngle.getX());
          double cosTheta = Math.cos(eulerAngle.getX());
            
          double y = vector.getY();
          double z = vector.getZ();
            
            vector.setY(y * cosTheta - z * sinTheta);
        vector.setZ(z * cosTheta + y * sinTheta);
    }
}
    
public void rotateY() {
    for(int i = 0; i < points.size(); i++) {
            
        Vector vector = points.get(i);
            
        double sinTheta = Math.sin(eulerAngle.getY());
        double cosTheta = Math.cos(eulerAngle.getY());
            
        double x = vector.getX();
        double z = vector.getZ();
            
        vector.setX(x * cosTheta - z * sinTheta);
      vector.setZ(z * cosTheta + x * sinTheta);
    }
}
    
public void rotateZ() {
        for(int i = 0; i < points.size(); i++) {
      
            Vector vector = points.get(i);
      
            double sinTheta = Math.sin(eulerAngle.getZ());
          double cosTheta = Math.cos(eulerAngle.getZ());
      
          double x = vector.getX();
          double y = vector.getY();
          
            vector.setX(x * cosTheta - y * sinTheta);
        vector.setY(y * cosTheta + x * sinTheta);
    }
}

My setup method

    public void setupPoints() {
    
    ArrayList<Vector> points = new ArrayList<Vector>();
    
    points.add(getA());
    points.add(getB());
    points.add(getC());
    points.add(getD());
    points.add(getE());
    points.add(getF());
    points.add(getG());
    points.add(getH());
    
    this.points = points;
    
    rotateZ();
    rotateY();
    rotateX();
}

My points locations are (i know its not an exact cube):

enter image description here

My enviroment:

enter image description here

note: Only Y rotation works perfectly, i noticed that the rotation on the Y axis corresponds to that of the Z axis in a coordinate system where Y would be the "height", its the first time that i work on 3d space plan so i am learning. Thanks if you read until here.


Solution

  • Your rotation methods are correct, but they rotate around axes that pass through the origin.

    You are expecting them to rotate around an axis that passes through the center of the object. You don't see the problem with the rotation around Y axis because it happens to pass through the center of the cube.

    You can fix this by "moving" the object to the origin before rotating, and moving it back after rotating. For example, for the X rotation:

          double y = vector.getY() - objectCenterY;
          double z = vector.getZ() - objectCenterZ;
            
          vector.setY(y * cosTheta - z * sinTheta + objectCenterY);
          vector.setZ(z * cosTheta + y * sinTheta + objectCenterZ);