ammoJSPlugin.ts 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331
  1. import { Quaternion, Vector3, Matrix } from "../../Maths/math";
  2. import { IPhysicsEnginePlugin, PhysicsImpostorJoint } from "../../Physics/IPhysicsEngine";
  3. import { Logger } from "../../Misc/logger";
  4. import { PhysicsImpostor, IPhysicsEnabledObject } from "../../Physics/physicsImpostor";
  5. import { PhysicsJoint, IMotorEnabledJoint, DistanceJointData } from "../../Physics/physicsJoint";
  6. import { VertexBuffer } from "../../Meshes/buffer";
  7. import { VertexData } from "../../Meshes/mesh.vertexData";
  8. import { Nullable } from "../../types";
  9. import { AbstractMesh } from "../../Meshes/abstractMesh";
  10. import { Mesh } from "../../Meshes/mesh";
  11. import { PhysicsRaycastResult } from "../physicsRaycastResult";
  12. declare var Ammo: any;
  13. /**
  14. * AmmoJS Physics plugin
  15. * @see https://doc.babylonjs.com/how_to/using_the_physics_engine
  16. * @see https://github.com/kripken/ammo.js/
  17. */
  18. export class AmmoJSPlugin implements IPhysicsEnginePlugin {
  19. /**
  20. * Reference to the Ammo library
  21. */
  22. public bjsAMMO: any = {};
  23. /**
  24. * Created ammoJS world which physics bodies are added to
  25. */
  26. public world: any;
  27. /**
  28. * Name of the plugin
  29. */
  30. public name: string = "AmmoJSPlugin";
  31. private _timeStep: number = 1 / 60;
  32. private _fixedTimeStep: number = 1 / 60;
  33. private _maxSteps = 5;
  34. private _tmpQuaternion = new Quaternion();
  35. private _tmpAmmoTransform: any;
  36. private _tmpAmmoQuaternion: any;
  37. private _tmpAmmoConcreteContactResultCallback: any;
  38. private _collisionConfiguration: any;
  39. private _dispatcher: any;
  40. private _overlappingPairCache: any;
  41. private _solver: any;
  42. private _softBodySolver: any;
  43. private _tmpAmmoVectorA: any;
  44. private _tmpAmmoVectorB: any;
  45. private _tmpAmmoVectorC: any;
  46. private _tmpAmmoVectorD: any;
  47. private _tmpContactCallbackResult = false;
  48. private _tmpAmmoVectorRCA: any;
  49. private _tmpAmmoVectorRCB: any;
  50. private _raycastResult: PhysicsRaycastResult;
  51. private static readonly DISABLE_COLLISION_FLAG = 4;
  52. private static readonly KINEMATIC_FLAG = 2;
  53. private static readonly DISABLE_DEACTIVATION_FLAG = 4;
  54. /**
  55. * Initializes the ammoJS plugin
  56. * @param _useDeltaForWorldStep if the time between frames should be used when calculating physics steps (Default: true)
  57. * @param ammoInjection can be used to inject your own ammo reference
  58. */
  59. public constructor(private _useDeltaForWorldStep: boolean = true, ammoInjection: any = Ammo) {
  60. if (typeof ammoInjection === "function") {
  61. ammoInjection(this.bjsAMMO);
  62. } else {
  63. this.bjsAMMO = ammoInjection;
  64. }
  65. if (!this.isSupported()) {
  66. Logger.Error("AmmoJS is not available. Please make sure you included the js file.");
  67. return;
  68. }
  69. // Initialize the physics world
  70. this._collisionConfiguration = new this.bjsAMMO.btSoftBodyRigidBodyCollisionConfiguration();
  71. this._dispatcher = new this.bjsAMMO.btCollisionDispatcher(this._collisionConfiguration);
  72. this._overlappingPairCache = new this.bjsAMMO.btDbvtBroadphase();
  73. this._solver = new this.bjsAMMO.btSequentialImpulseConstraintSolver();
  74. this._softBodySolver = new this.bjsAMMO.btDefaultSoftBodySolver();
  75. this.world = new this.bjsAMMO.btSoftRigidDynamicsWorld(this._dispatcher, this._overlappingPairCache, this._solver, this._collisionConfiguration, this._softBodySolver);
  76. this._tmpAmmoConcreteContactResultCallback = new this.bjsAMMO.ConcreteContactResultCallback();
  77. this._tmpAmmoConcreteContactResultCallback.addSingleResult = () => { this._tmpContactCallbackResult = true; };
  78. // Create temp ammo variables
  79. this._tmpAmmoTransform = new this.bjsAMMO.btTransform();
  80. this._tmpAmmoTransform.setIdentity();
  81. this._tmpAmmoQuaternion = new this.bjsAMMO.btQuaternion(0, 0, 0, 1);
  82. this._tmpAmmoVectorA = new this.bjsAMMO.btVector3(0, 0, 0);
  83. this._tmpAmmoVectorB = new this.bjsAMMO.btVector3(0, 0, 0);
  84. this._tmpAmmoVectorC = new this.bjsAMMO.btVector3(0, 0, 0);
  85. this._tmpAmmoVectorD = new this.bjsAMMO.btVector3(0, 0, 0);
  86. }
  87. /**
  88. * Sets the gravity of the physics world (m/(s^2))
  89. * @param gravity Gravity to set
  90. */
  91. public setGravity(gravity: Vector3): void {
  92. this._tmpAmmoVectorA.setValue(gravity.x, gravity.y, gravity.z);
  93. this.world.setGravity(this._tmpAmmoVectorA);
  94. this.world.getWorldInfo().set_m_gravity(this._tmpAmmoVectorA);
  95. }
  96. /**
  97. * Amount of time to step forward on each frame (only used if useDeltaForWorldStep is false in the constructor)
  98. * @param timeStep timestep to use in seconds
  99. */
  100. public setTimeStep(timeStep: number) {
  101. this._timeStep = timeStep;
  102. }
  103. /**
  104. * Increment to step forward in the physics engine (If timeStep is set to 1/60 and fixedTimeStep is set to 1/120 the physics engine should run 2 steps per frame) (Default: 1/60)
  105. * @param fixedTimeStep fixedTimeStep to use in seconds
  106. */
  107. public setFixedTimeStep(fixedTimeStep: number) {
  108. this._fixedTimeStep = fixedTimeStep;
  109. }
  110. /**
  111. * Sets the maximum number of steps by the physics engine per frame (Default: 5)
  112. * @param maxSteps the maximum number of steps by the physics engine per frame
  113. */
  114. public setMaxSteps(maxSteps: number) {
  115. this._maxSteps = maxSteps;
  116. }
  117. /**
  118. * Gets the current timestep (only used if useDeltaForWorldStep is false in the constructor)
  119. * @returns the current timestep in seconds
  120. */
  121. public getTimeStep(): number {
  122. return this._timeStep;
  123. }
  124. // Ammo's contactTest and contactPairTest take a callback that runs synchronously, wrap them so that they are easier to consume
  125. private _isImpostorInContact(impostor: PhysicsImpostor) {
  126. this._tmpContactCallbackResult = false;
  127. this.world.contactTest(impostor.physicsBody, this._tmpAmmoConcreteContactResultCallback);
  128. return this._tmpContactCallbackResult;
  129. }
  130. // Ammo's collision events have some weird quirks
  131. // contactPairTest fires too many events as it fires events even when objects are close together but contactTest does not
  132. // so only fire event if both contactTest and contactPairTest have a hit
  133. private _isImpostorPairInContact(impostorA: PhysicsImpostor, impostorB: PhysicsImpostor) {
  134. this._tmpContactCallbackResult = false;
  135. this.world.contactPairTest(impostorA.physicsBody, impostorB.physicsBody, this._tmpAmmoConcreteContactResultCallback);
  136. return this._tmpContactCallbackResult;
  137. }
  138. // Ammo's behavior when maxSteps > 0 does not behave as described in docs
  139. // @see http://www.bulletphysics.org/mediawiki-1.5.8/index.php/Stepping_The_World
  140. //
  141. // When maxSteps is 0 do the entire simulation in one step
  142. // When maxSteps is > 0, run up to maxStep times, if on the last step the (remaining step - fixedTimeStep) is < fixedTimeStep, the remainder will be used for the step. (eg. if remainder is 1.001 and fixedTimeStep is 1 the last step will be 1.001, if instead it did 2 steps (1, 0.001) issues occuered when having a tiny step in ammo)
  143. // Note: To get deterministic physics, timeStep would always need to be divisible by fixedTimeStep
  144. private _stepSimulation(timeStep: number = 1 / 60, maxSteps: number = 10, fixedTimeStep: number = 1 / 60) {
  145. if (maxSteps == 0) {
  146. this.world.stepSimulation(timeStep, 0);
  147. } else {
  148. while (maxSteps > 0 && timeStep > 0) {
  149. if (timeStep - fixedTimeStep < fixedTimeStep) {
  150. this.world.stepSimulation(timeStep, 0);
  151. timeStep = 0;
  152. } else {
  153. timeStep -= fixedTimeStep;
  154. this.world.stepSimulation(fixedTimeStep, 0);
  155. }
  156. maxSteps--;
  157. }
  158. }
  159. }
  160. /**
  161. * Moves the physics simulation forward delta seconds and updates the given physics imposters
  162. * Prior to the step the imposters physics location is set to the position of the babylon meshes
  163. * After the step the babylon meshes are set to the position of the physics imposters
  164. * @param delta amount of time to step forward
  165. * @param impostors array of imposters to update before/after the step
  166. */
  167. public executeStep(delta: number, impostors: Array<PhysicsImpostor>): void {
  168. for (var impostor of impostors) {
  169. // Update physics world objects to match babylon world
  170. if (!impostor.soft) {
  171. impostor.beforeStep();
  172. }
  173. }
  174. this._stepSimulation(this._useDeltaForWorldStep ? delta : this._timeStep, this._maxSteps, this._fixedTimeStep);
  175. for (var mainImpostor of impostors) {
  176. // After physics update make babylon world objects match physics world objects
  177. if (mainImpostor.soft) {
  178. this.afterSoftStep(mainImpostor);
  179. }
  180. else {
  181. mainImpostor.afterStep();
  182. }
  183. // Handle collision event
  184. if (mainImpostor._onPhysicsCollideCallbacks.length > 0) {
  185. if (this._isImpostorInContact(mainImpostor)) {
  186. for (var collideCallback of mainImpostor._onPhysicsCollideCallbacks) {
  187. for (var otherImpostor of collideCallback.otherImpostors) {
  188. if (mainImpostor.physicsBody.isActive() || otherImpostor.physicsBody.isActive()) {
  189. if (this._isImpostorPairInContact(mainImpostor, otherImpostor)) {
  190. mainImpostor.onCollide({ body: otherImpostor.physicsBody });
  191. otherImpostor.onCollide({ body: mainImpostor.physicsBody });
  192. }
  193. }
  194. }
  195. }
  196. }
  197. }
  198. }
  199. }
  200. /**
  201. * Update babylon mesh vertices vertices to match physics world object
  202. * @param impostor imposter to match
  203. */
  204. public afterSoftStep(impostor: PhysicsImpostor): void {
  205. var normalDirection = (impostor.type === PhysicsImpostor.ClothImpostor) ? 1 : -1;
  206. var object = impostor.object;
  207. var vertexPositions = object.getVerticesData(VertexBuffer.PositionKind);
  208. if (!vertexPositions) {
  209. vertexPositions = [];
  210. }
  211. var vertexNormals = object.getVerticesData(VertexBuffer.NormalKind);
  212. if (!vertexNormals) {
  213. vertexNormals = [];
  214. }
  215. var nbVertices = vertexPositions.length / 3;
  216. var bodyVertices = impostor.physicsBody.get_m_nodes();
  217. var node: any;
  218. var nodePositions: any;
  219. var nodeNormals: any;
  220. var x, y, z: number;
  221. var nx, ny, nz: number;
  222. for (var n = 0; n < nbVertices; n++) {
  223. node = bodyVertices.at(n);
  224. nodePositions = node.get_m_x();
  225. x = nodePositions.x();
  226. y = nodePositions.y();
  227. z = nodePositions.z() * normalDirection;
  228. var nodeNormals = node.get_m_n();
  229. nx = nodeNormals.x();
  230. ny = nodeNormals.y();
  231. nz = nodeNormals.z() * normalDirection;
  232. vertexPositions[3 * n] = x;
  233. vertexPositions[3 * n + 1] = y;
  234. vertexPositions[3 * n + 2] = z;
  235. vertexNormals[3 * n] = nx;
  236. vertexNormals[3 * n + 1] = ny;
  237. vertexNormals[3 * n + 2] = nz;
  238. }
  239. var vertex_data = new VertexData();
  240. vertex_data.positions = vertexPositions;
  241. vertex_data.normals = vertexNormals;
  242. vertex_data.uvs = object.getVerticesData(VertexBuffer.UVKind);
  243. vertex_data.colors = object.getVerticesData(VertexBuffer.ColorKind);
  244. if (object && object.getIndices) {
  245. vertex_data.indices = object.getIndices();
  246. }
  247. vertex_data.applyToMesh(<Mesh>object);
  248. }
  249. private _tmpVector = new Vector3();
  250. private _tmpMatrix = new Matrix();
  251. /**
  252. * Applies an impulse on the imposter
  253. * @param impostor imposter to apply impulse to
  254. * @param force amount of force to be applied to the imposter
  255. * @param contactPoint the location to apply the impulse on the imposter
  256. */
  257. public applyImpulse(impostor: PhysicsImpostor, force: Vector3, contactPoint: Vector3) {
  258. if (!impostor.soft) {
  259. impostor.physicsBody.activate();
  260. var worldPoint = this._tmpAmmoVectorA;
  261. var impulse = this._tmpAmmoVectorB;
  262. // Convert contactPoint into world space
  263. if (impostor.object && impostor.object.getWorldMatrix) {
  264. impostor.object.getWorldMatrix().invertToRef(this._tmpMatrix);
  265. Vector3.TransformCoordinatesToRef(contactPoint, this._tmpMatrix, this._tmpVector);
  266. contactPoint = this._tmpVector;
  267. }
  268. worldPoint.setValue(contactPoint.x, contactPoint.y, contactPoint.z);
  269. impulse.setValue(force.x, force.y, force.z);
  270. impostor.physicsBody.applyImpulse(impulse, worldPoint);
  271. }
  272. else {
  273. Logger.Warn("Cannot be applied to a soft body");
  274. }
  275. }
  276. /**
  277. * Applies a force on the imposter
  278. * @param impostor imposter to apply force
  279. * @param force amount of force to be applied to the imposter
  280. * @param contactPoint the location to apply the force on the imposter
  281. */
  282. public applyForce(impostor: PhysicsImpostor, force: Vector3, contactPoint: Vector3) {
  283. if (!impostor.soft) {
  284. impostor.physicsBody.activate();
  285. var worldPoint = this._tmpAmmoVectorA;
  286. var impulse = this._tmpAmmoVectorB;
  287. // Convert contactPoint into world space
  288. if (impostor.object && impostor.object.getWorldMatrix) {
  289. impostor.object.getWorldMatrix().invertToRef(this._tmpMatrix);
  290. Vector3.TransformCoordinatesToRef(contactPoint, this._tmpMatrix, this._tmpVector);
  291. contactPoint = this._tmpVector;
  292. }
  293. worldPoint.setValue(contactPoint.x, contactPoint.y, contactPoint.z);
  294. impulse.setValue(force.x, force.y, force.z);
  295. impostor.physicsBody.applyForce(impulse, worldPoint);
  296. }
  297. else {
  298. Logger.Warn("Cannot be applied to a soft body");
  299. }
  300. }
  301. /**
  302. * Creates a physics body using the plugin
  303. * @param impostor the imposter to create the physics body on
  304. */
  305. public generatePhysicsBody(impostor: PhysicsImpostor) {
  306. impostor._pluginData = { toDispose: [] };
  307. //parent-child relationship
  308. if (impostor.parent) {
  309. if (impostor.physicsBody) {
  310. this.removePhysicsBody(impostor);
  311. impostor.forceUpdate();
  312. }
  313. return;
  314. }
  315. if (impostor.isBodyInitRequired()) {
  316. var colShape = this._createShape(impostor);
  317. var mass = impostor.getParam("mass");
  318. impostor._pluginData.mass = mass;
  319. if (impostor.soft) {
  320. colShape.get_m_cfg().set_collisions(0x11);
  321. colShape.get_m_cfg().set_kDP(impostor.getParam("damping"));
  322. Ammo.castObject(colShape, Ammo.btCollisionObject).getCollisionShape().setMargin(impostor.getParam("margin"));
  323. colShape.setActivationState(AmmoJSPlugin.DISABLE_DEACTIVATION_FLAG);
  324. this.world.addSoftBody(colShape, 1, -1);
  325. impostor.physicsBody = colShape;
  326. impostor._pluginData.toDispose.concat([colShape]);
  327. this.setBodyPressure(impostor, 0);
  328. if (impostor.type === PhysicsImpostor.SoftbodyImpostor) {
  329. this.setBodyPressure(impostor, impostor.getParam("pressure"));
  330. }
  331. this.setBodyStiffness(impostor, impostor.getParam("stiffness"));
  332. this.setBodyVelocityIterations(impostor, impostor.getParam("velocityIterations"));
  333. this.setBodyPositionIterations(impostor, impostor.getParam("positionIterations"));
  334. }
  335. else {
  336. var localInertia = new Ammo.btVector3(0, 0, 0);
  337. var startTransform = new Ammo.btTransform();
  338. startTransform.setIdentity();
  339. if (mass !== 0) {
  340. colShape.calculateLocalInertia(mass, localInertia);
  341. }
  342. this._tmpAmmoVectorA.setValue(impostor.object.position.x, impostor.object.position.y, impostor.object.position.z);
  343. this._tmpAmmoQuaternion.setValue(impostor.object.rotationQuaternion!.x, impostor.object.rotationQuaternion!.y, impostor.object.rotationQuaternion!.z, impostor.object.rotationQuaternion!.w);
  344. startTransform.setOrigin(this._tmpAmmoVectorA);
  345. startTransform.setRotation(this._tmpAmmoQuaternion);
  346. var myMotionState = new Ammo.btDefaultMotionState(startTransform);
  347. var rbInfo = new Ammo.btRigidBodyConstructionInfo(mass, myMotionState, colShape, localInertia);
  348. var body = new Ammo.btRigidBody(rbInfo);
  349. // Make objects kinematic if it's mass is 0
  350. if (mass === 0) {
  351. body.setCollisionFlags(body.getCollisionFlags() | AmmoJSPlugin.KINEMATIC_FLAG);
  352. body.setActivationState(AmmoJSPlugin.DISABLE_DEACTIVATION_FLAG);
  353. }
  354. // Disable collision if NoImpostor, but keep collision if shape is btCompoundShape
  355. if (impostor.type == PhysicsImpostor.NoImpostor && !colShape.getChildShape) {
  356. body.setCollisionFlags(body.getCollisionFlags() | AmmoJSPlugin.DISABLE_COLLISION_FLAG);
  357. }
  358. this.world.addRigidBody(body);
  359. impostor.physicsBody = body;
  360. impostor._pluginData.toDispose.concat([body, rbInfo, myMotionState, startTransform, localInertia, colShape]);
  361. }
  362. this.setBodyRestitution(impostor, impostor.getParam("restitution"));
  363. this.setBodyFriction(impostor, impostor.getParam("friction"));
  364. }
  365. }
  366. /**
  367. * Removes the physics body from the imposter and disposes of the body's memory
  368. * @param impostor imposter to remove the physics body from
  369. */
  370. public removePhysicsBody(impostor: PhysicsImpostor) {
  371. if (this.world) {
  372. this.world.removeRigidBody(impostor.physicsBody);
  373. impostor._pluginData.toDispose.forEach((d: any) => {
  374. this.bjsAMMO.destroy(d);
  375. });
  376. }
  377. }
  378. /**
  379. * Generates a joint
  380. * @param impostorJoint the imposter joint to create the joint with
  381. */
  382. public generateJoint(impostorJoint: PhysicsImpostorJoint) {
  383. var mainBody = impostorJoint.mainImpostor.physicsBody;
  384. var connectedBody = impostorJoint.connectedImpostor.physicsBody;
  385. if (!mainBody || !connectedBody) {
  386. return;
  387. }
  388. var jointData = impostorJoint.joint.jointData;
  389. if (!jointData.mainPivot) {
  390. jointData.mainPivot = new Vector3(0, 0, 0);
  391. }
  392. if (!jointData.connectedPivot) {
  393. jointData.connectedPivot = new Vector3(0, 0, 0);
  394. }
  395. var joint: any;
  396. switch (impostorJoint.joint.type) {
  397. case PhysicsJoint.DistanceJoint:
  398. var distance = (<DistanceJointData>jointData).maxDistance;
  399. if (distance) {
  400. jointData.mainPivot = new Vector3(0, -distance / 2, 0);
  401. jointData.connectedPivot = new Vector3(0, distance / 2, 0);
  402. }
  403. joint = new Ammo.btPoint2PointConstraint(mainBody, connectedBody, new Ammo.btVector3(jointData.mainPivot.x, jointData.mainPivot.y, jointData.mainPivot.z), new Ammo.btVector3(jointData.connectedPivot.x, jointData.connectedPivot.y, jointData.connectedPivot.z));
  404. break;
  405. case PhysicsJoint.HingeJoint:
  406. if (!jointData.mainAxis) {
  407. jointData.mainAxis = new Vector3(0, 0, 0);
  408. }
  409. if (!jointData.connectedAxis) {
  410. jointData.connectedAxis = new Vector3(0, 0, 0);
  411. }
  412. var mainAxis = new Ammo.btVector3(jointData.mainAxis.x, jointData.mainAxis.y, jointData.mainAxis.z);
  413. var connectedAxis = new Ammo.btVector3(jointData.connectedAxis.x, jointData.connectedAxis.y, jointData.connectedAxis.z);
  414. joint = new Ammo.btHingeConstraint(mainBody, connectedBody, new Ammo.btVector3(jointData.mainPivot.x, jointData.mainPivot.y, jointData.mainPivot.z), new Ammo.btVector3(jointData.connectedPivot.x, jointData.connectedPivot.y, jointData.connectedPivot.z), mainAxis, connectedAxis);
  415. break;
  416. case PhysicsJoint.BallAndSocketJoint:
  417. joint = new Ammo.btPoint2PointConstraint(mainBody, connectedBody, new Ammo.btVector3(jointData.mainPivot.x, jointData.mainPivot.y, jointData.mainPivot.z), new Ammo.btVector3(jointData.connectedPivot.x, jointData.connectedPivot.y, jointData.connectedPivot.z));
  418. break;
  419. default:
  420. Logger.Warn("JointType not currently supported by the Ammo plugin, falling back to PhysicsJoint.BallAndSocketJoint");
  421. joint = new Ammo.btPoint2PointConstraint(mainBody, connectedBody, new Ammo.btVector3(jointData.mainPivot.x, jointData.mainPivot.y, jointData.mainPivot.z), new Ammo.btVector3(jointData.connectedPivot.x, jointData.connectedPivot.y, jointData.connectedPivot.z));
  422. break;
  423. }
  424. this.world.addConstraint(joint, !impostorJoint.joint.jointData.collision);
  425. impostorJoint.joint.physicsJoint = joint;
  426. }
  427. /**
  428. * Removes a joint
  429. * @param impostorJoint the imposter joint to remove the joint from
  430. */
  431. public removeJoint(impostorJoint: PhysicsImpostorJoint) {
  432. if (this.world) {
  433. this.world.removeConstraint(impostorJoint.joint.physicsJoint);
  434. }
  435. }
  436. // adds all verticies (including child verticies) to the triangle mesh
  437. private _addMeshVerts(btTriangleMesh: any, topLevelObject: IPhysicsEnabledObject, object: IPhysicsEnabledObject) {
  438. var triangleCount = 0;
  439. if (object && object.getIndices && object.getWorldMatrix && object.getChildMeshes) {
  440. var indices = object.getIndices();
  441. if (!indices) {
  442. indices = [];
  443. }
  444. var vertexPositions = object.getVerticesData(VertexBuffer.PositionKind);
  445. if (!vertexPositions) {
  446. vertexPositions = [];
  447. }
  448. object.computeWorldMatrix(false);
  449. var faceCount = indices.length / 3;
  450. for (var i = 0; i < faceCount; i++) {
  451. var triPoints = [];
  452. for (var point = 0; point < 3; point++) {
  453. var v = new Vector3(vertexPositions[(indices[(i * 3) + point] * 3) + 0], vertexPositions[(indices[(i * 3) + point] * 3) + 1], vertexPositions[(indices[(i * 3) + point] * 3) + 2]);
  454. // Adjust for initial scaling
  455. Matrix.ScalingToRef(object.scaling.x, object.scaling.y, object.scaling.z, this._tmpMatrix);
  456. v = Vector3.TransformCoordinates(v, this._tmpMatrix);
  457. var vec: any;
  458. if (point == 0) {
  459. vec = this._tmpAmmoVectorA;
  460. } else if (point == 1) {
  461. vec = this._tmpAmmoVectorB;
  462. } else {
  463. vec = this._tmpAmmoVectorC;
  464. }
  465. vec.setValue(v.x, v.y, v.z);
  466. triPoints.push(vec);
  467. }
  468. btTriangleMesh.addTriangle(triPoints[0], triPoints[1], triPoints[2]);
  469. triangleCount++;
  470. }
  471. object.getChildMeshes().forEach((m) => {
  472. triangleCount += this._addMeshVerts(btTriangleMesh, topLevelObject, m);
  473. });
  474. }
  475. return triangleCount;
  476. }
  477. /**
  478. * Initialise the soft body vertices to match its object's (mesh) vertices
  479. * Softbody vertices (nodes) are in world space and to match this
  480. * The object's position and rotation is set to zero and so its vertices are also then set in world space
  481. * @param impostor to create the softbody for
  482. */
  483. private _softVertexData(impostor: PhysicsImpostor) : VertexData {
  484. var object = impostor.object;
  485. if (object && object.getIndices && object.getWorldMatrix && object.getChildMeshes) {
  486. var indices = object.getIndices();
  487. if (!indices) {
  488. indices = [];
  489. }
  490. var vertexPositions = object.getVerticesData(VertexBuffer.PositionKind);
  491. if (!vertexPositions) {
  492. vertexPositions = [];
  493. }
  494. var vertexNormals = object.getVerticesData(VertexBuffer.NormalKind);
  495. if (!vertexNormals) {
  496. vertexNormals = [];
  497. }
  498. object.computeWorldMatrix(false);
  499. var newPoints = [];
  500. var newNorms = [];
  501. for (var i = 0; i < vertexPositions.length; i += 3) {
  502. var v = new Vector3(vertexPositions[i], vertexPositions[i + 1], vertexPositions[i + 2]);
  503. var n = new Vector3(vertexNormals[i], vertexNormals[i + 1], vertexNormals[i + 2]);
  504. v = Vector3.TransformCoordinates(v, object.getWorldMatrix());
  505. n = Vector3.TransformNormal(n, object.getWorldMatrix());
  506. newPoints.push(v.x, v.y, v.z);
  507. newNorms.push(n.x, n.y, n.z);
  508. }
  509. var vertex_data = new VertexData();
  510. vertex_data.positions = newPoints;
  511. vertex_data.normals = newNorms;
  512. vertex_data.uvs = object.getVerticesData(VertexBuffer.UVKind);
  513. vertex_data.colors = object.getVerticesData(VertexBuffer.ColorKind);
  514. if (object && object.getIndices) {
  515. vertex_data.indices = object.getIndices();
  516. }
  517. vertex_data.applyToMesh(<Mesh>object);
  518. object.position = Vector3.Zero();
  519. object.rotationQuaternion = null;
  520. object.rotation = Vector3.Zero();
  521. object.computeWorldMatrix(true);
  522. return vertex_data;
  523. }
  524. return VertexData.ExtractFromMesh(<Mesh>object);
  525. }
  526. /**
  527. * Create an impostor's soft body
  528. * @param impostor to create the softbody for
  529. */
  530. private _createSoftbody(impostor: PhysicsImpostor) {
  531. var object = impostor.object;
  532. if (object && object.getIndices) {
  533. var indices = object.getIndices();
  534. if (!indices) {
  535. indices = [];
  536. }
  537. var vertex_data = this._softVertexData(impostor);
  538. var vertexPositions = vertex_data.positions;
  539. var vertexNormals = vertex_data.normals;
  540. if (vertexPositions === null || vertexNormals === null) {
  541. return new Ammo.btCompoundShape();
  542. }
  543. else {
  544. var triPoints = [];
  545. var triNorms = [];
  546. for (var i = 0; i < vertexPositions.length; i += 3) {
  547. var v = new Vector3(vertexPositions[i], vertexPositions[i + 1], vertexPositions[i + 2]);
  548. var n = new Vector3(vertexNormals[i], vertexNormals[i + 1], vertexNormals[i + 2]);
  549. triPoints.push(v.x, v.y, -v.z);
  550. triNorms.push(n.x, n.y, -n.z);
  551. }
  552. var softBody = new Ammo.btSoftBodyHelpers().CreateFromTriMesh(
  553. this.world.getWorldInfo(),
  554. triPoints,
  555. object.getIndices(),
  556. indices.length / 3,
  557. true
  558. );
  559. var nbVertices = vertexPositions.length / 3;
  560. var bodyVertices = softBody.get_m_nodes();
  561. var node: any;
  562. var nodeNormals: any;
  563. for (var i = 0; i < nbVertices; i++) {
  564. node = bodyVertices.at(i);
  565. var nodeNormals = node.get_m_n();
  566. nodeNormals.setX(triNorms[3 * i]);
  567. nodeNormals.setY(triNorms[3 * i + 1]);
  568. nodeNormals.setZ(triNorms[3 * i + 2]);
  569. }
  570. softBody.get_m_cfg().set_collisions(0x11);
  571. return softBody;
  572. }
  573. }
  574. }
  575. /**
  576. * Create cloth for an impostor
  577. * @param impostor to create the softbody for
  578. */
  579. private _createCloth(impostor: PhysicsImpostor) {
  580. var object = impostor.object;
  581. if (object && object.getIndices) {
  582. var indices = object.getIndices();
  583. if (!indices) {
  584. indices = [];
  585. }
  586. var vertex_data = this._softVertexData(impostor);
  587. var vertexPositions = vertex_data.positions;
  588. var vertexNormals = vertex_data.normals;
  589. if (vertexPositions === null || vertexNormals === null) {
  590. return new Ammo.btCompoundShape();
  591. }
  592. else {
  593. var len = vertexPositions.length;
  594. var segments = Math.sqrt(len / 3);
  595. impostor.segments = segments;
  596. var segs = segments - 1;
  597. this._tmpAmmoVectorA.setValue(vertexPositions[0], vertexPositions[1], vertexPositions[2]);
  598. this._tmpAmmoVectorB.setValue(vertexPositions[3 * segs], vertexPositions[3 * segs + 1], vertexPositions[3 * segs + 2]);
  599. this._tmpAmmoVectorD.setValue(vertexPositions[len - 3], vertexPositions[len - 2], vertexPositions[len - 1]);
  600. this._tmpAmmoVectorC.setValue(vertexPositions[len - 3 - 3 * segs], vertexPositions[len - 2 - 3 * segs], vertexPositions[len - 1 - 3 * segs]);
  601. var clothBody = new Ammo.btSoftBodyHelpers().CreatePatch(
  602. this.world.getWorldInfo(),
  603. this._tmpAmmoVectorA,
  604. this._tmpAmmoVectorB,
  605. this._tmpAmmoVectorC,
  606. this._tmpAmmoVectorD,
  607. segments,
  608. segments,
  609. impostor.getParam("fixedPoints"),
  610. true
  611. );
  612. clothBody.get_m_cfg().set_collisions(0x11);
  613. return clothBody;
  614. }
  615. }
  616. }
  617. // adds all verticies (including child verticies) to the convex hull shape
  618. private _addHullVerts(btConvexHullShape: any, topLevelObject: IPhysicsEnabledObject, object: IPhysicsEnabledObject) {
  619. var triangleCount = 0;
  620. if (object && object.getIndices && object.getWorldMatrix && object.getChildMeshes) {
  621. var indices = object.getIndices();
  622. if (!indices) {
  623. indices = [];
  624. }
  625. var vertexPositions = object.getVerticesData(VertexBuffer.PositionKind);
  626. if (!vertexPositions) {
  627. vertexPositions = [];
  628. }
  629. object.computeWorldMatrix(false);
  630. var faceCount = indices.length / 3;
  631. for (var i = 0; i < faceCount; i++) {
  632. var triPoints = [];
  633. for (var point = 0; point < 3; point++) {
  634. var v = new Vector3(vertexPositions[(indices[(i * 3) + point] * 3) + 0], vertexPositions[(indices[(i * 3) + point] * 3) + 1], vertexPositions[(indices[(i * 3) + point] * 3) + 2]);
  635. // Adjust for initial scaling
  636. Matrix.ScalingToRef(object.scaling.x, object.scaling.y, object.scaling.z, this._tmpMatrix);
  637. v = Vector3.TransformCoordinates(v, this._tmpMatrix);
  638. var vec: any;
  639. if (point == 0) {
  640. vec = this._tmpAmmoVectorA;
  641. } else if (point == 1) {
  642. vec = this._tmpAmmoVectorB;
  643. } else {
  644. vec = this._tmpAmmoVectorC;
  645. }
  646. vec.setValue(v.x, v.y, v.z);
  647. triPoints.push(vec);
  648. }
  649. btConvexHullShape.addPoint(triPoints[0], true);
  650. btConvexHullShape.addPoint(triPoints[1], true);
  651. btConvexHullShape.addPoint(triPoints[2], true);
  652. triangleCount++;
  653. }
  654. object.getChildMeshes().forEach((m) => {
  655. triangleCount += this._addHullVerts(btConvexHullShape, topLevelObject, m);
  656. });
  657. }
  658. return triangleCount;
  659. }
  660. private _createShape(impostor: PhysicsImpostor, ignoreChildren = false) {
  661. var object = impostor.object;
  662. var returnValue: any;
  663. var extendSize = impostor.getObjectExtendSize();
  664. if (!ignoreChildren) {
  665. var meshChildren = impostor.object.getChildMeshes ? impostor.object.getChildMeshes(true) : [];
  666. returnValue = new Ammo.btCompoundShape();
  667. // Add shape of all children to the compound shape
  668. var childrenAdded = 0;
  669. meshChildren.forEach((childMesh) => {
  670. var childImpostor = childMesh.getPhysicsImpostor();
  671. if (childImpostor) {
  672. var shape = this._createShape(childImpostor);
  673. // Position needs to be scaled based on parent's scaling
  674. var parentMat = childMesh.parent!.getWorldMatrix().clone();
  675. var s = new Vector3();
  676. parentMat.decompose(s);
  677. this._tmpAmmoTransform.getOrigin().setValue(childMesh.position.x * s.x, childMesh.position.y * s.y, childMesh.position.z * s.z);
  678. this._tmpAmmoQuaternion.setValue(childMesh.rotationQuaternion!.x, childMesh.rotationQuaternion!.y, childMesh.rotationQuaternion!.z, childMesh.rotationQuaternion!.w);
  679. this._tmpAmmoTransform.setRotation(this._tmpAmmoQuaternion);
  680. returnValue.addChildShape(this._tmpAmmoTransform, shape);
  681. childImpostor.dispose();
  682. childrenAdded++;
  683. }
  684. });
  685. if (childrenAdded > 0) {
  686. // Add parents shape as a child if present
  687. if (impostor.type != PhysicsImpostor.NoImpostor) {
  688. var shape = this._createShape(impostor, true);
  689. if (shape) {
  690. this._tmpAmmoTransform.getOrigin().setValue(0, 0, 0);
  691. this._tmpAmmoQuaternion.setValue(0, 0, 0, 1);
  692. this._tmpAmmoTransform.setRotation(this._tmpAmmoQuaternion);
  693. returnValue.addChildShape(this._tmpAmmoTransform, shape);
  694. }
  695. }
  696. return returnValue;
  697. } else {
  698. // If no children with impostors create the actual shape below instead
  699. Ammo.destroy(returnValue);
  700. returnValue = null;
  701. }
  702. }
  703. switch (impostor.type) {
  704. case PhysicsImpostor.SphereImpostor:
  705. returnValue = new Ammo.btSphereShape(extendSize.x / 2);
  706. break;
  707. case PhysicsImpostor.CylinderImpostor:
  708. this._tmpAmmoVectorA.setValue(extendSize.x / 2, extendSize.y / 2, extendSize.z / 2);
  709. returnValue = new Ammo.btCylinderShape(this._tmpAmmoVectorA);
  710. break;
  711. case PhysicsImpostor.PlaneImpostor:
  712. case PhysicsImpostor.BoxImpostor:
  713. this._tmpAmmoVectorA.setValue(extendSize.x / 2, extendSize.y / 2, extendSize.z / 2);
  714. returnValue = new Ammo.btBoxShape(this._tmpAmmoVectorA);
  715. break;
  716. case PhysicsImpostor.MeshImpostor:
  717. if (impostor.getParam("mass") == 0) {
  718. // Only create btBvhTriangleMeshShape impostor is static
  719. // See https://pybullet.org/Bullet/phpBB3/viewtopic.php?t=7283
  720. var tetraMesh = new Ammo.btTriangleMesh();
  721. impostor._pluginData.toDispose.concat([tetraMesh]);
  722. var triangeCount = this._addMeshVerts(tetraMesh, object, object);
  723. if (triangeCount == 0) {
  724. returnValue = new Ammo.btCompoundShape();
  725. } else {
  726. returnValue = new Ammo.btBvhTriangleMeshShape(tetraMesh);
  727. }
  728. break;
  729. }
  730. // Otherwise create convexHullImpostor
  731. case PhysicsImpostor.ConvexHullImpostor:
  732. var convexMesh = new Ammo.btConvexHullShape();
  733. var triangeCount = this._addHullVerts(convexMesh, object, object);
  734. if (triangeCount == 0) {
  735. // Cleanup Unused Convex Hull Shape
  736. impostor._pluginData.toDispose.concat([convexMesh]);
  737. returnValue = new Ammo.btCompoundShape();
  738. } else {
  739. returnValue = convexMesh;
  740. }
  741. break;
  742. case PhysicsImpostor.NoImpostor:
  743. // Fill with sphere but collision is disabled on the rigid body in generatePhysicsBody, using an empty shape caused unexpected movement with joints
  744. returnValue = new Ammo.btSphereShape(extendSize.x / 2);
  745. break;
  746. case PhysicsImpostor.SoftbodyImpostor:
  747. // Only usable with a mesh that has sufficient and shared vertices
  748. returnValue = this._createSoftbody(impostor);
  749. break;
  750. case PhysicsImpostor.ClothImpostor:
  751. // Only usable with a mesh that has sufficient and shared vertices
  752. returnValue = this._createCloth(impostor);
  753. break;
  754. default:
  755. Logger.Warn("The impostor type is not currently supported by the ammo plugin.");
  756. break;
  757. }
  758. return returnValue;
  759. }
  760. /**
  761. * Sets the physics body position/rotation from the babylon mesh's position/rotation
  762. * @param impostor imposter containing the physics body and babylon object
  763. */
  764. public setTransformationFromPhysicsBody(impostor: PhysicsImpostor) {
  765. impostor.physicsBody.getMotionState().getWorldTransform(this._tmpAmmoTransform);
  766. impostor.object.position.set(this._tmpAmmoTransform.getOrigin().x(), this._tmpAmmoTransform.getOrigin().y(), this._tmpAmmoTransform.getOrigin().z());
  767. if (!impostor.object.rotationQuaternion) {
  768. if (impostor.object.rotation) {
  769. this._tmpQuaternion.set(this._tmpAmmoTransform.getRotation().x(), this._tmpAmmoTransform.getRotation().y(), this._tmpAmmoTransform.getRotation().z(), this._tmpAmmoTransform.getRotation().w());
  770. this._tmpQuaternion.toEulerAnglesToRef(impostor.object.rotation);
  771. }
  772. } else {
  773. impostor.object.rotationQuaternion.set(this._tmpAmmoTransform.getRotation().x(), this._tmpAmmoTransform.getRotation().y(), this._tmpAmmoTransform.getRotation().z(), this._tmpAmmoTransform.getRotation().w());
  774. }
  775. }
  776. /**
  777. * Sets the babylon object's position/rotation from the physics body's position/rotation
  778. * @param impostor imposter containing the physics body and babylon object
  779. * @param newPosition new position
  780. * @param newRotation new rotation
  781. */
  782. public setPhysicsBodyTransformation(impostor: PhysicsImpostor, newPosition: Vector3, newRotation: Quaternion) {
  783. var trans = impostor.physicsBody.getWorldTransform();
  784. // If rotation/position has changed update and activate riged body
  785. if (
  786. trans.getOrigin().x() != newPosition.x ||
  787. trans.getOrigin().y() != newPosition.y ||
  788. trans.getOrigin().z() != newPosition.z ||
  789. trans.getRotation().x() != newRotation.x ||
  790. trans.getRotation().y() != newRotation.y ||
  791. trans.getRotation().z() != newRotation.z ||
  792. trans.getRotation().w() != newRotation.w
  793. ) {
  794. this._tmpAmmoVectorA.setValue(newPosition.x, newPosition.y, newPosition.z);
  795. trans.setOrigin(this._tmpAmmoVectorA);
  796. this._tmpAmmoQuaternion.setValue(newRotation.x, newRotation.y, newRotation.z, newRotation.w);
  797. trans.setRotation(this._tmpAmmoQuaternion);
  798. impostor.physicsBody.setWorldTransform(trans);
  799. if (impostor.mass == 0) {
  800. // Kinematic objects must be updated using motion state
  801. var motionState = impostor.physicsBody.getMotionState();
  802. if (motionState) {
  803. motionState.setWorldTransform(trans);
  804. }
  805. } else {
  806. impostor.physicsBody.activate();
  807. }
  808. }
  809. }
  810. /**
  811. * If this plugin is supported
  812. * @returns true if its supported
  813. */
  814. public isSupported(): boolean {
  815. return this.bjsAMMO !== undefined;
  816. }
  817. /**
  818. * Sets the linear velocity of the physics body
  819. * @param impostor imposter to set the velocity on
  820. * @param velocity velocity to set
  821. */
  822. public setLinearVelocity(impostor: PhysicsImpostor, velocity: Vector3) {
  823. this._tmpAmmoVectorA.setValue(velocity.x, velocity.y, velocity.z);
  824. if (impostor.soft) {
  825. impostor.physicsBody.linearVelocity(this._tmpAmmoVectorA);
  826. }
  827. else {
  828. impostor.physicsBody.setLinearVelocity(this._tmpAmmoVectorA);
  829. }
  830. }
  831. /**
  832. * Sets the angular velocity of the physics body
  833. * @param impostor imposter to set the velocity on
  834. * @param velocity velocity to set
  835. */
  836. public setAngularVelocity(impostor: PhysicsImpostor, velocity: Vector3) {
  837. this._tmpAmmoVectorA.setValue(velocity.x, velocity.y, velocity.z);
  838. if (impostor.soft) {
  839. impostor.physicsBody.angularVelocity(this._tmpAmmoVectorA);
  840. }
  841. else {
  842. impostor.physicsBody.setAngularVelocity(this._tmpAmmoVectorA);
  843. }
  844. }
  845. /**
  846. * gets the linear velocity
  847. * @param impostor imposter to get linear velocity from
  848. * @returns linear velocity
  849. */
  850. public getLinearVelocity(impostor: PhysicsImpostor): Nullable<Vector3> {
  851. if (impostor.soft) {
  852. var v = impostor.physicsBody.linearVelocity();
  853. }
  854. else {
  855. var v = impostor.physicsBody.getLinearVelocity();
  856. }
  857. if (!v) {
  858. return null;
  859. }
  860. return new Vector3(v.x(), v.y(), v.z());
  861. }
  862. /**
  863. * gets the angular velocity
  864. * @param impostor imposter to get angular velocity from
  865. * @returns angular velocity
  866. */
  867. public getAngularVelocity(impostor: PhysicsImpostor): Nullable<Vector3> {
  868. if (impostor.soft) {
  869. var v = impostor.physicsBody.angularVelocity();
  870. }
  871. else {
  872. var v = impostor.physicsBody.getAngularVelocity();
  873. }
  874. if (!v) {
  875. return null;
  876. }
  877. return new Vector3(v.x(), v.y(), v.z());
  878. }
  879. /**
  880. * Sets the mass of physics body
  881. * @param impostor imposter to set the mass on
  882. * @param mass mass to set
  883. */
  884. public setBodyMass(impostor: PhysicsImpostor, mass: number) {
  885. if (impostor.soft) {
  886. impostor.physicsBody.setTotalMass(mass, false);
  887. }
  888. else {
  889. impostor.physicsBody.setMassProps(mass);
  890. }
  891. impostor._pluginData.mass = mass;
  892. }
  893. /**
  894. * Gets the mass of the physics body
  895. * @param impostor imposter to get the mass from
  896. * @returns mass
  897. */
  898. public getBodyMass(impostor: PhysicsImpostor): number {
  899. return impostor._pluginData.mass;
  900. }
  901. /**
  902. * Gets friction of the impostor
  903. * @param impostor impostor to get friction from
  904. * @returns friction value
  905. */
  906. public getBodyFriction(impostor: PhysicsImpostor): number {
  907. return impostor._pluginData.friction;
  908. }
  909. /**
  910. * Sets friction of the impostor
  911. * @param impostor impostor to set friction on
  912. * @param friction friction value
  913. */
  914. public setBodyFriction(impostor: PhysicsImpostor, friction: number) {
  915. if (impostor.soft) {
  916. impostor.physicsBody.get_m_cfg().set_kDF(friction);
  917. }
  918. else {
  919. impostor.physicsBody.setFriction(friction);
  920. }
  921. impostor._pluginData.friction = friction;
  922. }
  923. /**
  924. * Gets restitution of the impostor
  925. * @param impostor impostor to get restitution from
  926. * @returns restitution value
  927. */
  928. public getBodyRestitution(impostor: PhysicsImpostor): number {
  929. return impostor._pluginData.restitution;
  930. }
  931. /**
  932. * Sets resitution of the impostor
  933. * @param impostor impostor to set resitution on
  934. * @param restitution resitution value
  935. */
  936. public setBodyRestitution(impostor: PhysicsImpostor, restitution: number) {
  937. impostor.physicsBody.setRestitution(restitution);
  938. impostor._pluginData.restitution = restitution;
  939. }
  940. /**
  941. * Gets pressure inside the impostor
  942. * @param impostor impostor to get pressure from
  943. * @returns pressure value
  944. */
  945. public getBodyPressure(impostor: PhysicsImpostor): number {
  946. if (!impostor.soft) {
  947. Logger.Warn("Pressure is not a property of a rigid body");
  948. return 0;
  949. }
  950. return impostor._pluginData.pressure;
  951. }
  952. /**
  953. * Sets pressure inside a soft body impostor
  954. * Cloth and rope must remain 0 pressure
  955. * @param impostor impostor to set pressure on
  956. * @param pressure pressure value
  957. */
  958. public setBodyPressure(impostor: PhysicsImpostor, pressure: number) {
  959. if (impostor.soft) {
  960. if (impostor.type === PhysicsImpostor.SoftbodyImpostor) {
  961. impostor.physicsBody.get_m_cfg().set_kPR(pressure);
  962. impostor._pluginData.pressure = pressure;
  963. }
  964. else {
  965. impostor.physicsBody.get_m_cfg().set_kPR(0);
  966. impostor._pluginData.pressure = 0;
  967. }
  968. }
  969. else {
  970. Logger.Warn("Pressure can only be applied to a softbody");
  971. }
  972. }
  973. /**
  974. * Gets stiffness of the impostor
  975. * @param impostor impostor to get stiffness from
  976. * @returns pressure value
  977. */
  978. public getBodyStiffness(impostor: PhysicsImpostor): number {
  979. if (!impostor.soft) {
  980. Logger.Warn("Stiffness is not a property of a rigid body");
  981. return 0;
  982. }
  983. return impostor._pluginData.stiffness;
  984. }
  985. /**
  986. * Sets stiffness of the impostor
  987. * @param impostor impostor to set stiffness on
  988. * @param stiffness stiffness value from 0 to 1
  989. */
  990. public setBodyStiffness(impostor: PhysicsImpostor, stiffness: number) {
  991. if (impostor.soft) {
  992. stiffness = stiffness < 0 ? 0 : stiffness;
  993. stiffness = stiffness > 1 ? 1 : stiffness;
  994. impostor.physicsBody.get_m_materials().at(0).set_m_kLST(stiffness);
  995. impostor._pluginData.stiffness = stiffness;
  996. }
  997. else {
  998. Logger.Warn("Stiffness cannot be applied to a rigid body");
  999. }
  1000. }
  1001. /**
  1002. * Gets velocityIterations of the impostor
  1003. * @param impostor impostor to get velocity iterations from
  1004. * @returns velocityIterations value
  1005. */
  1006. public getBodyVelocityIterations(impostor: PhysicsImpostor): number {
  1007. if (!impostor.soft) {
  1008. Logger.Warn("Velocity iterations is not a property of a rigid body");
  1009. return 0;
  1010. }
  1011. return impostor._pluginData.velocityIterations;
  1012. }
  1013. /**
  1014. * Sets velocityIterations of the impostor
  1015. * @param impostor impostor to set velocity iterations on
  1016. * @param velocityIterations velocityIterations value
  1017. */
  1018. public setBodyVelocityIterations(impostor: PhysicsImpostor, velocityIterations: number) {
  1019. if (impostor.soft) {
  1020. velocityIterations = velocityIterations < 0 ? 0 : velocityIterations;
  1021. impostor.physicsBody.get_m_cfg().set_viterations(velocityIterations);
  1022. impostor._pluginData.velocityIterations = velocityIterations;
  1023. }
  1024. else {
  1025. Logger.Warn("Velocity iterations cannot be applied to a rigid body");
  1026. }
  1027. }
  1028. /**
  1029. * Gets positionIterations of the impostor
  1030. * @param impostor impostor to get position iterations from
  1031. * @returns positionIterations value
  1032. */
  1033. public getBodyPositionIterations(impostor: PhysicsImpostor): number {
  1034. if (!impostor.soft) {
  1035. Logger.Warn("Position iterations is not a property of a rigid body");
  1036. return 0;
  1037. }
  1038. return impostor._pluginData.positionIterations;
  1039. }
  1040. /**
  1041. * Sets positionIterations of the impostor
  1042. * @param impostor impostor to set position on
  1043. * @param positionIterations positionIterations value
  1044. */
  1045. public setBodyPositionIterations(impostor: PhysicsImpostor, positionIterations: number) {
  1046. if (impostor.soft) {
  1047. positionIterations = positionIterations < 0 ? 0 : positionIterations;
  1048. impostor.physicsBody.get_m_cfg().set_piterations(positionIterations);
  1049. impostor._pluginData.positionIterations = positionIterations;
  1050. }
  1051. else {
  1052. Logger.Warn("Position iterations cannot be applied to a rigid body");
  1053. }
  1054. }
  1055. /**
  1056. * Append an anchor to a soft object
  1057. * @param impostor soft impostor to add anchor to
  1058. * @param otherImpostor rigid impostor as the anchor
  1059. * @param width ratio across width from 0 to 1
  1060. * @param height ratio up height from 0 to 1
  1061. * @param influence the elasticity between soft impostor and anchor from 0, very stretchy to 1, no strech
  1062. * @param noCollisionBetweenLinkedBodies when true collisions between soft impostor and anchor are ignored; default false
  1063. */
  1064. public appendAnchor(impostor: PhysicsImpostor, otherImpostor: PhysicsImpostor, width: number, height: number, influence: number = 1, noCollisionBetweenLinkedBodies: boolean = false) {
  1065. var segs = impostor.segments;
  1066. var nbAcross = Math.round((segs - 1) * width);
  1067. var nbUp = Math.round((segs - 1) * height);
  1068. var nbDown = segs - 1 - nbUp;
  1069. var node = nbAcross + segs * nbDown;
  1070. impostor.physicsBody.appendAnchor(node, otherImpostor.physicsBody, noCollisionBetweenLinkedBodies, influence);
  1071. }
  1072. /**
  1073. * Sleeps the physics body and stops it from being active
  1074. * @param impostor impostor to sleep
  1075. */
  1076. public sleepBody(impostor: PhysicsImpostor) {
  1077. Logger.Warn("sleepBody is not currently supported by the Ammo physics plugin");
  1078. }
  1079. /**
  1080. * Activates the physics body
  1081. * @param impostor impostor to activate
  1082. */
  1083. public wakeUpBody(impostor: PhysicsImpostor) {
  1084. impostor.physicsBody.activate();
  1085. }
  1086. /**
  1087. * Updates the distance parameters of the joint
  1088. * @param joint joint to update
  1089. * @param maxDistance maximum distance of the joint
  1090. * @param minDistance minimum distance of the joint
  1091. */
  1092. public updateDistanceJoint(joint: PhysicsJoint, maxDistance: number, minDistance?: number) {
  1093. Logger.Warn("updateDistanceJoint is not currently supported by the Ammo physics plugin");
  1094. }
  1095. /**
  1096. * Sets a motor on the joint
  1097. * @param joint joint to set motor on
  1098. * @param speed speed of the motor
  1099. * @param maxForce maximum force of the motor
  1100. * @param motorIndex index of the motor
  1101. */
  1102. public setMotor(joint: IMotorEnabledJoint, speed?: number, maxForce?: number, motorIndex?: number) {
  1103. joint.physicsJoint.enableAngularMotor(true, speed, maxForce);
  1104. }
  1105. /**
  1106. * Sets the motors limit
  1107. * @param joint joint to set limit on
  1108. * @param upperLimit upper limit
  1109. * @param lowerLimit lower limit
  1110. */
  1111. public setLimit(joint: IMotorEnabledJoint, upperLimit: number, lowerLimit?: number) {
  1112. Logger.Warn("setLimit is not currently supported by the Ammo physics plugin");
  1113. }
  1114. /**
  1115. * Syncs the position and rotation of a mesh with the impostor
  1116. * @param mesh mesh to sync
  1117. * @param impostor impostor to update the mesh with
  1118. */
  1119. public syncMeshWithImpostor(mesh: AbstractMesh, impostor: PhysicsImpostor) {
  1120. var body = impostor.physicsBody;
  1121. body.getMotionState().getWorldTransform(this._tmpAmmoTransform);
  1122. mesh.position.x = this._tmpAmmoTransform.getOrigin().x();
  1123. mesh.position.y = this._tmpAmmoTransform.getOrigin().y();
  1124. mesh.position.z = this._tmpAmmoTransform.getOrigin().z();
  1125. if (mesh.rotationQuaternion) {
  1126. mesh.rotationQuaternion.x = this._tmpAmmoTransform.getRotation().x();
  1127. mesh.rotationQuaternion.y = this._tmpAmmoTransform.getRotation().y();
  1128. mesh.rotationQuaternion.z = this._tmpAmmoTransform.getRotation().z();
  1129. mesh.rotationQuaternion.w = this._tmpAmmoTransform.getRotation().w();
  1130. }
  1131. }
  1132. /**
  1133. * Gets the radius of the impostor
  1134. * @param impostor impostor to get radius from
  1135. * @returns the radius
  1136. */
  1137. public getRadius(impostor: PhysicsImpostor): number {
  1138. var exntend = impostor.getObjectExtendSize();
  1139. return exntend.x / 2;
  1140. }
  1141. /**
  1142. * Gets the box size of the impostor
  1143. * @param impostor impostor to get box size from
  1144. * @param result the resulting box size
  1145. */
  1146. public getBoxSizeToRef(impostor: PhysicsImpostor, result: Vector3): void {
  1147. var exntend = impostor.getObjectExtendSize();
  1148. result.x = exntend.x;
  1149. result.y = exntend.y;
  1150. result.z = exntend.z;
  1151. }
  1152. /**
  1153. * Disposes of the impostor
  1154. */
  1155. public dispose() {
  1156. // Dispose of world
  1157. Ammo.destroy(this.world);
  1158. Ammo.destroy(this._solver);
  1159. Ammo.destroy(this._overlappingPairCache);
  1160. Ammo.destroy(this._dispatcher);
  1161. Ammo.destroy(this._collisionConfiguration);
  1162. // Dispose of tmp variables
  1163. Ammo.destroy(this._tmpAmmoVectorA);
  1164. Ammo.destroy(this._tmpAmmoVectorB);
  1165. Ammo.destroy(this._tmpAmmoVectorC);
  1166. Ammo.destroy(this._tmpAmmoTransform);
  1167. Ammo.destroy(this._tmpAmmoQuaternion);
  1168. Ammo.destroy(this._tmpAmmoConcreteContactResultCallback);
  1169. this.world = null;
  1170. }
  1171. /**
  1172. * Does a raycast in the physics world
  1173. * @param from when should the ray start?
  1174. * @param to when should the ray end?
  1175. * @returns PhysicsRaycastResult
  1176. */
  1177. public raycast(from: Vector3, to: Vector3): PhysicsRaycastResult {
  1178. this._tmpAmmoVectorRCA = new this.bjsAMMO.btVector3(from.x, from.y, from.z);
  1179. this._tmpAmmoVectorRCB = new this.bjsAMMO.btVector3(to.x, to.y, to.z);
  1180. var rayCallback = new this.bjsAMMO.ClosestRayResultCallback(this._tmpAmmoVectorRCA, this._tmpAmmoVectorRCB);
  1181. this.world.rayTest(this._tmpAmmoVectorRCA, this._tmpAmmoVectorRCB, rayCallback);
  1182. this._raycastResult.reset(from, to);
  1183. if (rayCallback.hasHit()) {
  1184. // TODO: do we want/need the body? If so, set all the data
  1185. /*
  1186. var rigidBody = this.bjsAMMO.btRigidBody.prototype.upcast(
  1187. rayCallback.get_m_collisionObject()
  1188. );
  1189. var body = {};
  1190. */
  1191. this._raycastResult.setHitData(
  1192. {
  1193. x: rayCallback.get_m_hitNormalWorld().x(),
  1194. y: rayCallback.get_m_hitNormalWorld().y(),
  1195. z: rayCallback.get_m_hitNormalWorld().z(),
  1196. },
  1197. {
  1198. x: rayCallback.get_m_hitPointWorld().x(),
  1199. y: rayCallback.get_m_hitPointWorld().y(),
  1200. z: rayCallback.get_m_hitPointWorld().z(),
  1201. }
  1202. );
  1203. this._raycastResult.calculateHitDistance();
  1204. }
  1205. return this._raycastResult;
  1206. }
  1207. }