Skip to content

Commit a605ad8

Browse files
committed
Handle nonn intersects aborts
1 parent 343964d commit a605ad8

1 file changed

Lines changed: 91 additions & 174 deletions

File tree

src/physics/arcade/SeparateY.js

Lines changed: 91 additions & 174 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
*/
66

77
var CONST = require('./const');
8-
var FuzzyEqual = require('../../math/fuzzy/Equal');
98
var GetOverlapY = require('./GetOverlapY');
109

1110
/**
@@ -33,60 +32,55 @@ var SeparateY = function (body1, body2, overlapOnly, bias)
3332

3433
var overlap = result[0];
3534
var faceTop = result[1];
36-
// var faceBottom = !faceTop;
35+
var intersects = result[2];
3736

3837
var velocity1 = body1.velocity;
3938
var velocity2 = body2.velocity;
4039

4140
var blocked1 = body1.blocked;
4241
var blocked2 = body2.blocked;
4342

43+
var body1BlockedY = (blocked1.up || blocked1.down);
44+
var body2BlockedY = (blocked2.up || blocked2.down);
45+
4446
var body1Immovable = (body1.physicsType === CONST.STATIC_BODY || body1.immovable);
4547
var body2Immovable = (body2.physicsType === CONST.STATIC_BODY || body2.immovable);
4648

4749
// Can't separate two immovable bodies, or a body with its own custom separation logic
48-
if (overlapOnly || (body1Immovable && body2Immovable) || body1.customSeparateY || body2.customSeparateY)
50+
if (!intersects || overlapOnly || (body1Immovable && body2Immovable) || body1.customSeparateY || body2.customSeparateY)
4951
{
5052
// return true if there was some overlap, otherwise false.
5153
return (overlap !== 0) || (body1.embedded && body2.embedded);
5254
}
5355

56+
if (blocked1.up && blocked1.down)
57+
{
58+
body1Immovable = true;
59+
}
60+
61+
if (blocked2.up && blocked2.down)
62+
{
63+
body2Immovable = true;
64+
}
65+
5466
// Adjust their positions and velocities accordingly based on the amount of overlap
5567
var v1 = velocity1.y;
5668
var v2 = velocity2.y;
5769

5870
// At this point, the velocity from gravity, world rebounds, etc has been factored in.
5971
// The body is moving the direction it wants to, but may be blocked.
6072

61-
// console.log('Collision - top face of body1?', faceTop);
62-
63-
// console.log('body1', body1.gameObject.name);
64-
// console.log('blocked down', blocked1.down, 'blocked up', blocked1.up);
65-
// console.log('touching down', body1.touching.down, 'touching up', body1.touching.up);
66-
// console.log('world blocked down', body1.worldBlocked.down, 'world blocked up', body1.worldBlocked.up);
67-
// console.log('moving up?', (body1.deltaY() < 0), 'speed', body1.deltaY());
68-
// console.log('immovable', body1.immovable);
69-
// console.log('velocity', v1);
70-
71-
// console.log('');
72-
73-
// console.log('body2', body2.gameObject.name);
74-
// console.log('blocked down', blocked2.down, 'blocked up', blocked2.up);
75-
// console.log('touching down', body2.touching.down, 'touching up', body2.touching.up);
76-
// console.log('world blocked down', body2.worldBlocked.down, 'world blocked up', body2.worldBlocked.up);
77-
// console.log('moving up?', (body2.deltaY() < 0), 'speed', body2.deltaY());
78-
// console.log('immovable', body2.immovable);
79-
// console.log('velocity', v2);
80-
81-
// debugger;
82-
8373
var ny1 = v1;
8474
var ny2 = v2;
8575

86-
var body1BlockedY = (blocked1.up || blocked1.down);
87-
var body2BlockedY = (blocked2.up || blocked2.down);
88-
89-
if (!body1Immovable && !body1BlockedY && !body2Immovable && !body2BlockedY)
76+
if (body1Immovable && body2Immovable)
77+
{
78+
// Both bodies are equally blocked, we can't do anything with them
79+
console.log('Both bodies are equally blocked, kill velocity?');
80+
// ny1 = 0;
81+
// ny2 = 0;
82+
}
83+
else if (!body1Immovable && !body1BlockedY && !body2Immovable && !body2BlockedY)
9084
{
9185
// Neither body is immovable or blocked, so they get a new velocity based on mass
9286
var mass1 = body1.mass;
@@ -113,39 +107,25 @@ var SeparateY = function (body1, body2, overlapOnly, bias)
113107
// Body2 is blocked or never changes speed, so adjust body1 speed
114108
ny1 = v2 - v1 * body1.bounce.y;
115109
}
116-
else
117-
{
118-
// Both bodies are equally blocked
119-
console.log('Both bodies are equally blocked, kill velocity?');
120-
}
121110

122111
// Velocities calculated, time to work out what moves where
123-
124112
if (overlap !== 0)
125113
{
126-
// var p1 = (faceBottom) ? body1.bottom - body2.y : body2.bottom - body1.y;
127-
// console.log('impact', v1, v2, 'overlap', overlap, p1);
128-
129114
var share = overlap * 0.5;
130115
var amount1 = body1.getMoveY(share);
131116
var amount2 = body2.getMoveY(-share);
132117

133118
if (amount1 !== share)
134119
{
135-
// console.log('diff1', share, amount1, amount2);
136120
amount2 -= (share - amount1);
137121
}
138122
else if (amount2 !== -share)
139123
{
140-
// console.log('diff2', share, amount1, amount2);
141124
amount1 += (share + amount2);
142125
}
143126

144127
body1.y += amount1;
145128
body2.y += amount2;
146-
147-
// var p2 = (faceBottom) ? body1.bottom - body2.y : body2.bottom - body1.y;
148-
// console.log('post-impact', p2);
149129
}
150130

151131
// -------------------------------------------
@@ -154,41 +134,6 @@ var SeparateY = function (body1, body2, overlapOnly, bias)
154134

155135
if (!body1BlockedY && !body2BlockedY)
156136
{
157-
/*
158-
if (overlap !== 0)
159-
{
160-
// var p1 = (faceBottom) ? body1.bottom - body2.y : body2.bottom - body1.y;
161-
// console.log('impact', v1, v2, 'overlap', overlap, p1);
162-
163-
var share = overlap * 0.5;
164-
var amount1 = body1.getMoveY(share);
165-
var amount2 = body2.getMoveY(-share);
166-
167-
if (amount1 !== share)
168-
{
169-
// console.log('diff1', share, amount1, amount2);
170-
amount2 -= (share - amount1);
171-
}
172-
else if (amount2 !== -share)
173-
{
174-
// console.log('diff2', share, amount1, amount2);
175-
amount1 += (share + amount2);
176-
}
177-
178-
body1.y += amount1;
179-
body2.y += amount2;
180-
181-
// var p2 = (faceBottom) ? body1.bottom - body2.y : body2.bottom - body1.y;
182-
// console.log('post-impact', p2);
183-
}
184-
// else
185-
// {
186-
// console.log('zero overlap impact');
187-
// }
188-
189-
// console.log('----------------------------------');
190-
*/
191-
192137
velocity1.y = ny1;
193138
velocity2.y = ny2;
194139

@@ -199,126 +144,98 @@ var SeparateY = function (body1, body2, overlapOnly, bias)
199144
// 2) Body1 motion checks
200145
// -------------------------------------------
201146

202-
if (body1.deltaY() < 0)
147+
if (body1.deltaY() < 0 && blocked1.up && blocked1.by === body2)
203148
{
204-
console.log('up1');
149+
// console.log('up1');
150+
151+
// Body1 is moving UP and is blocked by Body2
205152

206-
// Body1 is moving UP
153+
if (faceTop)
154+
{
155+
// Body1 top hit Body2 bottom and is blocked from moving up
156+
body1.y = body2.bottom;
157+
}
158+
else
159+
{
160+
// Body1 bottom hit Body2 top and is blocked from moving up
161+
body1.bottom = body2.y;
162+
}
207163

208-
if (blocked1.up && blocked1.by === body2)
164+
if (ny1 < 0)
209165
{
210-
// And is blocked
211-
if (faceTop)
212-
{
213-
// Body1 top hit Body2 bottom and is blocked from moving up
214-
body1.y = body2.bottom;
215-
}
216-
else
217-
{
218-
// Body1 bottom hit Body2 top and is blocked from moving up
219-
body1.bottom = body2.y;
220-
}
221-
222-
if (ny1 < 0)
223-
{
224-
// Velocity hasn't been reversed, so cancel it
225-
ny1 = 0;
226-
}
166+
// Velocity hasn't been reversed, so cancel it
167+
ny1 = 0;
227168
}
228169
}
229-
else if (body1.deltaY() > 0)
170+
else if (body1.deltaY() > 0 && blocked1.down && blocked1.by === body2)
230171
{
231-
console.log('down1');
172+
// console.log('down1');
232173

233-
// Body1 is moving DOWN
174+
// Body1 is moving DOWN and is blocked by Body2
234175

235-
if (blocked1.down && blocked1.by === body2)
176+
if (faceTop)
236177
{
237-
// And is blocked
238-
if (faceTop)
239-
{
240-
// Body1 top hit Body2 bottom and is blocked from moving down
241-
body1.y = body2.bottom;
242-
}
243-
else
244-
{
245-
// Body1 bottom hit Body2 top and is blocked from moving down
246-
body1.bottom = body2.y;
247-
}
248-
249-
if (ny1 > 0)
250-
{
251-
// Velocity hasn't been reversed, so cancel it
252-
ny1 = 0;
253-
}
178+
// Body1 top hit Body2 bottom and is blocked from moving down
179+
body1.y = body2.bottom;
180+
}
181+
else
182+
{
183+
// Body1 bottom hit Body2 top and is blocked from moving down
184+
body1.bottom = body2.y;
185+
}
186+
187+
if (ny1 > 0)
188+
{
189+
// Velocity hasn't been reversed, so cancel it
190+
ny1 = 0;
254191
}
255-
}
256-
else if (FuzzyEqual(ny1, 0, 0.001))
257-
{
258-
// Body1 is static, don't apply any more velocity
259-
console.log('static 1');
260-
ny1 = 0;
261192
}
262193

263-
if (body2.deltaY() < 0)
194+
if (body2.deltaY() < 0 && blocked2.up && blocked2.by === body1)
264195
{
265-
console.log('up2');
196+
// console.log('up2');
266197

267-
// Body2 is moving UP
198+
// Body2 is moving UP and is blocked by Body1
268199

269-
if (blocked2.up && blocked2.by === body1)
200+
if (faceTop)
201+
{
202+
// Body2 bottom hit Body1 top and is blocked from moving up
203+
body2.bottom = body1.y;
204+
}
205+
else
270206
{
271-
// And is blocked
272-
if (faceTop)
273-
{
274-
// Body2 bottom hit Body1 top and is blocked from moving up
275-
body2.bottom = body1.y;
276-
}
277-
else
278-
{
279-
// Body2 top hit Body1 bottom and is blocked from moving up
280-
body2.y = body1.bottom;
281-
}
282-
283-
if (ny2 < 0)
284-
{
285-
// Velocity hasn't been reversed, so cancel it
286-
ny2 = 0;
287-
}
207+
// Body2 top hit Body1 bottom and is blocked from moving up
208+
body2.y = body1.bottom;
209+
}
210+
211+
if (ny2 < 0)
212+
{
213+
// Velocity hasn't been reversed, so cancel it
214+
ny2 = 0;
288215
}
289216
}
290-
else if (body2.deltaY() > 0)
217+
else if (body2.deltaY() > 0 && blocked2.down && blocked2.by === body1)
291218
{
292-
console.log('down2');
219+
// console.log('down2');
293220

294-
// Body2 is moving DOWN
221+
// Body2 is moving DOWN and is blocked by Body1
295222

296-
if (blocked2.down && blocked2.by === body1)
223+
if (faceTop)
297224
{
298-
// And is blocked
299-
if (faceTop)
300-
{
301-
// Body2 bottom hit Body1 top and is blocked from moving down
302-
body2.bottom = body1.y;
303-
}
304-
else
305-
{
306-
// Body2 top hit Body1 bottom and is blocked from moving down
307-
body2.y = body1.bottom;
308-
}
309-
310-
if (ny2 > 0)
311-
{
312-
// Velocity hasn't been reversed, so cancel it
313-
ny2 = 0;
314-
}
225+
// Body2 bottom hit Body1 top and is blocked from moving down
226+
body2.bottom = body1.y;
227+
}
228+
else
229+
{
230+
// Body2 top hit Body1 bottom and is blocked from moving down
231+
body2.y = body1.bottom;
232+
}
233+
234+
if (ny2 > 0)
235+
{
236+
// Velocity hasn't been reversed, so cancel it
237+
ny2 = 0;
315238
}
316-
}
317-
else if (FuzzyEqual(ny2, 0, 0.001))
318-
{
319-
// Body2 is static, don't apply any more velocity
320-
console.log('static2');
321-
ny2 = 0;
322239
}
323240

324241
velocity1.y = ny1;

0 commit comments

Comments
 (0)