【问题标题】:Position a body in cannon.js relative to local rotation相对于局部旋转在 cannon.js 中定位一个主体
【发布时间】:2017-10-19 16:26:03
【问题描述】:

我在 cannon.js 中有一个应用了四元数旋转的主体。我想将它沿一个向量相对于它的局部旋转移动 100 个单位。

例如

let body = new CANNON.Body({ mass: 0 });
body.quaternion.setFromAxisAngle(new CANNON.Vec3(0,0,1),(2*Math.PI)/6);
body.position.set(0,0,100); //this is wrong

使用body.position.set(x, y, z); 相对于世界移动身体而不是局部旋转。我想我需要在应用四元数之后添加一个向量,但是 cannon.js 的文档并不是特别有用,所以我无法弄清楚如何去做。

【问题讨论】:

    标签: javascript three.js cannon.js


    【解决方案1】:

    使用Quaternion#vmult 方法旋转向量,使用Vec3#add 将结果添加到位置。

    let body = new CANNON.Body({ mass: 0 });
    body.quaternion.setFromAxisAngle(new CANNON.Vec3(0,0,1),(2*Math.PI)/6);
    let relativeVector = new CANNON.Vec3(0,0,100);
    
    // Use quaternion to rotate the relative vector, store result in same vector
    body.quaternion.vmult(relativeVector, relativeVector);
    
    // Add position and relative vector, store in body.position
    body.position.vadd(relativeVector, body.position);
    

    【讨论】:

    猜你喜欢
    • 2014-03-15
    • 2015-07-17
    • 2012-11-04
    • 1970-01-01
    • 2018-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-15
    相关资源
    最近更新 更多