Skip to content

Commit 3a46297

Browse files
committed
Moved World methods into includes folder for tidying-up. Made SeparateXY and GetOverlapXY stand-alone functions for easy updating.
1 parent 8c0eaf3 commit 3a46297

17 files changed

Lines changed: 954 additions & 903 deletions

v3/src/physics/arcade/World.js

Lines changed: 107 additions & 903 deletions
Large diffs are not rendered by default.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
var Clamp = require('../../../math/Clamp');
2+
3+
var CircleBodyIntersects = function (circle, body)
4+
{
5+
var x = Clamp(circle.center.x, body.left, body.right);
6+
var y = Clamp(circle.center.y, body.top, body.bottom);
7+
8+
var dx = (circle.center.x - x) * (circle.center.x - x);
9+
var dy = (circle.center.y - y) * (circle.center.y - y);
10+
11+
return (dx + dy) <= (circle.halfWidth * circle.halfWidth);
12+
};
13+
14+
module.exports = CircleBodyIntersects;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
var Collide = function (object1, object2, collideCallback, processCallback, callbackContext)
2+
{
3+
if (collideCallback === undefined) { collideCallback = null; }
4+
if (processCallback === undefined) { processCallback = null; }
5+
if (callbackContext === undefined) { callbackContext = collideCallback; }
6+
7+
this._total = 0;
8+
9+
this.collideObjects(object1, object2, collideCallback, processCallback, callbackContext, false);
10+
11+
return (this._total > 0);
12+
};
13+
14+
module.exports = Collide;
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
var CollideHandler = function (object1, object2, collideCallback, processCallback, callbackContext, overlapOnly)
2+
{
3+
// Only collide valid objects
4+
if (object2 === undefined && object1.isParent)
5+
{
6+
return this.collideGroupVsSelf(object1, collideCallback, processCallback, callbackContext, overlapOnly);
7+
}
8+
9+
// If neither of the objects are set then bail out
10+
if (!object1 || !object2)
11+
{
12+
return;
13+
}
14+
15+
// A Body
16+
if (object1.body)
17+
{
18+
if (object2.body)
19+
{
20+
this.collideSpriteVsSprite(object1, object2, collideCallback, processCallback, callbackContext, overlapOnly);
21+
}
22+
else if (object2.isParent)
23+
{
24+
this.collideSpriteVsGroup(object1, object2, collideCallback, processCallback, callbackContext, overlapOnly);
25+
}
26+
else if (object2.isTilemap)
27+
{
28+
this.collideSpriteVsTilemapLayer(object1, object2, collideCallback, processCallback, callbackContext, overlapOnly);
29+
}
30+
}
31+
// GROUPS
32+
else if (object1.isParent)
33+
{
34+
if (object2.body)
35+
{
36+
this.collideSpriteVsGroup(object2, object1, collideCallback, processCallback, callbackContext, overlapOnly);
37+
}
38+
else if (object2.isParent)
39+
{
40+
this.collideGroupVsGroup(object1, object2, collideCallback, processCallback, callbackContext, overlapOnly);
41+
}
42+
else if (object2.isTilemap)
43+
{
44+
this.collideGroupVsTilemapLayer(object1, object2, collideCallback, processCallback, callbackContext, overlapOnly);
45+
}
46+
}
47+
// TILEMAP LAYERS
48+
else if (object1.isTilemap)
49+
{
50+
if (object2.body)
51+
{
52+
this.collideSpriteVsTilemapLayer(object2, object1, collideCallback, processCallback, callbackContext, overlapOnly);
53+
}
54+
else if (object2.isParent)
55+
{
56+
this.collideGroupVsTilemapLayer(object2, object1, collideCallback, processCallback, callbackContext, overlapOnly);
57+
}
58+
}
59+
};
60+
61+
module.exports = CollideHandler;
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
var CollideObjects = function (object1, object2, collideCallback, processCallback, callbackContext, overlapOnly)
2+
{
3+
var i;
4+
var object1isArray = Array.isArray(object1);
5+
var object2isArray = Array.isArray(object2);
6+
7+
if (!object1isArray && !object2isArray)
8+
{
9+
// Neither of them are arrays - do this first as it's the most common use-case
10+
this.collideHandler(object1, object2, collideCallback, processCallback, callbackContext, overlapOnly);
11+
}
12+
else if (!object1isArray && object2isArray)
13+
{
14+
// Object 2 is an Array
15+
for (i = 0; i < object2.length; i++)
16+
{
17+
this.collideHandler(object1, object2[i], collideCallback, processCallback, callbackContext, overlapOnly);
18+
}
19+
}
20+
else if (object1isArray && !object2isArray)
21+
{
22+
// Object 1 is an Array
23+
for (i = 0; i < object1.length; i++)
24+
{
25+
this.collideHandler(object1[i], object2, collideCallback, processCallback, callbackContext, overlapOnly);
26+
}
27+
}
28+
else
29+
{
30+
// They're both arrays
31+
for (i = 0; i < object1.length; i++)
32+
{
33+
for (var j = 0; j < object2.length; j++)
34+
{
35+
this.collideHandler(object1[i], object2[j], collideCallback, processCallback, callbackContext, overlapOnly);
36+
}
37+
}
38+
}
39+
};
40+
41+
module.exports = CollideObjects;
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
var CollideSpriteVsGroup = function (sprite, group, collideCallback, processCallback, callbackContext, overlapOnly)
2+
{
3+
if (group.length === 0)
4+
{
5+
return;
6+
}
7+
8+
var bodyA = sprite.body;
9+
10+
// Does sprite collide with anything?
11+
12+
var minMax = this.treeMinMax;
13+
14+
minMax.minX = bodyA.left;
15+
minMax.minY = bodyA.top;
16+
minMax.maxX = bodyA.right;
17+
minMax.maxY = bodyA.bottom;
18+
19+
var results = this.tree.search(minMax);
20+
21+
if (results.length < 2)
22+
{
23+
return;
24+
}
25+
26+
var children = group.getChildren();
27+
28+
for (var i = 0; i < children.length; i++)
29+
{
30+
var bodyB = children[i].body;
31+
32+
if (!bodyB || bodyA === bodyB || results.indexOf(bodyB) === -1)
33+
{
34+
continue;
35+
}
36+
37+
if (this.separate(bodyA, bodyB, processCallback, callbackContext, overlapOnly))
38+
{
39+
if (collideCallback)
40+
{
41+
collideCallback.call(callbackContext, bodyA.gameObject, bodyB.gameObject);
42+
}
43+
44+
this._total++;
45+
}
46+
}
47+
};
48+
49+
module.exports = CollideSpriteVsGroup;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
var CollideSpriteVsSprite = function (sprite1, sprite2, collideCallback, processCallback, callbackContext, overlapOnly)
2+
{
3+
if (!sprite1.body || !sprite2.body)
4+
{
5+
return false;
6+
}
7+
8+
if (this.separate(sprite1.body, sprite2.body, processCallback, callbackContext, overlapOnly))
9+
{
10+
if (collideCallback)
11+
{
12+
collideCallback.call(callbackContext, sprite1, sprite2);
13+
}
14+
15+
this._total++;
16+
}
17+
18+
return true;
19+
};
20+
21+
module.exports = CollideSpriteVsSprite;
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
var ComputeVelocity = function (axis, body, velocity, acceleration, drag, max)
2+
{
3+
if (max === undefined) { max = 10000; }
4+
5+
if (axis === 1 && body.allowGravity)
6+
{
7+
velocity += (this.gravity.x + body.gravity.x) * this.delta;
8+
}
9+
else if (axis === 2 && body.allowGravity)
10+
{
11+
velocity += (this.gravity.y + body.gravity.y) * this.delta;
12+
}
13+
14+
if (acceleration)
15+
{
16+
velocity += acceleration * this.delta;
17+
}
18+
else if (drag && body.allowDrag)
19+
{
20+
drag *= this.delta;
21+
22+
if (velocity - drag > 0)
23+
{
24+
velocity -= drag;
25+
}
26+
else if (velocity + drag < 0)
27+
{
28+
velocity += drag;
29+
}
30+
else
31+
{
32+
velocity = 0;
33+
}
34+
}
35+
36+
if (velocity > max)
37+
{
38+
velocity = max;
39+
}
40+
else if (velocity < -max)
41+
{
42+
velocity = -max;
43+
}
44+
45+
return velocity;
46+
};
47+
48+
module.exports = ComputeVelocity;
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
var GetOverlapX = function (body1, body2, overlapOnly, bias)
2+
{
3+
var overlap = 0;
4+
var maxOverlap = body1.deltaAbsX() + body2.deltaAbsX() + bias;
5+
6+
if (body1.deltaX() === 0 && body2.deltaX() === 0)
7+
{
8+
// They overlap but neither of them are moving
9+
body1.embedded = true;
10+
body2.embedded = true;
11+
}
12+
else if (body1.deltaX() > body2.deltaX())
13+
{
14+
// Body1 is moving right and / or Body2 is moving left
15+
overlap = body1.right - body2.x;
16+
17+
if ((overlap > maxOverlap && !overlapOnly) || body1.checkCollision.right === false || body2.checkCollision.left === false)
18+
{
19+
overlap = 0;
20+
}
21+
else
22+
{
23+
body1.touching.none = false;
24+
body1.touching.right = true;
25+
body2.touching.none = false;
26+
body2.touching.left = true;
27+
}
28+
}
29+
else if (body1.deltaX() < body2.deltaX())
30+
{
31+
// Body1 is moving left and/or Body2 is moving right
32+
overlap = body1.x - body2.width - body2.x;
33+
34+
if ((-overlap > maxOverlap && !overlapOnly) || body1.checkCollision.left === false || body2.checkCollision.right === false)
35+
{
36+
overlap = 0;
37+
}
38+
else
39+
{
40+
body1.touching.none = false;
41+
body1.touching.left = true;
42+
body2.touching.none = false;
43+
body2.touching.right = true;
44+
}
45+
}
46+
47+
// Resets the overlapX to zero if there is no overlap, or to the actual pixel value if there is
48+
body1.overlapX = overlap;
49+
body2.overlapX = overlap;
50+
51+
return overlap;
52+
};
53+
54+
module.exports = GetOverlapX;
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
var GetOverlapY = function (body1, body2, overlapOnly)
2+
{
3+
var overlap = 0;
4+
var maxOverlap = body1.deltaAbsY() + body2.deltaAbsY() + this.OVERLAP_BIAS;
5+
6+
if (body1.deltaY() === 0 && body2.deltaY() === 0)
7+
{
8+
// They overlap but neither of them are moving
9+
body1.embedded = true;
10+
body2.embedded = true;
11+
}
12+
else if (body1.deltaY() > body2.deltaY())
13+
{
14+
// Body1 is moving down and/or Body2 is moving up
15+
overlap = body1.bottom - body2.y;
16+
17+
if ((overlap > maxOverlap && !overlapOnly) || body1.checkCollision.down === false || body2.checkCollision.up === false)
18+
{
19+
overlap = 0;
20+
}
21+
else
22+
{
23+
body1.touching.none = false;
24+
body1.touching.down = true;
25+
body2.touching.none = false;
26+
body2.touching.up = true;
27+
}
28+
}
29+
else if (body1.deltaY() < body2.deltaY())
30+
{
31+
// Body1 is moving up and/or Body2 is moving down
32+
overlap = body1.y - body2.bottom;
33+
34+
if ((-overlap > maxOverlap && !overlapOnly) || body1.checkCollision.up === false || body2.checkCollision.down === false)
35+
{
36+
overlap = 0;
37+
}
38+
else
39+
{
40+
body1.touching.none = false;
41+
body1.touching.up = true;
42+
body2.touching.none = false;
43+
body2.touching.down = true;
44+
}
45+
}
46+
47+
// Resets the overlapY to zero if there is no overlap, or to the actual pixel value if there is
48+
body1.overlapY = overlap;
49+
body2.overlapY = overlap;
50+
51+
return overlap;
52+
};
53+
54+
module.exports = GetOverlapY;

0 commit comments

Comments
 (0)