Skip to content

Commit

Permalink
Merge pull request #28 from diarmidmackenzie/fix-infinite-loop-on-imp…
Browse files Browse the repository at this point in the history
…ulse-refactor

Fix infinite loop on impulse
  • Loading branch information
diarmidmackenzie authored Jan 6, 2023
2 parents daf2633 + 7b70bd2 commit bf61f08
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 17 deletions.
2 changes: 1 addition & 1 deletion CannonDriver.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ el.body.applyImpulse(

| event | description |
| ------------- | ------------------------------------------------------------ |
| `body-loaded` | Fired when physics body (`el.body`) has been created. |
| `body-loaded` | Fired when physics body (`el.body`) has been created, and has a shape configured. Also fired if the body's shape is modified later. |
| `collide` | Fired when two objects collide. Touching objects may fire `collide` events frequently. Unavailable with `driver: worker`. |

### Collisions
Expand Down
24 changes: 17 additions & 7 deletions dist/aframe-physics-system.js
Original file line number Diff line number Diff line change
Expand Up @@ -16819,7 +16819,7 @@ var Body = {
this.shouldUpdateWireframe = true;
}

this.isLoaded = true;
this.hasShape = true;
}

this.el.body = this.body;
Expand All @@ -16830,7 +16830,7 @@ var Body = {
this._play();
}

if (this.isLoaded) {
if (this.hasShape) {
this.el.emit('body-loaded', {body: this.el.body});
}
},
Expand Down Expand Up @@ -16861,9 +16861,13 @@ var Body = {

tick: function () {
if (this.shouldUpdateBody) {
this.isLoaded = true;

this._play();

// Calling play will result in the object being re-added to the
// physics system with the updated body / shape data.
// But we mustn't add it twice, so any previously loaded body should be paused first.
this._pause();
this.hasShape = true;
this._play()

this.el.emit('body-loaded', {body: this.el.body});
this.shouldUpdateBody = false;
Expand All @@ -16879,13 +16883,16 @@ var Body = {
* Registers the component with the physics system, if ready.
*/
play: function () {
if (this.isLoaded) this._play();
this._play();
},

/**
* Internal helper to register component with physics system.
*/
_play: function () {

if (!this.hasShape) return;

this.syncToPhysics();
this.system.addComponent(this);
this.system.addBody(this.body);
Expand All @@ -16896,10 +16903,13 @@ var Body = {
* Unregisters the component with the physics system.
*/
pause: function () {
if (this.isLoaded) this._pause();
this._pause();
},

_pause: function () {

if (!this.hasShape) return;

this.system.removeComponent(this);
if (this.body) this.system.removeBody(this.body);
if (this.wireframe) this.el.sceneEl.object3D.remove(this.wireframe);
Expand Down
2 changes: 1 addition & 1 deletion dist/aframe-physics-system.min.js

Large diffs are not rendered by default.

24 changes: 17 additions & 7 deletions src/components/body/body.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ var Body = {
this.shouldUpdateWireframe = true;
}

this.isLoaded = true;
this.hasShape = true;
}

this.el.body = this.body;
Expand All @@ -95,7 +95,7 @@ var Body = {
this._play();
}

if (this.isLoaded) {
if (this.hasShape) {
this.el.emit('body-loaded', {body: this.el.body});
}
},
Expand Down Expand Up @@ -126,9 +126,13 @@ var Body = {

tick: function () {
if (this.shouldUpdateBody) {
this.isLoaded = true;

this._play();

// Calling play will result in the object being re-added to the
// physics system with the updated body / shape data.
// But we mustn't add it twice, so any previously loaded body should be paused first.
this._pause();
this.hasShape = true;
this._play()

this.el.emit('body-loaded', {body: this.el.body});
this.shouldUpdateBody = false;
Expand All @@ -144,13 +148,16 @@ var Body = {
* Registers the component with the physics system, if ready.
*/
play: function () {
if (this.isLoaded) this._play();
this._play();
},

/**
* Internal helper to register component with physics system.
*/
_play: function () {

if (!this.hasShape) return;

this.syncToPhysics();
this.system.addComponent(this);
this.system.addBody(this.body);
Expand All @@ -161,10 +168,13 @@ var Body = {
* Unregisters the component with the physics system.
*/
pause: function () {
if (this.isLoaded) this._pause();
this._pause();
},

_pause: function () {

if (!this.hasShape) return;

this.system.removeComponent(this);
if (this.body) this.system.removeBody(this.body);
if (this.wireframe) this.el.sceneEl.object3D.remove(this.wireframe);
Expand Down
2 changes: 1 addition & 1 deletion tests/physics/body.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ suite('body', function () {
});

test('pause', function () {
component.isLoaded = true;
component.hasShape = true;
component.system = physics;
component.body = el.body = body;
component.pause();
Expand Down

0 comments on commit bf61f08

Please sign in to comment.