physicsJoint.ts 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. import { Vector3 } from "../Maths/math.vector";
  2. import { IPhysicsEnginePlugin } from "./IPhysicsEngine";
  3. /**
  4. * Interface for Physics-Joint data
  5. * @see https://doc.babylonjs.com/how_to/using_the_physics_engine
  6. */
  7. export interface PhysicsJointData {
  8. //Important for some engines, optional!
  9. /**
  10. * The main pivot of the joint
  11. */
  12. mainPivot?: Vector3;
  13. /**
  14. * The connected pivot of the joint
  15. */
  16. connectedPivot?: Vector3;
  17. /**
  18. * The main axis of the joint
  19. */
  20. mainAxis?: Vector3;
  21. /**
  22. * The connected axis of the joint
  23. */
  24. connectedAxis?: Vector3;
  25. /**
  26. * The collision of the joint
  27. */
  28. collision?: boolean;
  29. /**
  30. * Native Oimo/Cannon/Energy data
  31. */
  32. nativeParams?: any;
  33. }
  34. /**
  35. * This is a holder class for the physics joint created by the physics plugin
  36. * It holds a set of functions to control the underlying joint
  37. * @see https://doc.babylonjs.com/how_to/using_the_physics_engine
  38. */
  39. export class PhysicsJoint {
  40. private _physicsJoint: any;
  41. protected _physicsPlugin: IPhysicsEnginePlugin;
  42. /**
  43. * Initializes the physics joint
  44. * @param type The type of the physics joint
  45. * @param jointData The data for the physics joint
  46. */
  47. constructor(
  48. /**
  49. * The type of the physics joint
  50. */
  51. public type: number,
  52. /**
  53. * The data for the physics joint
  54. */
  55. public jointData: PhysicsJointData) {
  56. jointData.nativeParams = jointData.nativeParams || {};
  57. }
  58. /**
  59. * Gets the physics joint
  60. */
  61. public get physicsJoint(): any {
  62. return this._physicsJoint;
  63. }
  64. /**
  65. * Sets the physics joint
  66. */
  67. public set physicsJoint(newJoint: any) {
  68. if (this._physicsJoint) {
  69. //remove from the world
  70. }
  71. this._physicsJoint = newJoint;
  72. }
  73. /**
  74. * Sets the physics plugin
  75. */
  76. public set physicsPlugin(physicsPlugin: IPhysicsEnginePlugin) {
  77. this._physicsPlugin = physicsPlugin;
  78. }
  79. /**
  80. * Execute a function that is physics-plugin specific.
  81. * @param {Function} func the function that will be executed.
  82. * It accepts two parameters: the physics world and the physics joint
  83. */
  84. public executeNativeFunction(func: (world: any, physicsJoint: any) => void) {
  85. func(this._physicsPlugin.world, this._physicsJoint);
  86. }
  87. //TODO check if the native joints are the same
  88. //Joint Types
  89. /**
  90. * Distance-Joint type
  91. */
  92. public static DistanceJoint = 0;
  93. /**
  94. * Hinge-Joint type
  95. */
  96. public static HingeJoint = 1;
  97. /**
  98. * Ball-and-Socket joint type
  99. */
  100. public static BallAndSocketJoint = 2;
  101. /**
  102. * Wheel-Joint type
  103. */
  104. public static WheelJoint = 3;
  105. /**
  106. * Slider-Joint type
  107. */
  108. public static SliderJoint = 4;
  109. //OIMO
  110. /**
  111. * Prismatic-Joint type
  112. */
  113. public static PrismaticJoint = 5;
  114. //
  115. /**
  116. * Universal-Joint type
  117. * ENERGY FTW! (compare with this - @see http://ode-wiki.org/wiki/index.php?title=Manual:_Joint_Types_and_Functions)
  118. */
  119. public static UniversalJoint = 6;
  120. /**
  121. * Hinge-Joint 2 type
  122. */
  123. public static Hinge2Joint = PhysicsJoint.WheelJoint;
  124. //Cannon
  125. /**
  126. * Point to Point Joint type. Similar to a Ball-Joint. Different in parameters
  127. */
  128. public static PointToPointJoint = 8;
  129. //Cannon only at the moment
  130. /**
  131. * Spring-Joint type
  132. */
  133. public static SpringJoint = 9;
  134. /**
  135. * Lock-Joint type
  136. */
  137. public static LockJoint = 10;
  138. }
  139. /**
  140. * A class representing a physics distance joint
  141. * @see https://doc.babylonjs.com/how_to/using_the_physics_engine
  142. */
  143. export class DistanceJoint extends PhysicsJoint {
  144. /**
  145. *
  146. * @param jointData The data for the Distance-Joint
  147. */
  148. constructor(jointData: DistanceJointData) {
  149. super(PhysicsJoint.DistanceJoint, jointData);
  150. }
  151. /**
  152. * Update the predefined distance.
  153. * @param maxDistance The maximum preferred distance
  154. * @param minDistance The minimum preferred distance
  155. */
  156. public updateDistance(maxDistance: number, minDistance?: number) {
  157. this._physicsPlugin.updateDistanceJoint(this, maxDistance, minDistance);
  158. }
  159. }
  160. /**
  161. * Represents a Motor-Enabled Joint
  162. * @see https://doc.babylonjs.com/how_to/using_the_physics_engine
  163. */
  164. export class MotorEnabledJoint extends PhysicsJoint implements IMotorEnabledJoint {
  165. /**
  166. * Initializes the Motor-Enabled Joint
  167. * @param type The type of the joint
  168. * @param jointData The physical joint data for the joint
  169. */
  170. constructor(type: number, jointData: PhysicsJointData) {
  171. super(type, jointData);
  172. }
  173. /**
  174. * Set the motor values.
  175. * Attention, this function is plugin specific. Engines won't react 100% the same.
  176. * @param force the force to apply
  177. * @param maxForce max force for this motor.
  178. */
  179. public setMotor(force?: number, maxForce?: number) {
  180. this._physicsPlugin.setMotor(this, force || 0, maxForce);
  181. }
  182. /**
  183. * Set the motor's limits.
  184. * Attention, this function is plugin specific. Engines won't react 100% the same.
  185. * @param upperLimit The upper limit of the motor
  186. * @param lowerLimit The lower limit of the motor
  187. */
  188. public setLimit(upperLimit: number, lowerLimit?: number) {
  189. this._physicsPlugin.setLimit(this, upperLimit, lowerLimit);
  190. }
  191. }
  192. /**
  193. * This class represents a single physics Hinge-Joint
  194. * @see https://doc.babylonjs.com/how_to/using_the_physics_engine
  195. */
  196. export class HingeJoint extends MotorEnabledJoint {
  197. /**
  198. * Initializes the Hinge-Joint
  199. * @param jointData The joint data for the Hinge-Joint
  200. */
  201. constructor(jointData: PhysicsJointData) {
  202. super(PhysicsJoint.HingeJoint, jointData);
  203. }
  204. /**
  205. * Set the motor values.
  206. * Attention, this function is plugin specific. Engines won't react 100% the same.
  207. * @param {number} force the force to apply
  208. * @param {number} maxForce max force for this motor.
  209. */
  210. public setMotor(force?: number, maxForce?: number) {
  211. this._physicsPlugin.setMotor(this, force || 0, maxForce);
  212. }
  213. /**
  214. * Set the motor's limits.
  215. * Attention, this function is plugin specific. Engines won't react 100% the same.
  216. * @param upperLimit The upper limit of the motor
  217. * @param lowerLimit The lower limit of the motor
  218. */
  219. public setLimit(upperLimit: number, lowerLimit?: number) {
  220. this._physicsPlugin.setLimit(this, upperLimit, lowerLimit);
  221. }
  222. }
  223. /**
  224. * This class represents a dual hinge physics joint (same as wheel joint)
  225. * @see https://doc.babylonjs.com/how_to/using_the_physics_engine
  226. */
  227. export class Hinge2Joint extends MotorEnabledJoint {
  228. /**
  229. * Initializes the Hinge2-Joint
  230. * @param jointData The joint data for the Hinge2-Joint
  231. */
  232. constructor(jointData: PhysicsJointData) {
  233. super(PhysicsJoint.Hinge2Joint, jointData);
  234. }
  235. /**
  236. * Set the motor values.
  237. * Attention, this function is plugin specific. Engines won't react 100% the same.
  238. * @param {number} targetSpeed the speed the motor is to reach
  239. * @param {number} maxForce max force for this motor.
  240. * @param {motorIndex} the motor's index, 0 or 1.
  241. */
  242. public setMotor(targetSpeed?: number, maxForce?: number, motorIndex: number = 0) {
  243. this._physicsPlugin.setMotor(this, targetSpeed || 0, maxForce, motorIndex);
  244. }
  245. /**
  246. * Set the motor limits.
  247. * Attention, this function is plugin specific. Engines won't react 100% the same.
  248. * @param {number} upperLimit the upper limit
  249. * @param {number} lowerLimit lower limit
  250. * @param {motorIndex} the motor's index, 0 or 1.
  251. */
  252. public setLimit(upperLimit: number, lowerLimit?: number, motorIndex: number = 0) {
  253. this._physicsPlugin.setLimit(this, upperLimit, lowerLimit, motorIndex);
  254. }
  255. }
  256. /**
  257. * Interface for a motor enabled joint
  258. * @see https://doc.babylonjs.com/how_to/using_the_physics_engine
  259. */
  260. export interface IMotorEnabledJoint {
  261. /**
  262. * Physics joint
  263. */
  264. physicsJoint: any;
  265. /**
  266. * Sets the motor of the motor-enabled joint
  267. * @param force The force of the motor
  268. * @param maxForce The maximum force of the motor
  269. * @param motorIndex The index of the motor
  270. */
  271. setMotor(force?: number, maxForce?: number, motorIndex?: number): void;
  272. /**
  273. * Sets the limit of the motor
  274. * @param upperLimit The upper limit of the motor
  275. * @param lowerLimit The lower limit of the motor
  276. * @param motorIndex The index of the motor
  277. */
  278. setLimit(upperLimit: number, lowerLimit?: number, motorIndex?: number): void;
  279. }
  280. /**
  281. * Joint data for a Distance-Joint
  282. * @see https://doc.babylonjs.com/how_to/using_the_physics_engine
  283. */
  284. export interface DistanceJointData extends PhysicsJointData {
  285. /**
  286. * Max distance the 2 joint objects can be apart
  287. */
  288. maxDistance: number;
  289. //Oimo - minDistance
  290. //Cannon - maxForce
  291. }
  292. /**
  293. * Joint data from a spring joint
  294. * @see https://doc.babylonjs.com/how_to/using_the_physics_engine
  295. */
  296. export interface SpringJointData extends PhysicsJointData {
  297. /**
  298. * Length of the spring
  299. */
  300. length: number;
  301. /**
  302. * Stiffness of the spring
  303. */
  304. stiffness: number;
  305. /**
  306. * Damping of the spring
  307. */
  308. damping: number;
  309. /** this callback will be called when applying the force to the impostors. */
  310. forceApplicationCallback: () => void;
  311. }