2
\$\begingroup\$

I am working on a 2D game in LibGDX. Currently, I am testing interactions between a player's weapon and enemies - in particular, a sword and an enemy monster.

The player has a hurtbox and the sword has a hitbox. The player's hitbox and hurtbox are filtered so that they do not interact with each other. However, the p layer's hitbox and hurtbox are supposed to interact with the monster. However, a collision between the sword and the monster is only registered sometimes - specifically, four times when I launch the application, and then no more collisions will register. However, if the player bumps into the monster so that the monster gains some velocity, collisions will register again until the enemy stops moving.

Is there a reason for this / is there a pattern here for when collisions are registering and when they're not that I'm missing?

Note: I have tried both creating the hitbox only when the player is attacking, and having the hitbox be inactive until the player is attacking, then making it active, then setting it to be inactive again. Both cases result in the same failure to register all collisions.

This is the general code for the creation of the hitbox fixture:

    CircleShape shape = new CircleShape();
    shape.setRadius(25f * Constants.MPP);
    shape.setPosition(new Vector2(0f, 0f).scl(Constants.MPP));

    FixtureDef fixture = new FixtureDef();
    fixture.filter.categoryBits = Constants.CATEGORY_PLAYER_WEAPON;
    fixture.filter.maskBits = (short) (Constants.CATEGORY_MONSTER);
    fixture.isSensor = true;
    fixture.shape = shape;
    f = hitbox.createFixture(fHitbox);

    hitboxShape.dispose();

Thanks in advance!

\$\endgroup\$
1
  • \$\begingroup\$ Are you using the Box2D DebugRenderer? If not, you cannot be sure, that there is actually a collision, you might draw at the wrong position. Also, did you implement the beginContact and endContact methods? Are they called, when a collision begins/ends? \$\endgroup\$
    – Robert P
    May 5, 2015 at 13:45

0

You must log in to answer this question.

Browse other questions tagged .