3D 엔진 회전하는 동안 물체를 뻗어
-
08-09-2020 - |
문제
약 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;
. 제휴하지 않습니다 StackOverflow