Skip to content

Commit dc31e39

Browse files
committed
Pushing to carry on debugging at home
1 parent 0a87a0b commit dc31e39

3 files changed

Lines changed: 159 additions & 55 deletions

File tree

src/physics/arcade/Body.js

Lines changed: 105 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ var CircleContains = require('../../geom/circle/Contains');
88
var Class = require('../../utils/Class');
99
var CONST = require('./const');
1010
var Events = require('./events');
11+
var FuzzyEqual = require('../../math/fuzzy/Equal');
1112
var FuzzyLessThan = require('../../math/fuzzy/LessThan');
1213
var FuzzyGreaterThan = require('../../math/fuzzy/GreaterThan');
1314
var RadToDeg = require('../../math/RadToDeg');
@@ -685,6 +686,15 @@ var Body = new Class({
685686
*/
686687
this.blocked = { none: true, up: false, down: false, left: false, right: false, by: null };
687688

689+
/**
690+
* Whether this Body was blocked from moving in a given direction during the last step.
691+
*
692+
* @name Phaser.Physics.Arcade.Body#wasBlocked
693+
* @type {Phaser.Physics.Arcade.Types.ArcadeBodyCollision}
694+
* @since 3.17.0
695+
*/
696+
this.wasBlocked = { none: true, up: false, down: false, left: false, right: false };
697+
688698
/**
689699
* Whether this Body is colliding with a tile or the world boundary.
690700
*
@@ -884,24 +894,25 @@ var Body = new Class({
884894
*/
885895
preUpdate: function ()
886896
{
887-
var wasTouching = this.wasTouching;
897+
console.log('preUpdate', this.wasBlocked.down);
898+
888899
var touching = this.touching;
889900
var blocked = this.blocked;
890901
var worldBlocked = this.worldBlocked;
891902

892-
// Store and reset collision flags
893-
wasTouching.none = touching.none;
894-
wasTouching.up = touching.up;
895-
wasTouching.down = touching.down;
896-
wasTouching.left = touching.left;
897-
wasTouching.right = touching.right;
898-
899903
touching.none = true;
900904
touching.up = false;
901905
touching.down = false;
902906
touching.left = false;
903907
touching.right = false;
904908

909+
blocked.by = null;
910+
blocked.none = true;
911+
blocked.up = false;
912+
blocked.down = false;
913+
blocked.left = false;
914+
blocked.right = false;
915+
905916
worldBlocked.none = true;
906917
worldBlocked.left = false;
907918
worldBlocked.right = false;
@@ -922,22 +933,21 @@ var Body = new Class({
922933
this.position.x = sprite.x + sprite.scaleX * (this.offset.x - sprite.displayOriginX);
923934
this.position.y = sprite.y + sprite.scaleY * (this.offset.y - sprite.displayOriginY);
924935

925-
if (this.collideWorldBounds)
936+
if (this.collideWorldBounds && this.checkWorldBounds())
926937
{
927-
this.checkWorldBounds();
938+
console.log('preUpdate cwb', worldBlocked.down, 'was', this.wasBlocked.down);
939+
940+
blocked.up = worldBlocked.up;
941+
blocked.down = worldBlocked.down;
942+
blocked.left = worldBlocked.left;
943+
blocked.right = worldBlocked.right;
944+
blocked.none = false;
928945
}
929946
else
930947
{
931948
this.updateCenter();
932949
}
933950

934-
blocked.by = null;
935-
blocked.up = worldBlocked.up;
936-
blocked.down = worldBlocked.down;
937-
blocked.left = worldBlocked.left;
938-
blocked.right = worldBlocked.right;
939-
blocked.none = (!blocked.up && !blocked.down && !blocked.left && !blocked.right);
940-
941951
this.rotation = sprite.rotation;
942952

943953
this.preRotation = this.rotation;
@@ -962,6 +972,8 @@ var Body = new Class({
962972
*/
963973
update: function (delta)
964974
{
975+
console.log('update', this.wasBlocked.down);
976+
965977
var velocity = this.velocity;
966978
var position = this.position;
967979

@@ -973,34 +985,72 @@ var Body = new Class({
973985
position.y += this.getMoveY(velocity.y * delta);
974986
}
975987

988+
console.log('update2', this.worldBlocked.down, 'was', this.wasBlocked.down);
989+
976990
// Calculate the delta
977991
this._dx = position.x - this.prev.x;
978992
this._dy = position.y - this.prev.y;
979993

980994
var worldBlocked = this.worldBlocked;
981995

982996
// World Bounds check
983-
if (this.collideWorldBounds && !worldBlocked.none)
997+
if (this.collideWorldBounds)
984998
{
985-
var bx = (this.worldBounce) ? -this.worldBounce.x : -this.bounce.x;
986-
var by = (this.worldBounce) ? -this.worldBounce.y : -this.bounce.y;
987-
988-
// Reverse the velocity for the bounce
999+
console.log('update3', this.wasBlocked.down, 'w', this.worldBlocked.down, 'n', this.worldBlocked.none);
9891000

990-
if ((worldBlocked.left && velocity.x < 0) || (worldBlocked.right && velocity.x > 0))
991-
{
992-
velocity.x *= bx;
993-
}
1001+
var wasBlocked = this.wasBlocked;
9941002

995-
if ((worldBlocked.down && velocity.y > 0) || (worldBlocked.up && velocity.y < 0))
1003+
if (!worldBlocked.none)
9961004
{
997-
velocity.y *= by;
1005+
var bx = (this.worldBounce) ? -this.worldBounce.x : -this.bounce.x;
1006+
var by = (this.worldBounce) ? -this.worldBounce.y : -this.bounce.y;
1007+
1008+
// Reverse the velocity for the bounce
1009+
1010+
// if ((worldBlocked.left && velocity.x < 0) || (worldBlocked.right && velocity.x > 0))
1011+
// {
1012+
// velocity.x *= bx;
1013+
// }
1014+
1015+
console.log(worldBlocked.down, wasBlocked.down);
1016+
1017+
if (worldBlocked.down && velocity.y > 0)
1018+
{
1019+
velocity.y *= by;
1020+
1021+
// vy should now be negative
1022+
1023+
console.log('down', velocity.y, this._dy, wasBlocked.down);
1024+
1025+
// if (this._dy < -this.minBounceVelocity.y)
1026+
// {
1027+
// velocity.y = 0;
1028+
// }
1029+
}
1030+
else if (worldBlocked.up && velocity.y < 0)
1031+
{
1032+
// vy should now be positive
1033+
velocity.y *= by;
1034+
1035+
console.log('up', velocity.y, this._dy, wasBlocked.up);
1036+
1037+
// if (this._dy < this.minBounceVelocity.y)
1038+
// {
1039+
// velocity.y = 0;
1040+
// }
1041+
}
9981042
}
999-
1043+
10001044
if (this.onWorldBounds)
10011045
{
10021046
this.world.emit(Events.WORLD_BOUNDS, this, worldBlocked.up, worldBlocked.down, worldBlocked.left, worldBlocked.right);
10031047
}
1048+
1049+
wasBlocked.none = worldBlocked.none;
1050+
wasBlocked.up = worldBlocked.up;
1051+
wasBlocked.down = worldBlocked.down;
1052+
wasBlocked.left = worldBlocked.left;
1053+
wasBlocked.right = worldBlocked.right;
10041054
}
10051055

10061056
this.updateCenter();
@@ -1086,6 +1136,26 @@ var Body = new Class({
10861136
gameObject.angle += this.deltaZ();
10871137
}
10881138

1139+
// Store collision flags
1140+
var wasTouching = this.wasTouching;
1141+
var wasBlocked = this.wasBlocked;
1142+
var touching = this.touching;
1143+
var worldBlocked = this.worldBlocked;
1144+
1145+
wasTouching.none = touching.none;
1146+
wasTouching.up = touching.up;
1147+
wasTouching.down = touching.down;
1148+
wasTouching.left = touching.left;
1149+
wasTouching.right = touching.right;
1150+
1151+
console.log('postUpdate', worldBlocked.down, 'was', wasBlocked.down);
1152+
1153+
// wasBlocked.none = worldBlocked.none;
1154+
// wasBlocked.up = worldBlocked.up;
1155+
// wasBlocked.down = worldBlocked.down;
1156+
// wasBlocked.left = worldBlocked.left;
1157+
// wasBlocked.right = worldBlocked.right;
1158+
10891159
this.prev.x = this.position.x;
10901160
this.prev.y = this.position.y;
10911161
},
@@ -1900,14 +1970,20 @@ var Body = new Class({
19001970

19011971
if (amount < 0 && check.up && pos.y + amount < bounds.y)
19021972
{
1973+
worldBlocked.none = false;
19031974
worldBlocked.up = true;
1975+
19041976
blocked.up = true;
1977+
19051978
return amount - ((pos.y + amount) - bounds.y);
19061979
}
19071980
else if (amount > 0 && check.down && this.bottom + amount > bounds.bottom)
19081981
{
1982+
worldBlocked.none = false;
19091983
worldBlocked.down = true;
1984+
19101985
blocked.down = true;
1986+
19111987
return amount - ((this.bottom + amount) - bounds.bottom);
19121988
}
19131989
}

src/physics/arcade/SeparateY.js

Lines changed: 47 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ var SeparateY = function (body1, body2, overlapOnly, bias)
3333

3434
var overlap = result[0];
3535
var faceTop = result[1];
36-
var faceBottom = !faceTop;
36+
// var faceBottom = !faceTop;
3737

3838
var velocity1 = body1.velocity;
3939
var velocity2 = body2.velocity;
@@ -83,9 +83,12 @@ var SeparateY = function (body1, body2, overlapOnly, bias)
8383
var ny1 = v1;
8484
var ny2 = v2;
8585

86-
if (!body1Immovable && !body2Immovable)
86+
var body1BlockedY = (blocked1.up || blocked1.down);
87+
var body2BlockedY = (blocked2.up || blocked2.down);
88+
89+
if (!body1Immovable && !body1BlockedY && !body2Immovable && !body2BlockedY)
8790
{
88-
// Neither body is immovable, so they get a new velocity based on mass
91+
// Neither body is immovable or blocked, so they get a new velocity based on mass
8992
var mass1 = body1.mass;
9093
var mass2 = body2.mass;
9194

@@ -100,27 +103,58 @@ var SeparateY = function (body1, body2, overlapOnly, bias)
100103
ny1 = avg + nv1 * body1.bounce.y;
101104
ny2 = avg + nv2 * body2.bounce.y;
102105
}
103-
else if (body1Immovable)
106+
else if (body1BlockedY || body1Immovable)
107+
{
108+
// Body1 is blocked or never changes speed, so adjust body2 speed
109+
ny2 = v1 - v2 * body2.bounce.y;
110+
}
111+
else if (body2BlockedY || body2Immovable)
104112
{
105-
// Body1 is immovable, so carries on at the same speed regardless, adjust body2 speed
106-
// ny2 = v1 - v2 * body2.bounce.y;
107-
ny2 = v1 - v2;
113+
// Body2 is blocked or never changes speed, so adjust body1 speed
114+
ny1 = v2 - v1 * body1.bounce.y;
108115
}
109-
else if (body2Immovable)
116+
else
110117
{
111-
// Body2 is immovable, so carries on at the same speed regardless, adjust body1 speed
112-
// ny1 = v2 - v1 * body1.bounce.y;
113-
ny1 = v2 - v1;
118+
// Both bodies are equally blocked
119+
console.log('Both bodies are equally blocked, kill velocity?');
114120
}
115121

116122
// Velocities calculated, time to work out what moves where
117123

124+
if (overlap !== 0)
125+
{
126+
// var p1 = (faceBottom) ? body1.bottom - body2.y : body2.bottom - body1.y;
127+
// console.log('impact', v1, v2, 'overlap', overlap, p1);
128+
129+
var share = overlap * 0.5;
130+
var amount1 = body1.getMoveY(share);
131+
var amount2 = body2.getMoveY(-share);
132+
133+
if (amount1 !== share)
134+
{
135+
// console.log('diff1', share, amount1, amount2);
136+
amount2 -= (share - amount1);
137+
}
138+
else if (amount2 !== -share)
139+
{
140+
// console.log('diff2', share, amount1, amount2);
141+
amount1 += (share + amount2);
142+
}
143+
144+
body1.y += amount1;
145+
body2.y += amount2;
146+
147+
// var p2 = (faceBottom) ? body1.bottom - body2.y : body2.bottom - body1.y;
148+
// console.log('post-impact', p2);
149+
}
150+
118151
// -------------------------------------------
119152
// 1) Bail out if nothing is blocking anything
120153
// -------------------------------------------
121154

122-
if (blocked1.none && blocked2.none)
155+
if (!body1BlockedY && !body2BlockedY)
123156
{
157+
/*
124158
if (overlap !== 0)
125159
{
126160
// var p1 = (faceBottom) ? body1.bottom - body2.y : body2.bottom - body1.y;
@@ -153,26 +187,13 @@ var SeparateY = function (body1, body2, overlapOnly, bias)
153187
// }
154188
155189
// console.log('----------------------------------');
190+
*/
156191

157192
velocity1.y = ny1;
158193
velocity2.y = ny2;
159194

160195
return true;
161196
}
162-
else if (blocked1.none)
163-
{
164-
// Body2 is blocked from moving, so Body1 needs to move
165-
166-
}
167-
else if (blocked2.none)
168-
{
169-
// Body1 is blocked from moving, so Body2 needs to move
170-
171-
}
172-
else
173-
{
174-
// Nothing can move anywhere!
175-
}
176197

177198
// -------------------------------------------
178199
// 2) Body1 motion checks

src/physics/arcade/World.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -913,6 +913,8 @@ var World = new Class({
913913
return;
914914
}
915915

916+
console.log('------->');
917+
916918
// Update all active bodies
917919
var body;
918920
var bodies = this.bodies.entries;
@@ -946,6 +948,11 @@ var World = new Class({
946948
this.step(fixedDelta);
947949
}
948950

951+
if (stepsThisFrame > 1)
952+
{
953+
console.log('extra step');
954+
}
955+
949956
this.stepsLastFrame = stepsThisFrame;
950957
},
951958

0 commit comments

Comments
 (0)