Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix infinite loop on impulse #28

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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