How do I have more than one Hitbox?


(Secretmapper) #1

Short of creating another entity, how would one have more than one hitbox? For example, a person hitbox and a sight hitbox in one entity.

In the docs, there is a mask property “that could be used for specialized collisions”. Now how would one go about doing that?

Thanks.


(Secretmapper) #2

In this thread it was suggested to have a new Hitbox use its collide method directly. The only problem is that the collide method of the Mask Class (and Hitbox) takes in a mask as an argument. The entities collide method however takes in a collision type (a string).

Getting under the hood I realized that the entity class does a sort of doubly linked list with the entities and checks one by one if the mask collides with the entities mask. I was pretty surprised by this :slight_smile: I thought Flashpunk was using some sort of quad tree implementation. Well I’m not optimizing just yet, you know what they say, premature ones are evil.

So that’s that, it involves using World methods to get the mask and using collide.


(JP Mortiboys) #3

I too was originally surprised when I saw that the collision system is very basic, but a test involving a few hundred entities shooting bullets at each other showed me that the rendering system is much slower than the collision system, and for normal games rendering will always be the bottleneck, especially if you use the type parameter correctly (of course, should FP move to Stage3d this will be different).

Anyway, the way I see the multiple collision mask situation is this : you have “passive” masks and “active” masks. A passive mask is how other entities see you - your hitbox, or whatever. Active masks are masks you use to collide with other things, that the rest of the world doesn’t really need to know about.

Multiple passive masks are the real problem - say for example you want localised damage so your head is more vulnerable than your body. You could either create a sub-entity for the head and forward collision messages on to the main entity, or have a single passive mask and separate the cases out yourself - both require additional code (less for the latter, but unlike the former wouldn’t allow a different entity to selectively target the head - useful if you’re handling a big boss character where the head is very far from the rest of the body).

Multiple active masks, on the other hand, are easy-peasy. Since the outside world doesn’t need to know about them, just hold an internal reference and swap your mask property around before calling the collide function, then swap back to your passive mask at the end of the update function:

public class SnifferThing extends Entity {
  private var passiveMask: Mask; // The area we can get hit in
  private var sniffMask: Mask; // The area we can smell stuff in
  private var personalSpaceMask: Mask; // How far away we want to stay from things

  override public function update():void {
    var e:Entity;
    if (feelsLikeSniffing()) {
      mask = sniffMask;
      if (e = collide("smelly", x, y)) {
        // We can smell something
      }
    }

    // Handle movement
    mask = personalSpaceMask;
    moveBy(dx, dy, "solid", true);

    // Set mask back so other entities can collide with us
    mask = passiveMask;
  }
}

Note that if you just wanted OOBB hitboxes you could store your masks as Rects and use setHitboxTo, it would work the same way.