diff --git a/src/core/body.js b/src/core/body.js index 62515704..ddeb5627 100644 --- a/src/core/body.js +++ b/src/core/body.js @@ -363,6 +363,17 @@ scratch.done(); }, + /** + * Body#onStep() -> this + * + * Intended to be overridden by subclasses. Called when the associated + * world is stepped. + **/ + onStep: function() { + // Override this function to update any associated graphic + return this; + }, + /** * Body#setWorld( world ) -> this * - world (Object): The world (or null) @@ -372,15 +383,21 @@ * Usually this is called internally. Shouldn't be a need to call this yourself usually. **/ setWorld: function( world ){ - - if ( this.disconnect && this._world ){ - this.disconnect( this._world ); + if(this._world) { + this._world.off("step", this.onStep); + if ( this.disconnect ){ + this.disconnect( this._world ); + } } this._world = world; - if ( this.connect && world ){ - this.connect( world ); + if(this._world) { + this._world.on("step", this.onStep, this); + + if ( this.connect ){ + this.connect( this._world ); + } } return this; diff --git a/src/core/world.js b/src/core/world.js index d13a82e1..36991f2a 100644 --- a/src/core/world.js +++ b/src/core/world.js @@ -641,6 +641,27 @@ this._integrator.integrate( this._bodies, dt ); }, + /** chainable + * Physics.world#stepDelta( dt ) -> this + * - dt (Number): Delta time in ms + * + * Step the world BY a specified change in time + */ + stepDelta: function(dt) { + // If paused or at the first step, calculate the appropriate time + if ( this._paused || this._animTime === undefined ){ + this._animTime = this._animTime || Physics.util.ticker.now(); + } + + // Do nothing while paused + if(this._paused) { + return this; + } + + // Otherwise increment the current animation time and step normally + return this.step(this._animTime.valueOf() + dt); + }, + /** chainable * Physics.world#step( [now] ) -> this * - now (Number): Current unix timestamp diff --git a/src/util/helpers.js b/src/util/helpers.js index 42b25481..48cfed90 100644 --- a/src/util/helpers.js +++ b/src/util/helpers.js @@ -38,6 +38,22 @@ Physics.util.clearArray = function clearArray(arr){ return arr; }; +/** + * Physics.util.rad2deg(radians) -> Number + * radians (Number): Radians to convert to degrees + */ +Physics.util.rad2deg = function rad2deg(radians) { + return radians * (180/3.14159265); +}; + +/** + * Physics.util.deg2rad(radians) -> Number + * degrees (Number): Degrees to convert to radians + */ +Physics.util.deg2rad = function deg2rad(degrees) { + return degrees * (3.14159265/180); +}; + /** * Physics.util.throttle( fn, delay ) -> Function * - fn (Function): The function to throttle