@@ -28,8 +28,10 @@ var SAT = {};
2828
2929 if ( prevCol ) {
3030 // estimate total motion
31- var motion = bodyA . speed * bodyA . speed + bodyA . angularSpeed * bodyA . angularSpeed
32- + bodyB . speed * bodyB . speed + bodyB . angularSpeed * bodyB . angularSpeed ;
31+ var parentA = bodyA . parent ,
32+ parentB = bodyB . parent ,
33+ motion = parentA . speed * parentA . speed + parentA . angularSpeed * parentA . angularSpeed
34+ + parentB . speed * parentB . speed + parentB . angularSpeed * parentB . angularSpeed ;
3335
3436 // we may be able to (partially) reuse collision result
3537 // but only safe if collision was resting
@@ -113,7 +115,7 @@ var SAT = {};
113115 if ( Vertices . contains ( bodyA . vertices , verticesB [ 0 ] ) )
114116 supports . push ( verticesB [ 0 ] ) ;
115117
116- if ( Vertices . contains ( bodyA . vertices , verticesB [ 1 ] ) )
118+ if ( verticesB [ 1 ] && Vertices . contains ( bodyA . vertices , verticesB [ 1 ] ) )
117119 supports . push ( verticesB [ 1 ] ) ;
118120
119121 // find the supports from bodyA that are inside bodyB
@@ -123,12 +125,12 @@ var SAT = {};
123125 if ( Vertices . contains ( bodyB . vertices , verticesA [ 0 ] ) )
124126 supports . push ( verticesA [ 0 ] ) ;
125127
126- if ( supports . length < 2 && Vertices . contains ( bodyB . vertices , verticesA [ 1 ] ) )
128+ if ( verticesA [ 1 ] && supports . length < 2 && Vertices . contains ( bodyB . vertices , verticesA [ 1 ] ) )
127129 supports . push ( verticesA [ 1 ] ) ;
128130 }
129131
130132 // account for the edge case of overlapping but no vertex containment
131- if ( supports . length < 2 )
133+ if ( supports . length < 1 )
132134 supports = [ verticesB [ 0 ] ] ;
133135
134136 collision . supports = supports ;
@@ -220,8 +222,8 @@ var SAT = {};
220222 bodyAPosition = bodyA . position ,
221223 distance ,
222224 vertex ,
223- vertexA = vertices [ 0 ] ,
224- vertexB = vertices [ 1 ] ;
225+ vertexA ,
226+ vertexB ;
225227
226228 // find closest vertex on bodyB
227229 for ( var i = 0 ; i < vertices . length ; i ++ ) {
@@ -244,15 +246,21 @@ var SAT = {};
244246 nearestDistance = - Vector . dot ( normal , vertexToBody ) ;
245247 vertexB = vertex ;
246248
247- var nextIndex = ( vertexA . index + 1 ) % vertices . length ;
248- vertex = vertices [ nextIndex ] ;
249- vertexToBody . x = vertex . x - bodyAPosition . x ;
250- vertexToBody . y = vertex . y - bodyAPosition . y ;
251- distance = - Vector . dot ( normal , vertexToBody ) ;
252- if ( distance < nearestDistance ) {
253- vertexB = vertex ;
249+ // if the closest vertex is internal, we can't use the next connected vertex
250+ if ( ! vertexA . isInternal ) {
251+ var nextIndex = ( vertexA . index + 1 ) % vertices . length ;
252+ vertex = vertices [ nextIndex ] ;
253+ vertexToBody . x = vertex . x - bodyAPosition . x ;
254+ vertexToBody . y = vertex . y - bodyAPosition . y ;
255+ distance = - Vector . dot ( normal , vertexToBody ) ;
256+ if ( distance < nearestDistance ) {
257+ vertexB = vertex ;
258+ }
254259 }
255260
261+ if ( ! vertexB )
262+ return [ vertexA ] ;
263+
256264 return [ vertexA , vertexB ] ;
257265 } ;
258266
0 commit comments