Jump to content

Issue with bounce physics


soper103
 Share

Recommended Posts

Hey guys,

I'm pretty new to game dev and I am starting with Phaser. I am working on a basic Brickout type game, and am having an issue with getting the ball to bounce off of the paddle. I essentially just turned on "bounce" for the ball and turned on collision between the ball and the paddle.

For some reason, when the ball collides with the paddle, it loses a lot of its y velocity. It is moving down toward the paddle, so I expect it to rebound off of the paddle and start moving up at the same velocity. However, after the collision, it is essentially just moving side to side while moving up very slowly. I'm not sure why this is happening because the bounce is fine off other aspects of the level, I am only having this issue with the collision between the paddle and the ball. There are platforms that I load in from a tilemap, and the ball bounces off of them as expected. The relevant code can be seen below.

    create: function () {        this.map = this.game.add.tilemap('level1');        this.map.addTilesetImage('tiles', 'gameTiles');        //create layer        this.backgroundLayer = this.map.createLayer('backgroundLayer');        this.platformLayer = this.map.createLayer('platformLayer');        //collision on blockedLayer        this.map.setCollisionBetween(1, 4000, true, 'platformLayer');        this.backgroundLayer.resizeWorld();        this.paddle = this.game.add.sprite(500, 700, 'paddle');        this.game.physics.enable(this.paddle);        this.paddle.body.collideWorldBounds = true;        this.cursors = this.game.input.keyboard.createCursorKeys();        this.ball = this.game.add.sprite(500, 100, 'ball');        this.game.physics.enable(this.ball);        this.ball.body.collideWorldBounds = true;        this.ball.body.velocity.y = 100 + Math.random()*100;        this.ball.body.velocity.x = 100 + Math.random()*100;        this.ball.body.bounce.set(1.05);    },    update: function () {        this.game.physics.arcade.collide(this.paddle, this.platformLayer);        this.game.physics.arcade.collide(this.ball, this.paddle);        this.game.physics.arcade.collide(this.ball, this.platformLayer);        // Paddle movement.        this.paddle.body.velocity.x = 0;        this.paddle.body.velocity.y = 0;        if (this.cursors.left.isDown)        {            this.paddle.body.velocity.x = -300;        }        else if (this.cursors.right.isDown)        {            this.paddle.body.velocity.x = 300;        }        if (this.ball.position.y > this.paddle.position.y + 10)        {            this.ball.position.x = 500;            this.ball.position.y = 100;            this.ball.body.velocity.y = 100 + Math.random()*100;            this.ball.body.velocity.x = 100 + Math.random()*100;        }    },
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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