Skip to content

Commit a0c09fb

Browse files
Merge pull request #20 from diarmidmackenzie/ammo-perf
Ammo performance improvements
2 parents 78290ee + 9e29823 commit a0c09fb

File tree

5 files changed

+21
-4
lines changed

5 files changed

+21
-4
lines changed

AmmoDriver.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,15 @@ An `ammo-body` component may be added to any entity in a scene. While having onl
134134
The `type` of an ammo body can be one of the following:
135135

136136
- `dynamic`: A freely-moving object. Dynamic bodies have mass, collide with other bodies, bounce or slow during collisions, and fall if gravity is enabled.
137+
137138
- `static`: A fixed-position object. Other bodies may collide with static bodies, but static bodies themselves are unaffected by gravity and collisions. These bodies should typically not be moved after initialization as they cannot impart forces on `dynamic` bodies.
139+
140+
- If you do re-position a static object after initialization, you'll need to explicitly update the physics system with the new position. You can do that like this:
141+
142+
```
143+
this.el.components['ammo-body'].syncToPhysics();
144+
```
145+
138146
- `kinematic`: Like a `static` body, except that they can be moved via updating the position of the entity. Unlike a `static` body, they impart forces on `dynamic` bodies when moved. Useful for animated or remote (networked) objects.
139147
140148
#### Activation States
@@ -252,6 +260,10 @@ Any entity with an `ammo-body` component can also have 1 or more `ammo-shape` co
252260
- `fit: all` – Requires a mesh to exist on the entity. The specified shape will be created to contain all the vertices of the mesh.
253261
- `fit: manual` – Does not require a mesh, however you must specifiy either the `halfExtents` or `sphereRadius` manually. This is not supported for `hull`, `hacd`, `vhacd` and `mesh` types.
254262

263+
Note that in general, `fit: manual` is more performant than `fit: all`. This is because `fit: all` iterates over every point in the geometry to determine a suitable bounding volume, whereas `fit: manual` can just create the shape to the specified parameters. This is particularly important if you are going to be spawning new instances of objects while the physics simulation is ongoing.
264+
265+
Note that there is currently no caching of shapes generated from geometries, so even if you are creating shapes for the same geometry over & over you'll still pay this performance penalty for each new `ammo-shape`.
266+
255267
### `ammo-constraint`
256268
257269
The `ammo-constraint` component is used to bind `ammo-bodies` together using hinges, fixed distances, or fixed attachment points. Note that an `ammo-shape` is not required for `ammo-constraint` to work, however you may get strange results with some constraint types.

dist/aframe-physics-system.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16566,7 +16566,9 @@ let AmmoBody = {
1656616566

1656716567
beforeStep: function() {
1656816568
this._updateShapes();
16569-
if (this.data.type !== TYPE.DYNAMIC) {
16569+
// Note that since static objects don't move,
16570+
// we don't sync them to physics on a routine basis.
16571+
if (this.data.type === TYPE.KINEMATIC) {
1657016572
this.syncToPhysics();
1657116573
}
1657216574
},

dist/aframe-physics-system.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/components/pinboard.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ AFRAME.registerComponent('dynamic-ball', {
6969

7070
if (this.data.physics === "ammo") {
7171
el.setAttribute('ammo-body', 'type:dynamic')
72-
el.setAttribute('ammo-shape', 'type:sphere; fit:all')
72+
// Explicitly specifying a shape is more efficient than auto-fitting.
73+
el.setAttribute('ammo-shape', 'type:sphere; fit:manual; sphereRadius: 0.3')
7374
}
7475
else if (this.data.physics === "cannon") {
7576
// necessary to explicitly specify sphere radius, as async call to

src/components/body/ammo-body.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,9 @@ let AmmoBody = {
356356

357357
beforeStep: function() {
358358
this._updateShapes();
359-
if (this.data.type !== TYPE.DYNAMIC) {
359+
// Note that since static objects don't move,
360+
// we don't sync them to physics on a routine basis.
361+
if (this.data.type === TYPE.KINEMATIC) {
360362
this.syncToPhysics();
361363
}
362364
},

0 commit comments

Comments
 (0)