Skip to content

Commit a1579c8

Browse files
committed
Merged the impact physics runner
1 parent 001a0cb commit a1579c8

13 files changed

Lines changed: 693 additions & 1 deletion

File tree

v3/src/checksum.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
var CHECKSUM = {
2-
build: 'b1e0db40-5608-11e7-9343-73f81cbe0a9c'
2+
build: '93f8c470-56db-11e7-a08e-87dbe325345b'
33
};
44
module.exports = CHECKSUM;

v3/src/physics/impact/Body.js

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
// Phaser.Physics.Impact.Body
2+
3+
var Class = require('../../utils/Class');
4+
var UpdateVelocity = require('./UpdateVelocity');
5+
var UpdateMotion = require('./UpdateMotion');
6+
var Trace = require('./Trace');
7+
var COLLIDES = require('./COLLIDES');
8+
var TYPE = require('./TYPE');
9+
10+
/**
11+
* An Impact.js compatible physics body.
12+
* This re-creates the properties you'd get on an Entity and the math needed to update them.
13+
*
14+
* @class
15+
*/
16+
17+
var Body = new Class({
18+
19+
initialize:
20+
21+
function Body (world, x, y)
22+
{
23+
this.world = world;
24+
25+
this.enabled = true;
26+
27+
this.size = { x: 16, y: 16 };
28+
this.offset = { x: 0, y: 0 };
29+
this.pos = { x: x, y: y };
30+
this.last = { x: 0, y: 0 };
31+
this.vel = { x: 0, y: 0 };
32+
this.accel = { x: 0, y: 0 };
33+
this.friction = { x: 0, y: 0 };
34+
this.maxVel = { x: 100, y: 100 };
35+
this.gravityFactor = 1;
36+
this.standing = false;
37+
this.bounciness = 0;
38+
this.minBounceVelocity = 40;
39+
40+
this.type = TYPE.NONE;
41+
this.checkAgainst = TYPE.NONE;
42+
this.collides = COLLIDES.NEVER;
43+
44+
// min 44 deg, max 136 deg
45+
this.slopeStanding = { min: 0.767944870877505, max: 2.3736477827122884 };
46+
},
47+
48+
update: function (delta)
49+
{
50+
this.last.x = this.pos.x;
51+
this.last.y = this.pos.y;
52+
53+
this.vel.y += this.world.gravity * delta * this.gravityFactor;
54+
55+
UpdateVelocity(this, delta);
56+
57+
var mx = this.vel.x * delta;
58+
var my = this.vel.y * delta;
59+
60+
var res = Trace(this.pos.x, this.pos.y, mx, my, this.size.x, this.size.y);
61+
62+
UpdateMotion(this, res);
63+
},
64+
65+
skipHash: function ()
66+
{
67+
return (!this.enabled || (this.type === 0 && this.checkAgainst === 0 && this.collides === 0));
68+
},
69+
70+
touches: function (other)
71+
{
72+
return !(
73+
this.pos.x >= other.pos.x + other.size.x ||
74+
this.pos.x + this.size.x <= other.pos.x ||
75+
this.pos.y >= other.pos.y + other.size.y ||
76+
this.pos.y + this.size.y <= other.pos.y
77+
);
78+
},
79+
80+
setVelocityX: function (x)
81+
{
82+
this.vel.x = x;
83+
84+
return this;
85+
},
86+
87+
setVelocityY: function (y)
88+
{
89+
this.vel.y = y;
90+
91+
return this;
92+
},
93+
94+
setVelocity: function (x, y)
95+
{
96+
this.vel.x = x;
97+
this.vel.y = y;
98+
99+
return this;
100+
},
101+
102+
setMaxVelocity: function (x, y)
103+
{
104+
if (y === undefined) { y = x; }
105+
106+
this.maxVel.x = x;
107+
this.maxVel.y = y;
108+
109+
return this;
110+
},
111+
112+
setTypeNone: function ()
113+
{
114+
this.type = TYPE.NONE;
115+
116+
return this;
117+
},
118+
119+
setTypeA: function ()
120+
{
121+
this.type = TYPE.A;
122+
123+
return this;
124+
},
125+
126+
setTypeB: function ()
127+
{
128+
this.type = TYPE.B;
129+
130+
return this;
131+
},
132+
133+
setCheckAgainstNone: function ()
134+
{
135+
this.checkAgainst = TYPE.NONE;
136+
137+
return this;
138+
},
139+
140+
setCheckAgainstA: function ()
141+
{
142+
this.checkAgainst = TYPE.A;
143+
144+
return this;
145+
},
146+
147+
setCheckAgainstB: function ()
148+
{
149+
this.checkAgainst = TYPE.B;
150+
151+
return this;
152+
},
153+
154+
setCollidesNever: function ()
155+
{
156+
this.collides = COLLIDES.NEVER;
157+
158+
return this;
159+
},
160+
161+
setLite: function ()
162+
{
163+
this.collides = COLLIDES.LITE;
164+
165+
return this;
166+
},
167+
168+
setPassive: function ()
169+
{
170+
this.collides = COLLIDES.PASSIVE;
171+
172+
return this;
173+
},
174+
175+
setActive: function ()
176+
{
177+
this.collides = COLLIDES.ACTIVE;
178+
179+
return this;
180+
},
181+
182+
setFixed: function ()
183+
{
184+
this.collides = COLLIDES.FIXED;
185+
186+
return this;
187+
},
188+
189+
check: function( other ) {},
190+
191+
collideWith: function( other, axis ) {}
192+
193+
});
194+
195+
module.exports = Body;

v3/src/physics/impact/COLLIDES.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Collision Types - Determine if and how entities collide with each other
2+
3+
// In ACTIVE vs. LITE or FIXED vs. ANY collisions, only the "weak" entity moves,
4+
// while the other one stays fixed. In ACTIVE vs. ACTIVE and ACTIVE vs. PASSIVE
5+
// collisions, both entities are moved. LITE or PASSIVE entities don't collide
6+
// with other LITE or PASSIVE entities at all. The behaiviour for FIXED vs.
7+
// FIXED collisions is undefined.
8+
9+
module.exports = {
10+
11+
NEVER: 0,
12+
LITE: 1,
13+
PASSIVE: 2,
14+
ACTIVE: 4,
15+
FIXED: 8
16+
17+
};

v3/src/physics/impact/SeperateX.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
var Trace = require('./Trace');
2+
3+
var SeperateX = function (world, left, right, weak)
4+
{
5+
var nudge = left.pos.x + left.size.x - right.pos.x;
6+
7+
// We have a weak entity, so just move this one
8+
if (weak)
9+
{
10+
var strong = (left === weak) ? right : left;
11+
12+
weak.vel.x = -weak.vel.x * weak.bounciness + strong.vel.x;
13+
14+
var resWeak = Trace(weak.pos.x, weak.pos.y, weak === left ? -nudge : nudge, 0, weak.size.x, weak.size.y);
15+
16+
weak.pos.x = resWeak.pos.x;
17+
}
18+
else
19+
{
20+
var v2 = (left.vel.x - right.vel.x) / 2;
21+
22+
left.vel.x = -v2;
23+
right.vel.x = v2;
24+
25+
var resLeft = Trace(left.pos.x, left.pos.y, -nudge / 2, 0, left.size.x, left.size.y);
26+
27+
left.pos.x = Math.floor(resLeft.pos.x);
28+
29+
var resRight = Trace(right.pos.x, right.pos.y, nudge / 2, 0, right.size.x, right.size.y);
30+
31+
right.pos.x = Math.ceil(resRight.pos.x);
32+
}
33+
};
34+
35+
module.exports = SeperateX;

v3/src/physics/impact/SeperateY.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
var Trace = require('./Trace');
2+
3+
var SeperateY = function (world, top, bottom, weak)
4+
{
5+
var nudge = (top.pos.y + top.size.y - bottom.pos.y);
6+
7+
if (weak)
8+
{
9+
var strong = (top === weak) ? bottom : top;
10+
11+
weak.vel.y = -weak.vel.y * weak.bounciness + strong.vel.y;
12+
13+
// Riding on a platform?
14+
var nudgeX = 0;
15+
16+
if (weak === top && Math.abs(weak.vel.y - strong.vel.y) < weak.minBounceVelocity)
17+
{
18+
weak.standing = true;
19+
nudgeX = strong.vel.x * world.delta;
20+
}
21+
22+
var resWeak = Trace(weak.pos.x, weak.pos.y, nudgeX, weak == top ? -nudge : nudge, weak.size.x, weak.size.y);
23+
24+
weak.pos.y = resWeak.pos.y;
25+
weak.pos.x = resWeak.pos.x;
26+
}
27+
else if (world.gravity && (bottom.standing || top.vel.y > 0))
28+
{
29+
var resTop = Trace(top.pos.x, top.pos.y, 0, -(top.pos.y + top.size.y - bottom.pos.y), top.size.x, top.size.y);
30+
31+
top.pos.y = resTop.pos.y;
32+
33+
if (top.bounciness > 0 && top.vel.y > top.minBounceVelocity)
34+
{
35+
top.vel.y *= -top.bounciness;
36+
}
37+
else
38+
{
39+
top.standing = true;
40+
top.vel.y = 0;
41+
}
42+
}
43+
else
44+
{
45+
var v2 = (top.vel.y - bottom.vel.y) / 2;
46+
47+
top.vel.y = -v2;
48+
bottom.vel.y = v2;
49+
50+
var nudgeX = bottom.vel.x * world.delta;
51+
52+
var resTop = Trace(top.pos.x, top.pos.y, nudgeX, -nudge/2, top.size.x, top.size.y);
53+
54+
top.pos.y = resTop.pos.y;
55+
56+
var resBottom = Trace(bottom.pos.x, bottom.pos.y, 0, nudge/2, bottom.size.x, bottom.size.y);
57+
58+
bottom.pos.y = resBottom.pos.y;
59+
}
60+
};
61+
62+
module.exports = SeperateY;

v3/src/physics/impact/Solver.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
var SeperateX = require('./SeperateX');
2+
var SeperateY = require('./SeperateY');
3+
var COLLIDES = require('./COLLIDES');
4+
var TYPE = require('./TYPE');
5+
6+
// Impact Physics Solver
7+
8+
var Solver = function (world, bodyA, bodyB)
9+
{
10+
var weak = null;
11+
12+
if (bodyA.collides === COLLIDES.LITE || bodyB.collides === COLLIDES.FIXED)
13+
{
14+
weak = bodyA;
15+
}
16+
else if (bodyB.collides === COLLIDES.LITE || bodyA.collides === COLLIDES.FIXED)
17+
{
18+
weak = bodyB;
19+
}
20+
21+
if (bodyA.last.x + bodyA.size.x > bodyB.last.x && bodyA.last.x < bodyB.last.x + bodyB.size.x)
22+
{
23+
if (bodyA.last.y < bodyB.last.y)
24+
{
25+
SeperateY(world, bodyA, bodyB, weak);
26+
}
27+
else
28+
{
29+
SeperateY(world, bodyB, bodyA, weak);
30+
}
31+
32+
bodyA.collideWith(bodyB, 'y');
33+
bodyB.collideWith(bodyA, 'y');
34+
}
35+
else if (bodyA.last.y + bodyA.size.y > bodyB.last.y && bodyA.last.y < bodyB.last.y + bodyB.size.y)
36+
{
37+
if (bodyA.last.x < bodyB.last.x)
38+
{
39+
SeperateX(world, bodyA, bodyB, weak);
40+
}
41+
else
42+
{
43+
SeperateX(world, bodyB, bodyA, weak);
44+
}
45+
46+
bodyA.collideWith(bodyB, 'x');
47+
bodyB.collideWith(bodyA, 'x');
48+
}
49+
};
50+
51+
module.exports = Solver;

v3/src/physics/impact/TYPE.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Collision Types - Determine if and how entities collide with each other
2+
3+
// In ACTIVE vs. LITE or FIXED vs. ANY collisions, only the "weak" entity moves,
4+
// while the other one stays fixed. In ACTIVE vs. ACTIVE and ACTIVE vs. PASSIVE
5+
// collisions, both entities are moved. LITE or PASSIVE entities don't collide
6+
// with other LITE or PASSIVE entities at all. The behaiviour for FIXED vs.
7+
// FIXED collisions is undefined.
8+
9+
module.exports = {
10+
11+
NONE: 0,
12+
A: 1,
13+
B: 2,
14+
BOTH: 3
15+
16+
};

v3/src/physics/impact/Trace.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
var Trace = function (x, y, vx, vy)
2+
{
3+
return {
4+
collision: { x: false, y: false, slope: false },
5+
pos: { x: x + vx, y: y + vy },
6+
tile: { x: 0, y: 0 }
7+
};
8+
};
9+
10+
module.exports = Trace;

0 commit comments

Comments
 (0)