physicsShape.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. import { PhysicsShapeType } from "./IPhysicsEnginePlugin.js";
  2. import { Matrix, Vector3, Quaternion, TmpVectors } from "../../Maths/math.vector.js";
  3. /**
  4. * PhysicsShape class.
  5. * This class is useful for creating a physics shape that can be used in a physics engine.
  6. * A Physic Shape determine how collision are computed. It must be attached to a body.
  7. */
  8. export class PhysicsShape {
  9. /**
  10. * Constructs a new physics shape.
  11. * @param options The options for the physics shape. These are:
  12. * * type: The type of the shape. This can be one of the following: SPHERE, BOX, CAPSULE, CYLINDER, CONVEX_HULL, MESH, HEIGHTFIELD, CONTAINER
  13. * * parameters: The parameters of the shape.
  14. * * pluginData: The plugin data of the shape. This is used if you already have a reference to the object on the plugin side.
  15. * You need to specify either type or pluginData.
  16. * @param scene The scene the shape belongs to.
  17. *
  18. * This code is useful for creating a new physics shape with the given type, options, and scene.
  19. * It also checks that the physics engine and plugin version are correct.
  20. * If not, it throws an error. This ensures that the shape is created with the correct parameters and is compatible with the physics engine.
  21. */
  22. constructor(options, scene) {
  23. /**
  24. * V2 Physics plugin private data for single shape
  25. */
  26. this._pluginData = undefined;
  27. this._isTrigger = false;
  28. this._isDisposed = false;
  29. if (!scene) {
  30. return;
  31. }
  32. const physicsEngine = scene.getPhysicsEngine();
  33. if (!physicsEngine) {
  34. throw new Error("No Physics Engine available.");
  35. }
  36. if (physicsEngine.getPluginVersion() != 2) {
  37. throw new Error("Plugin version is incorrect. Expected version 2.");
  38. }
  39. const physicsPlugin = physicsEngine.getPhysicsPlugin();
  40. if (!physicsPlugin) {
  41. throw new Error("No Physics Plugin available.");
  42. }
  43. this._physicsPlugin = physicsPlugin;
  44. if (options.pluginData !== undefined && options.pluginData !== null) {
  45. this._pluginData = options.pluginData;
  46. this._type = this._physicsPlugin.getShapeType(this);
  47. }
  48. else if (options.type !== undefined && options.type !== null) {
  49. this._type = options.type;
  50. const parameters = options.parameters ?? {};
  51. this._physicsPlugin.initShape(this, options.type, parameters);
  52. }
  53. }
  54. /**
  55. * Returns the string "PhysicsShape".
  56. * @returns "PhysicsShape"
  57. */
  58. getClassName() {
  59. return "PhysicsShape";
  60. }
  61. /**
  62. * Returns the type of the physics shape.
  63. * @returns The type of the physics shape.
  64. */
  65. get type() {
  66. return this._type;
  67. }
  68. /**
  69. * Set the membership mask of a shape. This is a bitfield of arbitrary
  70. * "categories" to which the shape is a member. This is used in combination
  71. * with the collide mask to determine if this shape should collide with
  72. * another.
  73. *
  74. * @param membershipMask Bitfield of categories of this shape.
  75. */
  76. set filterMembershipMask(membershipMask) {
  77. this._physicsPlugin.setShapeFilterMembershipMask(this, membershipMask);
  78. }
  79. /**
  80. * Get the membership mask of a shape.
  81. * @returns Bitmask of categories which this shape is a member of.
  82. */
  83. get filterMembershipMask() {
  84. return this._physicsPlugin.getShapeFilterMembershipMask(this);
  85. }
  86. /**
  87. * Sets the collide mask of a shape. This is a bitfield of arbitrary
  88. * "categories" to which this shape collides with. Given two shapes,
  89. * the engine will check if the collide mask and membership overlap:
  90. * shapeA.filterMembershipMask & shapeB.filterCollideMask
  91. *
  92. * If this value is zero (i.e. shapeB only collides with categories
  93. * which shapeA is _not_ a member of) then the shapes will not collide.
  94. *
  95. * Note, the engine will also perform the same test with shapeA and
  96. * shapeB swapped; the shapes will not collide if either shape has
  97. * a collideMask which prevents collision with the other shape.
  98. *
  99. * @param collideMask Bitmask of categories this shape should collide with
  100. */
  101. set filterCollideMask(collideMask) {
  102. this._physicsPlugin.setShapeFilterCollideMask(this, collideMask);
  103. }
  104. /**
  105. *
  106. * @returns Bitmask of categories that this shape should collide with
  107. */
  108. get filterCollideMask() {
  109. return this._physicsPlugin.getShapeFilterCollideMask(this);
  110. }
  111. /**
  112. *
  113. * @param material
  114. */
  115. set material(material) {
  116. this._physicsPlugin.setMaterial(this, material);
  117. this._material = material;
  118. }
  119. /**
  120. * Returns the material of the physics shape.
  121. * @returns The material of the physics shape.
  122. */
  123. get material() {
  124. if (!this._material) {
  125. this._material = this._physicsPlugin.getMaterial(this);
  126. }
  127. return this._material;
  128. }
  129. /**
  130. * Sets the density of the physics shape.
  131. * @param density The density of the physics shape.
  132. */
  133. set density(density) {
  134. this._physicsPlugin.setDensity(this, density);
  135. }
  136. /**
  137. * Returns the density of the physics shape.
  138. * @returns The density of the physics shape.
  139. */
  140. get density() {
  141. return this._physicsPlugin.getDensity(this);
  142. }
  143. /**
  144. * Utility to add a child shape to this container,
  145. * automatically computing the relative transform between
  146. * the container shape and the child instance.
  147. *
  148. * @param parentTransform The transform node associated with this shape
  149. * @param newChild The new PhysicsShape to add
  150. * @param childTransform The transform node associated with the child shape
  151. */
  152. addChildFromParent(parentTransform, newChild, childTransform) {
  153. const childToWorld = childTransform.computeWorldMatrix(true);
  154. const parentToWorld = parentTransform.computeWorldMatrix(true);
  155. const childToParent = TmpVectors.Matrix[0];
  156. childToWorld.multiplyToRef(Matrix.Invert(parentToWorld), childToParent);
  157. const translation = TmpVectors.Vector3[0];
  158. const rotation = TmpVectors.Quaternion[0];
  159. const scale = TmpVectors.Vector3[1];
  160. childToParent.decompose(scale, rotation, translation);
  161. this._physicsPlugin.addChild(this, newChild, translation, rotation, scale);
  162. }
  163. /**
  164. * Adds a child shape to a container with an optional transform
  165. * @param newChild The new PhysicsShape to add
  166. * @param translation Optional position of the child shape relative to this shape
  167. * @param rotation Optional rotation of the child shape relative to this shape
  168. * @param scale Optional scale of the child shape relative to this shape
  169. */
  170. addChild(newChild, translation, rotation, scale) {
  171. this._physicsPlugin.addChild(this, newChild, translation, rotation, scale);
  172. }
  173. /**
  174. * Removes a child shape from this shape.
  175. * @param childIndex The index of the child shape to remove
  176. */
  177. removeChild(childIndex) {
  178. this._physicsPlugin.removeChild(this, childIndex);
  179. }
  180. /**
  181. * Returns the number of children of a physics shape.
  182. * @returns The number of children of a physics shape.
  183. */
  184. getNumChildren() {
  185. return this._physicsPlugin.getNumChildren(this);
  186. }
  187. /**
  188. * Returns the bounding box of the physics shape.
  189. * @returns The bounding box of the physics shape.
  190. */
  191. getBoundingBox() {
  192. return this._physicsPlugin.getBoundingBox(this);
  193. }
  194. set isTrigger(isTrigger) {
  195. if (this._isTrigger === isTrigger) {
  196. return;
  197. }
  198. this._isTrigger = isTrigger;
  199. this._physicsPlugin.setTrigger(this, isTrigger);
  200. }
  201. get isTrigger() {
  202. return this._isTrigger;
  203. }
  204. /**
  205. * Dispose the shape and release its associated resources.
  206. */
  207. dispose() {
  208. if (this._isDisposed) {
  209. return;
  210. }
  211. this._physicsPlugin.disposeShape(this);
  212. this._isDisposed = true;
  213. }
  214. }
  215. /**
  216. * Helper object to create a sphere shape
  217. */
  218. export class PhysicsShapeSphere extends PhysicsShape {
  219. /**
  220. * Constructor for the Sphere Shape
  221. * @param center local center of the sphere
  222. * @param radius radius
  223. * @param scene scene to attach to
  224. */
  225. constructor(center, radius, scene) {
  226. super({ type: PhysicsShapeType.SPHERE, parameters: { center: center, radius: radius } }, scene);
  227. }
  228. /**
  229. * Derive an approximate sphere from the mesh.
  230. * @param mesh node from which to derive the sphere shape
  231. * @returns PhysicsShapeSphere
  232. */
  233. static FromMesh(mesh) {
  234. const bounds = mesh.getBoundingInfo();
  235. const centerLocal = bounds.boundingSphere.center;
  236. const he = bounds.boundingBox.extendSize;
  237. const radius = Math.max(he.x, he.y, he.z);
  238. return new PhysicsShapeSphere(centerLocal, radius, mesh.getScene());
  239. }
  240. }
  241. /**
  242. * Helper object to create a capsule shape
  243. */
  244. export class PhysicsShapeCapsule extends PhysicsShape {
  245. /**
  246. *
  247. * @param pointA Starting point that defines the capsule segment
  248. * @param pointB ending point of that same segment
  249. * @param radius radius
  250. * @param scene scene to attach to
  251. */
  252. constructor(pointA, pointB, radius, scene) {
  253. super({ type: PhysicsShapeType.CAPSULE, parameters: { pointA: pointA, pointB: pointB, radius: radius } }, scene);
  254. }
  255. /**
  256. * Derive an approximate capsule from the mesh. Note, this is
  257. * not the optimal bounding capsule.
  258. * @param mesh Node from which to derive a cylinder shape
  259. * @returns Physics Shape Capsule
  260. */
  261. static FromMesh(mesh) {
  262. const boundsLocal = mesh.getBoundingInfo();
  263. const radius = boundsLocal.boundingBox.extendSize.x;
  264. const pointFromCenter = new Vector3(0, boundsLocal.boundingBox.extendSize.y - radius, 0);
  265. const pointA = boundsLocal.boundingBox.center.add(pointFromCenter);
  266. const pointB = boundsLocal.boundingBox.center.subtract(pointFromCenter);
  267. return new PhysicsShapeCapsule(pointA, pointB, radius, mesh.getScene());
  268. }
  269. }
  270. /**
  271. * Helper object to create a cylinder shape
  272. */
  273. export class PhysicsShapeCylinder extends PhysicsShape {
  274. /**
  275. *
  276. * @param pointA Starting point that defines the cylinder segment
  277. * @param pointB ending point of that same segment
  278. * @param radius radius
  279. * @param scene scene to attach to
  280. */
  281. constructor(pointA, pointB, radius, scene) {
  282. super({ type: PhysicsShapeType.CYLINDER, parameters: { pointA: pointA, pointB: pointB, radius: radius } }, scene);
  283. }
  284. /**
  285. * Derive an approximate cylinder from the mesh. Note, this is
  286. * not the optimal bounding cylinder.
  287. * @param mesh Node from which to derive a cylinder shape
  288. * @returns Physics Shape Cylinder
  289. */
  290. static FromMesh(mesh) {
  291. const boundsLocal = mesh.getBoundingInfo();
  292. const radius = boundsLocal.boundingBox.extendSize.x;
  293. const pointFromCenter = new Vector3(0, boundsLocal.boundingBox.extendSize.y, 0);
  294. const pointA = boundsLocal.boundingBox.center.add(pointFromCenter);
  295. const pointB = boundsLocal.boundingBox.center.subtract(pointFromCenter);
  296. return new PhysicsShapeCylinder(pointA, pointB, radius, mesh.getScene());
  297. }
  298. }
  299. /**
  300. * Helper object to create a box shape
  301. */
  302. export class PhysicsShapeBox extends PhysicsShape {
  303. /**
  304. *
  305. * @param center local center of the box
  306. * @param rotation local orientation
  307. * @param extents size of the box in each direction
  308. * @param scene scene to attach to
  309. */
  310. constructor(center, rotation, extents, scene) {
  311. super({ type: PhysicsShapeType.BOX, parameters: { center: center, rotation: rotation, extents: extents } }, scene);
  312. }
  313. /**
  314. *
  315. * @param mesh
  316. * @returns PhysicsShapeBox
  317. */
  318. static FromMesh(mesh) {
  319. const bounds = mesh.getBoundingInfo();
  320. const centerLocal = bounds.boundingBox.center;
  321. const extents = bounds.boundingBox.extendSize.scale(2.0); //<todo.eoin extendSize seems to really be half-extents?
  322. return new PhysicsShapeBox(centerLocal, Quaternion.Identity(), extents, mesh.getScene());
  323. }
  324. }
  325. /**
  326. * Helper object to create a convex hull shape
  327. */
  328. export class PhysicsShapeConvexHull extends PhysicsShape {
  329. /**
  330. *
  331. * @param mesh the mesh to be used as topology infos for the convex hull
  332. * @param scene scene to attach to
  333. */
  334. constructor(mesh, scene) {
  335. super({ type: PhysicsShapeType.CONVEX_HULL, parameters: { mesh: mesh } }, scene);
  336. }
  337. }
  338. /**
  339. * Helper object to create a mesh shape
  340. */
  341. export class PhysicsShapeMesh extends PhysicsShape {
  342. /**
  343. *
  344. * @param mesh the mesh topology that will be used to create the shape
  345. * @param scene scene to attach to
  346. */
  347. constructor(mesh, scene) {
  348. super({ type: PhysicsShapeType.MESH, parameters: { mesh: mesh } }, scene);
  349. }
  350. }
  351. /**
  352. * A shape container holds a variable number of shapes. Use AddChild to append to newly created parent container.
  353. */
  354. export class PhysicsShapeContainer extends PhysicsShape {
  355. /**
  356. * Constructor of the Shape container
  357. * @param scene scene to attach to
  358. */
  359. constructor(scene) {
  360. super({ type: PhysicsShapeType.CONTAINER, parameters: {} }, scene);
  361. }
  362. }
  363. //# sourceMappingURL=physicsShape.js.map