physicsConstraint.d.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. import type { Scene } from "../../scene";
  2. import type { Vector3 } from "../../Maths/math.vector";
  3. import type { Nullable } from "../../types";
  4. import type { IPhysicsEnginePluginV2, PhysicsConstraintParameters, PhysicsConstraintAxisLimitMode, PhysicsConstraintMotorType, ConstrainedBodyPair } from "./IPhysicsEnginePlugin";
  5. import { PhysicsConstraintAxis, PhysicsConstraintType } from "./IPhysicsEnginePlugin";
  6. /**
  7. * This is a holder class for the physics constraint created by the physics plugin
  8. * It holds a set of functions to control the underlying constraint
  9. * @see https://doc.babylonjs.com/features/featuresDeepDive/physics/usingPhysicsEngine
  10. */
  11. export declare class PhysicsConstraint {
  12. /**
  13. * V2 Physics plugin private data for a physics material
  14. */
  15. _pluginData: any;
  16. /**
  17. * The V2 plugin used to create and manage this Physics Body
  18. */
  19. protected _physicsPlugin: IPhysicsEnginePluginV2;
  20. protected _options: PhysicsConstraintParameters;
  21. protected _type: PhysicsConstraintType;
  22. /**
  23. * @internal
  24. * The internal options that were used to init the constraint
  25. */
  26. _initOptions?: PhysicsConstraintParameters;
  27. /**
  28. * Constructs a new constraint for the physics constraint.
  29. * @param type The type of constraint to create.
  30. * @param options The options for the constraint.
  31. * @param scene The scene the constraint belongs to.
  32. *
  33. * This code is useful for creating a new constraint for the physics engine. It checks if the scene has a physics engine, and if the plugin version is correct.
  34. * If all checks pass, it initializes the constraint with the given type and options.
  35. */
  36. constructor(type: PhysicsConstraintType, options: PhysicsConstraintParameters, scene: Scene);
  37. /**
  38. * Gets the type of the constraint.
  39. *
  40. * @returns The type of the constraint.
  41. *
  42. */
  43. get type(): PhysicsConstraintType;
  44. /**
  45. * Retrieves the options of the physics constraint.
  46. *
  47. * @returns The physics constraint parameters.
  48. *
  49. */
  50. get options(): PhysicsConstraintParameters;
  51. /**
  52. * Enable/disable the constraint
  53. * @param isEnabled value for the constraint
  54. */
  55. set isEnabled(isEnabled: boolean);
  56. /**
  57. *
  58. * @returns true if constraint is enabled
  59. */
  60. get isEnabled(): boolean;
  61. /**
  62. * Enables or disables collisions for the physics engine.
  63. *
  64. * @param isEnabled - A boolean value indicating whether collisions should be enabled or disabled.
  65. *
  66. */
  67. set isCollisionsEnabled(isEnabled: boolean);
  68. /**
  69. * Gets whether collisions are enabled for this physics object.
  70. *
  71. * @returns `true` if collisions are enabled, `false` otherwise.
  72. *
  73. */
  74. get isCollisionsEnabled(): boolean;
  75. /**
  76. * Gets all bodies that are using this constraint
  77. * @returns
  78. */
  79. getBodiesUsingConstraint(): ConstrainedBodyPair[];
  80. /**
  81. * Disposes the constraint from the physics engine.
  82. *
  83. * This method is useful for cleaning up the physics engine when a body is no longer needed. Disposing the body will free up resources and prevent memory leaks.
  84. */
  85. dispose(): void;
  86. }
  87. /**
  88. * This describes a single limit used by Physics6DoFConstraint
  89. */
  90. export declare class Physics6DoFLimit {
  91. /**
  92. * The axis ID to limit
  93. */
  94. axis: PhysicsConstraintAxis;
  95. /**
  96. * An optional minimum limit for the axis.
  97. * Corresponds to a distance in meters for linear axes, an angle in radians for angular axes.
  98. */
  99. minLimit?: number;
  100. /**
  101. * An optional maximum limit for the axis.
  102. * Corresponds to a distance in meters for linear axes, an angle in radians for angular axes.
  103. */
  104. maxLimit?: number;
  105. /**
  106. * The stiffness of the constraint.
  107. */
  108. stiffness?: number;
  109. /**
  110. * A constraint parameter that specifies damping.
  111. */
  112. damping?: number;
  113. }
  114. /**
  115. * A generic constraint, which can be used to build more complex constraints than those specified
  116. * in PhysicsConstraintType. The axis and pivot options in PhysicsConstraintParameters define the space
  117. * the constraint operates in. This constraint contains a set of limits, which restrict the
  118. * relative movement of the bodies in that coordinate system
  119. */
  120. export declare class Physics6DoFConstraint extends PhysicsConstraint {
  121. /**
  122. * The collection of limits which this constraint will apply
  123. */
  124. limits: Physics6DoFLimit[];
  125. constructor(constraintParams: PhysicsConstraintParameters, limits: Physics6DoFLimit[], scene: Scene);
  126. /**
  127. * Sets the friction of the given axis of the physics engine.
  128. * @param axis - The axis of the physics engine to set the friction for.
  129. * @param friction - The friction to set for the given axis.
  130. *
  131. */
  132. setAxisFriction(axis: PhysicsConstraintAxis, friction: number): void;
  133. /**
  134. * Gets the friction of the given axis of the physics engine.
  135. * @param axis - The axis of the physics engine.
  136. * @returns The friction of the given axis, or null if the constraint hasn't been initialized yet.
  137. *
  138. */
  139. getAxisFriction(axis: PhysicsConstraintAxis): Nullable<number>;
  140. /**
  141. * Sets the limit mode for the given axis of the constraint.
  142. * @param axis The axis to set the limit mode for.
  143. * @param limitMode The limit mode to set.
  144. *
  145. * This method is useful for setting the limit mode for a given axis of the constraint. This is important for
  146. * controlling the behavior of the physics engine when the constraint is reached. By setting the limit mode,
  147. * the engine can be configured to either stop the motion of the objects, or to allow them to continue
  148. * moving beyond the constraint.
  149. */
  150. setAxisMode(axis: PhysicsConstraintAxis, limitMode: PhysicsConstraintAxisLimitMode): void;
  151. /**
  152. * Gets the limit mode of the given axis of the constraint.
  153. *
  154. * @param axis - The axis of the constraint.
  155. * @returns The limit mode of the given axis, or null if the constraint hasn't been initialized yet.
  156. *
  157. */
  158. getAxisMode(axis: PhysicsConstraintAxis): Nullable<PhysicsConstraintAxisLimitMode>;
  159. /**
  160. * Sets the minimum limit of a given axis of a constraint.
  161. * @param axis - The axis of the constraint.
  162. * @param minLimit - The minimum limit of the axis.
  163. *
  164. */
  165. setAxisMinLimit(axis: PhysicsConstraintAxis, minLimit: number): void;
  166. /**
  167. * Gets the minimum limit of the given axis of the physics engine.
  168. * @param axis - The axis of the physics engine.
  169. * @returns The minimum limit of the given axis, or null if the constraint hasn't been initialized yet.
  170. *
  171. */
  172. getAxisMinLimit(axis: PhysicsConstraintAxis): Nullable<number>;
  173. /**
  174. * Sets the maximum limit of the given axis for the physics engine.
  175. * @param axis - The axis to set the limit for.
  176. * @param limit - The maximum limit of the axis.
  177. *
  178. * This method is useful for setting the maximum limit of the given axis for the physics engine,
  179. * which can be used to control the movement of the physics object. This helps to ensure that the
  180. * physics object does not move beyond the given limit.
  181. */
  182. setAxisMaxLimit(axis: PhysicsConstraintAxis, limit: number): void;
  183. /**
  184. * Gets the maximum limit of the given axis of the physics engine.
  185. * @param axis - The axis of the physics engine.
  186. * @returns The maximum limit of the given axis, or null if the constraint hasn't been initialized yet.
  187. *
  188. */
  189. getAxisMaxLimit(axis: PhysicsConstraintAxis): Nullable<number>;
  190. /**
  191. * Sets the motor type of the given axis of the constraint.
  192. * @param axis - The axis of the constraint.
  193. * @param motorType - The type of motor to use.
  194. */
  195. setAxisMotorType(axis: PhysicsConstraintAxis, motorType: PhysicsConstraintMotorType): void;
  196. /**
  197. * Gets the motor type of the specified axis of the constraint.
  198. *
  199. * @param axis - The axis of the constraint.
  200. * @returns The motor type of the specified axis, or null if the constraint hasn't been initialized yet.
  201. *
  202. */
  203. getAxisMotorType(axis: PhysicsConstraintAxis): Nullable<PhysicsConstraintMotorType>;
  204. /**
  205. * Sets the target velocity of the motor associated with the given axis of the constraint.
  206. * @param axis - The axis of the constraint.
  207. * @param target - The target velocity of the motor.
  208. *
  209. * This method is useful for setting the target velocity of the motor associated with the given axis of the constraint.
  210. */
  211. setAxisMotorTarget(axis: PhysicsConstraintAxis, target: number): void;
  212. /**
  213. * Gets the target velocity of the motor associated to the given constraint axis.
  214. * @param axis - The constraint axis associated to the motor.
  215. * @returns The target velocity of the motor, or null if the constraint hasn't been initialized yet.
  216. *
  217. */
  218. getAxisMotorTarget(axis: PhysicsConstraintAxis): Nullable<number>;
  219. /**
  220. * Sets the maximum force of the motor of the given axis of the constraint.
  221. * @param axis - The axis of the constraint.
  222. * @param maxForce - The maximum force of the motor.
  223. *
  224. */
  225. setAxisMotorMaxForce(axis: PhysicsConstraintAxis, maxForce: number): void;
  226. /**
  227. * Gets the maximum force of the motor of the given axis of the constraint.
  228. * @param axis - The axis of the constraint.
  229. * @returns The maximum force of the motor, or null if the constraint hasn't been initialized yet.
  230. *
  231. */
  232. getAxisMotorMaxForce(axis: PhysicsConstraintAxis): Nullable<number>;
  233. }
  234. /**
  235. * Represents a Ball and Socket Constraint, used to simulate a joint
  236. *
  237. * @param pivotA - The first pivot, defined locally in the first body frame
  238. * @param pivotB - The second pivot, defined locally in the second body frame
  239. * @param axisA - The axis of the first body
  240. * @param axisB - The axis of the second body
  241. * @param scene - The scene the constraint is applied to
  242. * @returns The Ball and Socket Constraint
  243. *
  244. * This class is useful for simulating a joint between two bodies in a physics engine.
  245. * It allows for the two bodies to move relative to each other in a way that mimics a ball and socket joint, such as a shoulder or hip joint.
  246. */
  247. export declare class BallAndSocketConstraint extends PhysicsConstraint {
  248. constructor(pivotA: Vector3, pivotB: Vector3, axisA: Vector3, axisB: Vector3, scene: Scene);
  249. }
  250. /**
  251. * Creates a distance constraint.
  252. * @param maxDistance distance between bodies
  253. * @param scene The scene the constraint belongs to
  254. * @returns DistanceConstraint
  255. *
  256. * This code is useful for creating a distance constraint in a physics engine.
  257. * A distance constraint is a type of constraint that keeps two objects at a certain distance from each other.
  258. * The scene is used to add the constraint to the physics engine.
  259. */
  260. export declare class DistanceConstraint extends PhysicsConstraint {
  261. constructor(maxDistance: number, scene: Scene);
  262. }
  263. /**
  264. * Creates a HingeConstraint, which is a type of PhysicsConstraint.
  265. *
  266. * @param pivotA - The first pivot point, in world space.
  267. * @param pivotB - The second pivot point, in world space.
  268. * @param scene - The scene the constraint is used in.
  269. * @returns The new HingeConstraint.
  270. *
  271. * This code is useful for creating a HingeConstraint, which is a type of PhysicsConstraint.
  272. * This constraint is used to simulate a hinge joint between two rigid bodies, allowing them to rotate around a single axis.
  273. */
  274. export declare class HingeConstraint extends PhysicsConstraint {
  275. constructor(pivotA: Vector3, pivotB: Vector3, axisA: Vector3, axisB: Vector3, scene: Scene);
  276. }
  277. /**
  278. * Creates a SliderConstraint, which is a type of PhysicsConstraint.
  279. *
  280. * @param pivotA - The first pivot of the constraint, in world space.
  281. * @param pivotB - The second pivot of the constraint, in world space.
  282. * @param axisA - The first axis of the constraint, in world space.
  283. * @param axisB - The second axis of the constraint, in world space.
  284. * @param scene - The scene the constraint belongs to.
  285. * @returns The created SliderConstraint.
  286. *
  287. * This code is useful for creating a SliderConstraint, which is a type of PhysicsConstraint.
  288. * It allows the user to specify the two pivots and two axes of the constraint in world space, as well as the scene the constraint belongs to.
  289. * This is useful for creating a constraint between two rigid bodies that allows them to move along a certain axis.
  290. */
  291. export declare class SliderConstraint extends PhysicsConstraint {
  292. constructor(pivotA: Vector3, pivotB: Vector3, axisA: Vector3, axisB: Vector3, scene: Scene);
  293. }
  294. /**
  295. * Creates a LockConstraint, which is a type of PhysicsConstraint.
  296. *
  297. * @param pivotA - The first pivot of the constraint in local space.
  298. * @param pivotB - The second pivot of the constraint in local space.
  299. * @param axisA - The first axis of the constraint in local space.
  300. * @param axisB - The second axis of the constraint in local space.
  301. * @param scene - The scene the constraint belongs to.
  302. * @returns The created LockConstraint.
  303. *
  304. * This code is useful for creating a LockConstraint, which is a type of PhysicsConstraint.
  305. * It takes in two pivots and two axes in local space, as well as the scene the constraint belongs to, and creates a LockConstraint.
  306. */
  307. export declare class LockConstraint extends PhysicsConstraint {
  308. constructor(pivotA: Vector3, pivotB: Vector3, axisA: Vector3, axisB: Vector3, scene: Scene);
  309. }
  310. /**
  311. * Creates a PrismaticConstraint, which is a type of PhysicsConstraint.
  312. *
  313. * @param pivotA - The first pivot of the constraint in local space.
  314. * @param pivotB - The second pivot of the constraint in local space.
  315. * @param axisA - The first axis of the constraint in local space.
  316. * @param axisB - The second axis of the constraint in local space.
  317. * @param scene - The scene the constraint belongs to.
  318. * @returns The created LockConstraint.
  319. *
  320. * This code is useful for creating a PrismaticConstraint, which is a type of PhysicsConstraint.
  321. * It takes in two pivots and two axes in local space, as well as the scene the constraint belongs to, and creates a PrismaticConstraint.
  322. */
  323. export declare class PrismaticConstraint extends PhysicsConstraint {
  324. constructor(pivotA: Vector3, pivotB: Vector3, axisA: Vector3, axisB: Vector3, scene: Scene);
  325. }
  326. /**
  327. * Creates a SpringConstraint, which is a type of Physics6DoFConstraint. This constraint applies a force at the ends which is proportional
  328. * to the distance between ends, and a stiffness and damping factor. The force is calculated as (stiffness * positionError) - (damping * velocity)
  329. *
  330. * @param pivotA - The first pivot of the constraint in local space.
  331. * @param pivotB - The second pivot of the constraint in local space.
  332. * @param axisA - The first axis of the constraint in local space.
  333. * @param axisB - The second axis of the constraint in local space.
  334. * @param minDistance - The minimum distance between the two pivots.
  335. * @param maxDistance - The maximum distance between the two pivots.
  336. * @param stiffness - The stiffness of the spring.
  337. * @param damping - The damping of the spring.
  338. * @param scene - The scene the constraint belongs to.
  339. * @returns The created SpringConstraint.
  340. */
  341. export declare class SpringConstraint extends Physics6DoFConstraint {
  342. constructor(pivotA: Vector3, pivotB: Vector3, axisA: Vector3, axisB: Vector3, minDistance: number, maxDistance: number, stiffness: number, damping: number, scene: Scene);
  343. }