@needle-tools/car-physics
Version:
Car physics for Needle Engine: Create physical cars with ease
56 lines (46 loc) • 1.32 kB
text/typescript
import { Behaviour, Mathf, serializable } from '@needle-tools/engine';
import { CarPhysics } from './CarPhysics';
export class CarTouchControls extends Behaviour {
(CarPhysics)
carPhysics?: CarPhysics;
private steerLeftState: number = 0;
private steerRightState: number = 0;
private throttleState: number = 0;
private breakState: number = 0;
update() {
this.throttleInput();
this.steerInput();
}
private throttleInput() {
this.carPhysics?.accelerationImpulse(Mathf.clamp(this.throttleState + this.breakState, -1, 1));
}
private steerInput() {
this.carPhysics?.steerImpulse(Mathf.clamp(this.steerLeftState + this.steerRightState, -1, 1));
}
// ---
steerLeftPress() {
this.steerLeftState = -1;
}
steerLeftRelease() {
this.steerLeftState = 0;
}
steerRightPress() {
this.steerRightState = 1;
}
steerRightRelease() {
this.steerRightState = 0;
}
throttlePress() {
this.throttleState = 1;
}
throttleRelease() {
// do nothing
}
brakePress() {
this.throttleState = 0;
this.breakState = -1;
}
brakeRelease() {
this.breakState = 0;
}
}