Skip to content

Commit 792f458

Browse files
committed
Better "is moving" handling
1 parent 93f012d commit 792f458

1 file changed

Lines changed: 97 additions & 25 deletions

File tree

src/physics/arcade/SeparateY.js

Lines changed: 97 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,16 @@ var SeparateY = function (body1, body2, overlapOnly, bias)
6666
var ny2 = v2;
6767

6868
console.log('');
69-
console.log(body1.gameObject.name, 'overlaps', body2.gameObject.name, 'on the', ((topFace) ? 'top' : 'bottom'));
69+
console.log('frame', body1.world._frame);
70+
console.log('body1', body1.gameObject.name, 'overlaps body2', body2.gameObject.name, 'on the', ((topFace) ? 'top' : 'bottom'));
7071

7172
// At this point, the velocity from gravity, world rebounds, etc has been factored in.
7273
// The body is moving the direction it wants to, but may be blocked and rebound.
7374

74-
var move1 = (!body1Immovable && ((v1 >= 0 && !body1.isWorldBlockedDown()) || (v1 < 0 && !body1.isWorldBlockedUp())));
75-
var move2 = (!body2Immovable && ((v2 >= 0 && !body2.isWorldBlockedDown()) || (v2 < 0 && !body2.isWorldBlockedUp())));
75+
var moving1 = body1.movingY();
76+
var moving2 = body2.movingY();
7677

77-
if (move1 && move2)
78+
if (moving1 && moving2)
7879
{
7980
// Neither body is immovable, so they get a new velocity based on mass
8081
var mass1 = body1.mass;
@@ -97,10 +98,14 @@ var SeparateY = function (body1, body2, overlapOnly, bias)
9798
console.log('resolution 1');
9899
console.log('pre-impact v = body1', v1, 'body2', v2);
99100
console.log('post-impact v = body1', ny1, 'body2', ny2);
100-
console.log('avg', avg);
101-
console.log('nv', nv1, nv2);
102-
console.log('sqrt mult', bnv1, bnv2);
103-
console.log('delta', body1.deltaY(), body2.deltaY());
101+
console.log('pre-impact y = body1', body1.gameObject.y, 'body2', body2.gameObject.y);
102+
console.log('sleeping? = body1', body1.sleeping, 'body2', body2.sleeping);
103+
console.log('wb = body1', body1.worldBlocked.down, 'body2', body2.worldBlocked.down);
104+
105+
// console.log('avg', avg);
106+
// console.log('nv', nv1, nv2);
107+
// console.log('sqrt mult', bnv1, bnv2);
108+
// console.log('delta', body1.deltaY(), body2.deltaY());
104109
}
105110
else
106111
{
@@ -121,25 +126,41 @@ var SeparateY = function (body1, body2, overlapOnly, bias)
121126
console.log('resolution 2');
122127
console.log('pre-impact v = body1', v1, 'body2', v2);
123128
console.log('post-impact v = body1', ny1, 'body2', ny2);
124-
console.log('avg', avg);
125-
console.log('nv', nv1, nv2);
126-
console.log('sqrt', bnv1, bnv2);
127-
console.log('delta', body1.deltaY(), body2.deltaY());
129+
// console.log('avg', avg);
130+
// console.log('nv', nv1, nv2);
131+
// console.log('sqrt', bnv1, bnv2);
132+
// console.log('delta', body1.deltaY(), body2.deltaY());
128133
}
129134
}
130-
else if (body1Immovable)
135+
else if (!moving1 && moving2)
131136
{
132137
// Body1 is immovable, so adjust body2 speed
133138
ny2 = v1 - v2 * bounce2.y;
134-
}
135-
else if (body2Immovable)
139+
140+
console.log('resolution 3');
141+
console.log('pre-impact v = body1', v1, 'body2', v2);
142+
console.log('post-impact v = body1', ny1, 'body2', ny2);
143+
console.log('sleeping? = body1', body1.sleeping, 'body2', body2.sleeping);
144+
}
145+
else if (moving1 && !moving2)
136146
{
137147
// Body2 is immovable, so adjust body1 speed
138148
ny1 = v2 - v1 * bounce1.y;
139-
}
149+
150+
console.log('resolution 4');
151+
console.log('pre-impact v = body1', v1, 'body2', v2);
152+
console.log('post-impact v = body1', ny1, 'body2', ny2);
153+
console.log('sleeping? = body1', body1.sleeping, 'body2', body2.sleeping);
154+
}
140155
else
141156
{
142-
console.log('uh oh');
157+
console.log('neither moving, or both immovable');
158+
159+
console.log('resolution 5');
160+
console.log('pre-impact v = body1', v1, 'body2', v2);
161+
console.log('post-impact v = body1', ny1, 'body2', ny2);
162+
console.log('sleeping? = body1', body1.sleeping, 'body2', body2.sleeping);
163+
143164
ny1 = 0;
144165
ny2 = 0;
145166
}
@@ -239,7 +260,7 @@ var SeparateY = function (body1, body2, overlapOnly, bias)
239260
if (topFace)
240261
{
241262
// The top of Body1 overlaps with the bottom of Body2
242-
if (body1.isBlockedUp())
263+
if (body2.isBlockedUp())
243264
{
244265
body1.setBlockedUp(body2);
245266
console.log('ny1 > 0 topface up', body1.y);
@@ -275,8 +296,34 @@ var SeparateY = function (body1, body2, overlapOnly, bias)
275296
else
276297
{
277298
// Body1 is stationary
278-
body1.y += totalA;
279-
console.log('body1 stationary', body1.y, ny1, ny2);
299+
if (topFace)
300+
{
301+
// The top of Body1 overlaps with the bottom of Body2
302+
if (body2.isBlockedUp())
303+
{
304+
body1.setBlockedUp(body2);
305+
console.log('body1 stationary topface up', body1.y);
306+
}
307+
else
308+
{
309+
body1.y += totalA;
310+
console.log('body1 stationary topface add', body1.y);
311+
}
312+
}
313+
else if (bottomFace)
314+
{
315+
// The bottom of Body1 overlaps with the top of Body2
316+
if (body2.isBlockedDown())
317+
{
318+
body1.setBlockedDown(body2);
319+
console.log('body1 stationary bottomface down', body1.y);
320+
}
321+
else
322+
{
323+
body1.y += totalA;
324+
console.log('body1 stationary bottomface add', body1.y);
325+
}
326+
}
280327
}
281328

282329
if (ny2 < 0)
@@ -362,8 +409,34 @@ var SeparateY = function (body1, body2, overlapOnly, bias)
362409
else
363410
{
364411
// Body2 is stationary
365-
body2.y += totalB;
366-
console.log('body2 stationary', body2.y, ny1, ny2);
412+
if (topFace)
413+
{
414+
// The bottom of Body2 overlaps with the top of Body1
415+
if (body1.isBlockedDown())
416+
{
417+
body2.setBlockedDown(body1);
418+
console.log('body2 stationary topface down', body2.y);
419+
}
420+
else
421+
{
422+
body2.y += totalB;
423+
console.log('body2 stationary topface add', body2.y);
424+
}
425+
}
426+
else if (bottomFace)
427+
{
428+
// The top of Body2 overlaps with the bottom of Body1
429+
if (body1.isBlockedUp())
430+
{
431+
body2.setBlockedUp(body1);
432+
console.log('body2 stationary bottomface down', body2.y);
433+
}
434+
else
435+
{
436+
body2.y += totalB;
437+
console.log('body2 stationary bottomface add', body2.y);
438+
}
439+
}
367440
}
368441

369442
// We disregard the new velocity when a Body is world blocked AND blocked by something on the opposite face
@@ -403,11 +476,12 @@ var SeparateY = function (body1, body2, overlapOnly, bias)
403476
}
404477
else
405478
{
479+
console.log('waking body2 from', ny2, body2.prevVelocity.y);
406480
body2.wake();
407481
}
408482
}
409483

410-
console.log('SepY End', ny1, ny2);
484+
console.log('SepY End', ny1, ny2, 'y', body1.y, body2.y);
411485

412486
velocity1.y = ny1;
413487
velocity2.y = ny2;
@@ -420,8 +494,6 @@ var SeparateY = function (body1, body2, overlapOnly, bias)
420494

421495
console.log('');
422496

423-
window.dump = true;
424-
425497
// If we got this far then there WAS overlap, and separation is complete, so return true
426498
return true;
427499
};

0 commit comments

Comments
 (0)