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

Commit 717ec8c

Browse files
rebuild
1 parent a5b93b0 commit 717ec8c

31 files changed

+1415
-596
lines changed

dist/behaviors/attractor.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* PhysicsJS v0.7.0 - 2014-12-04
2+
* PhysicsJS v0.7.0 - 2014-12-08
33
* A modular, extendable, and easy-to-use physics engine for javascript
44
* http://wellcaffeinated.net/PhysicsJS
55
*

dist/behaviors/body-collision-detection.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* PhysicsJS v0.7.0 - 2014-12-04
2+
* PhysicsJS v0.7.0 - 2014-12-08
33
* A modular, extendable, and easy-to-use physics engine for javascript
44
* http://wellcaffeinated.net/PhysicsJS
55
*
@@ -149,11 +149,8 @@
149149
bodyB: bodyB
150150
};
151151

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

158155
// first get the min distance of between core objects
159156
support.useCore = true;
@@ -243,7 +240,7 @@
243240
};
244241

245242
/*
246-
* checkPair( bodyA, bodyB ) -> Object
243+
* checkPair( bodyA, bodyB[, disp] ) -> Object
247244
* - bodyA (Object): First body
248245
* - bodyB (Object): Second body
249246
* + (Object): Collision result

dist/behaviors/body-impulse-response.js

Lines changed: 76 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* PhysicsJS v0.7.0 - 2014-12-04
2+
* PhysicsJS v0.7.0 - 2014-12-08
33
* A modular, extendable, and easy-to-use physics engine for javascript
44
* http://wellcaffeinated.net/PhysicsJS
55
*
@@ -45,6 +45,26 @@
4545
,forceWakeupAboveOverlapThreshold: true
4646
};
4747

48+
function getUid( b ){
49+
return b.uid;
50+
}
51+
52+
function clampMTV( totalV, mtv, into ){
53+
54+
var m, n;
55+
n = mtv.norm();
56+
m = n - totalV.proj( mtv );
57+
m = Math.max( 0, Math.min( n, m ) );
58+
59+
if ( n === 0 ){
60+
into.zero();
61+
} else {
62+
into.clone( mtv ).mult( m/n );
63+
}
64+
65+
return into;
66+
}
67+
4868
return {
4969

5070
// extended
@@ -53,6 +73,8 @@
5373
parent.init.call( this );
5474
this.options.defaults( defaults );
5575
this.options( options );
76+
77+
this._bodyList = [];
5678
},
5779

5880
// no applyTo method
@@ -134,38 +156,34 @@
134156
,impulse
135157
,sign
136158
,max
159+
,ratio
137160
,inContact = contact
138161
;
139162

140163
if ( contact ){
141-
if ( mtv.normSq() < this.options.mtvThreshold ){
142-
mtv.mult( this.options.bodyExtractDropoff );
143-
} else if ( this.options.forceWakeupAboveOverlapThreshold ) {
144-
// wake up bodies if necessary
145-
bodyA.sleep( false );
146-
bodyB.sleep( false );
147-
}
148164

149165
if ( fixedA ){
150166

151-
// extract bodies
152-
bodyB.state.pos.vadd( mtv );
153-
bodyB.state.old.pos.vadd( mtv );
167+
clampMTV( bodyB._mtvTotal, mtv, tmp );
168+
bodyB._mtvTotal.vadd( tmp );
154169

155170
} else if ( fixedB ){
156171

157-
// extract bodies
158-
bodyA.state.pos.vsub( mtv );
159-
bodyA.state.old.pos.vsub( mtv );
172+
clampMTV( bodyA._mtvTotal, mtv.negate(), tmp );
173+
bodyA._mtvTotal.vadd( tmp );
174+
mtv.negate();
160175

161176
} else {
162177

163-
// extract bodies
164-
mtv.mult( 0.5 );
165-
bodyA.state.pos.vsub( mtv );
166-
bodyA.state.old.pos.vsub( mtv );
167-
bodyB.state.pos.vadd( mtv );
168-
bodyB.state.old.pos.vadd( mtv );
178+
ratio = 0.5; //bodyA.mass / ( bodyA.mass + bodyB.mass );
179+
mtv.mult( ratio );
180+
clampMTV( bodyB._mtvTotal, mtv, tmp );
181+
bodyB._mtvTotal.vadd( tmp );
182+
183+
mtv.clone( mtrans ).mult( ratio - 1 );
184+
clampMTV( bodyA._mtvTotal, mtv, tmp );
185+
bodyA._mtvTotal.vadd( tmp );
186+
169187
}
170188
}
171189

@@ -264,6 +282,14 @@
264282
scratch.done();
265283
},
266284

285+
// internal
286+
_pushUniq: function( body ){
287+
var idx = Physics.util.sortedIndex( this._bodyList, body, getUid );
288+
if ( this._bodyList[ idx ] !== body ){
289+
this._bodyList.splice( idx, 0, body );
290+
}
291+
},
292+
267293
/** internal
268294
* BodyImpulseResponseBehavior#respond( data )
269295
* - data (Object): event data
@@ -274,12 +300,22 @@
274300

275301
var self = this
276302
,col
277-
,collisions = Physics.util.shuffle(data.collisions)
303+
,collisions = data.collisions// Physics.util.shuffle(data.collisions)
304+
,i,l,b
278305
;
279306

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

282309
col = collisions[ i ];
310+
// add bodies to list for later
311+
this._pushUniq( col.bodyA );
312+
this._pushUniq( col.bodyB );
313+
// ensure they have mtv stat vectors
314+
col.bodyA._mtvTotal = col.bodyA._mtvTotal || new Physics.vector();
315+
col.bodyB._mtvTotal = col.bodyB._mtvTotal || new Physics.vector();
316+
col.bodyA._oldmtvTotal = col.bodyA._oldmtvTotal || new Physics.vector();
317+
col.bodyB._oldmtvTotal = col.bodyB._oldmtvTotal || new Physics.vector();
318+
283319
self.collideBodies(
284320
col.bodyA,
285321
col.bodyB,
@@ -289,6 +325,24 @@
289325
col.collidedPreviously
290326
);
291327
}
328+
329+
// apply mtv vectors from the average mtv vector
330+
for ( i = 0, l = this._bodyList.length; i < l; ++i ){
331+
b = this._bodyList.pop();
332+
// clampMTV( b._oldmtvTotal, b._mtvTotal, b._mtvTotal );
333+
334+
if ( b._mtvTotal.normSq() < this.options.mtvThreshold ){
335+
b._mtvTotal.mult( this.options.bodyExtractDropoff );
336+
} else if ( this.options.forceWakeupAboveOverlapThreshold ) {
337+
// wake up bodies if necessary
338+
b.sleep( false );
339+
}
340+
341+
b.state.pos.vadd( b._mtvTotal );
342+
b.state.old.pos.vadd( b._mtvTotal );
343+
b._oldmtvTotal.swap( b._mtvTotal );
344+
b._mtvTotal.zero();
345+
}
292346
}
293347
};
294348
});

dist/behaviors/constant-acceleration.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* PhysicsJS v0.7.0 - 2014-12-04
2+
* PhysicsJS v0.7.0 - 2014-12-08
33
* A modular, extendable, and easy-to-use physics engine for javascript
44
* http://wellcaffeinated.net/PhysicsJS
55
*

dist/behaviors/edge-collision-detection.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* PhysicsJS v0.7.0 - 2014-12-04
2+
* PhysicsJS v0.7.0 - 2014-12-08
33
* A modular, extendable, and easy-to-use physics engine for javascript
44
* http://wellcaffeinated.net/PhysicsJS
55
*

dist/behaviors/interactive.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* PhysicsJS v0.7.0 - 2014-12-04
2+
* PhysicsJS v0.7.0 - 2014-12-08
33
* A modular, extendable, and easy-to-use physics engine for javascript
44
* http://wellcaffeinated.net/PhysicsJS
55
*

dist/behaviors/newtonian.js

Lines changed: 75 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* PhysicsJS v0.7.0 - 2014-12-04
2+
* PhysicsJS v0.7.0 - 2014-12-08
33
* A modular, extendable, and easy-to-use physics engine for javascript
44
* http://wellcaffeinated.net/PhysicsJS
55
*
@@ -16,7 +16,7 @@
1616
}
1717
}(this, function (Physics) {
1818
'use strict';
19-
/**
19+
/**
2020
* class NewtonianBehavior < Behavior
2121
*
2222
* `Physics.behavior('newtonian')`.
@@ -54,42 +54,95 @@
5454
});
5555
this.options( options );
5656
},
57-
57+
58+
calcPotential: function( posA, posB, out ){
59+
60+
var strength = this.options.strength
61+
,minDistSq = this._minDistSq
62+
,maxDistSq = this._maxDistSq
63+
,normsq
64+
,g
65+
,pos
66+
;
67+
68+
pos = out || new Physics.vector();
69+
70+
// clone the position
71+
pos.clone( posB ).vsub( posA );
72+
// get the square distance
73+
normsq = pos.normSq();
74+
75+
if (normsq > minDistSq && normsq < maxDistSq){
76+
77+
g = strength / normsq;
78+
return pos.normalize().mult( g );
79+
}
80+
81+
return pos.zero();
82+
},
83+
5884
// extended
5985
behave: function( data ){
6086

6187
var bodies = this.getTargets()
6288
,body
6389
,other
64-
,strength = this.options.strength
65-
,minDistSq = this._minDistSq
66-
,maxDistSq = this._maxDistSq
6790
,scratch = Physics.scratchpad()
68-
,pos = scratch.vector()
69-
,normsq
70-
,g
91+
,potential = scratch.vector()
92+
,comp
93+
,bodyA
94+
,bodyB
95+
,posA = scratch.vector()
96+
,posB = scratch.vector()
97+
,i, j, k, m, l, ll, lll
7198
;
7299

73-
for ( var j = 0, l = bodies.length; j < l; j++ ){
74-
100+
for ( j = 0, l = bodies.length; j < l; j++ ){
101+
75102
body = bodies[ j ];
76103

77-
for ( var i = j + 1; i < l; i++ ){
78-
104+
for ( i = j + 1; i < l; i++ ){
105+
79106
other = bodies[ i ];
80-
// clone the position
81-
pos.clone( other.state.pos );
82-
pos.vsub( body.state.pos );
83-
// get the square distance
84-
normsq = pos.normSq();
85107

86-
if (normsq > minDistSq && normsq < maxDistSq){
108+
if ( body.name === 'compound' ){
109+
comp = body;
110+
} else if ( other.name === 'compound' ){
111+
comp = other;
112+
other = body;
113+
}
114+
115+
if ( comp ){
116+
if ( other.name === 'compound' ){
117+
for ( k = 0, ll = comp.children.length; k < ll; k++ ){
118+
bodyA = comp.children[ k ];
119+
comp.toWorldCoords( posA.clone( bodyA.state.pos ).vadd( comp.offset ) );
120+
for ( m = 0, lll = other.children.length; m < lll; m++ ){
121+
bodyB = other.children[ m ];
122+
other.toWorldCoords( posB.clone( bodyB.state.pos ).vadd( other.offset ) );
123+
this.calcPotential( posA, posB, potential );
124+
comp.accelerate( potential.mult( bodyB.mass ) );
125+
other.accelerate( potential.mult( bodyA.mass/bodyB.mass ).negate() );
126+
}
127+
}
128+
} else {
129+
for ( k = 0, ll = comp.children.length; k < ll; k++ ){
130+
bodyA = comp.children[ k ];
131+
comp.toWorldCoords( posA.clone( bodyA.state.pos ).vadd( comp.offset ) );
132+
this.calcPotential( posA, other.state.pos, potential );
133+
comp.accelerate( potential.mult( other.mass ) );
134+
other.accelerate( potential.mult( bodyA.mass/other.mass ).negate() );
135+
}
136+
}
87137

88-
g = strength / normsq;
138+
} else {
89139

90-
body.accelerate( pos.normalize().mult( g * other.mass ) );
91-
other.accelerate( pos.mult( body.mass/other.mass ).negate() );
140+
this.calcPotential( body.state.pos, other.state.pos, potential );
141+
body.accelerate( potential.mult( other.mass ) );
142+
other.accelerate( potential.mult( body.mass/other.mass ).negate() );
92143
}
144+
145+
comp = null;
93146
}
94147
}
95148

dist/behaviors/sweep-prune.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* PhysicsJS v0.7.0 - 2014-12-04
2+
* PhysicsJS v0.7.0 - 2014-12-08
33
* A modular, extendable, and easy-to-use physics engine for javascript
44
* http://wellcaffeinated.net/PhysicsJS
55
*

dist/behaviors/verlet-constraints.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* PhysicsJS v0.7.0 - 2014-12-04
2+
* PhysicsJS v0.7.0 - 2014-12-08
33
* A modular, extendable, and easy-to-use physics engine for javascript
44
* http://wellcaffeinated.net/PhysicsJS
55
*

dist/bodies/circle.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* PhysicsJS v0.7.0 - 2014-12-04
2+
* PhysicsJS v0.7.0 - 2014-12-08
33
* A modular, extendable, and easy-to-use physics engine for javascript
44
* http://wellcaffeinated.net/PhysicsJS
55
*

0 commit comments

Comments
 (0)