Swinging a sword hitbox - ideas?

I’ve started to create a combat system in my game and I’m having problem with my sword hitbox. The way I’m swinging the sword is like this:

Any ideas on how would I go about creating a hitbox for this attack?

You can try something like this:

Where the Red and Green points are the points defining the sword at the start of the swing, and the Blue and Orange points are the points at the end of the swing.

Afterwards, just use the four points to trace a polygon you can use for collision detection.

If you want a more elaborate shape, you could actually rotate the points with respect to the character’s shoulder, and get a couple more point samples to get a more curved slash.

All this if you want to calculate this in real time. Sometimes it’s easier to just define the area by hand and just use that collision area when needed.

The line RED-YELLOW should be curved outwards.

You can use trig functions and “moving”, hardcoded hitbox. Depends on what effect you want to achieve, though.

Can’t you just measure the distance from the hilt of the sword to the enemy next to you and if it’s close enough and to the right of the sword you kill it or w/e.

If you’d read, you’ll see that he already covered that.

Use radius with r = sprite’s arm’s length (uh weird English here).

Okay, thanks everyone for your tips. I will try to implement the idea from Oskuro.

If the game is simple enough, you don’t even need a sword hitbox. Just use the player hitbox (since your sword isn’t separately added).


if player attack
     if player facing enemy
          enemy.hp -= 1

An example of Jimmt’s post.


public void collision(GObject other)
{
    if (other instanceof Enemy)
    {
        if (isAttacking())
        {
            switch (this.x > other.x ? 0 : 1)
            {
                case 0:
                    // Player is on right of enemy. So direction should be left.
                    if (this.direction == DIRECTION_LEFT)
                    {
                        other.health--;
                    }
                    else
                    {
                        this.health--;
                    }
                    break;
                case 1:
                    // Player is on left of enemy. So direction should be right.
                    if (this.direction == DIRECTION_RIGHT)
                    {
                        other.health--;
                    }
                    else
                    {
                        this.health--;
                    }
                    break;
            }
        }
        else
        {
            this.health--;
        }
    }
}

This method is to be called when the player hits any collidable object.

other instanceof Enemy

Just dont :frowning:

Normally… ok normally
you just need a box that only appears during the right time
you may also consider a sphere collider
just make it appropriate in size and for most uses it will be fine

Street Fighter used aabbs and it worked fine.

But why?? Can you explain?

Instanceof usually indicates bad class structure. Instead of having one big method with 2 conditions -


class Foo {
     public Bar(Object a){
          if(a instanceof String){
           //string related stuff
          }
          if(b instanceof Integer){
           //Integer related stuff
          }
     }

}

It’s better to have subclasses and to override the methods.


class StringFoo extends Foo{
     @Override
     public Bar(){
      //do whatever is string related
     }
}
class IntegerFoo extends Foo{
     @Override
     public Bar(){
     //do whatever is Integer related
}

}

Well there you go.
Good to know actually :smiley:

But… isn’t “object instanceof Enemy” better than “object.getType()==EntityType.ENEMY” when all entities are hardcoded?

They’re pretty much the same…it’s just that instanceof always is like this, while == obviously has several uses.

But I love the usage of [icode]instanceof[/icode] for simple things. I can’t create a new class for every object’s collision. I’m used to it and are there any other benefits of avoiding it?