babylon.physicsJoint.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. module BABYLON {
  2. export interface PhysicsJointData {
  3. //Important for some engines, optional!
  4. mainPivot?: Vector3;
  5. connectedPivot?: Vector3;
  6. mainAxis?: Vector3,
  7. connectedAxis?: Vector3,
  8. collision?: boolean
  9. //Native Oimo/Cannon/Energy data
  10. nativeParams?: any;
  11. }
  12. /**
  13. * This is a holder class for the physics joint created by the physics plugin.
  14. * It holds a set of functions to control the underlying joint.
  15. */
  16. export class PhysicsJoint {
  17. private _physicsJoint;
  18. protected _physicsPlugin: IPhysicsEnginePlugin;
  19. constructor(public type: number, public jointData: PhysicsJointData) {
  20. jointData.nativeParams = jointData.nativeParams || {};
  21. }
  22. public get physicsJoint() {
  23. return this._physicsJoint;
  24. }
  25. public set physicsJoint(newJoint: any) {
  26. if (this._physicsJoint) {
  27. //remove from the wolrd
  28. }
  29. this._physicsJoint = newJoint;
  30. }
  31. public set physicsPlugin(physicsPlugin: IPhysicsEnginePlugin) {
  32. this._physicsPlugin = physicsPlugin;
  33. }
  34. /**
  35. * Execute a function that is physics-plugin specific.
  36. * @param {Function} func the function that will be executed.
  37. * It accepts two parameters: the physics world and the physics joint.
  38. */
  39. public executeNativeFunction(func : (world: any, physicsJoint:any) => void) {
  40. func(this._physicsPlugin.world, this._physicsJoint)
  41. }
  42. //TODO check if the native joints are the same
  43. //Joint Types
  44. public static DistanceJoint = 0;
  45. public static HingeJoint = 1;
  46. public static BallAndSocketJoint = 2;
  47. public static WheelJoint = 3;
  48. public static SliderJoint = 4;
  49. //OIMO
  50. public static PrismaticJoint = 5;
  51. //ENERGY FTW! (compare with this - http://ode-wiki.org/wiki/index.php?title=Manual:_Joint_Types_and_Functions)
  52. public static UniversalJoint = 6;
  53. public static Hinge2Joint = PhysicsJoint.WheelJoint;
  54. //Cannon
  55. //Similar to a Ball-Joint. Different in params
  56. public static PointToPointJoint = 8;
  57. //Cannon only at the moment
  58. public static SpringJoint = 9;
  59. public static LockJoint = 10;
  60. }
  61. /**
  62. * A class representing a physics distance joint.
  63. */
  64. export class DistanceJoint extends PhysicsJoint {
  65. constructor(jointData: DistanceJointData) {
  66. super(PhysicsJoint.DistanceJoint, jointData);
  67. }
  68. /**
  69. * Update the predefined distance.
  70. */
  71. public updateDistance(maxDistance: number, minDistance?: number) {
  72. this._physicsPlugin.updateDistanceJoint(this, maxDistance, minDistance);
  73. }
  74. }
  75. export class MotorEnabledJoint extends PhysicsJoint implements IMotorEnabledJoint {
  76. constructor(type: number, jointData:PhysicsJointData) {
  77. super(type, jointData);
  78. }
  79. /**
  80. * Set the motor values.
  81. * Attention, this function is plugin specific. Engines won't react 100% the same.
  82. * @param {number} force the force to apply
  83. * @param {number} maxForce max force for this motor.
  84. */
  85. public setMotor(force?: number, maxForce?: number) {
  86. this._physicsPlugin.setMotor(this, force, maxForce);
  87. }
  88. /**
  89. * Set the motor's limits.
  90. * Attention, this function is plugin specific. Engines won't react 100% the same.
  91. */
  92. public setLimit(upperLimit: number, lowerLimit?: number) {
  93. this._physicsPlugin.setLimit(this, upperLimit, lowerLimit);
  94. }
  95. }
  96. /**
  97. * This class represents a single hinge physics joint
  98. */
  99. export class HingeJoint extends MotorEnabledJoint {
  100. constructor(jointData:PhysicsJointData) {
  101. super(PhysicsJoint.HingeJoint, jointData);
  102. }
  103. /**
  104. * Set the motor values.
  105. * Attention, this function is plugin specific. Engines won't react 100% the same.
  106. * @param {number} force the force to apply
  107. * @param {number} maxForce max force for this motor.
  108. */
  109. public setMotor(force?: number, maxForce?: number) {
  110. this._physicsPlugin.setMotor(this, force, maxForce);
  111. }
  112. /**
  113. * Set the motor's limits.
  114. * Attention, this function is plugin specific. Engines won't react 100% the same.
  115. */
  116. public setLimit(upperLimit: number, lowerLimit?: number) {
  117. this._physicsPlugin.setLimit(this, upperLimit, lowerLimit);
  118. }
  119. }
  120. /**
  121. * This class represents a dual hinge physics joint (same as wheel joint)
  122. */
  123. export class Hinge2Joint extends MotorEnabledJoint {
  124. constructor(jointData:PhysicsJointData) {
  125. super(PhysicsJoint.Hinge2Joint, jointData);
  126. }
  127. /**
  128. * Set the motor values.
  129. * Attention, this function is plugin specific. Engines won't react 100% the same.
  130. * @param {number} force the force to apply
  131. * @param {number} maxForce max force for this motor.
  132. * @param {motorIndex} the motor's index, 0 or 1.
  133. */
  134. public setMotor(force?: number, maxForce?: number, motorIndex: number = 0) {
  135. this._physicsPlugin.setMotor(this, force, maxForce, motorIndex);
  136. }
  137. /**
  138. * Set the motor limits.
  139. * Attention, this function is plugin specific. Engines won't react 100% the same.
  140. * @param {number} upperLimit the upper limit
  141. * @param {number} lowerLimit lower limit
  142. * @param {motorIndex} the motor's index, 0 or 1.
  143. */
  144. public setLimit(upperLimit: number, lowerLimit?: number, motorIndex: number = 0) {
  145. this._physicsPlugin.setLimit(this, upperLimit, lowerLimit, motorIndex);
  146. }
  147. }
  148. export interface IMotorEnabledJoint {
  149. physicsJoint: any;
  150. setMotor(force?: number, maxForce?: number, motorIndex?: number);
  151. setLimit(upperLimit: number, lowerLimit?: number, motorIndex?: number);
  152. }
  153. export interface DistanceJointData extends PhysicsJointData {
  154. maxDistance: number;
  155. //Oimo - minDistance
  156. //Cannon - maxForce
  157. }
  158. export interface SpringJointData extends PhysicsJointData {
  159. length: number;
  160. stiffness: number;
  161. damping: number;
  162. }
  163. }