Javascript 3D引擎在旋转时向外拉伸对象
我做了一个小小的3d引擎 但我对旋转函数有一些问题。 它们使物体不时伸展。 以下是数学:Javascript 3D引擎在旋转时向外拉伸对象,javascript,canvas,3d-engine,Javascript,Canvas,3d Engine,我做了一个小小的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.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.rotateX=函数(角度){
var cos=数学cos(角度);
var sin=数学sin(角度);
对于(var i=0;i
您正在计算y
,并使用此新的y
计算z
。您可能应该使用旧的y
(旋转前):
我没有看过你的代码,但是你确定你是围绕着对象的中心旋转的吗…通过一些只使用其中一个函数的测试,我看到这些函数以某种方式延伸了对象。。。编辑。可能是整数舍入问题?Thanx,解决方案有时是一件小事:P
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;
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;