Jump to content

Polygon collision does not work


Recommended Posts

Hi Everyone!
Hopefully someone can help with the issue I have faced. I am building a simple racing game and want to implement collision between car and other objects. I am using p2 physics. When I create primitive body objects (e.g. body.setRectangle) collision works fine, but when i use polygon (body.loadPolygon) i have created in PhysicsEditor the car just rides throw these objects without any collisions. I have tried building very simple objects to exclude "holes" in collide, creating collision groups but unfortunately any of these things helped.

Attaching my code and a screenshot

Car class:

export default class extends Phaser.Sprite {
  constructor ({ game, x, y, asset }) {
    super(game, x, y, asset)
    game.physics.p2.enable(this, true)
    this.body.loadPolygon('physicsData', 'car')
    this.body.angle = -90
    this.cursors = game.input.keyboard.createCursorKeys()
    this.velocity = 0

Obstacle code:

  create () {
    this.game.physics.p2.defaultRestitution = 0.8
    this.map = this.game.add.tileSprite(0, 0, 2880, 1620, 'map')
    this.game.world.setBounds(0, 0, 8640, 4860)
    this.map.scale.setTo(3, 3)
    this.mapAbove = this.game.add.sprite(0, 0, 'race')
    this.game.physics.p2.enable(this.mapAbove, true)
    this.mapAbove.body.loadPolygon('physicsData', 'race')
    this.car = new Car({
      game: this.game,
      x: 3800,
      y: 3750,
      asset: 'car'


Also i am using this stuff as skeleton - https://github.com/lean/phaser-es6-webpack Could troubles be in incorrect packages?

Looking forward for Your reply,



Link to comment
Share on other sites


  • Recently Browsing   0 members

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