Skip to content

Commit 6a14504

Browse files
committed
Testing issue with sticky bodies
1 parent 1518f49 commit 6a14504

3 files changed

Lines changed: 70 additions & 68 deletions

File tree

src/physics/arcade/Body.js

Lines changed: 22 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66

77
var ArrayAdd = require('../../utils/array/Add');
88
var CircleContains = require('../../geom/circle/Contains');
9+
var CheckOverlapY = require('./CheckOverlapY');
910
var Class = require('../../utils/Class');
1011
var CONST = require('./const');
1112
var Events = require('./events');
1213
var FuzzyEqual = require('../../math/fuzzy/Equal');
1314
var FuzzyGreaterThan = require('../../math/fuzzy/GreaterThan');
1415
var FuzzyLessThan = require('../../math/fuzzy/LessThan');
15-
var CheckOverlapY = require('./CheckOverlapY');
16+
var IntersectsRect = require('./IntersectsRect');
1617
var RadToDeg = require('../../math/RadToDeg');
1718
var Rectangle = require('../../geom/rectangle/Rectangle');
1819
var RectangleContains = require('../../geom/rectangle/Contains');
@@ -292,7 +293,7 @@ var Body = new Class({
292293

293294
/**
294295
* The minimum velocity a body can move before it won't rebound and is considered for sleep.
295-
* The default is 10 but you may wish to change this based on game type.
296+
* The default is 15 but you may wish to change this based on game type.
296297
*
297298
* @name Phaser.Physics.Arcade.Body#minVelocity
298299
* @type {Phaser.Math.Vector2}
@@ -316,7 +317,8 @@ var Body = new Class({
316317

317318
this.sleepIterations = 60;
318319

319-
this.forcePosition = false;
320+
// 0 = none, 1 = soft block, 2 = hard block
321+
this.forcePosition = 0;
320322

321323
/**
322324
* The Body's absolute maximum change in position, in pixels per step.
@@ -978,7 +980,7 @@ var Body = new Class({
978980
this.overlapY = 0;
979981

980982
this.embedded = false;
981-
this.forcePosition = false;
983+
this.forcePosition = 0;
982984

983985
// Updates the transform values
984986
this.updateBounds();
@@ -1017,7 +1019,7 @@ var Body = new Class({
10171019
this.prevVelocity.set(0);
10181020
this.speed = 0;
10191021

1020-
this.forcePosition = true;
1022+
// this.forcePosition = true;
10211023

10221024
var worldBlocked = this.worldBlocked;
10231025

@@ -1028,10 +1030,12 @@ var Body = new Class({
10281030
if (worldBlocked.down)
10291031
{
10301032
this.bottom = worldBounds.bottom;
1033+
this.forcePosition = 2;
10311034
}
10321035
else if (worldBlocked.up)
10331036
{
10341037
this.y = worldBounds.y;
1038+
this.forcePosition = 2;
10351039
}
10361040
}
10371041
}
@@ -1123,7 +1127,7 @@ var Body = new Class({
11231127
this.checkWorldRebound();
11241128
}
11251129

1126-
if (!this.forcePosition)
1130+
if (this.forcePosition !== 2)
11271131
{
11281132
position.x += this.getMoveX(velocity.x * delta);
11291133
position.y += this.getMoveY(velocity.y * delta);
@@ -1300,16 +1304,6 @@ var Body = new Class({
13001304

13011305
var gameObject = this.gameObject;
13021306

1303-
// var newVelocityY = Math.abs(this.velocity.y);
1304-
1305-
// if (newVelocityY < this.minVelocity.y)
1306-
// {
1307-
// if (this.isBlockedDown())
1308-
// {
1309-
1310-
// }
1311-
// }
1312-
13131307
if (this.moves)
13141308
{
13151309
var mx = this.deltaMax.x;
@@ -1357,9 +1351,9 @@ var Body = new Class({
13571351
this.facing = CONST.FACING_DOWN;
13581352
}
13591353

1360-
if (this.forcePosition)
1354+
if (this.forcePosition !== 0)
13611355
{
1362-
// console.log(this.world._frame, this.gameObject.name, 'forcePosition', this.y);
1356+
console.log(this.world._frame, this.gameObject.name, 'forcePosition', this.y);
13631357

13641358
gameObject.x = this.x;
13651359
gameObject.y = this.y;
@@ -2191,18 +2185,12 @@ var Body = new Class({
21912185

21922186
this.setBlocker(by);
21932187

2194-
// if (forceY && !this.worldBlocked.up)
2195-
// if (forceY && !this.worldBlocked.up && this.velocity.y <= 0)
2196-
if (forceY && !this.worldBlocked.up && this.velocity.y <= 0)
2188+
if (forceY && this.forcePosition !== 2)
21972189
{
2198-
console.log(this.world._frame, this.gameObject.name, 'setBlockedUp');
2199-
2200-
this.y = by.bottom;
2201-
this.forcePosition = true;
2202-
2203-
if (this.bounce.y === 0 && this.isGravityBlockedY())
2190+
if (IntersectsRect(this, by))
22042191
{
2205-
this.velocity.y = 0;
2192+
this.y = by.bottom;
2193+
this.forcePosition = 1;
22062194
}
22072195
}
22082196

@@ -2227,19 +2215,12 @@ var Body = new Class({
22272215
// GetOverlapY = calls this, setting 'true' for forcing Y
22282216
// SeparateY = calls this, not setting anything, so 'true' for forcing Y
22292217

2230-
// if (forceY && !this.worldBlocked.down)
2231-
// if (forceY && !this.worldBlocked.down && this.velocity.y >= 0)
2232-
// if (!this.worldBlocked.down && this.velocity.y >= 0)
2233-
if (forceY && !this.worldBlocked.down && this.velocity.y >= 0)
2218+
if (forceY && this.forcePosition !== 2)
22342219
{
2235-
console.log(this.world._frame, this.gameObject.name, 'setBlockedDown');
2236-
2237-
this.bottom = by.y;
2238-
this.forcePosition = true;
2239-
2240-
if (this.bounce.y === 0 && this.isGravityBlockedY())
2220+
if (IntersectsRect(this, by))
22412221
{
2242-
this.velocity.y = 0;
2222+
this.bottom = by.y;
2223+
this.forcePosition = 1;
22432224
}
22442225
}
22452226

@@ -2280,7 +2261,7 @@ var Body = new Class({
22802261
if (forceY !== undefined && !this.wasBlocked.up)
22812262
{
22822263
this.y = forceY;
2283-
this.forcePosition = true;
2264+
this.forcePosition = 2;
22842265
}
22852266

22862267
return this;
@@ -2297,7 +2278,7 @@ var Body = new Class({
22972278
{
22982279
console.log(this.gameObject.name, 'world blocked down + position');
22992280
this.bottom = forceY;
2300-
this.forcePosition = true;
2281+
this.forcePosition = 2;
23012282
}
23022283

23032284
return this;

src/physics/arcade/GetOverlapY.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ var GetOverlapY = function (body1, body2, overlapOnly, bias)
6262
body1.setTouchingDown();
6363
body2.setTouchingUp();
6464

65-
body1.setBlockedDown(body2);
66-
body2.setBlockedUp(body1);
65+
body1.setBlockedDown(body2, intersects);
66+
body2.setBlockedUp(body1, intersects);
6767
}
6868
}
6969
else
@@ -85,8 +85,8 @@ var GetOverlapY = function (body1, body2, overlapOnly, bias)
8585
body1.setTouchingUp();
8686
body2.setTouchingDown();
8787

88-
body1.setBlockedUp(body2);
89-
body2.setBlockedDown(body1);
88+
body1.setBlockedUp(body2, intersects);
89+
body2.setBlockedDown(body1, intersects);
9090
}
9191
}
9292

src/physics/arcade/SeparateY.js

Lines changed: 44 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,15 @@ var SeparateY = function (body1, body2, overlapOnly, bias)
5151
var body1Immovable = (body1.physicsType === CONST.STATIC_BODY || body1.immovable);
5252
var body2Immovable = (body2.physicsType === CONST.STATIC_BODY || body2.immovable);
5353

54+
console.log('');
55+
console.log('frame', body1.world._frame);
56+
console.log('body1', body1.gameObject.name, 'overlaps body2', body2.gameObject.name, 'on the', ((topFace) ? 'top' : 'bottom'));
57+
5458
// Can't separate two immovable bodies, or a body with its own custom separation logic
5559
if (!intersects || overlapOnly || (body1Immovable && body2Immovable) || body1.customSeparateY || body2.customSeparateY)
5660
{
61+
console.log('SeparateY aborted');
62+
5763
// return true if there was some overlap, otherwise false.
5864
return ((intersects && overlap !== 0) || (body1.embedded && body2.embedded));
5965
}
@@ -65,10 +71,6 @@ var SeparateY = function (body1, body2, overlapOnly, bias)
6571
var ny1 = v1;
6672
var ny2 = v2;
6773

68-
console.log('');
69-
console.log('frame', body1.world._frame);
70-
console.log('body1', body1.gameObject.name, 'overlaps body2', body2.gameObject.name, 'on the', ((topFace) ? 'top' : 'bottom'));
71-
7274
// At this point, the velocity from gravity, world rebounds, etc has been factored in.
7375
// The body is moving the direction it wants to, but may be blocked and rebound.
7476

@@ -98,10 +100,8 @@ var SeparateY = function (body1, body2, overlapOnly, bias)
98100
console.log('resolution 1');
99101
console.log('pre-impact v = body1', v1, 'body2', v2);
100102
console.log('post-impact v = body1', ny1, 'body2', ny2);
101-
102-
// console.log('pre-impact y = body1', body1.gameObject.y, 'body2', body2.gameObject.y);
103-
// console.log('sleeping? = body1', body1.sleeping, 'body2', body2.sleeping);
104-
// console.log('wb = body1', body1.worldBlocked.down, 'body2', body2.worldBlocked.down);
103+
console.log('pre-impact y = body1', body1.gameObject.y, 'body2', body2.gameObject.y);
104+
console.log('wb = body1', body1.worldBlocked.down, 'body2', body2.worldBlocked.down);
105105

106106
// console.log('avg', avg);
107107
// console.log('nv', nv1, nv2);
@@ -141,8 +141,11 @@ var SeparateY = function (body1, body2, overlapOnly, bias)
141141
console.log('resolution 3');
142142
console.log('pre-impact v = body1', v1, 'body2', v2);
143143
console.log('post-impact v = body1', ny1, 'body2', ny2);
144+
console.log('pre-impact by = body1', body1.y, 'body2', body2.y);
145+
console.log('pre-impact gy = body1', body1.gameObject.y, 'body2', body2.gameObject.y);
146+
console.log('wb = body1', body1.worldBlocked.down, 'body2', body2.worldBlocked.down);
144147
console.log('sleeping? = body1', body1.sleeping, 'body2', body2.sleeping);
145-
}
148+
}
146149
else if (moving1 && !moving2)
147150
{
148151
// Body2 is immovable, so adjust body1 speed
@@ -151,8 +154,10 @@ var SeparateY = function (body1, body2, overlapOnly, bias)
151154
console.log('resolution 4');
152155
console.log('pre-impact v = body1', v1, 'body2', v2);
153156
console.log('post-impact v = body1', ny1, 'body2', ny2);
157+
console.log('pre-impact y = body1', body1.gameObject.y, 'body2', body2.gameObject.y);
158+
console.log('wb = body1', body1.worldBlocked.down, 'body2', body2.worldBlocked.down);
154159
console.log('sleeping? = body1', body1.sleeping, 'body2', body2.sleeping);
155-
}
160+
}
156161
else
157162
{
158163
console.log('neither moving, or both immovable');
@@ -211,6 +216,22 @@ var SeparateY = function (body1, body2, overlapOnly, bias)
211216
return true;
212217
}
213218

219+
// FLip flop?
220+
var flip1 = (v1 < 0 && ny1 > 0) || (v1 > 0 && ny1 < 0);
221+
var flip2 = (v2 < 0 && ny2 > 0) || (v2 > 0 && ny2 < 0);
222+
223+
if (!body1.sleeping && flip1 && Math.abs(ny1) < body1.minVelocity.y)
224+
{
225+
console.log('body1 flip vel too small, zeroing');
226+
ny1 = 0;
227+
}
228+
229+
if (!body2.sleeping && flip2 && Math.abs(ny2) < body2.minVelocity.y)
230+
{
231+
console.log('body2 flip vel too small, zeroing');
232+
ny2 = 0;
233+
}
234+
214235
// By this stage the bodies have their separation distance calculated (stored in totalA/B)
215236
// and they have their new post-impact velocity. So now we need to work out block state based on direction.
216237

@@ -228,7 +249,7 @@ var SeparateY = function (body1, body2, overlapOnly, bias)
228249
}
229250
else
230251
{
231-
body1.y += totalA;
252+
body1.y += body1.getMoveY(totalA);
232253
console.log('ny1 < 0 topface add', body1.y);
233254
}
234255
}
@@ -242,7 +263,7 @@ var SeparateY = function (body1, body2, overlapOnly, bias)
242263
}
243264
else
244265
{
245-
body1.y += totalA;
266+
body1.y += body1.getMoveY(totalA);
246267
console.log('ny1 < 0 bottomface add', body1.y);
247268
}
248269
}
@@ -268,7 +289,7 @@ var SeparateY = function (body1, body2, overlapOnly, bias)
268289
}
269290
else
270291
{
271-
body1.y += totalA;
292+
body1.y += body1.getMoveY(totalA);
272293
console.log('ny1 > 0 topface add', body1.y);
273294
}
274295
}
@@ -282,7 +303,7 @@ var SeparateY = function (body1, body2, overlapOnly, bias)
282303
}
283304
else
284305
{
285-
body1.y += totalA;
306+
body1.y += body1.getMoveY(totalA);
286307
console.log('ny1 > 0 bottomface add', body1.y);
287308
}
288309
}
@@ -307,8 +328,8 @@ var SeparateY = function (body1, body2, overlapOnly, bias)
307328
}
308329
else
309330
{
310-
body1.y += totalA;
311-
console.log('body1 stationary topface add', body1.y);
331+
console.log('body1 stationary topface add', body1.y, 'ta', totalA, (body1.y + totalA));
332+
body1.y += body1.getMoveY(totalA);
312333
}
313334
}
314335
else if (bottomFace)
@@ -321,7 +342,7 @@ var SeparateY = function (body1, body2, overlapOnly, bias)
321342
}
322343
else
323344
{
324-
body1.y += totalA;
345+
body1.y += body1.getMoveY(totalA);
325346
console.log('body1 stationary bottomface add', body1.y);
326347
}
327348
}
@@ -341,7 +362,7 @@ var SeparateY = function (body1, body2, overlapOnly, bias)
341362
}
342363
else
343364
{
344-
body2.y += totalB;
365+
body2.y += body2.getMoveY(totalB);
345366
console.log('ny2 < 0 topface add', body2.y);
346367
}
347368
}
@@ -355,7 +376,7 @@ var SeparateY = function (body1, body2, overlapOnly, bias)
355376
}
356377
else
357378
{
358-
body2.y += totalB;
379+
body2.y += body2.getMoveY(totalB);
359380
console.log('ny2 < 0 bottomface add', body2.y);
360381
}
361382
}
@@ -381,7 +402,7 @@ var SeparateY = function (body1, body2, overlapOnly, bias)
381402
}
382403
else
383404
{
384-
body2.y += totalB;
405+
body2.y += body2.getMoveY(totalB);
385406
console.log('ny2 > 0 topface add', body2.y);
386407
}
387408
}
@@ -395,7 +416,7 @@ var SeparateY = function (body1, body2, overlapOnly, bias)
395416
}
396417
else
397418
{
398-
body2.y += totalB;
419+
body2.y += body2.getMoveY(totalB);
399420
console.log('ny2 > 0 bottomface add', body2.y);
400421
}
401422
}
@@ -420,7 +441,7 @@ var SeparateY = function (body1, body2, overlapOnly, bias)
420441
}
421442
else
422443
{
423-
body2.y += totalB;
444+
body2.y += body2.getMoveY(totalB);
424445
console.log('body2 stationary topface add', body2.y);
425446
}
426447
}
@@ -434,7 +455,7 @@ var SeparateY = function (body1, body2, overlapOnly, bias)
434455
}
435456
else
436457
{
437-
body2.y += totalB;
458+
body2.y += body2.getMoveY(totalB);
438459
console.log('body2 stationary bottomface add', body2.y);
439460
}
440461
}

0 commit comments

Comments
 (0)