Skip to content

Commit e9ae465

Browse files
committed
Sorted out Body gravity settings and updated the example.
1 parent 6bddf1a commit e9ae465

8 files changed

Lines changed: 44 additions & 289 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ New features:
149149
* Tweens are now bound to their own TweenManager, not always the global game one. So you can create your own managers now (for you clark :)
150150
* ScaleManager.fullScreenTarget allows you to change the DOM element that the fullscreen API is called on (feature request #526)
151151
* Merged Georges p2 BodyDebug and reformatted for jshint pass. Looks awesome :)
152+
* ArcadePhysics.Body has a new gravityScale property, which is a modifier multiplied against the world gravity value on a Body.
152153

153154

154155
Updates:

examples/arcade physics/gravity.js

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create });
2+
var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, render: render });
33

44
function preload() {
55

@@ -10,6 +10,7 @@ function preload() {
1010
var sprite1;
1111
var sprite2;
1212
var sprite3;
13+
var sprite4;
1314

1415
function create() {
1516

@@ -19,33 +20,40 @@ function create() {
1920
game.physics.arcade.gravity.y = 100;
2021

2122
// Sprite 1 will use the World (global) gravity
22-
sprite1 = game.add.sprite(300, 32, 'ilkke');
23+
sprite1 = game.add.sprite(100, 96, 'ilkke');
2324

2425
// Sprite 2 is set to ignore the global gravity and use its own value
25-
sprite2 = game.add.sprite(400, 32, 'ilkke');
26+
sprite2 = game.add.sprite(300, 96, 'ilkke');
2627

28+
// Sprite 3 will use both the world gravity and its own gravityScale modifier
29+
sprite3 = game.add.sprite(500, 96, 'ilkke');
2730

28-
// Sprite 3 will use both the global gravity and its own value
29-
sprite3 = game.add.sprite(500, 32, 'ilkke');
30-
31-
// We obviously need to enable physics on those sprites
32-
game.physics.enable([sprite1,sprite2,sprite3], Phaser.Physics.ARCADE);
31+
// Sprite 4 will ignore all gravity
32+
sprite4 = game.add.sprite(700, 96, 'ilkke');
3333

34+
// Enable physics on those sprites
35+
game.physics.enable( [ sprite1, sprite2, sprite3, sprite4 ], Phaser.Physics.ARCADE);
3436

3537
sprite1.body.collideWorldBounds = true;
3638
sprite1.body.bounce.y = 0.8;
37-
3839

3940
sprite2.body.collideWorldBounds = true;
4041
sprite2.body.bounce.y = 0.8;
41-
sprite2.body.allowGravity = false;
42-
sprite3.body.gravityScale.y = 10;
43-
42+
sprite2.body.gravity.y = 200;
4443

4544
sprite3.body.collideWorldBounds = true;
4645
sprite3.body.bounce.y = 0.8;
47-
sprite3.body.gravityScale.y = 10;
46+
sprite3.body.gravityScale.y = 3;
4847

49-
48+
sprite4.body.allowGravity = false;
5049

5150
}
51+
52+
function render() {
53+
54+
game.debug.text('world gravity', sprite1.x - 32, 64);
55+
game.debug.text('local gravity', sprite2.x - 32, 64);
56+
game.debug.text('gravityScale', sprite3.x - 32, 64);
57+
game.debug.text('no gravity', sprite4.x - 32, 64);
58+
59+
}

examples/arcade physics/quadtree - ids.js

Lines changed: 0 additions & 92 deletions
This file was deleted.

src/Phaser.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,6 @@ var Phaser = Phaser || {
3838
SPRITEBATCH: 17,
3939
RETROFONT: 18,
4040

41-
// DYNAMIC: 1,
42-
// STATIC: 2,
43-
// KINEMATIC: 4,
44-
4541
// The various blend modes supported by pixi / phaser
4642
blendModes: {
4743
NORMAL:0,

src/core/Game.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,8 @@ Phaser.Game.prototype = {
599599

600600
this.time.update(time);
601601

602+
this.debug.preUpdate();
603+
602604
this.render();
603605

604606
},

src/physics/arcade/Body.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,13 @@ Phaser.Physics.Arcade.Body = function (sprite) {
124124
this.drag = new Phaser.Point();
125125

126126
/**
127-
* @property {boolean} allowGravity - Allow this Body to be influenced by world gravity?
127+
* @property {boolean} allowGravity - Allow this Body to be influenced by gravity? Either world or local.
128128
* @default
129129
*/
130130
this.allowGravity = true;
131131

132132
/**
133-
* @property {Phaser.Point} gravity - A local gravity applied to this Body. If set this over-rides any world gravity.
133+
* @property {Phaser.Point} gravity - A local gravity applied to this Body. If non-zero this over rides any world gravity, unless Body.allowGravity is set to false.
134134
*/
135135
this.gravity = new Phaser.Point(0, 0);
136136

src/physics/arcade/World.js

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -245,22 +245,25 @@ Phaser.Physics.Arcade.prototype = {
245245
body.rotation += (body.angularVelocity * this.game.time.physicsElapsed);
246246

247247
// Apply gravity using the p2 style gravityScale
248-
if (body.gravity.x !== 0)
248+
if (body.allowGravity)
249249
{
250-
body.velocity.x += body.gravity.x * this.game.time.physicsElapsed;
251-
}
252-
else
253-
{
254-
body.velocity.x += this.gravity.x * this.game.time.physicsElapsed * body.gravityScale.x;
255-
}
250+
if (body.gravity.x !== 0)
251+
{
252+
body.velocity.x += body.gravity.x * this.game.time.physicsElapsed;
253+
}
254+
else
255+
{
256+
body.velocity.x += this.gravity.x * this.game.time.physicsElapsed * body.gravityScale.x;
257+
}
256258

257-
if (body.gravity.y !== 0)
258-
{
259-
body.velocity.y += body.gravity.y * this.game.time.physicsElapsed;
260-
}
261-
else
262-
{
263-
body.velocity.y += this.gravity.y * this.game.time.physicsElapsed * body.gravityScale.y;
259+
if (body.gravity.y !== 0)
260+
{
261+
body.velocity.y += body.gravity.y * this.game.time.physicsElapsed;
262+
}
263+
else
264+
{
265+
body.velocity.y += this.gravity.y * this.game.time.physicsElapsed * body.gravityScale.y;
266+
}
264267
}
265268

266269
// Apply velocity

0 commit comments

Comments
 (0)