Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (576)
games submitted by our members
Games in WIP (497)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Swinging a sword hitbox - ideas?  (Read 1562 times)
0 Members and 1 Guest are viewing this topic.
Offline TotalSpelNerd

Senior Newbie


Exp: 3 years



« Posted 2013-07-14 19:17:36 »

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?
Offline Oskuro

JGO Coder


Medals: 32
Exp: 6 years


Coding in Style


« Reply #1 - Posted 2013-07-14 22:27:10 »

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.

Offline HeroesGraveDev

JGO Kernel


Medals: 212
Projects: 11
Exp: 2 years


If it wasn't Awesome, it wasn't me.


« Reply #2 - Posted 2013-07-14 23:33:05 »



The line RED-YELLOW should be curved outwards.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Mac70
« Reply #3 - Posted 2013-07-14 23:47:44 »

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

Check out my Devblog! Smiley
Offline jonjava
« Reply #4 - Posted 2013-07-14 23:52:06 »

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.

Offline Troncoso

JGO Coder


Medals: 20



« Reply #5 - Posted 2013-07-15 01:57:45 »

The line RED-YELLOW should be curved outwards.

If you'd read, you'll see that he already covered that.
Offline ReBirth
« Reply #6 - Posted 2013-07-15 05:12:51 »

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

Offline TotalSpelNerd

Senior Newbie


Exp: 3 years



« Reply #7 - Posted 2013-07-15 10:51:03 »

Okay, thanks everyone for your tips. I will try to implement the idea from Oskuro.
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 114
Projects: 4
Exp: 3 years



« Reply #8 - Posted 2013-07-15 16:30:34 »

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).
1  
2  
3  
if player attack
     if player facing enemy
          enemy.hp -= 1
 
Offline SHC
« Reply #9 - Posted 2013-07-15 17:29:29 »

An example of Jimmt's post.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
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.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline RobinB

JGO Knight


Medals: 37
Projects: 1
Exp: 3 years


Spacegame in progress


« Reply #10 - Posted 2013-07-15 19:43:57 »

1  
other instanceof Enemy


Just dont Sad
Offline Cero
« Reply #11 - Posted 2013-07-15 21:34:45 »

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

Offline relminator
« Reply #12 - Posted 2013-07-16 04:24:13 »

Street Fighter used aabbs and it worked fine. 
Offline SHC
« Reply #13 - Posted 2013-07-16 04:58:05 »

1  
other instanceof Enemy

Just dont Sad

But why?? Can you explain?

Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 114
Projects: 4
Exp: 3 years



« Reply #14 - Posted 2013-07-16 05:05:30 »

Instanceof usually indicates bad class structure. Instead of having one big method with 2 conditions -
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
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.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
class StringFoo extends Foo{
     @Override
     public Bar(){
      //do whatever is string related
    }
}
class IntegerFoo extends Foo{
     @Override
     public Bar(){
     //do whatever is Integer related
}

}
Offline Cero
« Reply #15 - Posted 2013-07-16 09:27:50 »

Street Fighter used aabbs and it worked fine. 
Well there you go.
Good to know actually Cheesy

Offline Orangy Tang

JGO Kernel


Medals: 51
Projects: 11


Monkey for a head


« Reply #16 - Posted 2013-07-16 15:32:20 »

http://www.java-gaming.org/topics/instanceof-is-it-ever-ok/27219/view.html

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline Mac70
« Reply #17 - Posted 2013-07-16 16:09:10 »

But... isn't "object instanceof Enemy" better than "object.getType()==EntityType.ENEMY" when all entities are hardcoded?

Check out my Devblog! Smiley
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 114
Projects: 4
Exp: 3 years



« Reply #18 - Posted 2013-07-16 16:12:29 »

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.
Offline SHC
« Reply #19 - Posted 2013-07-16 16:38:48 »

But I love the usage of
instanceof
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?

Offline RobinB

JGO Knight


Medals: 37
Projects: 1
Exp: 3 years


Spacegame in progress


« Reply #20 - Posted 2013-07-16 19:11:28 »

The problem with instanceof is bad seperation of logic.
You already have a class called Enemy, so i dont know why you are talking about new classes.

1  
if (other instanceof Enemy)

could be changed to:
1  
if (other.isAggro())

This way the logic of the object stays in the object itself, instead of random refrences in some classes to make up propertys.
One time instanceof would not seem bad, but it will keep creeping into other classes if you keep used to it.


Also, when you want to add fucntionality, like blinded enemys (not able to hit), you need to update all these instanceof Enemy references to something like:
1  
if (other instanceof Enemy && !((Enemy)other).isBlinded())

Feels a lot worse right?

What if we expand this even more:
1  
if ((other instanceof Enemy && !((Enemy)other).isBlinded()) || (other instanceof Player && ((Player)other).hasRage()) || (other instanceof Rock && ((Rock)other).isEvil()) || (other instanceof Turret && ((Turret)other).isActivated()))

Kill it!


Best practise is to make Instances from classes as less known as possible.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (15 views)
2014-04-15 18:08:23

BurntPizza (13 views)
2014-04-15 03:46:01

UprightPath (27 views)
2014-04-14 17:39:50

UprightPath (12 views)
2014-04-14 17:35:47

Porlus (29 views)
2014-04-14 15:48:38

tom_mai78101 (51 views)
2014-04-10 04:04:31

BurntPizza (110 views)
2014-04-08 23:06:04

tom_mai78101 (211 views)
2014-04-05 13:34:39

trollwarrior1 (179 views)
2014-04-04 12:06:45

CJLetsGame (185 views)
2014-04-01 02:16:10
List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:05:20
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!