This was edited with ai i could find the reason myself nether could ai i want bmgdrive type driving phis
var CarController = pc.createScript(‘carController’);
CarController.prototype.initialize = function () {
// — MOMENTUM & WEIGHT —
this.mass = 1500;
this.velocity = new pc.Vec3();
this.speed = 0;
this.steerAngle = 0;
// --- SUSPENSION ---
this.suspRestLength = 0.8;
this.springStiffness = 20000;
this.dampingFactor = 1500;
this.prevCompression = [0, 0, 0, 0]; // Initialized array
// --- TIRE PHYSICS ---
this.tractionGrip = 7.5;
this.driftGrip = 2.2;
// --- ENTITIES ---
this.body = this.entity.findByName('Body');
this.wheels = [
this.entity.findByName('WheelFL'),
this.entity.findByName('WheelFR'),
this.entity.findByName('WheelRL'),
this.entity.findByName('WheelRR')
];
this.resetPos = this.entity.getPosition().clone();
this.resetRot = this.entity.getRotation().clone();
if (this.app.keyboard) {
this.app.keyboard.on(pc.EVENT_KEYDOWN, this.onKeyDown, this);
}
};
CarController.prototype.update = function (dt) {
if (dt > 0.1) dt = 0.1; // Prevention of physics “explosions”
var app = this.app;
var kb = app.keyboard;
var throttle = (kb.isPressed(pc.KEY_W) ? 1 : 0) - (kb.isPressed(pc.KEY_S) ? 1 : 0);
var steerInput = (kb.isPressed(pc.KEY_A) ? 1 : 0) - (kb.isPressed(pc.KEY_D) ? 1 : 0);
var groundHits = 0;
var avgNormal = new pc.Vec3(0, 1, 0);
// 1. INDEPENDENT SUSPENSION (forEach avoids the " 0.45) {
groundHits++;
avgNormal.add(res.normal);
// Suspension Force Logic
var compression = Math.max(0, self.suspRestLength - res.distance);
var springForce = compression * self.springStiffness;
var damping = ((compression - self.prevCompression[i]) / dt) * self.dampingFactor;
self.prevCompression[i] = compression;
// Apply upward force based on simulated mass
self.velocity.y += ((springForce + damping) / self.mass) * dt;
} else {
self.prevCompression[i] = 0;
}
});
if (groundHits > 0) {
avgNormal.scale(1 / groundHits).normalize();
this.applyGroundPhysics(avgNormal, throttle, steerInput, dt);
} else {
this.applyAirPhysics(dt, throttle, steerInput);
}
// 2. FINAL INTEGRATION
var translation = this.velocity.clone().scale(dt);
if (!isNaN(translation.x)) {
this.entity.translate(translation);
}
this.updateVisuals(dt);
};
CarController.prototype.applyGroundPhysics = function (normal, throttle, steer, dt) {
// Surface Orientation
var currentRot = this.entity.getRotation();
var forward = this.entity.forward.clone();
var right = new pc.Vec3().cross(forward, normal).normalize();
var correctedForward = new pc.Vec3().cross(normal, right).normalize();
var targetMat = new pc.Mat4().setLookAt(new pc.Vec3(), correctedForward.scale(-1), normal);
var targetRot = new pc.Quat().setFromMat4(targetMat);
this.entity.setRotation(new pc.Quat().lerp(currentRot, targetRot, 10 * dt));
// Steering Geometry
this.speed = pc.math.lerp(this.speed, throttle * 55, (throttle !== 0 ? 1.5 : 0.8) * dt);
var sensitivity = pc.math.lerp(1.0, 0.3, Math.abs(this.speed) / 55);
this.steerAngle = pc.math.lerp(this.steerAngle, steer * sensitivity, 5 * dt);
var turnRadius = (this.speed / 2.8) * Math.tan(this.steerAngle * 40 * 0.01745);
this.entity.rotateLocal(0, turnRadius * dt * 57.29, 0);
// Friction & Slip Angle
var moveDir = this.velocity.clone().normalize();
var slipAngle = Math.abs(this.entity.forward.dot(moveDir));
var grip = pc.math.lerp(this.driftGrip, this.tractionGrip, slipAngle);
var targetVel = this.entity.forward.clone().scale(this.speed);
this.velocity.x = pc.math.lerp(this.velocity.x, targetVel.x, grip * dt);
this.velocity.z = pc.math.lerp(this.velocity.z, targetVel.z, grip * dt);
this.velocity.y = pc.math.clamp(this.velocity.y, -1, 5);
};
CarController.prototype.applyAirPhysics = function (dt, throttle, steer) {
this.velocity.y -= 25 * dt;
this.velocity.y = pc.math.clamp(this.velocity.y, -40, 40);
this.entity.rotateLocal(throttle * 110 * dt, 0, -steer * 110 * dt);
};
CarController.prototype.updateVisuals = function (dt) {
if (this.body) {
this.body.setLocalEulerAngles(this.speed * 0.1, 0, -this.steerAngle * (this.speed * 0.4));
}
this.wheels.forEach(function(w, i) {
if (!w) return;
if (i < 2) w.setLocalEulerAngles(90, -90 + (this.steerAngle * 35), 0);
if (w.children && w.children.length > 0) {
w.children[0].rotateLocal(0, this.speed * 20 * dt, 0);
}
}, this);
};
CarController.prototype.onKeyDown = function (e) {
if (e.key === pc.KEY_R) {
this.speed = 0;
this.velocity.set(0, 0, 0);
this.entity.setPosition(this.resetPos.x, this.resetPos.y + 1, this.resetPos.z);
this.entity.setRotation(this.resetRot);
}
};