66
77var CONST = require ( './const' ) ;
88var GetOverlapY = require ( './GetOverlapY' ) ;
9- var IntersectsRect = require ( './IntersectsRect' ) ;
109
1110/**
1211 * Separates two overlapping bodies on the Y-axis (vertically).
@@ -30,36 +29,33 @@ var IntersectsRect = require('./IntersectsRect');
3029var SeparateY = function ( body1 , body2 , overlapOnly , bias )
3130{
3231 console . log ( '' ) ;
33- console . log ( 'frame' , body1 . world . _frame , 'body1' , body1 . gameObject . name , 'vs body2' , body2 . gameObject . name , ) ;
32+ console . log ( '%c frame ' + body1 . world . _frame + ' ' , 'background-color: orange' ) ;
33+ console . log ( 'body1:' , body1 . gameObject . name , 'vs body2:' , body2 . gameObject . name ) ;
3434 console . log ( 'pre-GetOverlap by = body1' , body1 . y , 'body2' , body2 . y ) ;
3535 console . log ( 'pre-GetOverlap gy = body1' , body1 . gameObject . y , 'body2' , body2 . gameObject . y ) ;
3636
37- var result = GetOverlapY ( body1 , body2 , overlapOnly , bias ) ;
37+ var collisionInfo = GetOverlapY ( body1 , body2 , overlapOnly , bias ) ;
38+
39+ console . log ( collisionInfo ) ;
3840
3941 console . log ( 'post-GetOverlap by = body1' , body1 . y , 'body2' , body2 . y ) ;
4042 console . log ( 'post-GetOverlap gy = body1' , body1 . gameObject . y , 'body2' , body2 . gameObject . y ) ;
4143
42- var overlap = result . overlap ;
43- var topFace = result . topFace ;
44+ var overlap = collisionInfo . overlapY ;
45+ var topFace = collisionInfo . face === CONST . FACING_UP ;
4446 var bottomFace = ! topFace ;
45- var intersects = result . intersects ;
47+ var intersects = collisionInfo . intersects ;
4648
4749 var velocity1 = body1 . velocity ;
4850 var velocity2 = body2 . velocity ;
4951
50- var blocked1 = body1 . blocked ;
51- var blocked2 = body2 . blocked ;
52-
5352 var bounce1 = body1 . bounce ;
5453 var bounce2 = body2 . bounce ;
5554
56- var worldBlocked1 = body1 . worldBlocked ;
57- var worldBlocked2 = body2 . worldBlocked ;
58-
5955 var body1Immovable = ( body1 . physicsType === CONST . STATIC_BODY || body1 . immovable ) ;
6056 var body2Immovable = ( body2 . physicsType === CONST . STATIC_BODY || body2 . immovable ) ;
6157
62- console . log ( 'body1 overlaps body2 on the' , ( ( topFace ) ? 'top' : 'bottom' ) ) ;
58+ console . log ( 'body1 overlaps body2 across the' , ( ( topFace ) ? 'top' : 'bottom' ) , 'by' , overlap , 'px' ) ;
6359
6460 // Can't separate two immovable bodies, or a body with its own custom separation logic
6561 if ( ! intersects || overlapOnly || ( body1Immovable && body2Immovable ) || body1 . customSeparateY || body2 . customSeparateY )
@@ -144,7 +140,7 @@ var SeparateY = function (body1, body2, overlapOnly, bias)
144140 // Body1 is immovable, so adjust body2 speed
145141 ny2 = v1 - v2 * bounce2 . y ;
146142
147- console . log ( 'resolution 3 ' ) ;
143+ console . log ( '%cresolution 3' , 'background-color: red; color: white ') ;
148144 console . log ( 'pre-impact v = body1' , v1 , 'body2' , v2 ) ;
149145 console . log ( 'post-impact v = body1' , ny1 , 'body2' , ny2 ) ;
150146 // console.log('pre-impact by = body1', body1.y, 'body2', body2.y);
@@ -177,38 +173,8 @@ var SeparateY = function (body1, body2, overlapOnly, bias)
177173 ny2 = 0 ;
178174 }
179175
180- var totalA = 0 ;
181- var totalB = 0 ;
182-
183- // Velocities calculated, time to work out what moves where
184- if ( overlap !== 0 )
185- {
186- // Try and give 50% separation to each body (this could be improved to give a speed ratio amount to each body)
187- var share = overlap * 0.5 ;
188-
189- if ( topFace )
190- {
191- totalA = body1 . getMoveY ( share ) ;
192-
193- if ( totalA < share )
194- {
195- share += ( share - totalA ) ;
196- }
197-
198- totalB = body2 . getMoveY ( - share ) ;
199- }
200- else
201- {
202- totalB = body2 . getMoveY ( share ) ;
203-
204- if ( totalB < share )
205- {
206- share += ( share - totalB ) ;
207- }
208-
209- totalA = body1 . getMoveY ( - share ) ;
210- }
211- }
176+ var totalA = collisionInfo . share1 ;
177+ var totalB = collisionInfo . share2 ;
212178
213179 console . log ( 'split at' , totalA , totalB , 'of' , overlap ) ;
214180
@@ -250,7 +216,7 @@ var SeparateY = function (body1, body2, overlapOnly, bias)
250216 // The top of Body1 overlaps with the bottom of Body2
251217 if ( body2 . isBlockedUp ( ) )
252218 {
253- body1 . setBlockedUp ( body2 ) ;
219+ body1 . setBlockedUp ( collisionInfo , body2 ) ;
254220 console . log ( 'ny1 < 0 topface up' , body1 . y ) ;
255221 }
256222 else
@@ -264,7 +230,7 @@ var SeparateY = function (body1, body2, overlapOnly, bias)
264230 // The bottom of Body1 overlaps with the top of Body2
265231 if ( body2 . isBlockedDown ( ) )
266232 {
267- body1 . setBlockedDown ( body2 ) ;
233+ body1 . setBlockedDown ( collisionInfo , body2 ) ;
268234 console . log ( 'ny1 < 0 bottomface down' , body1 . y ) ;
269235 }
270236 else
@@ -290,7 +256,7 @@ var SeparateY = function (body1, body2, overlapOnly, bias)
290256 // The top of Body1 overlaps with the bottom of Body2
291257 if ( body2 . isBlockedUp ( ) )
292258 {
293- body1 . setBlockedUp ( body2 ) ;
259+ body1 . setBlockedUp ( collisionInfo , body2 ) ;
294260 console . log ( 'ny1 > 0 topface up' , body1 . y ) ;
295261 }
296262 else
@@ -304,7 +270,7 @@ var SeparateY = function (body1, body2, overlapOnly, bias)
304270 // The bottom of Body1 overlaps with the top of Body2
305271 if ( body2 . isBlockedDown ( ) )
306272 {
307- body1 . setBlockedDown ( body2 ) ;
273+ body1 . setBlockedDown ( collisionInfo , body2 ) ;
308274 console . log ( 'ny1 > 0 bottomface down' , body1 . y ) ;
309275 }
310276 else
@@ -335,7 +301,7 @@ var SeparateY = function (body1, body2, overlapOnly, bias)
335301
336302 if ( body2 . isBlockedUp ( ) )
337303 {
338- body1 . setBlockedUp ( body2 ) ;
304+ body1 . setBlockedUp ( collisionInfo , body2 ) ;
339305 console . log ( 'body1 stationary topface up' , body1 . y ) ;
340306 }
341307 }
@@ -350,7 +316,7 @@ var SeparateY = function (body1, body2, overlapOnly, bias)
350316
351317 if ( body2 . isBlockedDown ( ) )
352318 {
353- body1 . setBlockedDown ( body2 ) ;
319+ body1 . setBlockedDown ( collisionInfo , body2 ) ;
354320 console . log ( 'body1 stationary bottomface down' , body1 . y ) ;
355321 }
356322 }
@@ -365,7 +331,7 @@ var SeparateY = function (body1, body2, overlapOnly, bias)
365331 // The bottom of Body2 overlaps with the top of Body1
366332 if ( body1 . isBlockedDown ( ) )
367333 {
368- body2 . setBlockedDown ( body1 ) ;
334+ body2 . setBlockedDown ( collisionInfo , body1 ) ;
369335 console . log ( 'ny2 < 0 topface down' , body2 . y ) ;
370336 }
371337 else
@@ -379,7 +345,7 @@ var SeparateY = function (body1, body2, overlapOnly, bias)
379345 // The top of Body2 overlaps with the bottom of Body1
380346 if ( body1 . isBlockedUp ( ) )
381347 {
382- body2 . setBlockedUp ( body1 ) ;
348+ body2 . setBlockedUp ( collisionInfo , body1 ) ;
383349 console . log ( 'ny2 < 0 bottomface down' , body2 . y ) ;
384350 }
385351 else
@@ -405,7 +371,7 @@ var SeparateY = function (body1, body2, overlapOnly, bias)
405371 // The bottom of Body2 overlaps with the top of Body1
406372 if ( body1 . isBlockedDown ( ) )
407373 {
408- body2 . setBlockedDown ( body1 ) ;
374+ body2 . setBlockedDown ( collisionInfo , body1 ) ;
409375 console . log ( 'ny2 > 0 topface down' , body2 . y ) ;
410376 }
411377 else
@@ -419,7 +385,7 @@ var SeparateY = function (body1, body2, overlapOnly, bias)
419385 // The top of Body2 overlaps with the bottom of Body1
420386 if ( body1 . isBlockedUp ( ) )
421387 {
422- body2 . setBlockedUp ( body1 ) ;
388+ body2 . setBlockedUp ( collisionInfo , body1 ) ;
423389 console . log ( 'ny2 > 0 bottomface up' , body2 . y ) ;
424390 }
425391 else
@@ -450,7 +416,7 @@ var SeparateY = function (body1, body2, overlapOnly, bias)
450416
451417 if ( body1 . isBlockedDown ( ) )
452418 {
453- body2 . setBlockedDown ( body1 ) ;
419+ body2 . setBlockedDown ( collisionInfo , body1 ) ;
454420 console . log ( 'body2 stationary topface down' , body2 . y ) ;
455421 }
456422 }
@@ -465,7 +431,7 @@ var SeparateY = function (body1, body2, overlapOnly, bias)
465431
466432 if ( body1 . isBlockedUp ( ) )
467433 {
468- body2 . setBlockedUp ( body1 ) ;
434+ body2 . setBlockedUp ( collisionInfo , body1 ) ;
469435 console . log ( 'body2 stationary bottomface down' , body2 . y ) ;
470436 }
471437 }
0 commit comments