我制作了一个小型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;
}
}
答案 0 :(得分:5)
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;