Hello Guest

Author Topic: Multiple hitboxes  (Read 6695 times)

0 Members and 1 Guest are viewing this topic.

Fatesauce

  • Member
  • Posts: 26
    • View Profile
Multiple hitboxes
« on: July 06, 2015, 02:40:15 PM »
Is it possible to set more than one hitbox per entity without creating a new entity for the extra hitbox?

My reasoning is, I want to have an Entity with a normal hitbox (for collisions, projectiles etc), and a larger hitbox surrounding the Entity which I will use to detect the Entities proximity to things.

If we cant, thats fine, but I think it would be a good feature to have ;)

Fruckert

  • Member
  • Posts: 60
  • Total Square
    • View Profile
Re: Multiple hitboxes
« Reply #1 on: July 06, 2015, 03:46:19 PM »
Yes, it is!
Just make more than one collider, and use AddCollider.

Commanderstitch

  • Member
  • Posts: 37
  • Personal Text
    • View Profile
    • commanderstitch Homepage (coming someday)
Re: Multiple hitboxes
« Reply #2 on: July 06, 2015, 07:11:19 PM »
NO WAY THAT"S AWEOMSE
- Commanderstitch

Kyle

  • Administrator
  • Member
  • Posts: 258
    • View Profile
Re: Multiple hitboxes
« Reply #3 on: July 06, 2015, 07:45:59 PM »
Yep, you can use SetHitbox as a shortcut function to set the collider to a BoxCollider -- but you can also do AddCollider() to add as many colliders as you want to an Entity.  If you have multiple colliders though the shortcut functions will only use the first available collider on the list of colliders.

Commanderstitch

  • Member
  • Posts: 37
  • Personal Text
    • View Profile
    • commanderstitch Homepage (coming someday)
Re: Multiple hitboxes
« Reply #4 on: July 06, 2015, 08:02:06 PM »
Is there a tips and tricks thread anywhere? what other cool secrets are hiding in this library i wonder? :D
- Commanderstitch

Kyle

  • Administrator
  • Member
  • Posts: 258
    • View Profile
Re: Multiple hitboxes
« Reply #5 on: July 06, 2015, 10:35:29 PM »
I really should just be making examples, but I always feel like Otter is changing a lot still and examples would quickly become outdated...

Fatesauce

  • Member
  • Posts: 26
    • View Profile
Re: Multiple hitboxes
« Reply #6 on: July 11, 2015, 05:40:54 PM »
Another question on this.  I have 2 hit boxes:

Code: [Select]
BoxCollider collider1 = new BoxCollider(46, 47, (int)ColType.PLAYER);
            collider1.SetPosition(37, 5);
            BoxCollider collider2 = new BoxCollider(80, 57, (int)ColType.PLAYER);
            collider2.SetPosition(20, 52);
            AddColliders(collider1, collider2);

Now im not really sure how to retrieve the 2nd collider in my update method.  I noticed after a little testing that only the first collider is being checked.
I had a play around and could come across anything too obvious to me.  From what I gather, AddColliders adds the objects to a List, and the Collider function only calls the 1st index of that list.  How about the rest?

Fruckert

  • Member
  • Posts: 60
  • Total Square
    • View Profile
Re: Multiple hitboxes
« Reply #7 on: July 11, 2015, 06:30:28 PM »
I always keep the colliders in some private fields on the entity class.

Fatesauce

  • Member
  • Posts: 26
    • View Profile
Re: Multiple hitboxes
« Reply #8 on: July 12, 2015, 07:42:45 PM »
yeah but what do you use to check collisions for each box?

Kyle

  • Administrator
  • Member
  • Posts: 258
    • View Profile
Re: Multiple hitboxes
« Reply #9 on: July 12, 2015, 07:55:29 PM »
The collider itself can check for collisions.  If you use Entity.Collide() it's actually just using the first available Collider.  You can store a reference to each collider, and then use them individually.  For example if you had one collider for checking for platforms, and another collider for checking for power ups, you could maybe do something like:

Code: [Select]
// in your entity
BoxCollider colliderPlatforms = new BoxCollider(...);
BoxCollider colliderPickups = new BoxCollider(...);
// later in update or something:
if (colliderPlatforms.Overlap(...)) { // do platformer stuff }
if (colliderPickups.Overlap(...)) { // pick up the power up }

Of course that's very rough pseudocode but hopefully it gives you a general idea of how it works.