Skip to content

Commit 4e993ce

Browse files
committed
Much better seperation code and testing block exchange
1 parent 728a6da commit 4e993ce

1 file changed

Lines changed: 103 additions & 62 deletions

File tree

src/physics/arcade/SeparateY.js

Lines changed: 103 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/**
1+
/**
22
* @author Richard Davey <rich@photonstorm.com>
33
* @copyright 2019 Photon Storm Ltd.
44
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
@@ -65,7 +65,7 @@ var SeparateY = function (body1, body2, overlapOnly, bias)
6565
var ny1 = v1;
6666
var ny2 = v2;
6767

68-
// console.log(body1.gameObject.name, 'overlaps', body2.gameObject.name, 'on the', ((topFace) ? 'top' : 'bottom'));
68+
console.log(body1.gameObject.name, 'overlaps', body2.gameObject.name, 'on the', ((topFace) ? 'top' : 'bottom'));
6969

7070
// At this point, the velocity from gravity, world rebounds, etc has been factored in.
7171
// The body is moving the direction it wants to, but may be blocked and rebound.
@@ -84,20 +84,20 @@ var SeparateY = function (body1, body2, overlapOnly, bias)
8484
nv1 -= avg;
8585
nv2 -= avg;
8686

87-
ny1 = avg + nv1 * body1.bounce.y;
88-
ny2 = avg + nv2 * body2.bounce.y;
87+
ny1 = avg + nv1 * bounce1.y;
88+
ny2 = avg + nv2 * bounce2.y;
8989

90-
// console.log('*1', ny1, ny2, 'vs', v1, v2, 'avg', avg, 'nv', nv1, nv2, body1.bounce.y, body2.bounce.y);
90+
console.log('*1', ny1, ny2, 'vs', v1, v2, 'avg', avg, 'nv', nv1, nv2, 'bounce', body1.bounce.y, body2.bounce.y, 'delta', body1.deltaY(), body2.deltaY());
9191
}
9292
else if (body1Immovable)
9393
{
9494
// Body1 is immovable, so adjust body2 speed
95-
ny2 = v1 - v2 * body2.bounce.y;
95+
ny2 = v1 - v2 * bounce2.y;
9696
}
9797
else if (body2Immovable)
9898
{
9999
// Body2 is immovable, so adjust body1 speed
100-
ny1 = v2 - v1 * body1.bounce.y;
100+
ny1 = v2 - v1 * bounce1.y;
101101
}
102102

103103
var totalA = 0;
@@ -106,26 +106,45 @@ var SeparateY = function (body1, body2, overlapOnly, bias)
106106
// Velocities calculated, time to work out what moves where
107107
if (overlap !== 0)
108108
{
109+
// Try and give 50% separation to each body
109110
var share = overlap * 0.5;
110111

111-
for (var i = 0; i < 10; i++)
112+
if (topFace)
112113
{
113-
var amount1 = (topFace) ? body1.getMoveY(share) : body1.getMoveY(-share);
114-
var amount2 = (topFace) ? body2.getMoveY(-share) : body2.getMoveY(share);
114+
totalA = body1.getMoveY(share);
115+
116+
if (totalA < share)
117+
{
118+
share += (share - totalA);
119+
}
115120

116-
totalA += amount1;
117-
totalB += amount2;
121+
totalB = body2.getMoveY(-share);
122+
}
123+
else
124+
{
125+
totalB = body2.getMoveY(share);
118126

119-
if (Math.abs(totalA) + Math.abs(totalB) >= overlap)
127+
if (totalB < share)
120128
{
121-
break;
129+
share += (share - totalB);
122130
}
131+
132+
totalA = body1.getMoveY(-share);
123133
}
124134
}
125135

126-
// console.log('split at', totalA, totalB, 'of', overlap);
136+
console.log('split at', totalA, totalB, 'of', overlap);
127137

128-
if (body1.deltaY() < 0)
138+
// console.log('d1', body1.deltaY(), 'd2', body2.deltaY());
139+
140+
// By this stage the bodies have their separation distance calculated (stored in totalA/B)
141+
// and they have their new post-impact velocity. So now we need to work out world block state based on direction.
142+
143+
// Then, adjust for rebounded direction, if any.
144+
145+
console.log('preb', worldBlocked1.up, worldBlocked1.down, worldBlocked2.up, worldBlocked2.down);
146+
147+
if (ny1 < 0)
129148
{
130149
// Body1 is moving UP
131150

@@ -135,11 +154,6 @@ var SeparateY = function (body1, body2, overlapOnly, bias)
135154
if (worldBlocked2.up)
136155
{
137156
body1.setWorldBlockedUp(body2.bottom);
138-
139-
if (velocity1.y < 0)
140-
{
141-
velocity1.y *= bounce1.y;
142-
}
143157
}
144158
else
145159
{
@@ -158,8 +172,14 @@ var SeparateY = function (body1, body2, overlapOnly, bias)
158172
body1.y += totalA;
159173
}
160174
}
175+
176+
// If Body1 cannot move up, it doesn't matter what new velocity it has.
177+
if (worldBlocked1.up && body1.sleeping)
178+
{
179+
ny1 = 0;
180+
}
161181
}
162-
else if (body1.deltaY() > 0)
182+
else if (ny1 > 0)
163183
{
164184
// Body1 is moving DOWN
165185

@@ -181,118 +201,139 @@ var SeparateY = function (body1, body2, overlapOnly, bias)
181201
if (worldBlocked2.down)
182202
{
183203
body1.setWorldBlockedDown(body2.y);
184-
185-
if (velocity1.y > 0)
186-
{
187-
velocity1.y *= bounce1.y;
188-
}
189204
}
190205
else
191206
{
192207
body1.y += totalA;
193208
}
194209
}
210+
211+
// If Body1 cannot move down, it doesn't matter what new velocity it has.
212+
if (worldBlocked1.down && body1.sleeping)
213+
{
214+
ny1 = 0;
215+
}
195216
}
196217
else
197218
{
198219
// Body1 is stationary
199220
body1.y += totalA;
200221
}
201222

202-
/*
203223
if (body2.deltaY() < 0)
204224
{
205225
// Body2 is moving UP
206226

207227
if (topFace)
208228
{
209-
// The top of Body1 overlaps with the bottom of Body2
210-
if (worldBlocked1.up)
229+
// The bottom of Body2 overlaps with the top of Body1
230+
if (worldBlocked1.down)
211231
{
212-
body2.setWorldBlockedUp(body2.bottom);
213-
214-
if (velocity1.y < 0)
215-
{
216-
velocity1.y *= bounce1.y;
217-
}
232+
body2.setWorldBlockedDown(body1.y);
218233
}
219234
else
220235
{
221-
body1.y += totalA;
236+
body2.y += totalB;
222237
}
223238
}
224239
else if (bottomFace)
225240
{
226-
// The bottom of Body1 overlaps with the top of Body2
227-
if (worldBlocked2.down)
241+
// The top of Body2 overlaps with the bottom of Body1
242+
if (worldBlocked1.up)
228243
{
229-
body1.setWorldBlockedDown(body2.y);
244+
body2.setWorldBlockedUp(body1.bottom);
230245
}
231246
else
232247
{
233-
body1.y += totalA;
248+
body2.y += totalB;
234249
}
235250
}
251+
252+
// If Body2 cannot move up, it doesn't matter what new velocity it has.
253+
if (worldBlocked2.up && body2.sleeping)
254+
{
255+
ny2 = 0;
256+
}
236257
}
237258
else if (body2.deltaY() > 0)
238259
{
239260
// Body2 is moving DOWN
240261

241262
if (topFace)
242263
{
243-
// The top of Body1 overlaps with the bottom of Body2
244-
if (worldBlocked2.up)
264+
// The bottom of Body2 overlaps with the top of Body1
265+
if (worldBlocked1.down)
245266
{
246-
body1.setWorldBlockedUp(body2.bottom);
267+
body2.setWorldBlockedDown(body1.y);
247268
}
248269
else
249270
{
250-
body1.y += totalA;
271+
body2.y += totalB;
251272
}
252273
}
253274
else if (bottomFace)
254275
{
255-
// The bottom of Body1 overlaps with the top of Body2
256-
if (worldBlocked2.down)
276+
// The top of Body2 overlaps with the bottom of Body1
277+
if (worldBlocked1.up)
257278
{
258-
body1.setWorldBlockedDown(body2.y);
259-
260-
if (velocity1.y > 0)
261-
{
262-
velocity1.y *= bounce1.y;
263-
}
279+
body2.setWorldBlockedUp(body1.bottom);
264280
}
265281
else
266282
{
267283
body1.y += totalA;
268284
}
269285
}
286+
287+
// If Body2 cannot move down, it doesn't matter what new velocity it has.
288+
if (worldBlocked2.down && body2.sleeping)
289+
{
290+
ny2 = 0;
291+
}
270292
}
271293
else
272294
{
273295
// Body2 is stationary
274296
body2.y += totalB;
275297
}
276-
*/
277298

278-
body2.y += totalB;
299+
console.log('postb', worldBlocked1.up, worldBlocked1.down, worldBlocked2.up, worldBlocked2.down);
300+
301+
// We disregard the new velocity when:
302+
// Body is world blocked AND touching / blocked on the opposite face
279303

280-
if (body1.sleeping && Math.abs(ny1) < 10)
304+
if (worldBlocked1.up && worldBlocked1.down)
281305
{
282306
ny1 = 0;
283307
}
284-
else
308+
309+
if (worldBlocked2.up && worldBlocked2.down)
285310
{
286-
body1.wake();
311+
ny2 = 0;
287312
}
288313

289-
if (body2.sleeping && Math.abs(ny2) < 10)
314+
if (body1.sleeping)
290315
{
291-
ny2 = 0;
316+
if (Math.abs(ny1) < 10)
317+
{
318+
ny1 = 0;
319+
}
320+
else
321+
{
322+
console.log('waking body1 from', ny1, body1.prevVelocity.y);
323+
body1.wake();
324+
}
292325
}
293-
else
326+
327+
if (body2.sleeping)
294328
{
295-
body2.wake();
329+
if (Math.abs(ny2) < 10)
330+
{
331+
ny2 = 0;
332+
}
333+
else
334+
{
335+
body2.wake();
336+
}
296337
}
297338

298339
velocity1.y = ny1;

0 commit comments

Comments
 (0)