physicsJoint.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /**
  2. * This is a holder class for the physics joint created by the physics plugin
  3. * It holds a set of functions to control the underlying joint
  4. * @see https://doc.babylonjs.com/features/featuresDeepDive/physics/usingPhysicsEngine
  5. */
  6. export class PhysicsJoint {
  7. /**
  8. * Initializes the physics joint
  9. * @param type The type of the physics joint
  10. * @param jointData The data for the physics joint
  11. */
  12. constructor(
  13. /**
  14. * The type of the physics joint
  15. */
  16. type,
  17. /**
  18. * The data for the physics joint
  19. */
  20. jointData) {
  21. this.type = type;
  22. this.jointData = jointData;
  23. jointData.nativeParams = jointData.nativeParams || {};
  24. }
  25. /**
  26. * Gets the physics joint
  27. */
  28. get physicsJoint() {
  29. return this._physicsJoint;
  30. }
  31. /**
  32. * Sets the physics joint
  33. */
  34. set physicsJoint(newJoint) {
  35. if (this._physicsJoint) {
  36. //remove from the world
  37. }
  38. this._physicsJoint = newJoint;
  39. }
  40. /**
  41. * Sets the physics plugin
  42. */
  43. set physicsPlugin(physicsPlugin) {
  44. this._physicsPlugin = physicsPlugin;
  45. }
  46. /**
  47. * Execute a function that is physics-plugin specific.
  48. * @param {Function} func the function that will be executed.
  49. * It accepts two parameters: the physics world and the physics joint
  50. */
  51. executeNativeFunction(func) {
  52. func(this._physicsPlugin.world, this._physicsJoint);
  53. }
  54. }
  55. //TODO check if the native joints are the same
  56. //Joint Types
  57. /**
  58. * Distance-Joint type
  59. */
  60. PhysicsJoint.DistanceJoint = 0;
  61. /**
  62. * Hinge-Joint type
  63. */
  64. PhysicsJoint.HingeJoint = 1;
  65. /**
  66. * Ball-and-Socket joint type
  67. */
  68. PhysicsJoint.BallAndSocketJoint = 2;
  69. /**
  70. * Wheel-Joint type
  71. */
  72. PhysicsJoint.WheelJoint = 3;
  73. /**
  74. * Slider-Joint type
  75. */
  76. PhysicsJoint.SliderJoint = 4;
  77. //OIMO
  78. /**
  79. * Prismatic-Joint type
  80. */
  81. PhysicsJoint.PrismaticJoint = 5;
  82. //
  83. /**
  84. * Universal-Joint type
  85. * ENERGY FTW! (compare with this - @see http://ode-wiki.org/wiki/index.php?title=Manual:_Joint_Types_and_Functions)
  86. */
  87. PhysicsJoint.UniversalJoint = 6;
  88. /**
  89. * Hinge-Joint 2 type
  90. */
  91. PhysicsJoint.Hinge2Joint = PhysicsJoint.WheelJoint;
  92. //Cannon
  93. /**
  94. * Point to Point Joint type. Similar to a Ball-Joint. Different in parameters
  95. */
  96. PhysicsJoint.PointToPointJoint = 8;
  97. //Cannon only at the moment
  98. /**
  99. * Spring-Joint type
  100. */
  101. PhysicsJoint.SpringJoint = 9;
  102. /**
  103. * Lock-Joint type
  104. */
  105. PhysicsJoint.LockJoint = 10;
  106. /**
  107. * A class representing a physics distance joint
  108. * @see https://doc.babylonjs.com/features/featuresDeepDive/physics/usingPhysicsEngine
  109. */
  110. export class DistanceJoint extends PhysicsJoint {
  111. /**
  112. *
  113. * @param jointData The data for the Distance-Joint
  114. */
  115. constructor(jointData) {
  116. super(PhysicsJoint.DistanceJoint, jointData);
  117. }
  118. /**
  119. * Update the predefined distance.
  120. * @param maxDistance The maximum preferred distance
  121. * @param minDistance The minimum preferred distance
  122. */
  123. updateDistance(maxDistance, minDistance) {
  124. this._physicsPlugin.updateDistanceJoint(this, maxDistance, minDistance);
  125. }
  126. }
  127. /**
  128. * Represents a Motor-Enabled Joint
  129. * @see https://doc.babylonjs.com/features/featuresDeepDive/physics/usingPhysicsEngine
  130. */
  131. export class MotorEnabledJoint extends PhysicsJoint {
  132. /**
  133. * Initializes the Motor-Enabled Joint
  134. * @param type The type of the joint
  135. * @param jointData The physical joint data for the joint
  136. */
  137. constructor(type, jointData) {
  138. super(type, jointData);
  139. }
  140. /**
  141. * Set the motor values.
  142. * Attention, this function is plugin specific. Engines won't react 100% the same.
  143. * @param force the force to apply
  144. * @param maxForce max force for this motor.
  145. */
  146. setMotor(force, maxForce) {
  147. this._physicsPlugin.setMotor(this, force || 0, maxForce);
  148. }
  149. /**
  150. * Set the motor's limits.
  151. * Attention, this function is plugin specific. Engines won't react 100% the same.
  152. * @param upperLimit The upper limit of the motor
  153. * @param lowerLimit The lower limit of the motor
  154. */
  155. setLimit(upperLimit, lowerLimit) {
  156. this._physicsPlugin.setLimit(this, upperLimit, lowerLimit);
  157. }
  158. }
  159. /**
  160. * This class represents a single physics Hinge-Joint
  161. * @see https://doc.babylonjs.com/features/featuresDeepDive/physics/usingPhysicsEngine
  162. */
  163. export class HingeJoint extends MotorEnabledJoint {
  164. /**
  165. * Initializes the Hinge-Joint
  166. * @param jointData The joint data for the Hinge-Joint
  167. */
  168. constructor(jointData) {
  169. super(PhysicsJoint.HingeJoint, jointData);
  170. }
  171. /**
  172. * Set the motor values.
  173. * Attention, this function is plugin specific. Engines won't react 100% the same.
  174. * @param {number} force the force to apply
  175. * @param {number} maxForce max force for this motor.
  176. */
  177. setMotor(force, maxForce) {
  178. this._physicsPlugin.setMotor(this, force || 0, maxForce);
  179. }
  180. /**
  181. * Set the motor's limits.
  182. * Attention, this function is plugin specific. Engines won't react 100% the same.
  183. * @param upperLimit The upper limit of the motor
  184. * @param lowerLimit The lower limit of the motor
  185. */
  186. setLimit(upperLimit, lowerLimit) {
  187. this._physicsPlugin.setLimit(this, upperLimit, lowerLimit);
  188. }
  189. }
  190. /**
  191. * This class represents a dual hinge physics joint (same as wheel joint)
  192. * @see https://doc.babylonjs.com/features/featuresDeepDive/physics/usingPhysicsEngine
  193. */
  194. export class Hinge2Joint extends MotorEnabledJoint {
  195. /**
  196. * Initializes the Hinge2-Joint
  197. * @param jointData The joint data for the Hinge2-Joint
  198. */
  199. constructor(jointData) {
  200. super(PhysicsJoint.Hinge2Joint, jointData);
  201. }
  202. /**
  203. * Set the motor values.
  204. * Attention, this function is plugin specific. Engines won't react 100% the same.
  205. * @param targetSpeed the speed the motor is to reach
  206. * @param maxForce max force for this motor.
  207. * @param motorIndex motor's index, 0 or 1.
  208. */
  209. setMotor(targetSpeed, maxForce, motorIndex = 0) {
  210. this._physicsPlugin.setMotor(this, targetSpeed || 0, maxForce, motorIndex);
  211. }
  212. /**
  213. * Set the motor limits.
  214. * Attention, this function is plugin specific. Engines won't react 100% the same.
  215. * @param upperLimit the upper limit
  216. * @param lowerLimit lower limit
  217. * @param motorIndex the motor's index, 0 or 1.
  218. */
  219. setLimit(upperLimit, lowerLimit, motorIndex = 0) {
  220. this._physicsPlugin.setLimit(this, upperLimit, lowerLimit, motorIndex);
  221. }
  222. }
  223. //# sourceMappingURL=physicsJoint.js.map