Jump to content

Turn off gravity with p2 physics?


ForgeableSum
 Share

Recommended Posts

None of this seems to work:

 

guy = game.add.sprite(100, 100, 'sprite');
game.physics.startSystem(1);
game.physics.p2.enable(guy, true);
guy.body.allowGravity = false;
guy.body.gravity.x = 0;
guy.body.gravity.y = 0;
guy.body.gravity = 0; 
 
 
"guy," the sprite, always acts as if there is gravity and falls to the bottom of the game world. 
 
Am I missing something here?
Link to comment
Share on other sites

This really doesn't make any sense I've read through the docs 10x, set every property imaginable and yet still the sprite flys around when added to the game world:

 

game.physics.startSystem(Phaser.Physics.P2JS);game.physics.p2.applyGravity = false; game.physics.p2.applySpringForces = false; game.physics.p2.gravity = 0; guy = game.add.sprite(100, 100, 'sprite');game.physics.p2.enable(guy, true);game.physics.p2.enableBody(guy); guy.body.allowGravity = false;guy.body.gravity.x = 0;guy.body.gravity.y = 0;
Link to comment
Share on other sites

 

You should be able to turn off the gravity on the world you create.

world.gravity = [0, 0];

or you can set the body to be static with 

.body.mass = 0,

or by setting its velocity to 0 :

.body.velocity[0] = 0;.body.velocity[1] = 0;

I've tried all 3 of these. No luck :(

 

The sprite falls downward the moment I enable p2 physics on it. Setting the mass to 0 will cause it not to appear. 

Link to comment
Share on other sites

Hi,

you nearly got it :) You mixed some arcade methods and you aren't aware of the p2 property gravityScale.

//turn off globallygame.physics.p2.gravity.y = 0;//or thisgame.physics.p2.applyGravity = false//turn off locally (say: ignore gravity)//this property is not exposed by phaser so access //the original body from p2 with body.datasprite1.body.data.gravityScale = 0;

See the appropriate phaser example:

 
Whenever I'm searching for a property and I'm not able to find it in the documentation I switch over to 'source mode' to look for relevant methods. These are the relevant ones from P2 and Phaser:
Link to comment
Share on other sites

 

Hi,

you nearly got it :) You mixed some arcade methods and you aren't aware of the p2 property gravityScale.

//turn off globallygame.physics.p2.gravity.y = 0;//or thisgame.physics.p2.applyGravity = false//turn off locally (say: ignore gravity)//this property is not exposed by phaser so access //the original body from p2 with body.datasprite1.body.data.gravityScale = 0;

See the appropriate phaser example:

 
Whenever I'm searching for a property and I'm not able to find it in the documentation I switch over to 'source mode' to look for relevant methods. These are the relevant ones from P2 and Phaser:

 

Thanks, I guess I've gotten too used to arcade physics.

 

I tried those properties and no luck. What's odd is that the first time I loaded the screen the sprite did not move with gravity turned off. Then when I turned gravity back on, reloaded, turned it off, reloaded, it no longer acted as if gravity was turned off. Or maybe that was all in my imagination and I'm just going mad. Here's a code pen:

 

http://codepen.io/anon/pen/yNgqeV

Link to comment
Share on other sites

You're object is placed outside of the world bounds so it gets pushed inside. Therefore your object has some amount of velocity but there is no gravity at all - the object is just floating around.

 

Here two fixed versions to make this clear.

 

Gravity working:

http://codepen.io/anon/pen/WvRKxr

 

and then gravity disabled on the object with gravity scale.

http://codepen.io/anon/pen/jPyprr

Link to comment
Share on other sites

You're object is placed outside of the world bounds so it gets pushed inside. Therefore your object has some amount of velocity but there is no gravity at all - the object is just floating around.

 

Here two fixed versions to make this clear.

 

Gravity working:

http://codepen.io/anon/pen/WvRKxr

 

and then gravity disabled on the object with gravity scale.

http://codepen.io/anon/pen/jPyprr

Wow, the velocity set because of the world bounds really threw me off. It all makes sense now, thanks! 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...