Skip to content

Commit

Permalink
Fix wheels position on the vehicle #270
Browse files Browse the repository at this point in the history
  • Loading branch information
neu5 committed Jan 23, 2024
1 parent d375e21 commit 9ecc02a
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 74 deletions.
70 changes: 68 additions & 2 deletions packages/client/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { ArcRotateCamera, Engine, Scene, Vector3 } from "@babylonjs/core";
import {
ArcRotateCamera,
Axis,
Engine,
Scene,
Space,
Vector3,
} from "@babylonjs/core";
import "toastify-js/src/toastify.css";
import { FEATURES_NAMES, features } from "@neu5/types/src";

Expand Down Expand Up @@ -85,7 +92,7 @@ ui.MobileControls.updateControls({ dialog, game, mobileControlsEls });

const startEngineLoop = ({
engine,
// playersMap,
playersMap,
scene,
}: {
engine: Engine;
Expand All @@ -110,6 +117,65 @@ const startEngineLoop = ({

camera.attachControl(canvas, true);

const maxVehicleForce = 2200;
const maxSteerValue = 0.6;
const steeringIncrement = 0.005;
const steerRecover = 0.05;
let forwardForce = 0;
let steerValue = 0;
let steerDirection = 0;

const chassisPhysicsBody = playersMap[0].chassisPhysicsBody;
const vehicle = playersMap[0].vehicle;
const wheelMeshes = playersMap[0].wheelMeshes;

scene.onBeforeRenderObservable.add(() => {
forwardForce = 0;
steerDirection = 0;

steerValue += steerDirection * steeringIncrement;
steerValue = Math.min(Math.max(steerValue, -maxSteerValue), maxSteerValue);
steerValue *= 1 - (1 - Math.abs(steerDirection)) * steerRecover;

if (vehicle === null) {
return;
}

console.log(vehicle);

vehicle.wheels[2].steering = steerValue;
vehicle.wheels[3].steering = steerValue;

vehicle.wheels[2].force = forwardForce * maxVehicleForce;
vehicle.wheels[3].force = forwardForce * maxVehicleForce;

vehicle.update();

vehicle.wheels.forEach((wheel, index) => {
if (!wheelMeshes[index]) return;
wheelMesh = wheelMeshes[index];
wheelMesh.position.copyFrom(wheel.transform.position);

if (wheelMesh.rotationQuaternion && wheel.transform.rotationQuaternion) {
wheelMesh.rotationQuaternion.copyFrom(
wheel.transform.rotationQuaternion
);
}

wheelMesh.rotate(Axis.Z, Math.PI / 2, Space.LOCAL);
});

if (vehicle.nWheelsOnGround <= 2) {
chassisPhysicsBody.setMassProperties({
centerOfMass: new Vector3(0, 0, 0),
});
} else {
chassisPhysicsBody.setMassProperties({
centerOfMass: new Vector3(0, -0.5, 0),
});
}
});

engine.runRenderLoop(() => {
scene.render();

Expand Down
35 changes: 21 additions & 14 deletions packages/client/src/scene/scene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,18 @@ const startRace = async ({

if (playersMap.length) {
playersMap.forEach((player: any) => {
player.vehicle = addVehicle({
const { chassisPhysicsBody, wheelMeshes, vehicle } = addVehicle({
colorName: player.color,
scene,
shadowGenerator,
});

Object.assign(player, {
chassisPhysicsBody,
wheelMeshes,
vehicle,
});

// player.vehicle = addRigidVehicle({
// colorName: player.color,
// scene,
Expand All @@ -198,19 +205,19 @@ const startRace = async ({
});
}

setInterval(() => {
playersMap.forEach((player: Player) => {
if (player.isCurrentPlayer) {
sendAction(
Object.entries(actions)
.filter(
([key, value]) => value === true // eslint-disable-line
)
.map(([name]) => name)
);
}
});
}, 50);
// setInterval(() => {
// playersMap.forEach((player: Player) => {
// if (player.isCurrentPlayer) {
// sendAction(
// Object.entries(actions)
// .filter(
// ([key, value]) => value === true // eslint-disable-line
// )
// .map(([name]) => name)
// );
// }
// });
// }, 50);

return { playersMap, scene };
};
Expand Down
60 changes: 2 additions & 58 deletions packages/client/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import {
Axis,
Color3,
MeshBuilder,
PhysicsBody,
PhysicsEngine,
PhysicsMotionType,
PhysicsShapeConvexHull,
Quaternion,
Space,
StandardMaterial,
Vector3,
} from "@babylonjs/core";
Expand Down Expand Up @@ -170,7 +167,7 @@ const addVehicle = ({
const physicsEngine = scene.getPhysicsEngine();
let vehicle: RaycastVehicle | null = null;

if (physicsEngine instanceof PhysicsEngine) {
if (physicsEngine !== null) {
vehicle = new RaycastVehicle(chassisPhysicsBody, physicsEngine, scene);
}

Expand Down Expand Up @@ -212,60 +209,7 @@ const addVehicle = ({
vehicle.addAntiRollAxle({ wheelA: 0, wheelB: 1, force: 10000 }); // right rear - left rear
vehicle.addAntiRollAxle({ wheelA: 2, wheelB: 3, force: 10000 }); // left front - right rear

const maxVehicleForce = 2200;
const maxSteerValue = 0.6;
const steeringIncrement = 0.005;
const steerRecover = 0.05;
let forwardForce = 0;
let steerValue = 0;
let steerDirection = 0;

scene.onBeforeRenderObservable.add(() => {
forwardForce = 0;
steerDirection = 0;

steerValue += steerDirection * steeringIncrement;
steerValue = Math.min(Math.max(steerValue, -maxSteerValue), maxSteerValue);
steerValue *= 1 - (1 - Math.abs(steerDirection)) * steerRecover;

if (vehicle === null) {
return;
}

vehicle.wheels[2].steering = steerValue;
vehicle.wheels[3].steering = steerValue;

vehicle.wheels[2].force = forwardForce * maxVehicleForce;
vehicle.wheels[3].force = forwardForce * maxVehicleForce;

vehicle.update();

vehicle.wheels.forEach((wheel, index) => {
if (!wheelMeshes[index]) return;
wheelMesh = wheelMeshes[index];
wheelMesh.position.copyFrom(wheel.transform.position);

if (wheelMesh.rotationQuaternion && wheel.transform.rotationQuaternion) {
wheelMesh.rotationQuaternion.copyFrom(
wheel.transform.rotationQuaternion
);
}

wheelMesh.rotate(Axis.Z, Math.PI / 2, Space.LOCAL);
});

if (vehicle.nWheelsOnGround <= 2) {
chassisPhysicsBody.setMassProperties({
centerOfMass: new Vector3(0, 0, 0),
});
} else {
chassisPhysicsBody.setMassProperties({
centerOfMass: new Vector3(0, -0.5, 0),
});
}
});

return vehicle;
return { chassisPhysicsBody, wheelMeshes, vehicle };
};

const TOAST_COLORS = {
Expand Down

0 comments on commit 9ecc02a

Please sign in to comment.