babylon.physicsJoint.ts 11 KB

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