IPhysicsEnginePlugin.d.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. import type { Vector3, Quaternion } from "../../Maths/math.vector";
  2. import type { IRaycastQuery, PhysicsRaycastResult } from "../physicsRaycastResult";
  3. import type { PhysicsBody } from "./physicsBody";
  4. import type { PhysicsShape } from "./physicsShape";
  5. import type { PhysicsConstraint } from "./physicsConstraint";
  6. import type { BoundingBox } from "../../Culling/boundingBox";
  7. import type { TransformNode } from "../../Meshes/transformNode";
  8. import type { PhysicsMaterial } from "./physicsMaterial";
  9. import type { Mesh } from "../../Meshes/mesh";
  10. import type { Nullable } from "../../types.js";
  11. import type { Observable } from "../../Misc/observable.js";
  12. /** How a specific axis can be constrained */
  13. export declare enum PhysicsConstraintAxisLimitMode {
  14. FREE = 0,
  15. LIMITED = 1,
  16. LOCKED = 2
  17. }
  18. /** The constraint specific axis to use when setting Friction, `ConstraintAxisLimitMode`, max force, ... */
  19. export declare enum PhysicsConstraintAxis {
  20. LINEAR_X = 0,
  21. LINEAR_Y = 1,
  22. LINEAR_Z = 2,
  23. ANGULAR_X = 3,
  24. ANGULAR_Y = 4,
  25. ANGULAR_Z = 5,
  26. LINEAR_DISTANCE = 6
  27. }
  28. /** Type of Constraint */
  29. export declare enum PhysicsConstraintType {
  30. /**
  31. * A ball and socket constraint will attempt to line up the pivot
  32. * positions in each body, and have no restrictions on rotation
  33. */
  34. BALL_AND_SOCKET = 1,
  35. /**
  36. * A distance constraint will attempt to keep the pivot locations
  37. * within a specified distance.
  38. */
  39. DISTANCE = 2,
  40. /**
  41. * A hinge constraint will keep the pivot positions aligned as well
  42. * as two angular axes. The remaining angular axis will be free to rotate.
  43. */
  44. HINGE = 3,
  45. /**
  46. * A slider constraint allows bodies to translate along one axis and
  47. * rotate about the same axis. The remaining two axes are locked in
  48. * place
  49. */
  50. SLIDER = 4,
  51. /**
  52. * A lock constraint will attempt to keep the pivots completely lined
  53. * up between both bodies, allowing no relative movement.
  54. */
  55. LOCK = 5,
  56. PRISMATIC = 6,
  57. SIX_DOF = 7
  58. }
  59. /** Type of Shape */
  60. export declare enum PhysicsShapeType {
  61. SPHERE = 0,
  62. CAPSULE = 1,
  63. CYLINDER = 2,
  64. BOX = 3,
  65. CONVEX_HULL = 4,
  66. CONTAINER = 5,
  67. MESH = 6,
  68. HEIGHTFIELD = 7
  69. }
  70. /** Optional motor which attempts to move a body at a specific velocity, or at a specific position */
  71. export declare enum PhysicsConstraintMotorType {
  72. NONE = 0,
  73. VELOCITY = 1,
  74. POSITION = 2
  75. }
  76. export declare enum PhysicsEventType {
  77. COLLISION_STARTED = "COLLISION_STARTED",
  78. COLLISION_CONTINUED = "COLLISION_CONTINUED",
  79. COLLISION_FINISHED = "COLLISION_FINISHED",
  80. TRIGGER_ENTERED = "TRIGGER_ENTERED",
  81. TRIGGER_EXITED = "TRIGGER_EXITED"
  82. }
  83. /**
  84. * Base collision object
  85. */
  86. export interface IBasePhysicsCollisionEvent {
  87. /**
  88. * 1st physics body that collided
  89. */
  90. collider: PhysicsBody;
  91. /**
  92. * 2nd physics body that collided
  93. */
  94. collidedAgainst: PhysicsBody;
  95. /**
  96. * index in instances array for the collider
  97. */
  98. colliderIndex: number;
  99. /**
  100. * index in instances array for the collidedAgainst
  101. */
  102. collidedAgainstIndex: number;
  103. /**
  104. * Event type
  105. */
  106. type: PhysicsEventType;
  107. }
  108. /**
  109. * Collision object that is the parameter when notification for collision fires.
  110. */
  111. export interface IPhysicsCollisionEvent extends IBasePhysicsCollisionEvent {
  112. /**
  113. * World position where the collision occured
  114. */
  115. point: Nullable<Vector3>;
  116. /**
  117. * Penetration distance
  118. */
  119. distance: number;
  120. /**
  121. * Impulse value computed by the solver response
  122. */
  123. impulse: number;
  124. /**
  125. * Collision world normal direction
  126. */
  127. normal: Nullable<Vector3>;
  128. }
  129. /**
  130. * Parameters used to describe the Shape
  131. */
  132. export interface PhysicsShapeParameters {
  133. /**
  134. * Shape center position
  135. */
  136. center?: Vector3;
  137. /**
  138. * Radius for cylinder, shape and capsule
  139. */
  140. radius?: number;
  141. /**
  142. * First point position that defines the cylinder or capsule
  143. */
  144. pointA?: Vector3;
  145. /**
  146. * Second point position that defines the cylinder or capsule
  147. */
  148. pointB?: Vector3;
  149. /**
  150. * Shape orientation
  151. */
  152. rotation?: Quaternion;
  153. /**
  154. * Dimesion extention for the box
  155. */
  156. extents?: Vector3;
  157. /**
  158. * Mesh used for Mesh shape or convex hull. It can be different than the mesh the body is attached to.
  159. */
  160. mesh?: Mesh;
  161. /**
  162. * Use children hierarchy
  163. */
  164. includeChildMeshes?: boolean;
  165. /**
  166. * The size of the heightfield in the X axis
  167. */
  168. heightFieldSizeX?: number;
  169. /**
  170. * The size of the heightfield in the Z axis
  171. */
  172. heightFieldSizeZ?: number;
  173. /**
  174. * The number of samples along the X axis
  175. */
  176. numHeightFieldSamplesX?: number;
  177. /**
  178. * The number of samples along the Z axis
  179. */
  180. numHeightFieldSamplesZ?: number;
  181. /**
  182. * The data for the heightfield
  183. */
  184. heightFieldData?: Float32Array;
  185. }
  186. /**
  187. * Parameters used to describe a Constraint
  188. */
  189. export interface PhysicsConstraintParameters {
  190. /**
  191. * Location of the constraint pivot in the space of first body
  192. */
  193. pivotA?: Vector3;
  194. /**
  195. * Location of the constraint pivot in the space of the second body
  196. */
  197. pivotB?: Vector3;
  198. /**
  199. * An axis in the space of the first body which determines how
  200. * distances/angles are measured for LINEAR_X/ANGULAR_X limits.
  201. */
  202. axisA?: Vector3;
  203. /**
  204. * An axis in the space of the second body which determines how
  205. * distances/angles are measured for LINEAR_X/ANGULAR_X limits.
  206. */
  207. axisB?: Vector3;
  208. /**
  209. * An axis in the space of the first body which determines how
  210. * distances/angles are measured for LINEAR_Y/ANGULAR_Y limits.
  211. */
  212. perpAxisA?: Vector3;
  213. /**
  214. * An axis in the space of the second body which determines how
  215. * distances/angles are measured for LINEAR_Y/ANGULAR_Y limits.
  216. */
  217. perpAxisB?: Vector3;
  218. /**
  219. * The maximum distance that can seperate the two pivots.
  220. * Only used for DISTANCE constraints
  221. */
  222. maxDistance?: number;
  223. /**
  224. * Determines if the connected bodies should collide. Generally,
  225. * it is preferable to set this to false, especially if the constraint
  226. * positions the bodies so that they overlap. Otherwise, the constraint
  227. * will "fight" the collision detection and may cause jitter.
  228. */
  229. collision?: boolean;
  230. }
  231. /**
  232. * Parameters used to describe mass and inertia of the Physics Body
  233. */
  234. export interface PhysicsMassProperties {
  235. /**
  236. * The center of mass, in local space. This is The
  237. * point the body will rotate around when applying
  238. * an angular velocity.
  239. *
  240. * If not provided, the physics engine will compute
  241. * an appropriate value.
  242. */
  243. centerOfMass?: Vector3;
  244. /**
  245. * The total mass of this object, in kilograms. This
  246. * affects how easy it is to move the body. A value
  247. * of zero will be used as an infinite mass.
  248. *
  249. * If not provided, the physics engine will compute
  250. * an appropriate value.
  251. */
  252. mass?: number;
  253. /**
  254. * The principal moments of inertia of this object
  255. * for a unit mass. This determines how easy it is
  256. * for the body to rotate. A value of zero on any
  257. * axis will be used as infinite interia about that
  258. * axis.
  259. *
  260. * If not provided, the physics engine will compute
  261. * an appropriate value.
  262. */
  263. inertia?: Vector3;
  264. /**
  265. * The rotation rotating from inertia major axis space
  266. * to parent space (i.e., the rotation which, when
  267. * applied to the 3x3 inertia tensor causes the inertia
  268. * tensor to become a diagonal matrix). This determines
  269. * how the values of inertia are aligned with the parent
  270. * object.
  271. *
  272. * If not provided, the physics engine will compute
  273. * an appropriate value.
  274. */
  275. inertiaOrientation?: Quaternion;
  276. }
  277. /**
  278. * Indicates how the body will behave.
  279. */
  280. export declare enum PhysicsMotionType {
  281. STATIC = 0,
  282. ANIMATED = 1,
  283. DYNAMIC = 2
  284. }
  285. /**
  286. * Controls the body sleep mode.
  287. */
  288. export declare enum PhysicsActivationControl {
  289. SIMULATION_CONTROLLED = 0,
  290. ALWAYS_ACTIVE = 1,
  291. ALWAYS_INACTIVE = 2
  292. }
  293. /**
  294. * Represents a pair of bodies connected by a constraint.
  295. */
  296. export type ConstrainedBodyPair = {
  297. parentBody: PhysicsBody;
  298. parentBodyIndex: number;
  299. childBody: PhysicsBody;
  300. childBodyIndex: number;
  301. };
  302. /** @internal */
  303. export interface IPhysicsEnginePluginV2 {
  304. /**
  305. * Physics plugin world instance
  306. */
  307. world: any;
  308. /**
  309. * Physics plugin name
  310. */
  311. name: string;
  312. /**
  313. * Collision observable
  314. */
  315. onCollisionObservable: Observable<IPhysicsCollisionEvent>;
  316. /**
  317. * Collision ended observable
  318. */
  319. onCollisionEndedObservable: Observable<IBasePhysicsCollisionEvent>;
  320. /**
  321. * Trigger observable
  322. */
  323. onTriggerCollisionObservable: Observable<IBasePhysicsCollisionEvent>;
  324. setGravity(gravity: Vector3): void;
  325. setTimeStep(timeStep: number): void;
  326. getTimeStep(): number;
  327. executeStep(delta: number, bodies: Array<PhysicsBody>): void;
  328. getPluginVersion(): number;
  329. initBody(body: PhysicsBody, motionType: PhysicsMotionType, position: Vector3, orientation: Quaternion): void;
  330. initBodyInstances(body: PhysicsBody, motionType: PhysicsMotionType, mesh: Mesh): void;
  331. updateBodyInstances(body: PhysicsBody, mesh: Mesh): void;
  332. removeBody(body: PhysicsBody): void;
  333. sync(body: PhysicsBody): void;
  334. syncTransform(body: PhysicsBody, transformNode: TransformNode): void;
  335. setShape(body: PhysicsBody, shape: Nullable<PhysicsShape>): void;
  336. getShape(body: PhysicsBody): Nullable<PhysicsShape>;
  337. getShapeType(shape: PhysicsShape): PhysicsShapeType;
  338. setEventMask(body: PhysicsBody, eventMask: number, instanceIndex?: number): void;
  339. getEventMask(body: PhysicsBody, instanceIndex?: number): number;
  340. setMotionType(body: PhysicsBody, motionType: PhysicsMotionType, instanceIndex?: number): void;
  341. getMotionType(body: PhysicsBody, instanceIndex?: number): PhysicsMotionType;
  342. computeMassProperties(body: PhysicsBody, instanceIndex?: number): PhysicsMassProperties;
  343. setMassProperties(body: PhysicsBody, massProps: PhysicsMassProperties, instanceIndex?: number): void;
  344. getMassProperties(body: PhysicsBody, instanceIndex?: number): PhysicsMassProperties;
  345. setLinearDamping(body: PhysicsBody, damping: number, instanceIndex?: number): void;
  346. getLinearDamping(body: PhysicsBody, instanceIndex?: number): number;
  347. setAngularDamping(body: PhysicsBody, damping: number, instanceIndex?: number): void;
  348. getAngularDamping(body: PhysicsBody, instanceIndex?: number): number;
  349. setLinearVelocity(body: PhysicsBody, linVel: Vector3, instanceIndex?: number): void;
  350. getLinearVelocityToRef(body: PhysicsBody, linVel: Vector3, instanceIndex?: number): void;
  351. applyImpulse(body: PhysicsBody, impulse: Vector3, location: Vector3, instanceIndex?: number): void;
  352. applyAngularImpulse(body: PhysicsBody, angularImpulse: Vector3, instanceIndex?: number): void;
  353. applyForce(body: PhysicsBody, force: Vector3, location: Vector3, instanceIndex?: number): void;
  354. setAngularVelocity(body: PhysicsBody, angVel: Vector3, instanceIndex?: number): void;
  355. getAngularVelocityToRef(body: PhysicsBody, angVel: Vector3, instanceIndex?: number): void;
  356. getBodyGeometry(body: PhysicsBody): {};
  357. disposeBody(body: PhysicsBody): void;
  358. setCollisionCallbackEnabled(body: PhysicsBody, enabled: boolean, instanceIndex?: number): void;
  359. setCollisionEndedCallbackEnabled(body: PhysicsBody, enabled: boolean, instanceIndex?: number): void;
  360. addConstraint(body: PhysicsBody, childBody: PhysicsBody, constraint: PhysicsConstraint, instanceIndex?: number, childInstanceIndex?: number): void;
  361. getCollisionObservable(body: PhysicsBody, instanceIndex?: number): Observable<IPhysicsCollisionEvent>;
  362. getCollisionEndedObservable(body: PhysicsBody, instanceIndex?: number): Observable<IBasePhysicsCollisionEvent>;
  363. setGravityFactor(body: PhysicsBody, factor: number, instanceIndex?: number): void;
  364. getGravityFactor(body: PhysicsBody, instanceIndex?: number): number;
  365. setTargetTransform(body: PhysicsBody, position: Vector3, rotation: Quaternion, instanceIndex?: number): void;
  366. initShape(shape: PhysicsShape, type: PhysicsShapeType, options: PhysicsShapeParameters): void;
  367. setShapeFilterMembershipMask(shape: PhysicsShape, membershipMask: number): void;
  368. getShapeFilterMembershipMask(shape: PhysicsShape): number;
  369. setShapeFilterCollideMask(shape: PhysicsShape, collideMask: number): void;
  370. getShapeFilterCollideMask(shape: PhysicsShape): number;
  371. setMaterial(shape: PhysicsShape, material: PhysicsMaterial): void;
  372. getMaterial(shape: PhysicsShape): PhysicsMaterial;
  373. setDensity(shape: PhysicsShape, density: number): void;
  374. getDensity(shape: PhysicsShape): number;
  375. addChild(shape: PhysicsShape, newChild: PhysicsShape, translation?: Vector3, rotation?: Quaternion, scale?: Vector3): void;
  376. removeChild(shape: PhysicsShape, childIndex: number): void;
  377. getNumChildren(shape: PhysicsShape): number;
  378. getBoundingBox(shape: PhysicsShape): BoundingBox;
  379. disposeShape(shape: PhysicsShape): void;
  380. setTrigger(shape: PhysicsShape, isTrigger: boolean): void;
  381. initConstraint(constraint: PhysicsConstraint, body: PhysicsBody, childBody: PhysicsBody): void;
  382. setEnabled(constraint: PhysicsConstraint, isEnabled: boolean): void;
  383. getEnabled(constraint: PhysicsConstraint): boolean;
  384. setCollisionsEnabled(constraint: PhysicsConstraint, isEnabled: boolean): void;
  385. getCollisionsEnabled(constraint: PhysicsConstraint): boolean;
  386. setAxisFriction(constraint: PhysicsConstraint, axis: PhysicsConstraintAxis, friction: number): void;
  387. getAxisFriction(constraint: PhysicsConstraint, axis: PhysicsConstraintAxis): Nullable<number>;
  388. setAxisMode(constraint: PhysicsConstraint, axis: PhysicsConstraintAxis, limitMode: PhysicsConstraintAxisLimitMode): void;
  389. getAxisMode(constraint: PhysicsConstraint, axis: PhysicsConstraintAxis): Nullable<PhysicsConstraintAxisLimitMode>;
  390. setAxisMinLimit(constraint: PhysicsConstraint, axis: PhysicsConstraintAxis, minLimit: number): void;
  391. getAxisMinLimit(constraint: PhysicsConstraint, axis: PhysicsConstraintAxis): Nullable<number>;
  392. setAxisMaxLimit(constraint: PhysicsConstraint, axis: PhysicsConstraintAxis, limit: number): void;
  393. getAxisMaxLimit(constraint: PhysicsConstraint, axis: PhysicsConstraintAxis): Nullable<number>;
  394. setAxisMotorType(constraint: PhysicsConstraint, axis: PhysicsConstraintAxis, motorType: PhysicsConstraintMotorType): void;
  395. getAxisMotorType(constraint: PhysicsConstraint, axis: PhysicsConstraintAxis): Nullable<PhysicsConstraintMotorType>;
  396. setAxisMotorTarget(constraint: PhysicsConstraint, axis: PhysicsConstraintAxis, target: number): void;
  397. getAxisMotorTarget(constraint: PhysicsConstraint, axis: PhysicsConstraintAxis): Nullable<number>;
  398. setAxisMotorMaxForce(constraint: PhysicsConstraint, axis: PhysicsConstraintAxis, maxForce: number): void;
  399. getAxisMotorMaxForce(constraint: PhysicsConstraint, axis: PhysicsConstraintAxis): Nullable<number>;
  400. disposeConstraint(constraint: PhysicsConstraint): void;
  401. getBodiesUsingConstraint(constraint: PhysicsConstraint): ConstrainedBodyPair[];
  402. raycast(from: Vector3, to: Vector3, result: PhysicsRaycastResult, query?: IRaycastQuery): void;
  403. dispose(): void;
  404. }