javascript - 3D Engine 在旋转时拉伸(stretch)物体

标签 javascript canvas 3d-engine

我制作了一个小型 3d 引擎。

但我在旋转功能方面遇到了一些问题。 它们使物体不时拉伸(stretch)。 这是数学:

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;

关于javascript - 3D Engine 在旋转时拉伸(stretch)物体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6081281/

相关文章:

javascript - HTML 5 Canvas 和 jquery?

javascript - 使用 Javascript 添加到正文时 HTML5 Canvas 会重置

actionscript - 谷歌地图如何制作全景图?

c# - 轻量级 3D 图形引擎 .NET(紧凑且完整的框架)

javascript - 是否有一些与 STL 集成的 JavaScript 3d 物理引擎?

javascript - 我应该如何显示 getUserMedia()

javascript - 未处理的PromiseRejection警告: MongooseServerSelectionError

c# - Controller 针对单个请求执行两次

javascript - Bluebird 在与 Sequelize/MySQL 一起使用时显示损坏的堆栈跟踪

javascript - 获取一个点从另一个点旋转一定距离的X和Y