如题,欧拉角对旋转中 万向节死锁对问题 可以给一个处理对办法么,目的就是实现一个正方体的 前后左右的任意旋转
我使用的旋转方法如下,交替旋转几次就全乱了
旋转Y轴
this.testNode.is3DNode = true; // 这个必须要有
console.log(this.testNode.eulerAngles);
this.rotationY += 90;
let newRotation = new cc.Quat();
let newEulerAngles = this.testNode.eulerAngles;
newEulerAngles.y = this.rotationY;
newRotation.fromEuler(newEulerAngles);
this.testNode.setRotation(newRotation.x, newRotation.y, newRotation.z, newRotation.w);
旋转X轴:
this.testNode.is3DNode = true; // 这个必须要有
console.log(this.testNode.eulerAngles);
this.rotationX += 90;
let newRotation = new cc.Quat();
let newEulerAngles = this.testNode.eulerAngles;
newEulerAngles.x = this.rotationX;
newRotation.fromEuler(newEulerAngles);
this.testNode.setRotation(newRotation.x, newRotation.y, newRotation.z, newRotation.w);
感谢cocos技术大拿哲峰 给的解决方法,这里贴出代码!
invert (out, a) {
var a0 = a.x, a1 = a.y, a2 = a.z, a3 = a.w;
var dot = a0 * a0 + a1 * a1 + a2 * a2 + a3 * a3;
var invDot = dot ? 1.0 / dot : 0;
// TODO: Would be faster to return [0,0,0,0] immediately if dot == 0
out.x = -a0 * invDot;
out.y = -a1 * invDot;
out.z = -a2 * invDot;
out.w = a3 * invDot;
return out;
},
transformQuat (out, a, q) {
// benchmarks: http://jsperf.com/quaternion-transform-vec3-implementations
var x = a.x, y = a.y, z = a.z;
var qx = q.x, qy = q.y, qz = q.z, qw = q.w;
// calculate quat * vec
var ix = qw * x + qy * z - qz * y;
var iy = qw * y + qz * x - qx * z;
var iz = qw * z + qx * y - qy * x;
var iw = -qx * x - qy * y - qz * z;
// calculate result * inverse quat
out.x = ix * qw + iw * -qx + iy * -qz - iz * -qy;
out.y = iy * qw + iw * -qy + iz * -qx - ix * -qz;
out.z = iz * qw + iw * -qz + ix * -qy - iy * -qx;
return out;
},
mul (out, a, b) {
var ax = a.x, ay = a.y, az = a.z, aw = a.w,
bx = b.x, by = b.y, bz = b.z, bw = b.w;
out.x = ax * bw + aw * bx + ay * bz - az * by;
out.y = ay * bw + aw * by + az * bx - ax * bz;
out.z = az * bw + aw * bz + ax * by - ay * bx;
out.w = aw * bw - ax * bx - ay * by - az * bz;
return out;
},
fromAxisAngle (out, axis, rad) {
rad = rad * 0.5;
var s = Math.sin(rad);
out.x = s * axis.x;
out.y = s * axis.y;
out.z = s * axis.z;
out.w = Math.cos(rad);
return out;
},
rotateAround (out, rot, axis, rad) {
var v3_tmp = new cc.Vec3();
var q_tmp = new cc.Quat();
// get inv-axis (local to rot)
this.invert(q_tmp, rot);
this.transformQuat(v3_tmp, axis, q_tmp);
// rotate by inv-axis
this.fromAxisAngle(q_tmp, v3_tmp, rad);
this.mul(out, rot, q_tmp);
return out;
},
使用此方法进行旋转
this.testNode.quat = this.rotateAround(new cc.Quat(), this.testNode.quat, cc.v3(0, 1, 0), Math.PI * 0.5);
可否补充下注释呢
cc.v3(0, 1, 0)是绕着 y 轴往右旋转,试了一下,cc.v3(1, 1, 0) 是往右下角旋转,但奇怪的是前者一切正常,后者在旋转途中发生了变形,大哥知道是怎么回事吗?