1

I'm using JBox2D with LibGDX and would like to detect simultaneous collisions with a sensor. The problem I'm having is that one collision can be detected, but if any other collisions occur at the same time they don't get passed through.

E.g. Bullets hit zombies. If a zombie is colliding with a window body then the bullets hit the zombie but the collision isn't passed through to the contact listener.

Here is a simplified example for how I'm sending contact information and the bodies created.

Contact Listener

/** All current contacts */      
val contacts = ListBuffer[Contact]()

override def create(): Unit = {

  world.setContactListener(new ContactListener() {
    override def postSolve(contact: Contact, impulse: ContactImpulse): Unit = {}

    override def endContact(contact: Contact): Unit = {}

    override def beginContact(contact: Contact): Unit = {
      contacts += contact
    }

    override def preSolve(contact: Contact, oldManifold: Manifold): Unit = {

    }
  })
}

def update(): Unit ={

  /** Send all contacts to the collided objects */
  contacts.foreach(c => {
    if (c.getFixtureA != null){
      c.getFixtureA.getUserData.asInstanceOf[GameObject].collisionDetected(c)
    }
    if (c.getFixtureB != null){
      c.getFixtureB.getUserData.asInstanceOf[GameObject].collisionDetected(c)
    }
  })

  contacts.clear()
}

Collision detected implementation for bullets.

  override def collisionDetected(contact: Contact): Unit = {
    super.collisionDetected(contact)

    contact.getFixtureA.getUserData match {
      case zombie: Zombie => zombie.lowerHealth(damage)
      case _ =>
    }

    contact.getFixtureB.getUserData match {
      case zombie: Zombie => zombie.lowerHealth(damage)
      case _ =>
    }
    alive = false
  }

Bullet Body

  val bodyDef = {
    val bD = new BodyDef()
    bD.`type` = BodyDef.BodyType.DynamicBody
    bD.angularDamping = 100
    bD
  }

  lazy val polygon: Rectangle = {
    val rect = new Rectangle(sprite.getBoundingRectangle())
    rect
  }

  lazy val box2dShape: Shape = {
    val circle = new CircleShape()
    circle.setRadius(0.1f * Constants.WorldToBox)
    circle
  }

  lazy val fixtureDef: FixtureDef = {
    val fD = new FixtureDef()
    fD.filter.categoryBits = Constants.CategoryBullet
    fD.filter.maskBits = Constants.MaskBullet
    fD
  }

  lazy val fixture: Fixture = {
    fixtureDef.shape = box2dShape
    fixtureDef.density = 1f

    val f = body.createFixture(fixtureDef)

    f.setUserData(this)

    box2dShape.dispose()
    f
  }

  lazy val body: Body = controller.world.createBody(bodyDef)

  body.setBullet(true)

The bullet and zombie bodies are very similar apart from the zombie being a sensor and the bullet having body.setBullet(true).

If any more information is required let me know.

What could be the cause of this?

Michael
  • 3,411
  • 4
  • 25
  • 56

0 Answers0