@threenative/physics 0.3.2 → 0.3.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{Area3D-BQDiAwQb.d.ts → Area3D-DI_kCf7I.d.ts} +70 -1
- package/dist/index.d.ts +72 -3
- package/dist/index.js +350 -3
- package/dist/native/index.d.ts +2 -2
- package/dist/native/index.js +346 -4
- package/dist/navigation/index.d.ts +1 -1
- package/package.json +3 -3
|
@@ -154,6 +154,62 @@ interface IPhysicsCharacterState {
|
|
|
154
154
|
readonly groundCollider?: number;
|
|
155
155
|
readonly groundNormal?: IPhysicsVector3;
|
|
156
156
|
}
|
|
157
|
+
/** One record is wheel contact (0 or 1), suspension length in metres, and rotation in radians. */
|
|
158
|
+
declare const PHYSICS_VEHICLE_WHEEL_STRIDE = 3;
|
|
159
|
+
/** Godot's `VehicleWheel3D`, as backend-neutral data: the numbers, never a vehicle object. */
|
|
160
|
+
interface IPhysicsVehicleWheelOptions {
|
|
161
|
+
/** The wheel's attachment point in chassis-local space. */
|
|
162
|
+
readonly position: IPhysicsVector3;
|
|
163
|
+
readonly wheelRadius: number;
|
|
164
|
+
readonly suspensionRestLength: number;
|
|
165
|
+
/**
|
|
166
|
+
* Suspension stiffness, mass-normalised: the square of the suspension's natural frequency in
|
|
167
|
+
* rad/s, not newtons per metre. Sag is about `9.81 / (4 * suspensionStiffness)` metres, so `100`
|
|
168
|
+
* is a road car and `20` — Godot's default number — bottoms the strut out under its own weight.
|
|
169
|
+
*/
|
|
170
|
+
readonly suspensionStiffness: number;
|
|
171
|
+
/** Damping while the suspension compresses. */
|
|
172
|
+
readonly dampingCompression: number;
|
|
173
|
+
/** Damping while the suspension extends. */
|
|
174
|
+
readonly dampingRelaxation: number;
|
|
175
|
+
/** How hard this tyre grips; a higher value brakes harder and flips the car more easily. */
|
|
176
|
+
readonly wheelFrictionSlip: number;
|
|
177
|
+
/** Maximum travel either side of the rest length, in metres. */
|
|
178
|
+
readonly maxSuspensionTravel?: number;
|
|
179
|
+
readonly useAsSteering: boolean;
|
|
180
|
+
readonly useAsTraction: boolean;
|
|
181
|
+
}
|
|
182
|
+
interface IPhysicsVehicleCreateOptions {
|
|
183
|
+
/** The chassis body the wheels hang from; it must be dynamic. */
|
|
184
|
+
readonly bodyId: number;
|
|
185
|
+
/** Chassis-local forward axis: `0` = x, `2` = z. */
|
|
186
|
+
readonly forwardAxis: 0 | 2;
|
|
187
|
+
/**
|
|
188
|
+
* The wheels' axle, chassis-local. The vehicle's forward is `up × axle`, so its sign decides
|
|
189
|
+
* which way a positive `engineForce` drives and which way a positive `speed` means.
|
|
190
|
+
*/
|
|
191
|
+
readonly axle: IPhysicsVector3;
|
|
192
|
+
readonly wheels: readonly IPhysicsVehicleWheelOptions[];
|
|
193
|
+
}
|
|
194
|
+
interface IPhysicsVehicleInput {
|
|
195
|
+
/** Newtons per traction wheel. */
|
|
196
|
+
readonly engineForce: number;
|
|
197
|
+
/** Braking impulse per wheel. */
|
|
198
|
+
readonly brake: number;
|
|
199
|
+
/** Steering angle in radians, on steering wheels only. */
|
|
200
|
+
readonly steering: number;
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Reflects the most recently completed step, and reuses one record per vehicle, so the next call
|
|
204
|
+
* overwrites the object it returned. Read it in the same tick, or copy the fields out before the
|
|
205
|
+
* step advances.
|
|
206
|
+
*/
|
|
207
|
+
interface IPhysicsVehicleState {
|
|
208
|
+
/** Signed metres per second along the chassis forward axis. */
|
|
209
|
+
speed: number;
|
|
210
|
+
/** Reused flat per-wheel records; see `PHYSICS_VEHICLE_WHEEL_STRIDE`. */
|
|
211
|
+
readonly wheels: Float32Array;
|
|
212
|
+
}
|
|
157
213
|
interface IPhysicsInputSnapshot {
|
|
158
214
|
/** One eight-float record per kinematic body. The buffer is caller-owned and reusable. */
|
|
159
215
|
readonly kinematicTransforms: Readonly<Float32Array>;
|
|
@@ -214,6 +270,19 @@ interface IPhysicsSimulation {
|
|
|
214
270
|
* the same tick, or copy the fields out before the step advances.
|
|
215
271
|
*/
|
|
216
272
|
areaIntersections?(id: number): ReadonlySet<number>;
|
|
273
|
+
/**
|
|
274
|
+
* Attach ray-cast wheels to a dynamic chassis. A vehicle lives and dies with its body, so
|
|
275
|
+
* `removeBody()` releases its controller; there is no separate vehicle removal call.
|
|
276
|
+
*
|
|
277
|
+
* Optional because a backend that cannot honour it must fail loudly at construction rather than
|
|
278
|
+
* simulate a car that is not there. `VehicleBody3D` throws `TN_VEHICLE_NATIVE_UNAVAILABLE` when
|
|
279
|
+
* these are absent.
|
|
280
|
+
*/
|
|
281
|
+
createVehicle?(options: IPhysicsVehicleCreateOptions): number;
|
|
282
|
+
setVehicleInput?(id: number, input: IPhysicsVehicleInput): void;
|
|
283
|
+
readVehicleState?(id: number): IPhysicsVehicleState | undefined;
|
|
284
|
+
/** Cold-path respawn: move the chassis, drop both velocities, and face the given yaw in radians. */
|
|
285
|
+
resetVehicle?(id: number, position: IPhysicsVector3, yaw: number): void;
|
|
217
286
|
drainCollisionEvents(buffer: Uint32Array): number;
|
|
218
287
|
dispose(): void;
|
|
219
288
|
}
|
|
@@ -716,4 +785,4 @@ declare class Area3D {
|
|
|
716
785
|
dispose(): void;
|
|
717
786
|
}
|
|
718
787
|
|
|
719
|
-
export { type
|
|
788
|
+
export { type AreaEvent as $, Area3D as A, Buoyancy3D as B, CollisionShape3D as C, type IPhysicsRuntimeSimulation as D, type IPhysicsShapeDescriptor as E, type IPhysicsShapeQuery as F, type IPhysicsSimulation as G, type IPhysicsVehicleCreateOptions as H, type IPhysicsContext as I, type IPhysicsVehicleInput as J, type IPhysicsVehicleState as K, type IPointHit as L, type IRayHit as M, type IRigidBody3DOptions as N, type IShapeHit as O, MAX_PHYSICS_QUERY_RESULTS as P, PHYSICS_COLLISION_EVENT_STRIDE as Q, RigidBody3D as R, PHYSICS_TRANSFORM_STRIDE as S, PHYSICS_VEHICLE_WHEEL_STRIDE as T, type PhysicsBody3D as U, PhysicsDirectSpaceState3D as V, type PhysicsJointKind as W, type PhysicsQueryVector3 as X, type PhysicsShapeKind as Y, type RigidBodyType as Z, rapier as _, type IPhysicsWorldHandle as a, type AreaHandler as a0, type CollisionShapeKind as a1, type IArea3DOptions as a2, type IAreaContact as a3, type ICharacterBody3DOptions as a4, type IPhysicsOptions as a5, type PhysicsPlugin as a6, type INavigationAgent3DOptions as a7, type INavigationContext as a8, type INavigationObstacle3DOptions as a9, type INavigationRegion3DOptions as aa, NavigationAgent3D as ab, type NavigationAgentEvent as ac, type NavigationAgentHandler as ad, NavigationObstacle3D as ae, type NavigationPlugin as af, NavigationRegion3D as ag, recast as ah, type IPhysicsBodyHandle as b, type IPhysicsVector3 as c, type IPhysicsRotation as d, type IPhysicsJointLimit as e, type IPhysicsVehicleWheelOptions as f, type BuoyancyPointPosition as g, CharacterBody3D as h, type IBuoyancy3DOptions as i, type IBuoyancyHullPoint as j, type IBuoyancySurface as k, type IBuoyancySurfaceSample as l, type ICollisionShapeHandle as m, type IIntersectPointOptions as n, type IIntersectRayOptions as o, type IIntersectShapeOptions as p, type IPhysicsBodyCreateOptions as q, type IPhysicsCharacterOptions as r, type IPhysicsColliderHandle as s, type IPhysicsHandle as t, type IPhysicsInputSnapshot as u, type IPhysicsJointCreateOptions as v, type IPhysicsPointQuery as w, type IPhysicsQueryHit as x, type IPhysicsRayHit as y, type IPhysicsRayQuery as z };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { I as IPhysicsContext, a as IPhysicsWorldHandle, b as IPhysicsBodyHandle, c as IPhysicsVector3, d as IPhysicsRotation, e as IPhysicsJointLimit, R as RigidBody3D } from './Area3D-
|
|
2
|
-
export { A as Area3D, B as Buoyancy3D,
|
|
1
|
+
import { I as IPhysicsContext, a as IPhysicsWorldHandle, b as IPhysicsBodyHandle, c as IPhysicsVector3, d as IPhysicsRotation, e as IPhysicsJointLimit, R as RigidBody3D, C as CollisionShape3D, f as IPhysicsVehicleWheelOptions } from './Area3D-DI_kCf7I.js';
|
|
2
|
+
export { A as Area3D, B as Buoyancy3D, g as BuoyancyPointPosition, h as CharacterBody3D, i as IBuoyancy3DOptions, j as IBuoyancyHullPoint, k as IBuoyancySurface, l as IBuoyancySurfaceSample, m as ICollisionShapeHandle, n as IIntersectPointOptions, o as IIntersectRayOptions, p as IIntersectShapeOptions, q as IPhysicsBodyCreateOptions, r as IPhysicsCharacterOptions, s as IPhysicsColliderHandle, t as IPhysicsHandle, u as IPhysicsInputSnapshot, v as IPhysicsJointCreateOptions, w as IPhysicsPointQuery, x as IPhysicsQueryHit, y as IPhysicsRayHit, z as IPhysicsRayQuery, D as IPhysicsRuntimeSimulation, E as IPhysicsShapeDescriptor, F as IPhysicsShapeQuery, G as IPhysicsSimulation, H as IPhysicsVehicleCreateOptions, J as IPhysicsVehicleInput, K as IPhysicsVehicleState, L as IPointHit, M as IRayHit, N as IRigidBody3DOptions, O as IShapeHit, P as MAX_PHYSICS_QUERY_RESULTS, Q as PHYSICS_COLLISION_EVENT_STRIDE, S as PHYSICS_TRANSFORM_STRIDE, T as PHYSICS_VEHICLE_WHEEL_STRIDE, U as PhysicsBody3D, V as PhysicsDirectSpaceState3D, W as PhysicsJointKind, X as PhysicsQueryVector3, Y as PhysicsShapeKind, Z as RigidBodyType, _ as rapier } from './Area3D-DI_kCf7I.js';
|
|
3
3
|
import { Object3D } from 'three';
|
|
4
4
|
import { ISoftBodyCollision } from '@threenative/core';
|
|
5
5
|
import 'recast-navigation';
|
|
@@ -75,4 +75,73 @@ declare function softBodyCollision(...bodies: readonly RigidBody3D[]): ISoftBody
|
|
|
75
75
|
/** Rapier packs membership in the high 16 bits and the filter in the low 16. */
|
|
76
76
|
declare function interactionGroups(layer: number, mask: number): number;
|
|
77
77
|
|
|
78
|
-
|
|
78
|
+
/** Godot's `VehicleWheel3D`, one entry of `IVehicleBody3DOptions.wheels`. */
|
|
79
|
+
type IVehicleWheel3D = IPhysicsVehicleWheelOptions;
|
|
80
|
+
/**
|
|
81
|
+
* Which chassis-local axis the car drives along, and which way `engineForce` pushes it. `-z` is the
|
|
82
|
+
* default because that is the direction a Three.js object faces.
|
|
83
|
+
*/
|
|
84
|
+
type VehicleForwardAxis = "-x" | "-z" | "x" | "z";
|
|
85
|
+
interface IVehicleBody3DOptions {
|
|
86
|
+
/** The visual root the body drives; its position and quaternion are written back every step. */
|
|
87
|
+
readonly object: Object3D;
|
|
88
|
+
readonly entity?: string;
|
|
89
|
+
readonly physics?: IPhysicsContext;
|
|
90
|
+
/** @deprecated Prefer `physics`; a raw web world is backend-specific. */
|
|
91
|
+
readonly world?: IPhysicsWorldHandle | unknown;
|
|
92
|
+
/** The chassis collider, and the only shape the wheels are not tested against. */
|
|
93
|
+
readonly shape: CollisionShape3D;
|
|
94
|
+
/** Chassis mass in kg. Default 0, which Rapier fills in from the collider. */
|
|
95
|
+
readonly mass?: number;
|
|
96
|
+
readonly forwardAxis?: VehicleForwardAxis;
|
|
97
|
+
readonly wheels: readonly IVehicleWheel3D[];
|
|
98
|
+
/** Godot's collision_layer — which layers the chassis occupies. Default 1. */
|
|
99
|
+
readonly collisionLayer?: number;
|
|
100
|
+
/** Godot's collision_mask — which layers the chassis and its wheel rays scan. Default 0xffff. */
|
|
101
|
+
readonly collisionMask?: number;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Godot's `VehicleBody3D`: a dynamic chassis carried on ray-cast wheels with a real suspension.
|
|
105
|
+
*
|
|
106
|
+
* It is a `RigidBody3D` — the chassis collider, mass, transform sync and `dispose()` are the ones
|
|
107
|
+
* that class already has — plus the wheel controller, which is why a vehicle's lifetime is its
|
|
108
|
+
* body's lifetime and there is no separate removal call.
|
|
109
|
+
*/
|
|
110
|
+
declare class VehicleBody3D extends RigidBody3D {
|
|
111
|
+
#private;
|
|
112
|
+
/** A vehicle always drives an object; the base type only allows one for fixed bodies. */
|
|
113
|
+
readonly object: Object3D;
|
|
114
|
+
readonly wheels: readonly IVehicleWheel3D[];
|
|
115
|
+
constructor(options: IVehicleBody3DOptions);
|
|
116
|
+
/**
|
|
117
|
+
* Godot's `engine_force`, in newtons. Every wheel with `useAsTraction` receives the full value,
|
|
118
|
+
* so two driven wheels are twice the force. Negative is reverse.
|
|
119
|
+
*/
|
|
120
|
+
get engineForce(): number;
|
|
121
|
+
set engineForce(value: number);
|
|
122
|
+
/** Godot's `brake`, applied to every wheel. It is a braking impulse, not a force. */
|
|
123
|
+
get brake(): number;
|
|
124
|
+
set brake(value: number);
|
|
125
|
+
/** Godot's `steering`, in radians, applied to every wheel with `useAsSteering`. */
|
|
126
|
+
get steering(): number;
|
|
127
|
+
set steering(value: number);
|
|
128
|
+
/**
|
|
129
|
+
* Signed metres per second along `forwardAxis`: positive while accelerating forward. It reflects
|
|
130
|
+
* the last completed step, so read it after the physics update.
|
|
131
|
+
*/
|
|
132
|
+
get speed(): number;
|
|
133
|
+
/** Whether this wheel's suspension ray found ground on the last completed step. */
|
|
134
|
+
wheelContact(index: number): boolean;
|
|
135
|
+
/** How far the wheel hangs below its attachment point, in metres. Below the rest length while loaded. */
|
|
136
|
+
wheelSuspensionLength(index: number): number;
|
|
137
|
+
/** The wheel's spin angle in radians, for animating the mesh. */
|
|
138
|
+
wheelRotation(index: number): number;
|
|
139
|
+
/**
|
|
140
|
+
* Respawn: move the chassis, face `yaw` radians, and drop both velocities. Wheels keep their
|
|
141
|
+
* cached spin, so the first frame after a rescue is one frame of stale wheel animation.
|
|
142
|
+
*/
|
|
143
|
+
teleport(position: Pick<Object3D["position"], "x" | "y" | "z">, yaw: number): void;
|
|
144
|
+
dispose(): void;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export { CollisionShape3D, type IBuildStaticCollidersOptions, type IFixedJoint3DOptions, type IHingeJoint3DOptions, type IJoint3DOptions, IPhysicsBodyHandle, IPhysicsContext, IPhysicsJointLimit, IPhysicsRotation, IPhysicsVector3, IPhysicsWorldHandle, type IPinJoint3DOptions, type IStaticColliderContext, type IVehicleBody3DOptions, type IVehicleWheel3D, Joint3D, type PhysicsJointBody, RigidBody3D, type StaticColliderPredicate, VehicleBody3D, type VehicleForwardAxis, buildStaticColliders, interactionGroups, softBodyCollision };
|
package/dist/index.js
CHANGED
|
@@ -44,6 +44,7 @@ function f32LengthSquared(values) {
|
|
|
44
44
|
function effectiveContinuousCollision(type, requested) {
|
|
45
45
|
return type === "dynamic" && (requested ?? true);
|
|
46
46
|
}
|
|
47
|
+
var PHYSICS_VEHICLE_WHEEL_STRIDE = 3;
|
|
47
48
|
function requireFiniteVector(vector, label) {
|
|
48
49
|
if (typeof vector?.x !== "number" || typeof vector.y !== "number" || typeof vector.z !== "number" || !Number.isFinite(vector.x) || !Number.isFinite(vector.y) || !Number.isFinite(vector.z))
|
|
49
50
|
throw new Error(`TN_PHYSICS_NON_FINITE: ${label} must be a finite { x, y, z }.`);
|
|
@@ -83,6 +84,33 @@ function requirePhysicsJointCreateOptions(options) {
|
|
|
83
84
|
if (options.frameB !== void 0) requireFiniteRotation(options.frameB, "joint frameB");
|
|
84
85
|
return options;
|
|
85
86
|
}
|
|
87
|
+
function requireVehicleNumber(value, label, positive2) {
|
|
88
|
+
if (typeof value !== "number" || !Number.isFinite(value) || (positive2 ? value <= 0 : value < 0))
|
|
89
|
+
throw new Error(
|
|
90
|
+
`TN_VEHICLE_INVALID: ${label} must be a finite ${positive2 ? "positive" : "non-negative"} number.`
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
function requirePhysicsVehicleCreateOptions(options) {
|
|
94
|
+
if (typeof options !== "object" || options === null)
|
|
95
|
+
throw new Error("IPhysicsSimulation vehicle options must be an object.");
|
|
96
|
+
if (options.forwardAxis !== 0 && options.forwardAxis !== 2)
|
|
97
|
+
throw new Error("TN_VEHICLE_INVALID: forwardAxis must be 0 (x) or 2 (z).");
|
|
98
|
+
requireFiniteVector(options.axle, "vehicle axle");
|
|
99
|
+
if (!Array.isArray(options.wheels) || options.wheels.length < 1)
|
|
100
|
+
throw new Error("TN_VEHICLE_INVALID: a vehicle needs at least one wheel.");
|
|
101
|
+
for (const [index, wheel] of options.wheels.entries()) {
|
|
102
|
+
requireFiniteVector(wheel.position, `vehicle wheel ${index} position`);
|
|
103
|
+
requireVehicleNumber(wheel.wheelRadius, `wheel ${index} wheelRadius`, true);
|
|
104
|
+
requireVehicleNumber(wheel.suspensionRestLength, `wheel ${index} suspensionRestLength`, true);
|
|
105
|
+
requireVehicleNumber(wheel.suspensionStiffness, `wheel ${index} suspensionStiffness`, false);
|
|
106
|
+
requireVehicleNumber(wheel.dampingCompression, `wheel ${index} dampingCompression`, false);
|
|
107
|
+
requireVehicleNumber(wheel.dampingRelaxation, `wheel ${index} dampingRelaxation`, false);
|
|
108
|
+
requireVehicleNumber(wheel.wheelFrictionSlip, `wheel ${index} wheelFrictionSlip`, false);
|
|
109
|
+
if (wheel.maxSuspensionTravel !== void 0)
|
|
110
|
+
requireVehicleNumber(wheel.maxSuspensionTravel, `wheel ${index} maxSuspensionTravel`, false);
|
|
111
|
+
}
|
|
112
|
+
return options;
|
|
113
|
+
}
|
|
86
114
|
var selectedBackend;
|
|
87
115
|
function installPhysicsSimulationBackend(backend) {
|
|
88
116
|
selectedBackend = backend;
|
|
@@ -330,6 +358,20 @@ function characterState(simulationBody, byCollider) {
|
|
|
330
358
|
}
|
|
331
359
|
return state;
|
|
332
360
|
}
|
|
361
|
+
function vehicleSpeed(entry, forwardAxis, sign) {
|
|
362
|
+
const rotation = entry.body.rotation();
|
|
363
|
+
const velocity = entry.body.linvel();
|
|
364
|
+
const cx = -rotation.x;
|
|
365
|
+
const cy = -rotation.y;
|
|
366
|
+
const cz = -rotation.z;
|
|
367
|
+
const tx = 2 * (cy * velocity.z - cz * velocity.y);
|
|
368
|
+
const ty = 2 * (cz * velocity.x - cx * velocity.z);
|
|
369
|
+
const tz = 2 * (cx * velocity.y - cy * velocity.x);
|
|
370
|
+
const w = rotation.w;
|
|
371
|
+
const localX = velocity.x + w * tx + (cy * tz - cz * ty);
|
|
372
|
+
const localZ = velocity.z + w * tz + (cx * ty - cy * tx);
|
|
373
|
+
return (forwardAxis === 2 ? localZ : localX) * sign;
|
|
374
|
+
}
|
|
333
375
|
function writeRapierTransformRecord(entry, renderBuffer, offset) {
|
|
334
376
|
const translation = entry.body.translation();
|
|
335
377
|
const rotation = entry.body.rotation();
|
|
@@ -361,8 +403,10 @@ function createWebPhysicsSimulation(options) {
|
|
|
361
403
|
return true;
|
|
362
404
|
};
|
|
363
405
|
const pendingCollisionEvents = [];
|
|
406
|
+
const vehicles = /* @__PURE__ */ new Map();
|
|
364
407
|
let nextId = 0;
|
|
365
408
|
let nextJointId = 0;
|
|
409
|
+
let nextVehicleId = 0;
|
|
366
410
|
let disposed = false;
|
|
367
411
|
const requireLive = () => {
|
|
368
412
|
if (disposed) throw new Error("Physics simulation is disposed.");
|
|
@@ -399,6 +443,18 @@ function createWebPhysicsSimulation(options) {
|
|
|
399
443
|
const joint = options.world.impulseJoints.get(record.handle);
|
|
400
444
|
if (joint !== null) options.world.removeImpulseJoint(joint, true);
|
|
401
445
|
};
|
|
446
|
+
const removeVehicleRecord = (id) => {
|
|
447
|
+
const record = vehicles.get(id);
|
|
448
|
+
if (record === void 0) return;
|
|
449
|
+
vehicles.delete(id);
|
|
450
|
+
options.world.removeVehicleController(record.controller);
|
|
451
|
+
};
|
|
452
|
+
const requireVehicle = (id, operation) => {
|
|
453
|
+
const record = vehicles.get(id);
|
|
454
|
+
if (record === void 0)
|
|
455
|
+
throw new Error(`TN_VEHICLE_UNKNOWN: ${operation} references an unknown vehicle ${id}.`);
|
|
456
|
+
return record;
|
|
457
|
+
};
|
|
402
458
|
const simulation = {
|
|
403
459
|
version: options.version,
|
|
404
460
|
rawWorld: options.world,
|
|
@@ -514,6 +570,9 @@ function createWebPhysicsSimulation(options) {
|
|
|
514
570
|
for (const [jointId, joint] of joints) {
|
|
515
571
|
if (joint.bodyA === id || joint.bodyB === id) removeJointRecord(jointId);
|
|
516
572
|
}
|
|
573
|
+
for (const [vehicleId, vehicle] of vehicles) {
|
|
574
|
+
if (vehicle.bodyId === id) removeVehicleRecord(vehicleId);
|
|
575
|
+
}
|
|
517
576
|
bodies.delete(id);
|
|
518
577
|
byCollider.delete(entry.collider.handle);
|
|
519
578
|
areaIntersections.delete(id);
|
|
@@ -624,6 +683,19 @@ function createWebPhysicsSimulation(options) {
|
|
|
624
683
|
}
|
|
625
684
|
}
|
|
626
685
|
options.world.timestep = deltaTime;
|
|
686
|
+
for (const vehicle of vehicles.values()) {
|
|
687
|
+
vehicle.controller.updateVehicle(
|
|
688
|
+
deltaTime,
|
|
689
|
+
options.rapier.QueryFilterFlags.EXCLUDE_SENSORS,
|
|
690
|
+
vehicle.rayGroups
|
|
691
|
+
);
|
|
692
|
+
for (const [index, restLength] of vehicle.restLengths.entries()) {
|
|
693
|
+
if (vehicle.controller.wheelIsInContact(index) && (vehicle.controller.wheelSuspensionLength(index) ?? 0) < restLength) {
|
|
694
|
+
vehicle.body.wakeUp();
|
|
695
|
+
break;
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
}
|
|
627
699
|
options.world.step(options.eventQueue);
|
|
628
700
|
for (const entry of bodies.values()) entry.body.resetForces(true);
|
|
629
701
|
dirtyBodies.clear();
|
|
@@ -790,6 +862,95 @@ function createWebPhysicsSimulation(options) {
|
|
|
790
862
|
}
|
|
791
863
|
return current;
|
|
792
864
|
},
|
|
865
|
+
createVehicle: (vehicleOptions) => {
|
|
866
|
+
requireLive();
|
|
867
|
+
const requested = requirePhysicsVehicleCreateOptions(vehicleOptions);
|
|
868
|
+
const entry = requireDynamic(bodies.get(requested.bodyId), requested.bodyId, "createVehicle");
|
|
869
|
+
const id = nextVehicleId;
|
|
870
|
+
nextVehicleId += 1;
|
|
871
|
+
const controller = options.world.createVehicleController(entry.body);
|
|
872
|
+
const suspensionDirection = { x: 0, y: -1, z: 0 };
|
|
873
|
+
const wheels = new Float32Array(requested.wheels.length * PHYSICS_VEHICLE_WHEEL_STRIDE);
|
|
874
|
+
const axleComponent = requested.forwardAxis === 2 ? requested.axle.x : requested.axle.z;
|
|
875
|
+
const forwardComponent = requested.forwardAxis === 2 ? -axleComponent : axleComponent;
|
|
876
|
+
const record = {
|
|
877
|
+
body: entry.body,
|
|
878
|
+
bodyId: requested.bodyId,
|
|
879
|
+
controller,
|
|
880
|
+
rayGroups: entry.collider.collisionGroups(),
|
|
881
|
+
restLengths: requested.wheels.map((wheel) => wheel.suspensionRestLength),
|
|
882
|
+
direction: forwardComponent < 0 ? -1 : 1,
|
|
883
|
+
forwardAxis: requested.forwardAxis,
|
|
884
|
+
state: { speed: 0, wheels },
|
|
885
|
+
steering: [],
|
|
886
|
+
traction: [],
|
|
887
|
+
wheels
|
|
888
|
+
};
|
|
889
|
+
controller.indexUpAxis = 1;
|
|
890
|
+
controller.setIndexForwardAxis = requested.forwardAxis;
|
|
891
|
+
for (const [index, wheel] of requested.wheels.entries()) {
|
|
892
|
+
controller.addWheel(
|
|
893
|
+
wheel.position,
|
|
894
|
+
suspensionDirection,
|
|
895
|
+
requested.axle,
|
|
896
|
+
wheel.suspensionRestLength,
|
|
897
|
+
wheel.wheelRadius
|
|
898
|
+
);
|
|
899
|
+
controller.setWheelSuspensionStiffness(index, wheel.suspensionStiffness);
|
|
900
|
+
controller.setWheelSuspensionCompression(index, wheel.dampingCompression);
|
|
901
|
+
controller.setWheelSuspensionRelaxation(index, wheel.dampingRelaxation);
|
|
902
|
+
controller.setWheelFrictionSlip(index, wheel.wheelFrictionSlip);
|
|
903
|
+
if (wheel.maxSuspensionTravel !== void 0)
|
|
904
|
+
controller.setWheelMaxSuspensionTravel(index, wheel.maxSuspensionTravel);
|
|
905
|
+
if (wheel.useAsTraction) record.traction.push(index);
|
|
906
|
+
if (wheel.useAsSteering) record.steering.push(index);
|
|
907
|
+
}
|
|
908
|
+
vehicles.set(id, record);
|
|
909
|
+
return id;
|
|
910
|
+
},
|
|
911
|
+
setVehicleInput: (id, input) => {
|
|
912
|
+
requireLive();
|
|
913
|
+
if (typeof input?.engineForce !== "number" || typeof input.brake !== "number" || typeof input.steering !== "number" || !Number.isFinite(input.engineForce) || !Number.isFinite(input.brake) || !Number.isFinite(input.steering))
|
|
914
|
+
throw new Error(
|
|
915
|
+
"TN_PHYSICS_NON_FINITE: vehicle input needs a finite engineForce, brake and steering."
|
|
916
|
+
);
|
|
917
|
+
const { controller, steering, traction } = requireVehicle(id, "setVehicleInput");
|
|
918
|
+
for (let index = 0; index < traction.length; index += 1)
|
|
919
|
+
controller.setWheelEngineForce(traction[index], input.engineForce);
|
|
920
|
+
for (let index = 0; index < steering.length; index += 1)
|
|
921
|
+
controller.setWheelSteering(steering[index], input.steering);
|
|
922
|
+
for (let wheel = 0; wheel < controller.numWheels(); wheel += 1)
|
|
923
|
+
controller.setWheelBrake(wheel, input.brake);
|
|
924
|
+
},
|
|
925
|
+
readVehicleState: (id) => {
|
|
926
|
+
requireLive();
|
|
927
|
+
const record = requireVehicle(id, "readVehicleState");
|
|
928
|
+
const { controller, state, wheels } = record;
|
|
929
|
+
const entry = bodies.get(record.bodyId);
|
|
930
|
+
if (entry === void 0) throw new Error("TN_VEHICLE_UNKNOWN: the chassis left the backend.");
|
|
931
|
+
state.speed = vehicleSpeed(entry, record.forwardAxis, record.direction);
|
|
932
|
+
for (let index = 0; index < controller.numWheels(); index += 1) {
|
|
933
|
+
const offset = index * PHYSICS_VEHICLE_WHEEL_STRIDE;
|
|
934
|
+
wheels[offset] = controller.wheelIsInContact(index) ? 1 : 0;
|
|
935
|
+
wheels[offset + 1] = controller.wheelSuspensionLength(index) ?? 0;
|
|
936
|
+
wheels[offset + 2] = controller.wheelRotation(index) ?? 0;
|
|
937
|
+
}
|
|
938
|
+
return state;
|
|
939
|
+
},
|
|
940
|
+
resetVehicle: (id, position, yaw) => {
|
|
941
|
+
requireLive();
|
|
942
|
+
requireFiniteVector(position, "vehicle position");
|
|
943
|
+
if (typeof yaw !== "number" || !Number.isFinite(yaw))
|
|
944
|
+
throw new Error("TN_PHYSICS_NON_FINITE: vehicle yaw must be a finite number of radians.");
|
|
945
|
+
const record = requireVehicle(id, "resetVehicle");
|
|
946
|
+
const entry = requireDynamic(bodies.get(record.bodyId), record.bodyId, "resetVehicle");
|
|
947
|
+
entry.body.setTranslation(position, true);
|
|
948
|
+
entry.body.setRotation({ x: 0, y: Math.sin(yaw / 2), z: 0, w: Math.cos(yaw / 2) }, true);
|
|
949
|
+
entry.body.setLinvel({ x: 0, y: 0, z: 0 }, true);
|
|
950
|
+
entry.body.setAngvel({ x: 0, y: 0, z: 0 }, true);
|
|
951
|
+
options.world.propagateModifiedBodyPositionsToColliders();
|
|
952
|
+
dirtyBodies.add(entry);
|
|
953
|
+
},
|
|
793
954
|
drainCollisionEvents: (buffer) => {
|
|
794
955
|
requireLive();
|
|
795
956
|
requirePhysicsEventBuffer(buffer);
|
|
@@ -811,6 +972,7 @@ function createWebPhysicsSimulation(options) {
|
|
|
811
972
|
dispose: () => {
|
|
812
973
|
if (disposed) return;
|
|
813
974
|
disposed = true;
|
|
975
|
+
for (const id of [...vehicles.keys()]) removeVehicleRecord(id);
|
|
814
976
|
for (const entry of bodies.values()) {
|
|
815
977
|
if (entry.controller !== void 0)
|
|
816
978
|
options.world.removeCharacterController(entry.controller);
|
|
@@ -828,7 +990,14 @@ function createWebPhysicsSimulation(options) {
|
|
|
828
990
|
};
|
|
829
991
|
return simulation;
|
|
830
992
|
}
|
|
993
|
+
var warnedDeprecatedWorldOption = false;
|
|
831
994
|
function requirePhysicsSimulation(physics, world) {
|
|
995
|
+
if (world !== void 0 && !warnedDeprecatedWorldOption) {
|
|
996
|
+
warnedDeprecatedWorldOption = true;
|
|
997
|
+
console.warn(
|
|
998
|
+
"TN_DEPRECATED_PHYSICS_WORLD_OPTION: the constructor option `world` is deprecated on every physics node; pass an IPhysicsContext as `physics` instead. The node classes themselves are not deprecated."
|
|
999
|
+
);
|
|
1000
|
+
}
|
|
832
1001
|
if (physics?.simulation !== void 0) return physics.simulation;
|
|
833
1002
|
const candidate = typeof world === "object" && world !== null && "simulation" in world ? world.simulation : world;
|
|
834
1003
|
if (typeof candidate === "object" && candidate !== null && "createBody" in candidate && typeof candidate.createBody === "function" && "step" in candidate && typeof candidate.step === "function")
|
|
@@ -1399,6 +1568,15 @@ var CollisionShape3D = class _CollisionShape3D {
|
|
|
1399
1568
|
const width = (bounds.max.x - bounds.min.x) * Math.abs(mesh.scale.x);
|
|
1400
1569
|
const height = (bounds.max.y - bounds.min.y) * Math.abs(mesh.scale.y);
|
|
1401
1570
|
const depth = (bounds.max.z - bounds.min.z) * Math.abs(mesh.scale.z);
|
|
1571
|
+
const offset = Math.max(
|
|
1572
|
+
Math.abs(bounds.max.x + bounds.min.x) / 2 * Math.abs(mesh.scale.x),
|
|
1573
|
+
Math.abs(bounds.max.y + bounds.min.y) / 2 * Math.abs(mesh.scale.y),
|
|
1574
|
+
Math.abs(bounds.max.z + bounds.min.z) / 2 * Math.abs(mesh.scale.z)
|
|
1575
|
+
);
|
|
1576
|
+
if (offset > 1e-4 + 1e-3 * Math.max(width, height, depth))
|
|
1577
|
+
throw new Error(
|
|
1578
|
+
`TN_COLLISION_SHAPE_OFF_CENTRE: '${mesh.name || "mesh"}' has geometry centred ${offset.toFixed(3)} from its origin, so a ${inferred} collider would sit away from what is drawn. Call geometry.center() and move the mesh instead, pass "trimesh", or build level colliders with buildStaticColliders.`
|
|
1579
|
+
);
|
|
1402
1580
|
if (inferred === "sphere") {
|
|
1403
1581
|
geometry.computeBoundingSphere();
|
|
1404
1582
|
const radius = (geometry.boundingSphere?.radius ?? Math.max(width, height, depth) / 2) * Math.max(Math.abs(mesh.scale.x), Math.abs(mesh.scale.y), Math.abs(mesh.scale.z));
|
|
@@ -2065,6 +2243,7 @@ function rapier2(options = {}) {
|
|
|
2065
2243
|
let events = new Uint32Array(64);
|
|
2066
2244
|
const activeContacts = /* @__PURE__ */ new Map();
|
|
2067
2245
|
let debugSeries = [];
|
|
2246
|
+
let completedSteps = 0;
|
|
2068
2247
|
let unregisterObservations;
|
|
2069
2248
|
function buildContext(selected) {
|
|
2070
2249
|
return {
|
|
@@ -2141,6 +2320,7 @@ function rapier2(options = {}) {
|
|
|
2141
2320
|
};
|
|
2142
2321
|
for (const buoyancy of buoyancies) buoyancy.apply(dt);
|
|
2143
2322
|
simulation.step(dt, input);
|
|
2323
|
+
completedSteps += 1;
|
|
2144
2324
|
visible = growFloat(visible, bodies.size + areas.size);
|
|
2145
2325
|
let visibleResizeAttempts = 0;
|
|
2146
2326
|
for (; ; ) {
|
|
@@ -2262,7 +2442,8 @@ function rapier2(options = {}) {
|
|
|
2262
2442
|
};
|
|
2263
2443
|
function physicsObservations(ctx, runtime, request) {
|
|
2264
2444
|
if (simulation === void 0) throw new Error("Physics observations sampled before setup.");
|
|
2265
|
-
if (request.include?.includes("physicsDebugSeries") !== true)
|
|
2445
|
+
if (request.include?.includes("physicsDebugSeries") !== true)
|
|
2446
|
+
return { physicsSteps: completedSteps };
|
|
2266
2447
|
if (request.label !== void 0) {
|
|
2267
2448
|
if (debugSeries.some(({ label }) => label === request.label)) {
|
|
2268
2449
|
throw new Error(
|
|
@@ -2280,7 +2461,10 @@ function rapier2(options = {}) {
|
|
|
2280
2461
|
tick: runtime.tick()
|
|
2281
2462
|
});
|
|
2282
2463
|
}
|
|
2283
|
-
return {
|
|
2464
|
+
return {
|
|
2465
|
+
physicsDebugSeries: debugSeries.map((sample) => ({ ...sample })),
|
|
2466
|
+
physicsSteps: completedSteps
|
|
2467
|
+
};
|
|
2284
2468
|
}
|
|
2285
2469
|
function physicsDebugSnapshot(ctx) {
|
|
2286
2470
|
if (simulation === void 0) throw new Error("Physics observations sampled before setup.");
|
|
@@ -2372,4 +2556,167 @@ function physicsEntityId(ctx, body) {
|
|
|
2372
2556
|
return stableId;
|
|
2373
2557
|
}
|
|
2374
2558
|
|
|
2375
|
-
|
|
2559
|
+
// src/VehicleBody3D.ts
|
|
2560
|
+
var FORWARD_AXES = {
|
|
2561
|
+
"-x": { axle: { x: 0, y: 0, z: -1 }, index: 0 },
|
|
2562
|
+
"-z": { axle: { x: 1, y: 0, z: 0 }, index: 2 },
|
|
2563
|
+
x: { axle: { x: 0, y: 0, z: 1 }, index: 0 },
|
|
2564
|
+
z: { axle: { x: -1, y: 0, z: 0 }, index: 2 }
|
|
2565
|
+
};
|
|
2566
|
+
function requireFinite(value, label) {
|
|
2567
|
+
if (typeof value !== "number" || !Number.isFinite(value))
|
|
2568
|
+
throw new Error(`TN_VEHICLE_INVALID: ${label} must be a finite number.`);
|
|
2569
|
+
}
|
|
2570
|
+
var VehicleBody3D = class extends RigidBody3D {
|
|
2571
|
+
wheels;
|
|
2572
|
+
#object;
|
|
2573
|
+
#simulation;
|
|
2574
|
+
#readState;
|
|
2575
|
+
#setInput;
|
|
2576
|
+
#vehicle;
|
|
2577
|
+
#engineForce = 0;
|
|
2578
|
+
#brake = 0;
|
|
2579
|
+
#steering = 0;
|
|
2580
|
+
#disposed = false;
|
|
2581
|
+
constructor(options) {
|
|
2582
|
+
const forward = FORWARD_AXES[options.forwardAxis ?? "-z"];
|
|
2583
|
+
if (forward === void 0)
|
|
2584
|
+
throw new Error(
|
|
2585
|
+
`TN_VEHICLE_INVALID: forwardAxis '${String(options.forwardAxis)}' must be one of -x, -z, x, z.`
|
|
2586
|
+
);
|
|
2587
|
+
const simulation = requirePhysicsSimulation(options.physics, options.world);
|
|
2588
|
+
const createVehicle = simulation.createVehicle;
|
|
2589
|
+
const setVehicleInput = simulation.setVehicleInput;
|
|
2590
|
+
const readVehicleState = simulation.readVehicleState;
|
|
2591
|
+
if (createVehicle === void 0 || setVehicleInput === void 0 || readVehicleState === void 0)
|
|
2592
|
+
throw new Error(
|
|
2593
|
+
"TN_VEHICLE_NATIVE_UNAVAILABLE: the active physics backend has no vehicle controller. Use RigidBody3D instead."
|
|
2594
|
+
);
|
|
2595
|
+
super({
|
|
2596
|
+
collisionLayer: options.collisionLayer,
|
|
2597
|
+
collisionMask: options.collisionMask,
|
|
2598
|
+
entity: options.entity,
|
|
2599
|
+
mass: options.mass ?? 0,
|
|
2600
|
+
object: options.object,
|
|
2601
|
+
physics: options.physics,
|
|
2602
|
+
shape: options.shape,
|
|
2603
|
+
type: "dynamic",
|
|
2604
|
+
world: options.world
|
|
2605
|
+
});
|
|
2606
|
+
this.wheels = options.wheels;
|
|
2607
|
+
this.#object = options.object;
|
|
2608
|
+
this.#simulation = simulation;
|
|
2609
|
+
this.#readState = readVehicleState;
|
|
2610
|
+
this.#setInput = setVehicleInput;
|
|
2611
|
+
try {
|
|
2612
|
+
this.#vehicle = createVehicle.call(simulation, {
|
|
2613
|
+
axle: forward.axle,
|
|
2614
|
+
bodyId: this.body.id,
|
|
2615
|
+
forwardAxis: forward.index,
|
|
2616
|
+
wheels: options.wheels
|
|
2617
|
+
});
|
|
2618
|
+
} catch (error) {
|
|
2619
|
+
this.dispose();
|
|
2620
|
+
throw error;
|
|
2621
|
+
}
|
|
2622
|
+
}
|
|
2623
|
+
/**
|
|
2624
|
+
* Godot's `engine_force`, in newtons. Every wheel with `useAsTraction` receives the full value,
|
|
2625
|
+
* so two driven wheels are twice the force. Negative is reverse.
|
|
2626
|
+
*/
|
|
2627
|
+
get engineForce() {
|
|
2628
|
+
this.#requireLive("engineForce");
|
|
2629
|
+
return this.#engineForce;
|
|
2630
|
+
}
|
|
2631
|
+
set engineForce(value) {
|
|
2632
|
+
requireFinite(value, "engineForce");
|
|
2633
|
+
if (value === this.#engineForce) return;
|
|
2634
|
+
this.#engineForce = value;
|
|
2635
|
+
this.#pushInput();
|
|
2636
|
+
}
|
|
2637
|
+
/** Godot's `brake`, applied to every wheel. It is a braking impulse, not a force. */
|
|
2638
|
+
get brake() {
|
|
2639
|
+
this.#requireLive("brake");
|
|
2640
|
+
return this.#brake;
|
|
2641
|
+
}
|
|
2642
|
+
set brake(value) {
|
|
2643
|
+
requireFinite(value, "brake");
|
|
2644
|
+
if (value === this.#brake) return;
|
|
2645
|
+
this.#brake = value;
|
|
2646
|
+
this.#pushInput();
|
|
2647
|
+
}
|
|
2648
|
+
/** Godot's `steering`, in radians, applied to every wheel with `useAsSteering`. */
|
|
2649
|
+
get steering() {
|
|
2650
|
+
this.#requireLive("steering");
|
|
2651
|
+
return this.#steering;
|
|
2652
|
+
}
|
|
2653
|
+
set steering(value) {
|
|
2654
|
+
requireFinite(value, "steering");
|
|
2655
|
+
if (value === this.#steering) return;
|
|
2656
|
+
this.#steering = value;
|
|
2657
|
+
this.#pushInput();
|
|
2658
|
+
}
|
|
2659
|
+
/**
|
|
2660
|
+
* Signed metres per second along `forwardAxis`: positive while accelerating forward. It reflects
|
|
2661
|
+
* the last completed step, so read it after the physics update.
|
|
2662
|
+
*/
|
|
2663
|
+
get speed() {
|
|
2664
|
+
return this.#state().speed;
|
|
2665
|
+
}
|
|
2666
|
+
/** Whether this wheel's suspension ray found ground on the last completed step. */
|
|
2667
|
+
wheelContact(index) {
|
|
2668
|
+
return this.#wheel(index, 0) === 1;
|
|
2669
|
+
}
|
|
2670
|
+
/** How far the wheel hangs below its attachment point, in metres. Below the rest length while loaded. */
|
|
2671
|
+
wheelSuspensionLength(index) {
|
|
2672
|
+
return this.#wheel(index, 1);
|
|
2673
|
+
}
|
|
2674
|
+
/** The wheel's spin angle in radians, for animating the mesh. */
|
|
2675
|
+
wheelRotation(index) {
|
|
2676
|
+
return this.#wheel(index, 2);
|
|
2677
|
+
}
|
|
2678
|
+
/**
|
|
2679
|
+
* Respawn: move the chassis, face `yaw` radians, and drop both velocities. Wheels keep their
|
|
2680
|
+
* cached spin, so the first frame after a rescue is one frame of stale wheel animation.
|
|
2681
|
+
*/
|
|
2682
|
+
teleport(position, yaw) {
|
|
2683
|
+
this.#requireLive("teleport");
|
|
2684
|
+
if (this.#simulation.resetVehicle === void 0)
|
|
2685
|
+
throw new Error(
|
|
2686
|
+
"TN_VEHICLE_NATIVE_UNAVAILABLE: the active physics backend cannot respawn a vehicle."
|
|
2687
|
+
);
|
|
2688
|
+
requireFinite(yaw, "yaw");
|
|
2689
|
+
this.#simulation.resetVehicle(this.#vehicle, position, yaw);
|
|
2690
|
+
this.#object.position.set(position.x, position.y, position.z);
|
|
2691
|
+
}
|
|
2692
|
+
dispose() {
|
|
2693
|
+
super.dispose();
|
|
2694
|
+
this.#disposed = true;
|
|
2695
|
+
}
|
|
2696
|
+
#pushInput() {
|
|
2697
|
+
if (this.#disposed) throw new Error("VehicleBody3D input cannot be used after dispose.");
|
|
2698
|
+
this.#setInput(this.#vehicle, {
|
|
2699
|
+
brake: this.#brake,
|
|
2700
|
+
engineForce: this.#engineForce,
|
|
2701
|
+
steering: this.#steering
|
|
2702
|
+
});
|
|
2703
|
+
}
|
|
2704
|
+
#state() {
|
|
2705
|
+
this.#requireLive("vehicle state");
|
|
2706
|
+
const state = this.#readState(this.#vehicle);
|
|
2707
|
+
if (state === void 0) throw new Error("TN_VEHICLE_UNKNOWN: the vehicle left the backend.");
|
|
2708
|
+
return state;
|
|
2709
|
+
}
|
|
2710
|
+
#wheel(index, field) {
|
|
2711
|
+
if (!Number.isSafeInteger(index) || index < 0 || index >= this.wheels.length)
|
|
2712
|
+
throw new Error(
|
|
2713
|
+
`TN_VEHICLE_INVALID: wheel index ${String(index)} is outside 0 through ${this.wheels.length - 1}.`
|
|
2714
|
+
);
|
|
2715
|
+
return this.#state().wheels[index * PHYSICS_VEHICLE_WHEEL_STRIDE + field] ?? 0;
|
|
2716
|
+
}
|
|
2717
|
+
#requireLive(operation) {
|
|
2718
|
+
if (this.#disposed) throw new Error(`VehicleBody3D.${operation} cannot be used after dispose.`);
|
|
2719
|
+
}
|
|
2720
|
+
};
|
|
2721
|
+
|
|
2722
|
+
export { Area3D, Buoyancy3D, CharacterBody3D, CollisionShape3D, Joint3D, MAX_PHYSICS_QUERY_RESULTS, PHYSICS_COLLISION_EVENT_STRIDE, PHYSICS_TRANSFORM_STRIDE, PHYSICS_VEHICLE_WHEEL_STRIDE, PhysicsDirectSpaceState3D, RigidBody3D, VehicleBody3D, buildStaticColliders, interactionGroups, rapier2 as rapier, softBodyCollision };
|
package/dist/native/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
export { A as Area3D,
|
|
2
|
-
export { IBuildStaticCollidersOptions, IFixedJoint3DOptions, IHingeJoint3DOptions, IJoint3DOptions, IPinJoint3DOptions, IStaticColliderContext, Joint3D, PhysicsJointBody, StaticColliderPredicate, buildStaticColliders, interactionGroups, softBodyCollision } from '../index.js';
|
|
1
|
+
export { A as Area3D, $ as AreaEvent, a0 as AreaHandler, B as Buoyancy3D, g as BuoyancyPointPosition, h as CharacterBody3D, C as CollisionShape3D, a1 as CollisionShapeKind, a2 as IArea3DOptions, a3 as IAreaContact, i as IBuoyancy3DOptions, j as IBuoyancyHullPoint, k as IBuoyancySurface, l as IBuoyancySurfaceSample, a4 as ICharacterBody3DOptions, n as IIntersectPointOptions, o as IIntersectRayOptions, p as IIntersectShapeOptions, q as IPhysicsBodyCreateOptions, b as IPhysicsBodyHandle, r as IPhysicsCharacterOptions, s as IPhysicsColliderHandle, I as IPhysicsContext, t as IPhysicsHandle, u as IPhysicsInputSnapshot, v as IPhysicsJointCreateOptions, e as IPhysicsJointLimit, a5 as IPhysicsOptions, w as IPhysicsPointQuery, x as IPhysicsQueryHit, y as IPhysicsRayHit, z as IPhysicsRayQuery, d as IPhysicsRotation, D as IPhysicsRuntimeSimulation, E as IPhysicsShapeDescriptor, F as IPhysicsShapeQuery, G as IPhysicsSimulation, c as IPhysicsVector3, H as IPhysicsVehicleCreateOptions, J as IPhysicsVehicleInput, K as IPhysicsVehicleState, a as IPhysicsWorldHandle, L as IPointHit, M as IRayHit, N as IRigidBody3DOptions, O as IShapeHit, P as MAX_PHYSICS_QUERY_RESULTS, Q as PHYSICS_COLLISION_EVENT_STRIDE, S as PHYSICS_TRANSFORM_STRIDE, T as PHYSICS_VEHICLE_WHEEL_STRIDE, U as PhysicsBody3D, V as PhysicsDirectSpaceState3D, W as PhysicsJointKind, a6 as PhysicsPlugin, X as PhysicsQueryVector3, Y as PhysicsShapeKind, R as RigidBody3D, Z as RigidBodyType, _ as rapier } from '../Area3D-DI_kCf7I.js';
|
|
2
|
+
export { IBuildStaticCollidersOptions, IFixedJoint3DOptions, IHingeJoint3DOptions, IJoint3DOptions, IPinJoint3DOptions, IStaticColliderContext, IVehicleBody3DOptions, IVehicleWheel3D, Joint3D, PhysicsJointBody, StaticColliderPredicate, VehicleBody3D, VehicleForwardAxis, buildStaticColliders, interactionGroups, softBodyCollision } from '../index.js';
|
|
3
3
|
import 'three';
|
|
4
4
|
import '@threenative/core';
|
|
5
5
|
import 'recast-navigation';
|
package/dist/native/index.js
CHANGED
|
@@ -41,6 +41,7 @@ function f32LengthSquared(values) {
|
|
|
41
41
|
function effectiveContinuousCollision(type, requested) {
|
|
42
42
|
return type === "dynamic" && (requested ?? true);
|
|
43
43
|
}
|
|
44
|
+
var PHYSICS_VEHICLE_WHEEL_STRIDE = 3;
|
|
44
45
|
function requireFiniteVector(vector, label) {
|
|
45
46
|
if (typeof vector?.x !== "number" || typeof vector.y !== "number" || typeof vector.z !== "number" || !Number.isFinite(vector.x) || !Number.isFinite(vector.y) || !Number.isFinite(vector.z))
|
|
46
47
|
throw new Error(`TN_PHYSICS_NON_FINITE: ${label} must be a finite { x, y, z }.`);
|
|
@@ -80,6 +81,33 @@ function requirePhysicsJointCreateOptions(options) {
|
|
|
80
81
|
if (options.frameB !== void 0) requireFiniteRotation(options.frameB, "joint frameB");
|
|
81
82
|
return options;
|
|
82
83
|
}
|
|
84
|
+
function requireVehicleNumber(value, label, positive2) {
|
|
85
|
+
if (typeof value !== "number" || !Number.isFinite(value) || (positive2 ? value <= 0 : value < 0))
|
|
86
|
+
throw new Error(
|
|
87
|
+
`TN_VEHICLE_INVALID: ${label} must be a finite ${positive2 ? "positive" : "non-negative"} number.`
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
function requirePhysicsVehicleCreateOptions(options) {
|
|
91
|
+
if (typeof options !== "object" || options === null)
|
|
92
|
+
throw new Error("IPhysicsSimulation vehicle options must be an object.");
|
|
93
|
+
if (options.forwardAxis !== 0 && options.forwardAxis !== 2)
|
|
94
|
+
throw new Error("TN_VEHICLE_INVALID: forwardAxis must be 0 (x) or 2 (z).");
|
|
95
|
+
requireFiniteVector(options.axle, "vehicle axle");
|
|
96
|
+
if (!Array.isArray(options.wheels) || options.wheels.length < 1)
|
|
97
|
+
throw new Error("TN_VEHICLE_INVALID: a vehicle needs at least one wheel.");
|
|
98
|
+
for (const [index, wheel] of options.wheels.entries()) {
|
|
99
|
+
requireFiniteVector(wheel.position, `vehicle wheel ${index} position`);
|
|
100
|
+
requireVehicleNumber(wheel.wheelRadius, `wheel ${index} wheelRadius`, true);
|
|
101
|
+
requireVehicleNumber(wheel.suspensionRestLength, `wheel ${index} suspensionRestLength`, true);
|
|
102
|
+
requireVehicleNumber(wheel.suspensionStiffness, `wheel ${index} suspensionStiffness`, false);
|
|
103
|
+
requireVehicleNumber(wheel.dampingCompression, `wheel ${index} dampingCompression`, false);
|
|
104
|
+
requireVehicleNumber(wheel.dampingRelaxation, `wheel ${index} dampingRelaxation`, false);
|
|
105
|
+
requireVehicleNumber(wheel.wheelFrictionSlip, `wheel ${index} wheelFrictionSlip`, false);
|
|
106
|
+
if (wheel.maxSuspensionTravel !== void 0)
|
|
107
|
+
requireVehicleNumber(wheel.maxSuspensionTravel, `wheel ${index} maxSuspensionTravel`, false);
|
|
108
|
+
}
|
|
109
|
+
return options;
|
|
110
|
+
}
|
|
83
111
|
var selectedBackend;
|
|
84
112
|
function installPhysicsSimulationBackend(backend) {
|
|
85
113
|
selectedBackend = backend;
|
|
@@ -228,7 +256,14 @@ function requirePhysicsPointQuery(value) {
|
|
|
228
256
|
position: queryVector(query.position, "point position")
|
|
229
257
|
};
|
|
230
258
|
}
|
|
259
|
+
var warnedDeprecatedWorldOption = false;
|
|
231
260
|
function requirePhysicsSimulation(physics, world) {
|
|
261
|
+
if (world !== void 0 && !warnedDeprecatedWorldOption) {
|
|
262
|
+
warnedDeprecatedWorldOption = true;
|
|
263
|
+
console.warn(
|
|
264
|
+
"TN_DEPRECATED_PHYSICS_WORLD_OPTION: the constructor option `world` is deprecated on every physics node; pass an IPhysicsContext as `physics` instead. The node classes themselves are not deprecated."
|
|
265
|
+
);
|
|
266
|
+
}
|
|
232
267
|
if (physics?.simulation !== void 0) return physics.simulation;
|
|
233
268
|
const candidate = typeof world === "object" && world !== null && "simulation" in world ? world.simulation : world;
|
|
234
269
|
if (typeof candidate === "object" && candidate !== null && "createBody" in candidate && typeof candidate.createBody === "function" && "step" in candidate && typeof candidate.step === "function")
|
|
@@ -260,6 +295,24 @@ function primitiveShape(shape) {
|
|
|
260
295
|
throw new Error(`TN_NATIVE_PHYSICS_SHAPE_UNSUPPORTED: ${shape.kind} remains OPEN on native`);
|
|
261
296
|
return shape;
|
|
262
297
|
}
|
|
298
|
+
function nativeBodyShape(shape) {
|
|
299
|
+
if (shape.kind === "trimesh") {
|
|
300
|
+
if (shape.vertices === void 0 || shape.indices === void 0)
|
|
301
|
+
throw new Error("TN_NATIVE_PHYSICS_SHAPE_INVALID: trimesh requires vertices and indices");
|
|
302
|
+
return {
|
|
303
|
+
collisionLayer: shape.collisionLayer,
|
|
304
|
+
collisionMask: shape.collisionMask,
|
|
305
|
+
indices: shape.indices,
|
|
306
|
+
kind: shape.kind,
|
|
307
|
+
sensor: shape.sensor,
|
|
308
|
+
vertices: shape.vertices,
|
|
309
|
+
x: shape.x,
|
|
310
|
+
y: shape.y,
|
|
311
|
+
z: shape.z
|
|
312
|
+
};
|
|
313
|
+
}
|
|
314
|
+
return primitiveShape(shape);
|
|
315
|
+
}
|
|
263
316
|
function opaqueNativeShape(shape) {
|
|
264
317
|
return Object.freeze({ backend: "native", kind: shape.kind });
|
|
265
318
|
}
|
|
@@ -272,6 +325,31 @@ function growUint32(buffer, minimum) {
|
|
|
272
325
|
return length === buffer.length ? buffer : new Uint32Array(length);
|
|
273
326
|
}
|
|
274
327
|
var NATIVE_CHARACTER_STATE_STRIDE = 6;
|
|
328
|
+
var RAPIER_DEFAULT_MAX_SUSPENSION_TRAVEL = 5;
|
|
329
|
+
var NATIVE_VEHICLE_WHEEL_WIDTH = 12;
|
|
330
|
+
function flattenVehicleWheels(options) {
|
|
331
|
+
const wheels = new Float32Array(options.wheels.length * NATIVE_VEHICLE_WHEEL_WIDTH);
|
|
332
|
+
options.wheels.forEach((wheel, index) => {
|
|
333
|
+
wheels.set(
|
|
334
|
+
[
|
|
335
|
+
wheel.position.x,
|
|
336
|
+
wheel.position.y,
|
|
337
|
+
wheel.position.z,
|
|
338
|
+
wheel.wheelRadius,
|
|
339
|
+
wheel.suspensionRestLength,
|
|
340
|
+
wheel.suspensionStiffness,
|
|
341
|
+
wheel.dampingCompression,
|
|
342
|
+
wheel.dampingRelaxation,
|
|
343
|
+
wheel.wheelFrictionSlip,
|
|
344
|
+
wheel.maxSuspensionTravel ?? RAPIER_DEFAULT_MAX_SUSPENSION_TRAVEL,
|
|
345
|
+
wheel.useAsSteering ? 1 : 0,
|
|
346
|
+
wheel.useAsTraction ? 1 : 0
|
|
347
|
+
],
|
|
348
|
+
index * NATIVE_VEHICLE_WHEEL_WIDTH
|
|
349
|
+
);
|
|
350
|
+
});
|
|
351
|
+
return wheels;
|
|
352
|
+
}
|
|
275
353
|
function createNativePhysicsSimulation(raw, version) {
|
|
276
354
|
const bodyIds = /* @__PURE__ */ new Set();
|
|
277
355
|
const bodyHandles = /* @__PURE__ */ new Map();
|
|
@@ -279,6 +357,9 @@ function createNativePhysicsSimulation(raw, version) {
|
|
|
279
357
|
const characterIds = /* @__PURE__ */ new Set();
|
|
280
358
|
const characterState = /* @__PURE__ */ new Map();
|
|
281
359
|
const characterStatePresent = /* @__PURE__ */ new Set();
|
|
360
|
+
const vehicles = /* @__PURE__ */ new Map();
|
|
361
|
+
const vehicleBodies = /* @__PURE__ */ new Map();
|
|
362
|
+
const dynamicBodies = /* @__PURE__ */ new Set();
|
|
282
363
|
let characterStates = new Float32Array(48);
|
|
283
364
|
const rayOutput = new Float32Array(8);
|
|
284
365
|
let areaPairs = new Uint32Array(32);
|
|
@@ -304,6 +385,12 @@ function createNativePhysicsSimulation(raw, version) {
|
|
|
304
385
|
throw new Error("TN_NATIVE_PHYSICS_INVALID: query returned an unknown body");
|
|
305
386
|
return handle;
|
|
306
387
|
};
|
|
388
|
+
const requireVehicle = (id, operation) => {
|
|
389
|
+
const record = vehicles.get(id);
|
|
390
|
+
if (record === void 0)
|
|
391
|
+
throw new Error(`TN_VEHICLE_UNKNOWN: ${operation} references an unknown vehicle ${id}.`);
|
|
392
|
+
return record;
|
|
393
|
+
};
|
|
307
394
|
const finiteQueryVector = (value, label) => {
|
|
308
395
|
if (!Number.isFinite(value.x) || !Number.isFinite(value.y) || !Number.isFinite(value.z))
|
|
309
396
|
throw new Error(`TN_NATIVE_PHYSICS_INVALID: ${label} contains a non-finite vector`);
|
|
@@ -409,7 +496,7 @@ function createNativePhysicsSimulation(raw, version) {
|
|
|
409
496
|
throw new Error("Physics body continuousCollision must be a boolean.");
|
|
410
497
|
requireFiniteVector(options.position, "body position");
|
|
411
498
|
requireFiniteRotation(options.rotation, "body rotation");
|
|
412
|
-
const shape =
|
|
499
|
+
const shape = nativeBodyShape(options.shape);
|
|
413
500
|
const sensor = requirePhysicsBodySensor(options);
|
|
414
501
|
const id = raw.createBody({
|
|
415
502
|
collisionLayer: shape.collisionLayer,
|
|
@@ -431,6 +518,7 @@ function createNativePhysicsSimulation(raw, version) {
|
|
|
431
518
|
const bodyHandleValue = physicsBodyHandle(id, rawHandle, options.entity);
|
|
432
519
|
bodyIds.add(id);
|
|
433
520
|
bodyHandles.set(id, bodyHandleValue);
|
|
521
|
+
if (options.type === "dynamic") dynamicBodies.add(id);
|
|
434
522
|
if (sensor) {
|
|
435
523
|
areaIntersections.set(id, /* @__PURE__ */ new Set());
|
|
436
524
|
}
|
|
@@ -471,6 +559,7 @@ function createNativePhysicsSimulation(raw, version) {
|
|
|
471
559
|
raw.removeBody(id);
|
|
472
560
|
bodyIds.delete(id);
|
|
473
561
|
bodyHandles.delete(id);
|
|
562
|
+
dynamicBodies.delete(id);
|
|
474
563
|
for (const [jointId, bodies] of jointBodies) {
|
|
475
564
|
if (bodies[0] === id || bodies[1] === id) jointBodies.delete(jointId);
|
|
476
565
|
}
|
|
@@ -478,6 +567,11 @@ function createNativePhysicsSimulation(raw, version) {
|
|
|
478
567
|
characterIds.delete(id);
|
|
479
568
|
characterState.delete(id);
|
|
480
569
|
characterStatePresent.delete(id);
|
|
570
|
+
for (const [vehicleId, bodyId2] of vehicleBodies) {
|
|
571
|
+
if (bodyId2 !== id) continue;
|
|
572
|
+
vehicleBodies.delete(vehicleId);
|
|
573
|
+
vehicles.delete(vehicleId);
|
|
574
|
+
}
|
|
481
575
|
invalidateObservations();
|
|
482
576
|
},
|
|
483
577
|
removeJoint: (id) => {
|
|
@@ -641,6 +735,73 @@ function createNativePhysicsSimulation(raw, version) {
|
|
|
641
735
|
requirePhysicsEventBuffer(buffer);
|
|
642
736
|
return raw.drainCollisionEvents(buffer);
|
|
643
737
|
},
|
|
738
|
+
createVehicle: (options) => {
|
|
739
|
+
requireLive();
|
|
740
|
+
const requested = requirePhysicsVehicleCreateOptions(options);
|
|
741
|
+
if (!bodyIds.has(requested.bodyId))
|
|
742
|
+
throw new Error(
|
|
743
|
+
`TN_PHYSICS_UNKNOWN_BODY: createVehicle references an unknown body ${requested.bodyId}.`
|
|
744
|
+
);
|
|
745
|
+
if (!dynamicBodies.has(requested.bodyId))
|
|
746
|
+
throw new Error(
|
|
747
|
+
`TN_PHYSICS_NOT_DYNAMIC: createVehicle needs a dynamic body; body ${requested.bodyId} is not dynamic.`
|
|
748
|
+
);
|
|
749
|
+
if (raw.createVehicle === void 0)
|
|
750
|
+
throw new Error("TN_NATIVE_PHYSICS_VEHICLE_MISSING: runtime ABI is too old");
|
|
751
|
+
const wheelCount = requested.wheels.length;
|
|
752
|
+
const id = raw.createVehicle({
|
|
753
|
+
axle: requested.axle,
|
|
754
|
+
bodyId: requested.bodyId,
|
|
755
|
+
forwardAxis: requested.forwardAxis,
|
|
756
|
+
wheels: flattenVehicleWheels(requested)
|
|
757
|
+
});
|
|
758
|
+
if (!Number.isSafeInteger(id) || id < 0)
|
|
759
|
+
throw new Error("TN_NATIVE_PHYSICS_INVALID: runtime returned an invalid vehicle id");
|
|
760
|
+
const buffer = new Float32Array(1 + wheelCount * PHYSICS_VEHICLE_WHEEL_STRIDE);
|
|
761
|
+
vehicles.set(id, {
|
|
762
|
+
buffer,
|
|
763
|
+
state: { speed: 0, wheels: buffer.subarray(1) }
|
|
764
|
+
});
|
|
765
|
+
vehicleBodies.set(id, requested.bodyId);
|
|
766
|
+
return id;
|
|
767
|
+
},
|
|
768
|
+
setVehicleInput: (id, input) => {
|
|
769
|
+
requireLive();
|
|
770
|
+
if (raw.setVehicleInput === void 0)
|
|
771
|
+
throw new Error("TN_NATIVE_PHYSICS_VEHICLE_MISSING: runtime ABI is too old");
|
|
772
|
+
if (typeof input?.engineForce !== "number" || typeof input.brake !== "number" || typeof input.steering !== "number" || !Number.isFinite(input.engineForce) || !Number.isFinite(input.brake) || !Number.isFinite(input.steering))
|
|
773
|
+
throw new Error(
|
|
774
|
+
"TN_PHYSICS_NON_FINITE: vehicle input needs a finite engineForce, brake and steering."
|
|
775
|
+
);
|
|
776
|
+
requireVehicle(id, "setVehicleInput");
|
|
777
|
+
raw.setVehicleInput(id, input);
|
|
778
|
+
},
|
|
779
|
+
readVehicleState: (id) => {
|
|
780
|
+
requireLive();
|
|
781
|
+
if (raw.readVehicleState === void 0)
|
|
782
|
+
throw new Error("TN_NATIVE_PHYSICS_VEHICLE_MISSING: runtime ABI is too old");
|
|
783
|
+
const record = requireVehicle(id, "readVehicleState");
|
|
784
|
+
const count = raw.readVehicleState(id, record.buffer);
|
|
785
|
+
if (!Number.isSafeInteger(count) || count < 1)
|
|
786
|
+
throw new Error("TN_VEHICLE_UNKNOWN: the vehicle left the backend.");
|
|
787
|
+
if (count !== record.buffer.length)
|
|
788
|
+
throw new Error(
|
|
789
|
+
`TN_VEHICLE_STATE_SHORT: readVehicleState wrote ${count} of ${record.buffer.length} floats for vehicle ${id}.`
|
|
790
|
+
);
|
|
791
|
+
record.state.speed = record.buffer[0] ?? 0;
|
|
792
|
+
return record.state;
|
|
793
|
+
},
|
|
794
|
+
resetVehicle: (id, position, yaw) => {
|
|
795
|
+
requireLive();
|
|
796
|
+
if (raw.resetVehicle === void 0)
|
|
797
|
+
throw new Error("TN_NATIVE_PHYSICS_VEHICLE_MISSING: runtime ABI is too old");
|
|
798
|
+
requireVehicle(id, "resetVehicle");
|
|
799
|
+
requireFiniteVector(position, "vehicle position");
|
|
800
|
+
if (typeof yaw !== "number" || !Number.isFinite(yaw))
|
|
801
|
+
throw new Error("TN_PHYSICS_NON_FINITE: vehicle yaw must be a finite number of radians.");
|
|
802
|
+
raw.resetVehicle(id, position, yaw);
|
|
803
|
+
bodyTransformsDirty = true;
|
|
804
|
+
},
|
|
644
805
|
dispose: () => {
|
|
645
806
|
if (disposed) return;
|
|
646
807
|
disposed = true;
|
|
@@ -649,6 +810,9 @@ function createNativePhysicsSimulation(raw, version) {
|
|
|
649
810
|
bodyTransformRows.clear();
|
|
650
811
|
bodyHandles.clear();
|
|
651
812
|
jointBodies.clear();
|
|
813
|
+
vehicles.clear();
|
|
814
|
+
vehicleBodies.clear();
|
|
815
|
+
dynamicBodies.clear();
|
|
652
816
|
characterIds.clear();
|
|
653
817
|
characterState.clear();
|
|
654
818
|
characterStatePresent.clear();
|
|
@@ -1190,6 +1354,15 @@ var CollisionShape3D = class _CollisionShape3D {
|
|
|
1190
1354
|
const width = (bounds.max.x - bounds.min.x) * Math.abs(mesh.scale.x);
|
|
1191
1355
|
const height = (bounds.max.y - bounds.min.y) * Math.abs(mesh.scale.y);
|
|
1192
1356
|
const depth = (bounds.max.z - bounds.min.z) * Math.abs(mesh.scale.z);
|
|
1357
|
+
const offset = Math.max(
|
|
1358
|
+
Math.abs(bounds.max.x + bounds.min.x) / 2 * Math.abs(mesh.scale.x),
|
|
1359
|
+
Math.abs(bounds.max.y + bounds.min.y) / 2 * Math.abs(mesh.scale.y),
|
|
1360
|
+
Math.abs(bounds.max.z + bounds.min.z) / 2 * Math.abs(mesh.scale.z)
|
|
1361
|
+
);
|
|
1362
|
+
if (offset > 1e-4 + 1e-3 * Math.max(width, height, depth))
|
|
1363
|
+
throw new Error(
|
|
1364
|
+
`TN_COLLISION_SHAPE_OFF_CENTRE: '${mesh.name || "mesh"}' has geometry centred ${offset.toFixed(3)} from its origin, so a ${inferred} collider would sit away from what is drawn. Call geometry.center() and move the mesh instead, pass "trimesh", or build level colliders with buildStaticColliders.`
|
|
1365
|
+
);
|
|
1193
1366
|
if (inferred === "sphere") {
|
|
1194
1367
|
geometry.computeBoundingSphere();
|
|
1195
1368
|
const radius = (geometry.boundingSphere?.radius ?? Math.max(width, height, depth) / 2) * Math.max(Math.abs(mesh.scale.x), Math.abs(mesh.scale.y), Math.abs(mesh.scale.z));
|
|
@@ -1812,6 +1985,169 @@ function softBodyCollision(...bodies) {
|
|
|
1812
1985
|
return new PhysicsSoftBodyCollision(bodies);
|
|
1813
1986
|
}
|
|
1814
1987
|
|
|
1988
|
+
// src/VehicleBody3D.ts
|
|
1989
|
+
var FORWARD_AXES = {
|
|
1990
|
+
"-x": { axle: { x: 0, y: 0, z: -1 }, index: 0 },
|
|
1991
|
+
"-z": { axle: { x: 1, y: 0, z: 0 }, index: 2 },
|
|
1992
|
+
x: { axle: { x: 0, y: 0, z: 1 }, index: 0 },
|
|
1993
|
+
z: { axle: { x: -1, y: 0, z: 0 }, index: 2 }
|
|
1994
|
+
};
|
|
1995
|
+
function requireFinite(value, label) {
|
|
1996
|
+
if (typeof value !== "number" || !Number.isFinite(value))
|
|
1997
|
+
throw new Error(`TN_VEHICLE_INVALID: ${label} must be a finite number.`);
|
|
1998
|
+
}
|
|
1999
|
+
var VehicleBody3D = class extends RigidBody3D {
|
|
2000
|
+
wheels;
|
|
2001
|
+
#object;
|
|
2002
|
+
#simulation;
|
|
2003
|
+
#readState;
|
|
2004
|
+
#setInput;
|
|
2005
|
+
#vehicle;
|
|
2006
|
+
#engineForce = 0;
|
|
2007
|
+
#brake = 0;
|
|
2008
|
+
#steering = 0;
|
|
2009
|
+
#disposed = false;
|
|
2010
|
+
constructor(options) {
|
|
2011
|
+
const forward = FORWARD_AXES[options.forwardAxis ?? "-z"];
|
|
2012
|
+
if (forward === void 0)
|
|
2013
|
+
throw new Error(
|
|
2014
|
+
`TN_VEHICLE_INVALID: forwardAxis '${String(options.forwardAxis)}' must be one of -x, -z, x, z.`
|
|
2015
|
+
);
|
|
2016
|
+
const simulation = requirePhysicsSimulation(options.physics, options.world);
|
|
2017
|
+
const createVehicle = simulation.createVehicle;
|
|
2018
|
+
const setVehicleInput = simulation.setVehicleInput;
|
|
2019
|
+
const readVehicleState = simulation.readVehicleState;
|
|
2020
|
+
if (createVehicle === void 0 || setVehicleInput === void 0 || readVehicleState === void 0)
|
|
2021
|
+
throw new Error(
|
|
2022
|
+
"TN_VEHICLE_NATIVE_UNAVAILABLE: the active physics backend has no vehicle controller. Use RigidBody3D instead."
|
|
2023
|
+
);
|
|
2024
|
+
super({
|
|
2025
|
+
collisionLayer: options.collisionLayer,
|
|
2026
|
+
collisionMask: options.collisionMask,
|
|
2027
|
+
entity: options.entity,
|
|
2028
|
+
mass: options.mass ?? 0,
|
|
2029
|
+
object: options.object,
|
|
2030
|
+
physics: options.physics,
|
|
2031
|
+
shape: options.shape,
|
|
2032
|
+
type: "dynamic",
|
|
2033
|
+
world: options.world
|
|
2034
|
+
});
|
|
2035
|
+
this.wheels = options.wheels;
|
|
2036
|
+
this.#object = options.object;
|
|
2037
|
+
this.#simulation = simulation;
|
|
2038
|
+
this.#readState = readVehicleState;
|
|
2039
|
+
this.#setInput = setVehicleInput;
|
|
2040
|
+
try {
|
|
2041
|
+
this.#vehicle = createVehicle.call(simulation, {
|
|
2042
|
+
axle: forward.axle,
|
|
2043
|
+
bodyId: this.body.id,
|
|
2044
|
+
forwardAxis: forward.index,
|
|
2045
|
+
wheels: options.wheels
|
|
2046
|
+
});
|
|
2047
|
+
} catch (error) {
|
|
2048
|
+
this.dispose();
|
|
2049
|
+
throw error;
|
|
2050
|
+
}
|
|
2051
|
+
}
|
|
2052
|
+
/**
|
|
2053
|
+
* Godot's `engine_force`, in newtons. Every wheel with `useAsTraction` receives the full value,
|
|
2054
|
+
* so two driven wheels are twice the force. Negative is reverse.
|
|
2055
|
+
*/
|
|
2056
|
+
get engineForce() {
|
|
2057
|
+
this.#requireLive("engineForce");
|
|
2058
|
+
return this.#engineForce;
|
|
2059
|
+
}
|
|
2060
|
+
set engineForce(value) {
|
|
2061
|
+
requireFinite(value, "engineForce");
|
|
2062
|
+
if (value === this.#engineForce) return;
|
|
2063
|
+
this.#engineForce = value;
|
|
2064
|
+
this.#pushInput();
|
|
2065
|
+
}
|
|
2066
|
+
/** Godot's `brake`, applied to every wheel. It is a braking impulse, not a force. */
|
|
2067
|
+
get brake() {
|
|
2068
|
+
this.#requireLive("brake");
|
|
2069
|
+
return this.#brake;
|
|
2070
|
+
}
|
|
2071
|
+
set brake(value) {
|
|
2072
|
+
requireFinite(value, "brake");
|
|
2073
|
+
if (value === this.#brake) return;
|
|
2074
|
+
this.#brake = value;
|
|
2075
|
+
this.#pushInput();
|
|
2076
|
+
}
|
|
2077
|
+
/** Godot's `steering`, in radians, applied to every wheel with `useAsSteering`. */
|
|
2078
|
+
get steering() {
|
|
2079
|
+
this.#requireLive("steering");
|
|
2080
|
+
return this.#steering;
|
|
2081
|
+
}
|
|
2082
|
+
set steering(value) {
|
|
2083
|
+
requireFinite(value, "steering");
|
|
2084
|
+
if (value === this.#steering) return;
|
|
2085
|
+
this.#steering = value;
|
|
2086
|
+
this.#pushInput();
|
|
2087
|
+
}
|
|
2088
|
+
/**
|
|
2089
|
+
* Signed metres per second along `forwardAxis`: positive while accelerating forward. It reflects
|
|
2090
|
+
* the last completed step, so read it after the physics update.
|
|
2091
|
+
*/
|
|
2092
|
+
get speed() {
|
|
2093
|
+
return this.#state().speed;
|
|
2094
|
+
}
|
|
2095
|
+
/** Whether this wheel's suspension ray found ground on the last completed step. */
|
|
2096
|
+
wheelContact(index) {
|
|
2097
|
+
return this.#wheel(index, 0) === 1;
|
|
2098
|
+
}
|
|
2099
|
+
/** How far the wheel hangs below its attachment point, in metres. Below the rest length while loaded. */
|
|
2100
|
+
wheelSuspensionLength(index) {
|
|
2101
|
+
return this.#wheel(index, 1);
|
|
2102
|
+
}
|
|
2103
|
+
/** The wheel's spin angle in radians, for animating the mesh. */
|
|
2104
|
+
wheelRotation(index) {
|
|
2105
|
+
return this.#wheel(index, 2);
|
|
2106
|
+
}
|
|
2107
|
+
/**
|
|
2108
|
+
* Respawn: move the chassis, face `yaw` radians, and drop both velocities. Wheels keep their
|
|
2109
|
+
* cached spin, so the first frame after a rescue is one frame of stale wheel animation.
|
|
2110
|
+
*/
|
|
2111
|
+
teleport(position, yaw) {
|
|
2112
|
+
this.#requireLive("teleport");
|
|
2113
|
+
if (this.#simulation.resetVehicle === void 0)
|
|
2114
|
+
throw new Error(
|
|
2115
|
+
"TN_VEHICLE_NATIVE_UNAVAILABLE: the active physics backend cannot respawn a vehicle."
|
|
2116
|
+
);
|
|
2117
|
+
requireFinite(yaw, "yaw");
|
|
2118
|
+
this.#simulation.resetVehicle(this.#vehicle, position, yaw);
|
|
2119
|
+
this.#object.position.set(position.x, position.y, position.z);
|
|
2120
|
+
}
|
|
2121
|
+
dispose() {
|
|
2122
|
+
super.dispose();
|
|
2123
|
+
this.#disposed = true;
|
|
2124
|
+
}
|
|
2125
|
+
#pushInput() {
|
|
2126
|
+
if (this.#disposed) throw new Error("VehicleBody3D input cannot be used after dispose.");
|
|
2127
|
+
this.#setInput(this.#vehicle, {
|
|
2128
|
+
brake: this.#brake,
|
|
2129
|
+
engineForce: this.#engineForce,
|
|
2130
|
+
steering: this.#steering
|
|
2131
|
+
});
|
|
2132
|
+
}
|
|
2133
|
+
#state() {
|
|
2134
|
+
this.#requireLive("vehicle state");
|
|
2135
|
+
const state = this.#readState(this.#vehicle);
|
|
2136
|
+
if (state === void 0) throw new Error("TN_VEHICLE_UNKNOWN: the vehicle left the backend.");
|
|
2137
|
+
return state;
|
|
2138
|
+
}
|
|
2139
|
+
#wheel(index, field) {
|
|
2140
|
+
if (!Number.isSafeInteger(index) || index < 0 || index >= this.wheels.length)
|
|
2141
|
+
throw new Error(
|
|
2142
|
+
`TN_VEHICLE_INVALID: wheel index ${String(index)} is outside 0 through ${this.wheels.length - 1}.`
|
|
2143
|
+
);
|
|
2144
|
+
return this.#state().wheels[index * PHYSICS_VEHICLE_WHEEL_STRIDE + field] ?? 0;
|
|
2145
|
+
}
|
|
2146
|
+
#requireLive(operation) {
|
|
2147
|
+
if (this.#disposed) throw new Error(`VehicleBody3D.${operation} cannot be used after dispose.`);
|
|
2148
|
+
}
|
|
2149
|
+
};
|
|
2150
|
+
|
|
1815
2151
|
// src/plugin.ts
|
|
1816
2152
|
var PHYSICS_DEBUG_BODY_LIMIT = 100;
|
|
1817
2153
|
var PHYSICS_DEBUG_SAMPLE_LIMIT = 100;
|
|
@@ -1856,6 +2192,7 @@ function rapier(options = {}) {
|
|
|
1856
2192
|
let events = new Uint32Array(64);
|
|
1857
2193
|
const activeContacts = /* @__PURE__ */ new Map();
|
|
1858
2194
|
let debugSeries = [];
|
|
2195
|
+
let completedSteps = 0;
|
|
1859
2196
|
let unregisterObservations;
|
|
1860
2197
|
function buildContext(selected) {
|
|
1861
2198
|
return {
|
|
@@ -1932,6 +2269,7 @@ function rapier(options = {}) {
|
|
|
1932
2269
|
};
|
|
1933
2270
|
for (const buoyancy of buoyancies) buoyancy.apply(dt);
|
|
1934
2271
|
simulation.step(dt, input);
|
|
2272
|
+
completedSteps += 1;
|
|
1935
2273
|
visible = growFloat(visible, bodies.size + areas.size);
|
|
1936
2274
|
let visibleResizeAttempts = 0;
|
|
1937
2275
|
for (; ; ) {
|
|
@@ -2053,7 +2391,8 @@ function rapier(options = {}) {
|
|
|
2053
2391
|
};
|
|
2054
2392
|
function physicsObservations(ctx, runtime, request) {
|
|
2055
2393
|
if (simulation === void 0) throw new Error("Physics observations sampled before setup.");
|
|
2056
|
-
if (request.include?.includes("physicsDebugSeries") !== true)
|
|
2394
|
+
if (request.include?.includes("physicsDebugSeries") !== true)
|
|
2395
|
+
return { physicsSteps: completedSteps };
|
|
2057
2396
|
if (request.label !== void 0) {
|
|
2058
2397
|
if (debugSeries.some(({ label }) => label === request.label)) {
|
|
2059
2398
|
throw new Error(
|
|
@@ -2071,7 +2410,10 @@ function rapier(options = {}) {
|
|
|
2071
2410
|
tick: runtime.tick()
|
|
2072
2411
|
});
|
|
2073
2412
|
}
|
|
2074
|
-
return {
|
|
2413
|
+
return {
|
|
2414
|
+
physicsDebugSeries: debugSeries.map((sample) => ({ ...sample })),
|
|
2415
|
+
physicsSteps: completedSteps
|
|
2416
|
+
};
|
|
2075
2417
|
}
|
|
2076
2418
|
function physicsDebugSnapshot(ctx) {
|
|
2077
2419
|
if (simulation === void 0) throw new Error("Physics observations sampled before setup.");
|
|
@@ -2163,4 +2505,4 @@ function physicsEntityId(ctx, body) {
|
|
|
2163
2505
|
return stableId;
|
|
2164
2506
|
}
|
|
2165
2507
|
|
|
2166
|
-
export { Area3D, Buoyancy3D, CharacterBody3D, CollisionShape3D, Joint3D, MAX_PHYSICS_QUERY_RESULTS, PHYSICS_COLLISION_EVENT_STRIDE, PHYSICS_TRANSFORM_STRIDE, PhysicsDirectSpaceState3D, RigidBody3D, buildStaticColliders, interactionGroups, rapier, softBodyCollision };
|
|
2508
|
+
export { Area3D, Buoyancy3D, CharacterBody3D, CollisionShape3D, Joint3D, MAX_PHYSICS_QUERY_RESULTS, PHYSICS_COLLISION_EVENT_STRIDE, PHYSICS_TRANSFORM_STRIDE, PHYSICS_VEHICLE_WHEEL_STRIDE, PhysicsDirectSpaceState3D, RigidBody3D, VehicleBody3D, buildStaticColliders, interactionGroups, rapier, softBodyCollision };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import '@threenative/core';
|
|
2
2
|
import 'recast-navigation';
|
|
3
|
-
export {
|
|
3
|
+
export { a7 as INavigationAgent3DOptions, a8 as INavigationContext, a9 as INavigationObstacle3DOptions, aa as INavigationRegion3DOptions, ab as NavigationAgent3D, ac as NavigationAgentEvent, ad as NavigationAgentHandler, ae as NavigationObstacle3D, af as NavigationPlugin, ag as NavigationRegion3D, ah as recast } from '../Area3D-DI_kCf7I.js';
|
|
4
4
|
import 'three';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@threenative/physics",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.4",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "Thin Rapier physics bindings for ThreeNative games.",
|
|
6
6
|
"keywords": [
|
|
@@ -55,7 +55,7 @@
|
|
|
55
55
|
"recast-navigation": "0.43.1"
|
|
56
56
|
},
|
|
57
57
|
"peerDependencies": {
|
|
58
|
-
"@threenative/core": ">=0.3.
|
|
58
|
+
"@threenative/core": ">=0.3.4 <0.4.0",
|
|
59
59
|
"three": ">=0.185.0 <0.186.0"
|
|
60
60
|
},
|
|
61
61
|
"devDependencies": {
|
|
@@ -64,7 +64,7 @@
|
|
|
64
64
|
"three": "0.185.1",
|
|
65
65
|
"tsup": "8.5.1",
|
|
66
66
|
"typescript": "5.9.3",
|
|
67
|
-
"@threenative/core": "0.3.
|
|
67
|
+
"@threenative/core": "0.3.4"
|
|
68
68
|
},
|
|
69
69
|
"scripts": {
|
|
70
70
|
"build": "tsup --config tsup.config.ts && publint",
|