cocos3d 怎样让摄像机朝根据当前方位前后左右移动,现在这个代码的方位是写死的,如果摄像机角度变了,移动方向就会出问题

cocos3d 怎样让摄像机朝根据当前方位前后左右移动,现在这个代码的方位是写死的,如果摄像机角度变了,移动方向就会出问题图片

start() {

    const q = this.node.rotation;

    const right = this.rotateVec3(q, new Vec3(1, 0, 0));

    const up = this.rotateVec3(q, new Vec3(0, 1, 0));

    const forward = this.rotateVec3(q, new Vec3(0, 0, 1));

    console.log(right.toString(), up.toString(), forward.toString());

  }

  public rotateVec3(q: Quat, v: Vec3, dest: Vec3 | null = null): Vec3 {

    if (dest === null) dest = new Vec3();

    const qx = q.x,

      qy = q.y,

      qz = q.z,

      qw = q.w;

    const x = v.x,

      y = v.y,

      z = v.z;

    const ix = x * qw + qy * z - qz * y,

      iy = y * qw + qz * x - qx * z,

      iz = z * qw + qx * y - qy * x,

      iw = -qx * x - qy * y - qz * z;

    dest.x = ix * qw + iw * -qx + iy * -qz - iz * -qy;

    dest.y = iy * qw + iw * -qy + iz * -qx - ix * -qz;

    dest.z = iz * qw + iw * -qz + ix * -qy - iy * -qx;

    return dest;

  }

算出旋转后三个方向再进行计算

2赞

多谢,完美解决我的问题了,cocos社区真活跃

该主题在最后一个回复创建后14天后自动关闭。不再允许新的回复。