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

P2 Colision detection only when impact is on top of the sprite


Recommended Posts


I wan't to make simple game with phaser.

Player is on bottom of the page and items are falling down from top and player should catch them.

But when I setup collision with p2 of course whole player is colliding with items, but I want just to detect touch on top not on sides.


//EDIT: Best would be to fire different event when the sprite touch part of the mask just from one direction.


Here is how I create player and items, this is just test so I'm using basic body mask.


// start the physics enginethis.physics.startSystem(Phaser.Physics.P2JS);// set the global gravitythis.physics.p2.gravity.y = 200;this.physics.p2.defaultRestitution = 0.8;this.physics.p2.setImpactEvents(true);this._playerCollisionGroup = this.physics.p2.createCollisionGroup();this._itemsCollisionGroup = this.physics.p2.createCollisionGroup();this.physics.p2.updateBoundsCollisionGroup();this._player = this.add.sprite(0, 930, 'player-idle');this.physics.p2.enable(this._player);this._player.body.fixedRotation = true;this._player.body.velocity.y = 0;this._player.body.velocity.x = 1;this._player.body.collideWorldBounds = true;this._player.body.setCircle(28);this._player.body.mass = 10;this._player.body.setZeroDamping();this._player.body.setCollisionGroup(this._playerCollisionGroup);this._player.body.collides(this._itemsCollisionGroup, this.hitItem, this);// create new group for itemsthis._itemGroup = this.add.group();this._itemGroup.enableBody = true;this._itemGroup.physicsBodyType = Phaser.Physics.P2JS;CatchGame.item.spawnItem(this);this.camera.follow(this._player);


var item = game._itemGroup.create(Math.floor(Math.random()*CatchGame.GAME_WIDTH), 0, 'items');item.animations.add('anim', [itemType], 10, true);item.animations.play('anim');item.body.collideWorldBounds = false;item.body.setRectangle(40, 40);item.body.setCollisionGroup(game._itemsCollisionGroup);item.body.collides([game._playerCollisionGroup]);item.body.velocity.x = 0;item.body.velocity.y = 1;item.checkWorldBounds = true;
Link to comment
Share on other sites


  • Recently Browsing   0 members

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