Jump to content

Changing state with box2d - gravity


matts444
 Share

Recommended Posts

I have an issue where the gravity value doesnt change when switching between states e.g. between levels. 

 

im sure this is an issue with the plugin as the value of this.game.physics.box2d.gravity.y is changing, but the gravity change is having no effect on any of the objects.

 

this only works the first time I load the state, when I start a new state with a new gravity.y with even a minus gravity value it has no different effect.

 

Any advice would be greatly appreciated.

Link to comment
Share on other sites

Sorry. yes in the first state the gravity is set to 'this.game.physics.box2d.gravity.y = 100'. In this state I change the value of the gravity on button click(anti-gravity button) to -100. Then if I click the restart button which calls 'this.game.state.restart()', the anti gravity button changes the value of the gravity still(from 100 to -100) but the gravity change has no physical effect on the objects. Hope this is clearer :) cheers for the reply!

Link to comment
Share on other sites

I created an example of what i mean in the gravity scale example file. I used the ball sprite as the button, left for restart state and right to change the gravity. If you run it and click the right button to change the gravity, then click the left button to restart the state and you'll see that the gravity is still at -300.

 

<!doctype html>
<html>
    <head>
        <meta charset="UTF-8" />
        <title>Phaser Box2D Example</title>
        <script src="js/phaser-arcade-physics.min.js" type="text/javascript"></script>
        <script src="js/box2d-plugin-full.min.js" type="text/javascript"></script>
    </head>
    <body>
 
        <script type="text/javascript">
 
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create });
 
function preload() {
 
    game.load.image('ball', 'assets/sprites/shinyball.png');
 
}
 
function create() {
 
    game.stage.backgroundColor = '#124184';
 
    // Enable Box2D physics
    game.physics.startSystem(Phaser.Physics.BOX2D);
    game.physics.box2d.setBoundsToWorld();
    game.physics.box2d.gravity.y = 300;
    game.physics.box2d.restitution = 0.8;
 
    var sprite1 = game.add.sprite(200, 100, 'ball');
    var sprite2 = game.add.sprite(400, 100, 'ball');
    var sprite3 = game.add.sprite(600, 100, 'ball');
    var button = game.add.button(300, 300, 'ball', restart, this);
    var button = game.add.button(500, 300, 'ball', gravity, this);
 
 
    //  Enable for physics. This creates a default rectangular body.
    game.physics.box2d.enable([ sprite1, sprite2, sprite3 ]);
    
    //  Adjust the gravity scale
    sprite1.body.gravityScale = 1;
    sprite2.body.gravityScale = 0.5;
    sprite3.body.gravityScale = 0.25;
 
    game.add.text(5, 5, 'Different gravity scales for bodies.', { fill: '#ffffff', font: '14pt Arial' });
}
 
function restart() {
    game.state.restart();
}
 
function gravity() {
    game.physics.box2d.gravity.y = -300;
    
}
        </script>
 
    </body>
</html>
Link to comment
Share on other sites

  • 11 months later...

Hi Tom,

I'm not sure why but today it's working. I think maybe I was editing deploy files, so nothing was changing locally.

I tried what you said and it also worked. Though the scale seems to be different by a factor of -50

//game.physics.box2d.gravity.y = map.gravity;
//game.physics.box2d.gravity.x = map.wind;
game.physics.box2d.world.SetGravity({x: (map.wind / 50) * -1, y: (map.gravity / 50) * -1});

 

Link to comment
Share on other sites

  • 4 weeks later...

I did more testing and there certainly is a bug in the Phaser box2d plugin when setting gravity x and y. Some times it does work and others not.

I ended up with something like the following and it seems to work. ?

var gravityX = (map.wind / game.physics.box2d.ptmRatio) * -1;
var gravityY = (map.gravity / game.physics.box2d.ptmRatio) * -1
game.physics.box2d.world.SetGravity({x: gravityX, y: gravityY});

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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