3D Cube not transforming correctly?

  Kiến thức lập trình

so I have a simple program which loads polygon coordinates from file, and draws from them a cube on the screen.

Problem is, the cube doesn’t seem to keep its form as a cube, like can be seen in the image below:

Distorted cube

Another picture of the beginning of the draw, where the cube is in form (without perspective/projection):

Cube beginning

My rotation function is, as I can’t figure out where else the problem could be:

public void RotatePoint(int index){
        double sx = Math.sin(Math.toRadians(this.rotation.x)), sy = Math.sin(Math.toRadians(this.rotation.y)), sz = Math.sin(Math.toRadians(this.rotation.z));
        double cx = Math.cos(Math.toRadians(this.rotation.x)), cy = Math.cos(Math.toRadians(this.rotation.y)), cz = Math.cos(Math.toRadians(this.rotation.z));
        SPOT spot = this.painted.Get(index);
        this.painted.Get(index).y = (cx * (spot.y - this.center.y) - sx * (spot.z - this.center.z)) + this.center.y;
        this.painted.Get(index).z = (sx * (spot.y - this.center.y) + cx * (spot.z - this.center.z)) + this.center.z;
        spot = this.painted.Get(index);
        this.painted.Get(index).x = (cy * (spot.x - this.center.x) - sy * (spot.z - this.center.z)) + this.center.x;
        this.painted.Get(index).z = (sy * (spot.x - this.center.x) + cy * (spot.z - this.center.z)) + this.center.z;
        spot = this.painted.Get(index);
        this.painted.Get(index).x = (cz * (spot.x - this.center.x) - sz * (spot.y - this.center.y)) + this.center.x;
        this.painted.Get(index).y = (cz * (spot.y - this.center.y) + sz * (spot.x - this.center.x)) + this.center.y;
    }

If I try only x- or y-axis rotation (by commenting out the rest), the cube seems to rotate just fine, keeping its form.

I’ve tried following multiple examples and searched for hours for help, but seem to end up with the same result everytime, a distorted cube.

Any help would be much appreciated, and I’ll provide more info if needed.

LEAVE A COMMENT