You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

three.js & cannon-es车辆车轮转动控制求助:无法结合横竖转动及判断进退

问题描述

我没法把车轮的水平转向和垂直转动结合起来,而且车轮只会向前转。我不知道怎么判断车辆是前进还是后退,所以没法正确处理车轮的运动。

代码实现细节

车轮水平转向

  • 按下"a"或"d"键时调用该函数
  • deg变量取值为0或Math.PI / 8
const steeringWheels = (deg) => {
    options.vehilce.wheelFrontOponaLeft.rotation.z = deg;
    options.vehilce.wheelFrontFelgaLeft.rotation.z = deg;
    options.vehilce.wheelFrontFelgaRight.rotation.z = deg;
    options.vehilce.wheelFrontOponaRight.rotation.z = deg;
}

车轮垂直转动

  • 每帧渲染时调用该函数
const moveVehicle = (delta) => {
    options.vehilce.model.position.copy(options.vehilce.cannon.chassisBody.position.clone().vadd(new CANNON.Vec3(0, -.2, 0)));
    options.vehilce.model.quaternion.copy(options.vehilce.cannon.chassisBody.quaternion);

    angularVelocity = options.vehilce.cannon.wheelBodies[0].angularVelocity.length();

    options.vehilce.wheelFrontFelgaLeft.rotateX(angularVelocity * delta);
    options.vehilce.wheelFrontFelgaRight.rotateX(angularVelocity * delta);
    options.vehilce.wheelFrontOponaLeft.rotateX(angularVelocity * delta);
    options.vehilce.wheelFrontOponaRight.rotateX(angularVelocity * delta);
    options.vehilce.wheelBackFelgaLeft.rotateX(angularVelocity * delta);
    options.vehilce.wheelBackFelgaRight.rotateX(angularVelocity * delta);
    options.vehilce.wheelBackOponaLeft.rotateX(angularVelocity * delta);
    options.vehilce.wheelBackOponaRight.rotateX(angularVelocity * delta);
}

项目运行步骤

  • 执行 npm install 安装依赖
  • 执行 parcel 启动项目

求助需求

希望能解决车辆运动时车轮的正确控制问题,欢迎各位对我的代码进行评审并给出任何改进建议。


内容的提问来源于stack exchange,提问作者Filip Kwiatkowski

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 15:45:51