gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 04:11:22 27 4
gpt4 key购买 nike

我制作了一个小型 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com