【发布时间】:2022-02-11 18:24:02
【问题描述】:
我在 A-frame 场景版本 1.0.4 中加载了我的机器人的 gltf 模型。到目前为止,我可以围绕它的 x、y、z 轴转动机器人,但是当我尝试将其向前移动时,它仍会沿其初始方向移动,而不是它所面对的方向。我希望能够在不使用任何外部库的情况下用我的键盘控制它的动作。我相信我必须使用四元数,但我还没有弄清楚如何使用它们。到目前为止,这是我的 A 帧代码。
<a-scene id="myScene">
<a-entity environment="preset:forest;"></a-entity>
<a-entity gltf-model="#humanoid" id="robot" position="0 20 0" rotation="0 0 0"
scale="0.0001 0.0001 0.0001" static-body>
</a-entity>
</a-scene>
这是我的脚本
const robot = document.getElementById('robot');
const update = () => {
if (keys.forward) {
let {x,y,z} = robot.object3D.position;
let ry = robot.object3D.rotation.y;
z += Math.cos(ry * Math.PI/180)/12;
x += Math.sin(ry * Math.PI/180)/12;
robot.object3D.position.set(x, y, z);
}
else if (keys.backwards) {
let {x, y, z} = robot.object3D.position;
let ry = robot.object3D.rotation.y;
z -= Math.cos(ry * Math.PI/180)/10;
x -= Math.sin(ry * Math.PI/180)/10;
robot.object3D.position.set(x, y, z);
}
if (keys.turnLeft) {
let {x, y, z} = robot.getAttribute('rotation');
y += 0.25;
robot.setAttribute('rotation',{x,y,z});
}
else if (keys.turnRight) {
let {x, y, z} = robot.getAttribute('rotation');
y -= 0.25;
robot.setAttribute('rotation',{x,y,z});
}
}
【问题讨论】:
-
听起来this线程很相似
-
这对相机按预期工作,我可以在相机面对的方向移动机器人,但不能在它面对的方向移动它。它显示错误“无法读取未定义的属性(读取'getWorldDirection')”。
-
@jacob1 用你的方法和 getWorldDirection 做了一个 anwser
标签: javascript html aframe