@@ -28,10 +28,9 @@ var Vector = require('../geometry/Vector');
2828 overlapBA ,
2929 minOverlap ,
3030 collision ,
31- prevCol = previousCollision ,
3231 canReusePrevCol = false ;
3332
34- if ( prevCol ) {
33+ if ( previousCollision ) {
3534 // estimate total motion
3635 var parentA = bodyA . parent ,
3736 parentB = bodyB . parent ,
@@ -40,20 +39,20 @@ var Vector = require('../geometry/Vector');
4039
4140 // we may be able to (partially) reuse collision result
4241 // but only safe if collision was resting
43- canReusePrevCol = prevCol && prevCol . collided && motion < 0.2 ;
42+ canReusePrevCol = previousCollision && previousCollision . collided && motion < 0.2 ;
4443
4544 // reuse collision object
46- collision = prevCol ;
45+ collision = previousCollision ;
4746 } else {
4847 collision = { collided : false , bodyA : bodyA , bodyB : bodyB } ;
4948 }
5049
51- if ( prevCol && canReusePrevCol ) {
50+ if ( previousCollision && canReusePrevCol ) {
5251 // if we can reuse the collision result
5352 // we only need to test the previously found axis
5453 var axisBodyA = collision . axisBody ,
5554 axisBodyB = axisBodyA === bodyA ? bodyB : bodyA ,
56- axes = [ axisBodyA . axes [ prevCol . axisNumber ] ] ;
55+ axes = [ axisBodyA . axes [ previousCollision . axisNumber ] ] ;
5756
5857 minOverlap = _overlapAxes ( axisBodyA . vertices , axisBodyB . vertices , axes ) ;
5958 collision . reused = true ;
@@ -94,7 +93,6 @@ var Vector = require('../geometry/Vector');
9493 collision . bodyA = bodyA . id < bodyB . id ? bodyA : bodyB ;
9594 collision . bodyB = bodyA . id < bodyB . id ? bodyB : bodyA ;
9695 collision . collided = true ;
97- collision . normal = minOverlap . axis ;
9896 collision . depth = minOverlap . overlap ;
9997 collision . parentA = collision . bodyA . parent ;
10098 collision . parentB = collision . bodyB . parent ;
@@ -103,20 +101,27 @@ var Vector = require('../geometry/Vector');
103101 bodyB = collision . bodyB ;
104102
105103 // ensure normal is facing away from bodyA
106- if ( Vector . dot ( collision . normal , Vector . sub ( bodyB . position , bodyA . position ) ) > 0 )
107- collision . normal = Vector . neg ( collision . normal ) ;
104+ if ( Vector . dot ( minOverlap . axis , Vector . sub ( bodyB . position , bodyA . position ) ) < 0 ) {
105+ collision . normal = {
106+ x : minOverlap . axis . x ,
107+ y : minOverlap . axis . y
108+ } ;
109+ } else {
110+ collision . normal = {
111+ x : - minOverlap . axis . x ,
112+ y : - minOverlap . axis . y
113+ } ;
114+ }
108115
109116 collision . tangent = Vector . perp ( collision . normal ) ;
110117
111- collision . penetration = {
112- x : collision . normal . x * collision . depth ,
113- y : collision . normal . y * collision . depth
114- } ;
118+ collision . penetration = collision . penetration || { } ;
119+ collision . penetration . x = collision . normal . x * collision . depth ;
120+ collision . penetration . y = collision . normal . y * collision . depth ;
115121
116122 // find support points, there is always either exactly one or two
117123 var verticesB = _findSupports ( bodyA , bodyB , collision . normal ) ,
118- supports = collision . supports || [ ] ;
119- supports . length = 0 ;
124+ supports = [ ] ;
120125
121126 // find the supports from bodyB that are inside bodyA
122127 if ( Vertices . contains ( bodyA . vertices , verticesB [ 0 ] ) )
0 commit comments