Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

3D Engine stretches objects out while rotating

I made a little 3d engine.

But I have some problems with the rotating functions. They make the object stretch out from time to time. Here's the math:

this.rotateX = function(angle) {
    var cos = Math.cos(angle);
    var sin = Math.sin(angle);

    for(var i = 0; i < this.points.length; i++) {
        this.points[i].y = sin * this.points[i].z + cos * this.points[i].y;
        this.points[i].z = -sin * this.points[i].y + cos * this.points[i].z;
    }
}

this.rotateY = function(angle) {
    var cos = Math.cos(angle);
    var sin = Math.sin(angle);

    for(var i = 0; i < this.points.length; i++) {
        this.points[i].x = cos * this.points[i].x - sin * this.points[i].z;
        this.points[i].z = sin * this.points[i].x + cos * this.points[i].z;
    }
}

this.rotateZ = function(angle) {
    var cos = Math.cos(angle);
    var sin = Math.sin(angle);

    for(var i = 0; i < this.points.length; i++) {
        this.points[i].x = cos * this.points[i].x + sin * this.points[i].y;
        this.points[i].y = -sin * this.points[i].x + cos * this.points[i].y;
    }
}
like image 828
seymar Avatar asked May 21 '11 11:05

seymar


1 Answers

this.points[i].y = sin * this.points[i].z + cos * this.points[i].y;
this.points[i].z = -sin * this.points[i].y + cos * this.points[i].z;

You are calculating y and using this new y to calculate z. You should probably use old y (before rotation):

var y = sin * this.points[i].z + cos * this.points[i].y;
var z = -sin * this.points[i].y + cos * this.points[i].z;
this.points[i].y = y;
this.points[i].z = z;
like image 64
Piotr Praszmo Avatar answered Sep 24 '22 13:09

Piotr Praszmo