Skip to content

Commit

Permalink
Sorted out Body gravity settings and updated the example.
Browse files Browse the repository at this point in the history
  • Loading branch information
photonstorm committed Mar 11, 2014
1 parent 6bddf1a commit e9ae465
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 289 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ New features:
* 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 :)
* ScaleManager.fullScreenTarget allows you to change the DOM element that the fullscreen API is called on (feature request #526)
* Merged Georges p2 BodyDebug and reformatted for jshint pass. Looks awesome :)
* ArcadePhysics.Body has a new gravityScale property, which is a modifier multiplied against the world gravity value on a Body.


Updates:
Expand Down
36 changes: 22 additions & 14 deletions examples/arcade physics/gravity.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

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

function preload() {

Expand All @@ -10,6 +10,7 @@ function preload() {
var sprite1;
var sprite2;
var sprite3;
var sprite4;

function create() {

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

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

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

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

// Sprite 3 will use both the global gravity and its own value
sprite3 = game.add.sprite(500, 32, 'ilkke');

// We obviously need to enable physics on those sprites
game.physics.enable([sprite1,sprite2,sprite3], Phaser.Physics.ARCADE);
// Sprite 4 will ignore all gravity
sprite4 = game.add.sprite(700, 96, 'ilkke');

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

sprite1.body.collideWorldBounds = true;
sprite1.body.bounce.y = 0.8;


sprite2.body.collideWorldBounds = true;
sprite2.body.bounce.y = 0.8;
sprite2.body.allowGravity = false;
sprite3.body.gravityScale.y = 10;

sprite2.body.gravity.y = 200;

sprite3.body.collideWorldBounds = true;
sprite3.body.bounce.y = 0.8;
sprite3.body.gravityScale.y = 10;
sprite3.body.gravityScale.y = 3;


sprite4.body.allowGravity = false;

}

function render() {

game.debug.text('world gravity', sprite1.x - 32, 64);
game.debug.text('local gravity', sprite2.x - 32, 64);
game.debug.text('gravityScale', sprite3.x - 32, 64);
game.debug.text('no gravity', sprite4.x - 32, 64);

}
92 changes: 0 additions & 92 deletions examples/arcade physics/quadtree - ids.js

This file was deleted.

4 changes: 0 additions & 4 deletions src/Phaser.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@ var Phaser = Phaser || {
SPRITEBATCH: 17,
RETROFONT: 18,

// DYNAMIC: 1,
// STATIC: 2,
// KINEMATIC: 4,

// The various blend modes supported by pixi / phaser
blendModes: {
NORMAL:0,
Expand Down
2 changes: 2 additions & 0 deletions src/core/Game.js
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,8 @@ Phaser.Game.prototype = {

this.time.update(time);

this.debug.preUpdate();

this.render();

},
Expand Down
4 changes: 2 additions & 2 deletions src/physics/arcade/Body.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,13 @@ Phaser.Physics.Arcade.Body = function (sprite) {
this.drag = new Phaser.Point();

/**
* @property {boolean} allowGravity - Allow this Body to be influenced by world gravity?
* @property {boolean} allowGravity - Allow this Body to be influenced by gravity? Either world or local.
* @default
*/
this.allowGravity = true;

/**
* @property {Phaser.Point} gravity - A local gravity applied to this Body. If set this over-rides any world gravity.
* @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.
*/
this.gravity = new Phaser.Point(0, 0);

Expand Down
31 changes: 17 additions & 14 deletions src/physics/arcade/World.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,22 +245,25 @@ Phaser.Physics.Arcade.prototype = {
body.rotation += (body.angularVelocity * this.game.time.physicsElapsed);

// Apply gravity using the p2 style gravityScale
if (body.gravity.x !== 0)
if (body.allowGravity)
{
body.velocity.x += body.gravity.x * this.game.time.physicsElapsed;
}
else
{
body.velocity.x += this.gravity.x * this.game.time.physicsElapsed * body.gravityScale.x;
}
if (body.gravity.x !== 0)
{
body.velocity.x += body.gravity.x * this.game.time.physicsElapsed;
}
else
{
body.velocity.x += this.gravity.x * this.game.time.physicsElapsed * body.gravityScale.x;
}

if (body.gravity.y !== 0)
{
body.velocity.y += body.gravity.y * this.game.time.physicsElapsed;
}
else
{
body.velocity.y += this.gravity.y * this.game.time.physicsElapsed * body.gravityScale.y;
if (body.gravity.y !== 0)
{
body.velocity.y += body.gravity.y * this.game.time.physicsElapsed;
}
else
{
body.velocity.y += this.gravity.y * this.game.time.physicsElapsed * body.gravityScale.y;
}
}

// Apply velocity
Expand Down
Loading

0 comments on commit e9ae465

Please sign in to comment.