最近想实现一下如下图功能。摄像机盯着一个节点,当你手指在屏幕滑动的时候,摄像机会按照你滑动的方向来旋转。
从论坛里抄了一些作业
const rotateAround = (center: Vec3, axis: Vec3, angle: number, isLimit: boolean = false) => {
let pos: Vec3 = this.node.position;// 当前节点就是摄像机
let rot: Quat = Quat.fromAxisAngle(new Quat, axis, angle);
let dir: Vec3 = Vec3.subtract(new Vec3, this.node.position, center);
Vec3.transformQuat(dir, dir, rot);
this.curOffset = new Vec3(dir);
Vec3.add(pos, center, dir);
var myrot = this.node.rotation;
Quat.multiply(myrot, rot, myrot);
if (isLimit) {
let eulerAngle = new Vec3;
myrot.getEulerAngles(eulerAngle);
if (eulerAngle.x < -70 || eulerAngle.x > 70)
return;
}
this.node.rotation = myrot;
}
//targetNode 是目标的节点
rotateAround(this.targetNode.position, new Vec3(0, -1, 0), delta.x / 300);
rotateAround(this.targetNode.position, Vec3.RIGHT, delta.y / 300, true)
但是如果你旋转的角度越大,这个手指滑动的方向就偏移的越离谱。我感觉x,y分量分别绕旋转UP和RIGHT轴旋转是不对的。如果摄像机视角已经被旋转到了从头顶往下看或者从底部往上看的时候。手指滑动就已经错乱的厉害了。求个指导方法。哎,四元数真是太难了。