Skip to content

Commit 8c82569

Browse files
committed
Added most interesting face result
1 parent eec2f4c commit 8c82569

1 file changed

Lines changed: 104 additions & 34 deletions

File tree

src/physics/arcade/CollisionInfo.js

Lines changed: 104 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -39,100 +39,165 @@ var CollisionInfo = {
3939

4040
var leftFace = (distanceX1 > distanceX2 && prevDistanceX1 > prevDistanceX2);
4141
var topFace = (distanceY1 > distanceY2 && prevDistanceY1 > prevDistanceY2);
42+
43+
var faceX = CONST.FACING_NONE;
44+
var faceY = CONST.FACING_NONE;
4245

43-
var testX = ((leftFace && (!body1.checkCollision.left || !body2.checkCollision.right)) || (!leftFace && (!body1.checkCollision.right || !body2.checkCollision.left)));
44-
var testY = ((topFace && (!body1.checkCollision.up || !body2.checkCollision.down)) || (!topFace && (!body1.checkCollision.down || !body2.checkCollision.up)));
45-
46-
var face = CONST.FACING_NONE;
47-
var intersects = false;
48-
var touching = false;
46+
var intersects = IntersectsRect(body1, body2, 0);
47+
var touching = (intersects) ? true : IntersectsRect(body1, body2, 1);
48+
49+
var intersectsX = intersects;
50+
var intersectsY = intersects;
4951

50-
if (testX || testY)
52+
var share = 0;
53+
var shareX1 = 0;
54+
var shareX2 = 0;
55+
var shareY1 = 0;
56+
var shareY2 = 0;
57+
58+
if (leftFace)
5159
{
52-
intersects = IntersectsRect(body1, body2, 0);
53-
touching = (intersects) ? true : IntersectsRect(body1, body2, 1);
60+
faceX = CONST.FACING_LEFT;
61+
62+
// body1 left is touching body2 right
63+
if (intersectsX && body1.checkCollision.left && body2.checkCollision.right)
64+
{
65+
overlapX = distanceX2;
66+
67+
share = overlapX * 0.5;
68+
69+
if (!body1.immovable)
70+
{
71+
shareX1 = body1.getMoveX(share);
72+
}
5473

55-
// Try and give 50% separation to each body (this could be improved to give a speed ratio amount to each body)
56-
var share = 0;
57-
var share1 = 0;
58-
var share2 = 0;
74+
if (shareX1 < share)
75+
{
76+
share += (share - shareX1);
77+
}
78+
79+
if (!body2.immovable)
80+
{
81+
shareX2 = body2.getMoveX(-share);
82+
}
83+
}
84+
else
85+
{
86+
intersectsX = false;
87+
}
5988
}
89+
else
90+
{
91+
faceX = CONST.FACING_RIGHT;
92+
93+
// body1 right is touching body2 left
94+
if (intersectsX && body1.checkCollision.right && body2.checkCollision.left)
95+
{
96+
overlapX = distanceX1;
6097

98+
share = overlapX * 0.5;
99+
100+
if (!body2.immovable)
101+
{
102+
shareX2 = body2.getMoveX(share);
103+
}
104+
105+
if (shareX2 < share)
106+
{
107+
share += (share - shareX2);
108+
}
109+
110+
if (!body1.immovable)
111+
{
112+
shareX1 = body1.getMoveX(-share);
113+
}
114+
}
115+
else
116+
{
117+
intersectsX = false;
118+
}
119+
}
61120

62-
63121
if (topFace)
64122
{
65-
face = CONST.FACING_UP;
123+
faceY = CONST.FACING_UP;
66124

67125
// body1 top is touching body2 bottom
68-
if (intersects && body1.checkCollision.up && body2.checkCollision.down)
126+
if (intersectsY && body1.checkCollision.up && body2.checkCollision.down)
69127
{
70128
overlapY = distanceY2;
71129

72130
share = overlapY * 0.5;
73131

74132
if (!body1.immovable)
75133
{
76-
share1 = body1.getMoveY(share);
134+
shareY1 = body1.getMoveY(share);
77135
}
78136

79-
if (share1 < share)
137+
if (shareY1 < share)
80138
{
81-
share += (share - share1);
139+
share += (share - shareY1);
82140
}
83141

84142
if (!body2.immovable)
85143
{
86-
share2 = body2.getMoveY(-share);
144+
shareY2 = body2.getMoveY(-share);
87145
}
88146
}
89147
else
90148
{
91-
intersects = false;
149+
intersectsY = false;
92150
}
93151
}
94152
else
95153
{
96-
face = CONST.FACING_DOWN;
154+
faceY = CONST.FACING_DOWN;
97155

98156
// body1 bottom is touching body2 top
99-
if (intersects && body1.checkCollision.down && body2.checkCollision.up)
157+
if (intersectsY && body1.checkCollision.down && body2.checkCollision.up)
100158
{
101159
overlapY = distanceY1;
102160

103161
share = overlapY * 0.5;
104162

105163
if (!body2.immovable)
106164
{
107-
share2 = body2.getMoveY(share);
165+
shareY2 = body2.getMoveY(share);
108166
}
109167

110-
if (share2 < share)
168+
if (shareY2 < share)
111169
{
112-
share += (share - share2);
170+
share += (share - shareY2);
113171
}
114172

115173
if (!body1.immovable)
116174
{
117-
share1 = body1.getMoveY(-share);
175+
shareY1 = body1.getMoveY(-share);
118176
}
119177
}
120178
else
121179
{
122-
intersects = false;
180+
intersectsY = false;
123181
}
124182
}
125183

184+
var face = (overlapX < overlapY) ? faceX : faceY;
185+
126186
if (data)
127187
{
128-
data.intersects = intersects;
188+
data.intersects = (intersectsX || intersectsY);
129189
data.touching = touching;
190+
data.overlapOnly = overlapOnly;
130191
data.overlapX = overlapX;
131192
data.overlapY = overlapY;
132193
data.face = face;
194+
data.faceX = faceX;
195+
data.faceY = faceY;
133196
data.set = false;
134-
data.share1 = share1;
135-
data.share2 = share2;
197+
data.shareX1 = shareX1;
198+
data.shareX2 = shareX2;
199+
data.shareY1 = shareY1;
200+
data.shareY2 = shareY2;
136201

137202
return data;
138203
}
@@ -141,14 +206,19 @@ var CollisionInfo = {
141206
return {
142207
body1: body1,
143208
body2: body2,
144-
intersects: intersects,
209+
intersects: (intersectsX || intersectsY),
145210
touching: touching,
211+
overlapOnly: overlapOnly,
146212
overlapX: overlapX,
147213
overlapY: overlapY,
148214
face: face,
215+
faceX: faceX,
216+
faceY: faceY,
149217
set: false,
150-
share1: share1,
151-
share2: share2
218+
shareX1: shareX1,
219+
shareX2: shareX2,
220+
shareY1: shareY1,
221+
shareY2: shareY2
152222
};
153223
}
154224
},

0 commit comments

Comments
 (0)