Jump to content
This forum will be closing down. Please move to the respective dedicated project forums.

Having trouble with P2 Physics Restitution


Recommended Posts

Hi guys

I'm trying to learn how to do a bounce in P2 physics.

I have it working with bodies, that are setup specifically in the scenes init() function.

I'm however having trouble, when I want to create a ball 'class' and then spawn the balls on a click, while having them bounce of each other.

I don't understand how to setup the P2 material inside a class, so it bounces against itself.

If anyone could help, that would be awesome :)

Link to comment
Share on other sites

Hi @enpu

I have pretty much everything working, but I can't figure out how to make the balls (created out of the ball class), to bounce against each other.

game.createClass('S05Ball', {
    init: function(x, y) {
        this.sprite = new game.Sprite('s05Ball.png');
        this.sprite.position.set(x, y);
        this.body = new game.Body({
            mass: 1,
            ccdSpeedThreshold: 1,
            position: [
                this.sprite.position.x / game.scene.world.ratio,
                this.sprite.position.y / game.scene.world.ratio
        this.body.velocity[1] = 1;
        this.body.restitution = 1;
        this.shape = new p2.Circle({
            radius: 16 / 2 / game.scene.world.ratio,
            material: new p2.Material()
        game.scene.world.addContactMaterial(new p2.ContactMaterial(game.scene.wallBShape.material, this.shape.material, {
            restitution: 0.25,
            stiffness: Number.MAX_VALUE // We need infinite stiffness to get exact restitution
    remove: function() {
    update: function() {
        this.sprite.position.x = this.body.position[0] * game.scene.world.ratio;
        this.sprite.position.y = this.body.position[1] * game.scene.world.ratio;
        this.sprite.rotation = this.body.angle;
        if (this.sprite.position.y > 320) {


Link to comment
Share on other sites


  • Recently Browsing   0 members

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