Skip to content
This repository has been archived by the owner on Dec 15, 2019. It is now read-only.

Commit

Permalink
rebuild
Browse files Browse the repository at this point in the history
  • Loading branch information
wellcaffeinated committed Dec 8, 2014
1 parent a5b93b0 commit 717ec8c
Show file tree
Hide file tree
Showing 31 changed files with 1,415 additions and 596 deletions.
2 changes: 1 addition & 1 deletion dist/behaviors/attractor.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* PhysicsJS v0.7.0 - 2014-12-04
* PhysicsJS v0.7.0 - 2014-12-08
* A modular, extendable, and easy-to-use physics engine for javascript
* http://wellcaffeinated.net/PhysicsJS
*
Expand Down
11 changes: 4 additions & 7 deletions dist/behaviors/body-collision-detection.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* PhysicsJS v0.7.0 - 2014-12-04
* PhysicsJS v0.7.0 - 2014-12-08
* A modular, extendable, and easy-to-use physics engine for javascript
* http://wellcaffeinated.net/PhysicsJS
*
Expand Down Expand Up @@ -149,11 +149,8 @@
bodyB: bodyB
};

// figure out how much the bodies moved relative to each other
tmp.clone( bodyA.state.pos ).vsub( bodyA.state.old.pos ).vsub( bodyB.state.pos ).vadd( bodyB.state.old.pos );
inc = Math.abs(tmp.proj( d ));
// let's increment the margin by half this value each iteration
inc = Math.max( 0.5 * inc, 1 );
// inc by 1% of the smallest dim.
inc = 1e-2 * Math.min(dimA || 1, dimB || 1);

// first get the min distance of between core objects
support.useCore = true;
Expand Down Expand Up @@ -243,7 +240,7 @@
};

/*
* checkPair( bodyA, bodyB ) -> Object
* checkPair( bodyA, bodyB[, disp] ) -> Object
* - bodyA (Object): First body
* - bodyB (Object): Second body
* + (Object): Collision result
Expand Down
98 changes: 76 additions & 22 deletions dist/behaviors/body-impulse-response.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* PhysicsJS v0.7.0 - 2014-12-04
* PhysicsJS v0.7.0 - 2014-12-08
* A modular, extendable, and easy-to-use physics engine for javascript
* http://wellcaffeinated.net/PhysicsJS
*
Expand Down Expand Up @@ -45,6 +45,26 @@
,forceWakeupAboveOverlapThreshold: true
};

function getUid( b ){
return b.uid;
}

function clampMTV( totalV, mtv, into ){

var m, n;
n = mtv.norm();
m = n - totalV.proj( mtv );
m = Math.max( 0, Math.min( n, m ) );

if ( n === 0 ){
into.zero();
} else {
into.clone( mtv ).mult( m/n );
}

return into;
}

return {

// extended
Expand All @@ -53,6 +73,8 @@
parent.init.call( this );
this.options.defaults( defaults );
this.options( options );

this._bodyList = [];
},

// no applyTo method
Expand Down Expand Up @@ -134,38 +156,34 @@
,impulse
,sign
,max
,ratio
,inContact = contact
;

if ( contact ){
if ( mtv.normSq() < this.options.mtvThreshold ){
mtv.mult( this.options.bodyExtractDropoff );
} else if ( this.options.forceWakeupAboveOverlapThreshold ) {
// wake up bodies if necessary
bodyA.sleep( false );
bodyB.sleep( false );
}

if ( fixedA ){

// extract bodies
bodyB.state.pos.vadd( mtv );
bodyB.state.old.pos.vadd( mtv );
clampMTV( bodyB._mtvTotal, mtv, tmp );
bodyB._mtvTotal.vadd( tmp );

} else if ( fixedB ){

// extract bodies
bodyA.state.pos.vsub( mtv );
bodyA.state.old.pos.vsub( mtv );
clampMTV( bodyA._mtvTotal, mtv.negate(), tmp );
bodyA._mtvTotal.vadd( tmp );
mtv.negate();

} else {

// extract bodies
mtv.mult( 0.5 );
bodyA.state.pos.vsub( mtv );
bodyA.state.old.pos.vsub( mtv );
bodyB.state.pos.vadd( mtv );
bodyB.state.old.pos.vadd( mtv );
ratio = 0.5; //bodyA.mass / ( bodyA.mass + bodyB.mass );
mtv.mult( ratio );
clampMTV( bodyB._mtvTotal, mtv, tmp );
bodyB._mtvTotal.vadd( tmp );

mtv.clone( mtrans ).mult( ratio - 1 );
clampMTV( bodyA._mtvTotal, mtv, tmp );
bodyA._mtvTotal.vadd( tmp );

}
}

Expand Down Expand Up @@ -264,6 +282,14 @@
scratch.done();
},

// internal
_pushUniq: function( body ){
var idx = Physics.util.sortedIndex( this._bodyList, body, getUid );
if ( this._bodyList[ idx ] !== body ){
this._bodyList.splice( idx, 0, body );
}
},

/** internal
* BodyImpulseResponseBehavior#respond( data )
* - data (Object): event data
Expand All @@ -274,12 +300,22 @@

var self = this
,col
,collisions = Physics.util.shuffle(data.collisions)
,collisions = data.collisions// Physics.util.shuffle(data.collisions)
,i,l,b
;

for ( var i = 0, l = collisions.length; i < l; ++i ){
for ( i = 0, l = collisions.length; i < l; ++i ){

col = collisions[ i ];
// add bodies to list for later
this._pushUniq( col.bodyA );
this._pushUniq( col.bodyB );
// ensure they have mtv stat vectors
col.bodyA._mtvTotal = col.bodyA._mtvTotal || new Physics.vector();
col.bodyB._mtvTotal = col.bodyB._mtvTotal || new Physics.vector();
col.bodyA._oldmtvTotal = col.bodyA._oldmtvTotal || new Physics.vector();
col.bodyB._oldmtvTotal = col.bodyB._oldmtvTotal || new Physics.vector();

self.collideBodies(
col.bodyA,
col.bodyB,
Expand All @@ -289,6 +325,24 @@
col.collidedPreviously
);
}

// apply mtv vectors from the average mtv vector
for ( i = 0, l = this._bodyList.length; i < l; ++i ){
b = this._bodyList.pop();
// clampMTV( b._oldmtvTotal, b._mtvTotal, b._mtvTotal );

if ( b._mtvTotal.normSq() < this.options.mtvThreshold ){
b._mtvTotal.mult( this.options.bodyExtractDropoff );
} else if ( this.options.forceWakeupAboveOverlapThreshold ) {
// wake up bodies if necessary
b.sleep( false );
}

b.state.pos.vadd( b._mtvTotal );
b.state.old.pos.vadd( b._mtvTotal );
b._oldmtvTotal.swap( b._mtvTotal );
b._mtvTotal.zero();
}
}
};
});
Expand Down
2 changes: 1 addition & 1 deletion dist/behaviors/constant-acceleration.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* PhysicsJS v0.7.0 - 2014-12-04
* PhysicsJS v0.7.0 - 2014-12-08
* A modular, extendable, and easy-to-use physics engine for javascript
* http://wellcaffeinated.net/PhysicsJS
*
Expand Down
2 changes: 1 addition & 1 deletion dist/behaviors/edge-collision-detection.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* PhysicsJS v0.7.0 - 2014-12-04
* PhysicsJS v0.7.0 - 2014-12-08
* A modular, extendable, and easy-to-use physics engine for javascript
* http://wellcaffeinated.net/PhysicsJS
*
Expand Down
2 changes: 1 addition & 1 deletion dist/behaviors/interactive.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* PhysicsJS v0.7.0 - 2014-12-04
* PhysicsJS v0.7.0 - 2014-12-08
* A modular, extendable, and easy-to-use physics engine for javascript
* http://wellcaffeinated.net/PhysicsJS
*
Expand Down
97 changes: 75 additions & 22 deletions dist/behaviors/newtonian.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* PhysicsJS v0.7.0 - 2014-12-04
* PhysicsJS v0.7.0 - 2014-12-08
* A modular, extendable, and easy-to-use physics engine for javascript
* http://wellcaffeinated.net/PhysicsJS
*
Expand All @@ -16,7 +16,7 @@
}
}(this, function (Physics) {
'use strict';
/**
/**
* class NewtonianBehavior < Behavior
*
* `Physics.behavior('newtonian')`.
Expand Down Expand Up @@ -54,42 +54,95 @@
});
this.options( options );
},


calcPotential: function( posA, posB, out ){

var strength = this.options.strength
,minDistSq = this._minDistSq
,maxDistSq = this._maxDistSq
,normsq
,g
,pos
;

pos = out || new Physics.vector();

// clone the position
pos.clone( posB ).vsub( posA );
// get the square distance
normsq = pos.normSq();

if (normsq > minDistSq && normsq < maxDistSq){

g = strength / normsq;
return pos.normalize().mult( g );
}

return pos.zero();
},

// extended
behave: function( data ){

var bodies = this.getTargets()
,body
,other
,strength = this.options.strength
,minDistSq = this._minDistSq
,maxDistSq = this._maxDistSq
,scratch = Physics.scratchpad()
,pos = scratch.vector()
,normsq
,g
,potential = scratch.vector()
,comp
,bodyA
,bodyB
,posA = scratch.vector()
,posB = scratch.vector()
,i, j, k, m, l, ll, lll
;

for ( var j = 0, l = bodies.length; j < l; j++ ){
for ( j = 0, l = bodies.length; j < l; j++ ){

body = bodies[ j ];

for ( var i = j + 1; i < l; i++ ){
for ( i = j + 1; i < l; i++ ){

other = bodies[ i ];
// clone the position
pos.clone( other.state.pos );
pos.vsub( body.state.pos );
// get the square distance
normsq = pos.normSq();

if (normsq > minDistSq && normsq < maxDistSq){
if ( body.name === 'compound' ){
comp = body;
} else if ( other.name === 'compound' ){
comp = other;
other = body;
}

if ( comp ){
if ( other.name === 'compound' ){
for ( k = 0, ll = comp.children.length; k < ll; k++ ){
bodyA = comp.children[ k ];
comp.toWorldCoords( posA.clone( bodyA.state.pos ).vadd( comp.offset ) );
for ( m = 0, lll = other.children.length; m < lll; m++ ){
bodyB = other.children[ m ];
other.toWorldCoords( posB.clone( bodyB.state.pos ).vadd( other.offset ) );
this.calcPotential( posA, posB, potential );
comp.accelerate( potential.mult( bodyB.mass ) );
other.accelerate( potential.mult( bodyA.mass/bodyB.mass ).negate() );
}
}
} else {
for ( k = 0, ll = comp.children.length; k < ll; k++ ){
bodyA = comp.children[ k ];
comp.toWorldCoords( posA.clone( bodyA.state.pos ).vadd( comp.offset ) );
this.calcPotential( posA, other.state.pos, potential );
comp.accelerate( potential.mult( other.mass ) );
other.accelerate( potential.mult( bodyA.mass/other.mass ).negate() );
}
}

g = strength / normsq;
} else {

body.accelerate( pos.normalize().mult( g * other.mass ) );
other.accelerate( pos.mult( body.mass/other.mass ).negate() );
this.calcPotential( body.state.pos, other.state.pos, potential );
body.accelerate( potential.mult( other.mass ) );
other.accelerate( potential.mult( body.mass/other.mass ).negate() );
}

comp = null;
}
}

Expand Down
2 changes: 1 addition & 1 deletion dist/behaviors/sweep-prune.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* PhysicsJS v0.7.0 - 2014-12-04
* PhysicsJS v0.7.0 - 2014-12-08
* A modular, extendable, and easy-to-use physics engine for javascript
* http://wellcaffeinated.net/PhysicsJS
*
Expand Down
2 changes: 1 addition & 1 deletion dist/behaviors/verlet-constraints.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* PhysicsJS v0.7.0 - 2014-12-04
* PhysicsJS v0.7.0 - 2014-12-08
* A modular, extendable, and easy-to-use physics engine for javascript
* http://wellcaffeinated.net/PhysicsJS
*
Expand Down
2 changes: 1 addition & 1 deletion dist/bodies/circle.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* PhysicsJS v0.7.0 - 2014-12-04
* PhysicsJS v0.7.0 - 2014-12-08
* A modular, extendable, and easy-to-use physics engine for javascript
* http://wellcaffeinated.net/PhysicsJS
*
Expand Down
2 changes: 1 addition & 1 deletion dist/bodies/compound.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* PhysicsJS v0.7.0 - 2014-12-04
* PhysicsJS v0.7.0 - 2014-12-08
* A modular, extendable, and easy-to-use physics engine for javascript
* http://wellcaffeinated.net/PhysicsJS
*
Expand Down
2 changes: 1 addition & 1 deletion dist/bodies/convex-polygon.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* PhysicsJS v0.7.0 - 2014-12-04
* PhysicsJS v0.7.0 - 2014-12-08
* A modular, extendable, and easy-to-use physics engine for javascript
* http://wellcaffeinated.net/PhysicsJS
*
Expand Down
2 changes: 1 addition & 1 deletion dist/bodies/rectangle.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* PhysicsJS v0.7.0 - 2014-12-04
* PhysicsJS v0.7.0 - 2014-12-08
* A modular, extendable, and easy-to-use physics engine for javascript
* http://wellcaffeinated.net/PhysicsJS
*
Expand Down
Loading

0 comments on commit 717ec8c

Please sign in to comment.