Skip to content

Commit 7c5f6ad

Browse files
committed
Don't apply the force until postUpdate - resolves all issues with body spacing :)
1 parent 963ed11 commit 7c5f6ad

1 file changed

Lines changed: 103 additions & 57 deletions

File tree

src/physics/arcade/Body.js

Lines changed: 103 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
var ArrayAdd = require('../../utils/array/Add');
88
var CircleContains = require('../../geom/circle/Contains');
99
var CheckOverlapY = require('./CheckOverlapY');
10-
var CollisionInfo = require('./CollisionInfo');
1110
var Class = require('../../utils/Class');
1211
var CONST = require('./const');
1312
var Events = require('./events');
@@ -320,6 +319,8 @@ var Body = new Class({
320319
// 0 = none, 1 = soft block, 2 = hard block
321320
this.forcePosition = 0;
322321

322+
this.snapTo = null;
323+
323324
/**
324325
* The Body's absolute maximum change in position, in pixels per step.
325326
*
@@ -1062,7 +1063,7 @@ var Body = new Class({
10621063
this.checkWorldRebound();
10631064
}
10641065

1065-
if (this.forcePosition !== 2)
1066+
if (this.forcePosition !== 5)
10661067
{
10671068
position.x += this.getMoveX(velocity.x * delta);
10681069
position.y += this.getMoveY(velocity.y * delta);
@@ -1147,12 +1148,26 @@ var Body = new Class({
11471148
this.facing = CONST.FACING_DOWN;
11481149
}
11491150

1150-
if (this.forcePosition !== 0)
1151+
if (this.forcePosition > 0)
11511152
{
1152-
console.log(this.world._frame, this.gameObject.name, 'forcePosition', this.y, 'type', this.forcePosition);
1153+
console.log(this.world._frame, this.gameObject.name, 'forcePosition. Type: ', this.forcePosition);
1154+
1155+
var snapX = this.x;
1156+
var snapY = this.y;
1157+
1158+
switch (this.forcePosition)
1159+
{
1160+
case 1:
1161+
snapY = this.snapTo.bottom;
1162+
break;
11531163

1154-
gameObject.x = this.x;
1155-
gameObject.y = this.y;
1164+
case 2:
1165+
snapY = this.snapTo.y - this.height;
1166+
break;
1167+
}
1168+
1169+
gameObject.x = snapX;
1170+
gameObject.y = snapY;
11561171

11571172
dx = 0;
11581173
dy = 0;
@@ -1188,66 +1203,87 @@ var Body = new Class({
11881203
this.prevVelocity.y = this.velocity.y;
11891204
},
11901205

1191-
sleep: function (forceY)
1206+
snapToBlocker: function ()
11921207
{
1193-
if (!this.sleeping)
1208+
if (this.velocity.y !== 0)
11941209
{
1195-
this.sleeping = true;
1210+
return;
1211+
}
11961212

1197-
console.log(this.gameObject.name, 'put to sleep on frame', this.world._frame, 'force?', forceY, 'at', this.y);
1213+
var blocked = this.blocked;
1214+
var worldBlocked = this.worldBlocked;
11981215

1199-
this.velocity.set(0);
1200-
this.prevVelocity.set(0);
1201-
this.speed = 0;
1216+
if (!worldBlocked.none)
1217+
{
1218+
console.log(this.gameObject.name, 'snapped to world bounds');
12021219

1203-
var blocked = this.blocked;
1204-
var worldBlocked = this.worldBlocked;
1220+
var worldBounds = this.world.bounds;
12051221

1206-
if (forceY && !worldBlocked.none)
1222+
if (worldBlocked.down)
12071223
{
1208-
console.log(this.gameObject.name, 'sleeping and fixed to world bounds');
1224+
this.bottom = worldBounds.bottom;
1225+
this.forcePosition = 5;
1226+
}
1227+
else if (worldBlocked.up)
1228+
{
1229+
this.y = worldBounds.y;
1230+
this.forcePosition = 5;
1231+
}
1232+
}
1233+
else if (!blocked.none)
1234+
{
1235+
console.log(this.gameObject.name, 'snapped to blocker bounds scanning ...');
12091236

1210-
var worldBounds = this.world.bounds;
1237+
var body2;
12111238

1212-
if (worldBlocked.down)
1213-
{
1214-
this.bottom = worldBounds.bottom;
1215-
this.forcePosition = 2;
1216-
}
1217-
else if (worldBlocked.up)
1239+
if (blocked.down)
1240+
{
1241+
body2 = this.getBlocker(this.blockers.down);
1242+
1243+
if (body2)
12181244
{
1219-
this.y = worldBounds.y;
1220-
this.forcePosition = 2;
1245+
console.log('blocker bounds found', body2.y);
1246+
1247+
this.bottom = body2.y;
1248+
1249+
this.forcePosition = 5;
12211250
}
12221251
}
1223-
else if (forceY && !blocked.none)
1252+
else if (blocked.up)
12241253
{
1225-
console.log(this.gameObject.name, 'sleeping and fixed to blocker bounds scanning ...');
1254+
body2 = this.getBlocker(this.blockers.up);
12261255

1227-
var body2;
1228-
1229-
if (blocked.down)
1256+
if (body2)
12301257
{
1231-
body2 = this.getBlocker(this.blockers.down);
1232-
1233-
if (body2)
1234-
{
1235-
console.log('blocker bounds found', body2.y);
1258+
console.log('blocker bounds found', body2.y);
12361259

1237-
this.bottom = body2.y;
1260+
this.y = body2.bottom;
12381261

1239-
this.forcePosition = 2;
1240-
}
1241-
}
1242-
else if (blocked.up)
1243-
{
1244-
// this.y = worldBounds.y;
1245-
this.forcePosition = 2;
1262+
this.forcePosition = 5;
12461263
}
12471264
}
12481265
}
12491266
},
12501267

1268+
sleep: function (forceY)
1269+
{
1270+
if (!this.sleeping)
1271+
{
1272+
this.sleeping = true;
1273+
1274+
console.log(this.gameObject.name, 'put to sleep on frame', this.world._frame, 'force?', forceY, 'at', this.y);
1275+
1276+
this.velocity.set(0);
1277+
this.prevVelocity.set(0);
1278+
this.speed = 0;
1279+
1280+
if (forceY)
1281+
{
1282+
this.snapToBlocker();
1283+
}
1284+
}
1285+
},
1286+
12511287
getBlocker: function (blockers)
12521288
{
12531289
for (var i = 0; i < blockers.length; i++)
@@ -1384,7 +1420,7 @@ var Body = new Class({
13841420

13851421
console.log(this.gameObject.name, 'rebounded up', newVelocityY, gravityY, 'frame', this.world._frame);
13861422

1387-
if (this.forcePosition === 2)
1423+
if (this.forcePosition === 5)
13881424
{
13891425
this.forcePosition = 0;
13901426
}
@@ -1414,7 +1450,7 @@ var Body = new Class({
14141450

14151451
console.log(this.gameObject.name, 'rebounded down', newVelocityY, gravityY, 'frame', this.world._frame);
14161452

1417-
if (this.forcePosition === 2)
1453+
if (this.forcePosition === 5)
14181454
{
14191455
this.forcePosition = 0;
14201456
}
@@ -1445,7 +1481,7 @@ var Body = new Class({
14451481

14461482
console.log(this.gameObject.name, 'rebounded zero-g', newVelocityY, velocity.y);
14471483

1448-
if (this.forcePosition === 2)
1484+
if (this.forcePosition === 5)
14491485
{
14501486
this.forcePosition = 0;
14511487
}
@@ -2265,19 +2301,23 @@ var Body = new Class({
22652301
}
22662302

22672303
// We don't reposition this body if it's already blocked on a face
2268-
if (this.forcePosition === 2 || this.worldBlocked.down || this.worldBlocked.up)
2304+
if (this.forcePosition === 5 || this.worldBlocked.down || this.worldBlocked.up)
22692305
{
22702306
return this;
22712307
}
22722308

2273-
if (body2 && !collisionInfo.set)
2309+
// if (body2 && !collisionInfo.set)
2310+
if (body2)
22742311
{
22752312
console.log(this.gameObject.name, 'setBlockedUp', body2.bottom);
22762313

2314+
this.snapTo = body2;
2315+
22772316
this.y = body2.bottom;
2317+
22782318
this.forcePosition = 1;
22792319

2280-
collisionInfo.set = true;
2320+
// collisionInfo.set = true;
22812321
}
22822322
}
22832323

@@ -2306,19 +2346,23 @@ var Body = new Class({
23062346
}
23072347

23082348
// We don't reposition this body if it's already blocked on a face
2309-
if (this.forcePosition === 2 || this.worldBlocked.down || this.worldBlocked.up)
2349+
if (this.forcePosition === 5 || this.worldBlocked.down || this.worldBlocked.up)
23102350
{
23112351
return this;
23122352
}
23132353

2314-
if (body2 && !collisionInfo.set)
2354+
// if (body2 && !collisionInfo.set)
2355+
if (body2)
23152356
{
23162357
console.log(this.gameObject.name, 'setBlockedDown', body2.y);
23172358

2359+
this.snapTo = body2;
2360+
23182361
this.bottom = body2.y;
2319-
this.forcePosition = 1;
23202362

2321-
collisionInfo.set = true;
2363+
this.forcePosition = 2;
2364+
2365+
// collisionInfo.set = true;
23222366
}
23232367
}
23242368

@@ -2367,9 +2411,11 @@ var Body = new Class({
23672411

23682412
if (forceY && this.y !== worldBounds.y)
23692413
{
2370-
console.log(this.gameObject.name, 'world blocked up + position');
23712414
this.y = worldBounds.y;
2372-
this.forcePosition = 2;
2415+
2416+
this.forcePosition = 5;
2417+
2418+
console.log(this.world._frame, this.gameObject.name, 'world blocked up + position', this.y);
23732419
}
23742420

23752421
return this;
@@ -2393,7 +2439,7 @@ var Body = new Class({
23932439
{
23942440
this.bottom = worldBounds.bottom;
23952441

2396-
this.forcePosition = 2;
2442+
this.forcePosition = 5;
23972443

23982444
console.log(this.world._frame, this.gameObject.name, 'world blocked down + position', this.y);
23992445
}

0 commit comments

Comments
 (0)