Skip to content

Commit f7329e0

Browse files
committed
Further physics modifications
1 parent 5526a73 commit f7329e0

5 files changed

Lines changed: 124 additions & 100 deletions

File tree

examples/wip/demo worm.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ var ad = 0;
1818

1919
function create() {
2020

21-
bmd = game.add.bitmapData(640, 480);
21+
bmd = game.add.bitmapData(800, 600);
2222

23-
for (var i = 0; i < 30; i++)
23+
for (var i = 0; i < 60; i++)
2424
{
2525
particles.push(new Phaser.Point(0, 0));
2626
}
@@ -31,6 +31,8 @@ function create() {
3131

3232
function mycircle(context, x, y, R, color) {
3333

34+
//R = 64;
35+
3436
context.fillStyle = color;
3537
context.beginPath();
3638
context.arc(x, y, R, 0, Math.PI * 2, true);
@@ -49,22 +51,26 @@ function update() {
4951
{
5052
var p = particles[t];
5153

52-
p.x = Math.sin(n) * 50 + Math.cos(n * 1.5) * 200;
53-
p.y = Math.sin(n / 2) * 20 + Math.sin(n * 2) * 150;
54+
// p.x = Math.sin(n) * 50 + Math.cos(n * 1.5) * 300;
55+
// p.y = Math.sin(n / 2) * 20 + Math.sin(n * 2) * 250;
56+
57+
p.x = Math.cos(n) * 50 + Math.sin(n * 1.5) * 300;
58+
p.y = Math.cos(n / 2) * 20 + Math.cos(n * 2) * 250;
5459

5560
var tx = p.x;
5661
var ty = p.y;
5762

5863
bmd.context.globalCompositeOperation = 'xor';
5964

60-
mycircle(bmd.context, p.x + 320, p.y + 240, Math.sin(t * 360 / particles.length / 2 * Math.PI / 180) * 60, 'rgba(255, 255, 255, 1)');
65+
//mycircle(bmd.context, p.x + 400, p.y + 300, Math.sin(t * 360 / particles.length / 2 * Math.PI / 180) * 50, 'rgba(255, 255, 0, 1)');
66+
mycircle(bmd.context, p.x + 400, p.y + 300, Math.sin(t * 360 / particles.length / 2 * Math.PI / 180) * 50, 'rgba(255, 255, 0, 1)');
6167

62-
n += 0.1;
68+
n += 0.05;
6369

64-
bmd.context.globalCompositeOperation = 'source-over';
70+
//bmd.context.globalCompositeOperation = 'source-over';
6571
}
6672

67-
n = oldn + 0.03;
73+
n = oldn + 0.02;
6874

6975
}
7076

examples/wip/physics-motion.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ function create() {
1818
bmd.fillStyle('#ffffff');
1919
game.add.sprite(0, 0, bmd);
2020

21-
sprite = game.add.sprite(32, 600, 'chunk');
21+
sprite = game.add.sprite(732, 0, 'chunk');
2222

2323
sprite.body.collideWorldBounds = true;
2424
sprite.body.bounce.setTo(0.5, 0.5);
25-
sprite.body.drag.setTo(0, -20);
26-
// sprite.body.drag.setTo(2, 0);
25+
//sprite.body.drag.setTo(0, -20);
26+
sprite.body.drag.setTo(5, 0);
2727
// sprite.body.sleepMin.setTo(-50, -20);
2828
// sprite.body.sleepMax.setTo(50, 20);
2929
// sprite.body.sleepDuration = 1000;
@@ -38,9 +38,9 @@ function create() {
3838

3939
function launch() {
4040

41-
sprite.body.velocity.setTo(0, -300);
41+
sprite.body.velocity.setTo(-100, 300);
4242

43-
// sprite.body.gravity.setTo(0, 200);
43+
sprite.body.gravity.setTo(0, -100);
4444

4545
}
4646

src/physics/arcade/ArcadePhysics.js

Lines changed: 12 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,8 @@ Phaser.Physics.Arcade.prototype = {
221221

222222
if (body.allowGravity)
223223
{
224-
this._gravityX = (this.gravity.x + body.gravity.x);
225-
this._gravityY = (this.gravity.y + body.gravity.y);
224+
this._gravityX = (this.gravity.x + body.gravity.x) * this.game.time.physicsElapsed;
225+
this._gravityY = (this.gravity.y + body.gravity.y) * this.game.time.physicsElapsed;
226226
}
227227
else
228228
{
@@ -236,61 +236,22 @@ Phaser.Physics.Arcade.prototype = {
236236
// body.rotation += (body.angularVelocity * this.game.time.physicsElapsed);
237237
// body.angularVelocity += this._velocityDelta;
238238

239-
// body.motionVelocity.x = (this.computeVelocity(body, body.velocity.x, body.acceleration.x, body.drag.x, body.maxVelocity.x, this._gravityX) - body.velocity.x) * 0.5;
240-
// body.motionVelocity.y = (this.computeVelocity(body, body.velocity.y, body.acceleration.y, body.drag.y, body.maxVelocity.y, this._gravityY) - body.velocity.y) * 0.5;
241-
242239
// velocity = velocity + gravity*delta_time/2
243240
// position = position + velocity*delta_time
244241
// velocity = velocity + gravity*delta_time/2
245242

243+
body.velocity.x += this._gravityX;
244+
body.velocity.y += this._gravityY;
245+
246+
body.motionVelocity.x = body.acceleration.x * this.game.time.physicsElapsed;
247+
body.motionVelocity.y = body.acceleration.y * this.game.time.physicsElapsed;
248+
246249
// temp = acc*dt
247250
// pos = pos + dt*(vel + temp/2)
248251
// vel = vel + temp
249252

250-
this._drag = 0;
251-
252-
if (body.drag.x !== 0 && body.velocity.x !== 0 && body.acceleration.x === 0)
253-
{
254-
if (body.velocity.x - body.drag.x > 0)
255-
{
256-
this._drag = -body.drag.x;
257-
}
258-
else if (body.velocity.x + body.drag.x < 0)
259-
{
260-
this._drag = body.drag.x;
261-
}
262-
}
263-
264-
// body.motionVelocity.x = body.acceleration.x + this._gravityX - body.drag.x * this.game.time.physicsElapsed;
265-
// body.motionVelocity.x = body.acceleration.x + this._gravityX * this.game.time.physicsElapsed;
266-
body.motionVelocity.x = (body.acceleration.x + this._gravityX + this._drag) * this.game.time.physicsElapsed;
267-
268-
this._drag = 0;
269-
270-
if (body.drag.y !== 0 && body.velocity.y !== 0 && body.acceleration.y === 0)
271-
{
272-
if (body.velocity.y - body.drag.y > 0)
273-
{
274-
this._drag = -body.drag.y;
275-
}
276-
else if (body.velocity.y + body.drag.y < 0)
277-
{
278-
this._drag = body.drag.y;
279-
}
280-
}
281-
282-
// body.motionVelocity.y = body.acceleration.y + this._gravityY - body.drag.y * this.game.time.physicsElapsed;
283-
// body.motionVelocity.y = body.acceleration.y + this._gravityY * this.game.time.physicsElapsed;
284-
body.motionVelocity.y = (body.acceleration.y + this._gravityY + this._drag) * this.game.time.physicsElapsed;
285-
286-
// if (body.sleeping === false)
287-
// {
288-
// body.x = body.x + this.game.time.physicsElapsed * (body.velocity.x + body.motionVelocity.x / 2);
289-
// body.velocity.x = body.velocity.x + body.motionVelocity.x;
290-
291-
// body.y = body.y + this.game.time.physicsElapsed * (body.velocity.y + body.motionVelocity.y / 2);
292-
// body.velocity.y = body.velocity.y + body.motionVelocity.y;
293-
// }
253+
// body.motionVelocity.x = (body.acceleration.x + this._gravityX) * this.game.time.physicsElapsed;
254+
// body.motionVelocity.y = (body.acceleration.y + this._gravityY) * this.game.time.physicsElapsed;
294255

295256
},
296257

@@ -306,11 +267,11 @@ Phaser.Physics.Arcade.prototype = {
306267
* @param {number} gravity - The acceleration due to gravity. Gravity will not induce drag.
307268
* @return {number} The altered Velocity value.
308269
*/
309-
computeVelocity: function (body, velocity, acceleration, drag, max) {
270+
computeVelocity: function (body, velocity, acceleration, drag, max, gravity) {
310271

311272
max = max || 10000;
312273

313-
velocity += acceleration;
274+
velocity += (acceleration + gravity) * this.game.time.physicsElapsed;
314275

315276
if (acceleration === 0 && drag !== 0)
316277
{
@@ -330,15 +291,6 @@ Phaser.Physics.Arcade.prototype = {
330291
}
331292
}
332293

333-
if (velocity > max)
334-
{
335-
velocity = max;
336-
}
337-
else if (velocity < -max)
338-
{
339-
velocity = -max;
340-
}
341-
342294
return velocity;
343295

344296
},

0 commit comments

Comments
 (0)