Lefota

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.anchor.setTo(0.5)
    this.body.clearShapes()
    this.body.loadPolygon('physicsData', 'car')
    this.body.angle = -90
    this.cursors = game.input.keyboard.createCursorKeys()
    this.velocity = 0
}

Obstacle code:

  create () {
    this.game.physics.startSystem(Phaser.Physics.P2JS)
    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.clearShapes()
    this.mapAbove.body.loadPolygon('physicsData', 'race')
    this.car = new Car({
      game: this.game,
      x: 3800,
      y: 3750,
      asset: 'car'
    })

    this.game.add.existing(this.car)
    this.game.camera.follow(this.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,

Thanks!

collides.png

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.