I cannot combine the horizontal movement of the wheels with the vertical movement.
Additionally, the wheels only move forward. I have no idea how to check if the vehicle is moving forward or backward in order to be able to properly handle the movement of the wheels.
Watch the video: https://youtu.be/NDMsXG6lmc8
How it looks from the code side:
Turning the wheels horizontally:
- The function is run after pressing the "a" or "d" key
- The deg variable takes values {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;
}
Rotating the wheels vertically
- Function called when each frame is rendered
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);
}
You can check out the full code here: https://github.com/kwiatu-dev/sphere-collision/tree/feature
To run the project, type:
- npm install
- parcel
I would like to be able to properly control the movement of the wheels while the vehicle is moving. If you are able to help me, please help.
Feel free to review the code and give me any tips. I'm just starting to learn.