UNPKG

@needle-tools/car-physics

Version:

Car physics for Needle Engine: Create physical cars with ease

50 lines 2.47 kB
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; import { Behaviour, DropListener, serializable, findObjectOfType, getBoundingBox } from "@needle-tools/engine"; import { CarPhysics } from "./CarPhysics"; import { CarSelection } from "./CarSelection"; import { CarController } from "./CarController"; import { Object3D } from "three"; export class CarDropHelper extends Behaviour { dropListener = null; onEnable() { this.dropListener?.onDropped.addEventListener(this.onDropped); } onDisable() { this.dropListener?.onDropped.removeEventListener(this.onDropped); } onDropped = (evt) => { let object = evt.object; // try to find the correct rotation // if the object X is longer than Z, rotate it to face forward // it's potentially now rotated by 180 degrees but not sure how we can automatically determine that without wheels const bounds = getBoundingBox(evt.object); const lengthX = bounds.max.x - bounds.min.x; const lengthZ = bounds.max.z - bounds.min.z; if (lengthX > lengthZ) { const parent = evt.object.parent; const obj = new Object3D(); obj.matrix.copy(evt.object.matrix); obj.matrix.decompose(obj.position, obj.quaternion, obj.scale); obj.add(evt.object); evt.object.matrix.identity(); evt.object.matrix.decompose(evt.object.position, evt.object.quaternion, evt.object.scale); evt.object.rotateY(Math.PI / 2); parent?.add(obj); object = obj; } object.position.y += 1; object.addComponent(CarPhysics); const ctrl = object.addComponent(CarController); const sel = findObjectOfType(CarSelection); sel?.selectCar(ctrl); }; } __decorate([ serializable(DropListener) ], CarDropHelper.prototype, "dropListener", void 0); //# sourceMappingURL=CarDropHelper.js.map