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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

  • Recently Browsing   0 members

    No registered users viewing this page.