Jump to content

Prevent a game object to be affected by the velocity


Pau
 Share

Recommended Posts

Hello,

I am making a kind of flappy bird. I want the pipes to move only horizontally. They have physics, so i can use the setVelocityX method. But they are falling down because of the gravity.  I can change the position changing the x value of the pipe in the update method, but i would like to use the setVelocityX, it is more clear.

var columna = this.physics.add.group();
...
columna.setVelocityX(-200);

Thanks!

Link to comment
Share on other sites

I'm assuming you are using Arcade physics here, but please correct me if I'm wrong.

There are a couple of ways to use gravity in arcade physics. You can either set it up in the game's config object so that it applies to every dynamic physics body, or, you can apply it to individual physics bodies.

In a flappy bird like game, the only game entity that you want to have gravity is the bird itself. So if that's the case, I think it makes sense to set the gravity to 0 in the game's config object, and then individually set the gravity for the bird itself.

Link to comment
Share on other sites

Hi @Pau ,

Adding to what they have said above, in the collision of the bird with the pipe you will have to add a callback to adjust the velocityY and prevent the pipe from moving vertically.

In my case, when there is a collision, I do not set the velocity.Y, I simply set the velocity.X of the pipes to 0 and let the collided pipe go up or down freely.

Link to comment
Share on other sites

Sorry, these solutions are not working for me.

I think it was my fault, because i posted so few lines of code. In the following code  you can see what i have made trying to implement your proposals:

function nuevaColumna() {
        var columna = this.physics.add.group({ gravityY: 0 });

        var hueco = Math.floor(Math.random() * 5) + 1;
        for (var i = 0; i < 8; i++) {
            //El agujero son dos casillas, por eso ponemos hole +1
            if (i != hueco && i != hueco + 1) {
                var cubo = columna.create(800, i * 60 + 10, 'pipe',{ gravityY: 0 });
                cubo.allowGravity = false;
            }
        }
        columna.setVelocityX(-200);

I have attached the whole "game" too, in order to prevent misunderstandings.

Thank so much for your help.

exercise.zip

Link to comment
Share on other sites

Hola @Pau ,

With this 2 changes the code works (just like @snowbillr said.):


// In config variable:

var config = {
        type: Phaser.AUTO,
        width: 800,
        height: 600,
        scene: {
            preload: preload,
            create: create
        },
        physics: {
            default: 'arcade',
            arcade: {
                debug: true,
                gravity: { y: 0 } /* <<<<<<<<<< --- before 400, now by default gravity is 0 */
            }
        }
    };


// In create function:

function create() {

        var pajaro = this.physics.add.sprite(50, 100, 'pajaro');
        pajaro.setGravity(0,400); /* <<<<<<<<<<< --- */

        /*** More code here ***/
};

Tested:

screenshot.png.301368bde42bf7b314df8cfd4a5ee1d1.png

Regards.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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