3D 引擎在旋转时拉伸对象

3D Engine stretches objects out while rotating

本文关键字:对象 旋转 引擎 3D      更新时间:2023-09-26

我做了一个小的3D引擎。

但是我对旋转功能有一些问题。它们使物体不时伸展。这是数学:

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;
    }
}
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;

您正在计算y并使用此新y来计算z。您可能应该使用旧y(轮换之前):

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;