1
\$\begingroup\$

Context: I'm using Box2D through LibGDX in Java. The sword and the player are in separate classes. Both extend LibGDX's Sprite class.

I've got a sword attached to my character that swings when I press 'X' on the keyboard, but I'm having trouble defining its hitbox. I want the hitbox to cover the area that the sword currently is, acting as an extension of my character so it doesn't push him around or slow down his movement. However, when the hero slashes his sword on a wall, I want it to push him back.

The animation: first framesecond frame

I want the hitbox to appear only for the duration of the animation (there are only two frames in the animation), then disappear until the sword is swung again.

Would I have to set the hitbox's position to certain coordinates for each frame of the animation in a long switch-case? How could I easily find these coordinates, or is it just trial and error?

\$\endgroup\$
5
  • 1
    \$\begingroup\$ Have you considered using QueryAABB for this purpose? \$\endgroup\$
    – DMGregory
    Commented Feb 26, 2020 at 0:15
  • \$\begingroup\$ @DMGregory never heard of it; what all does it entail? \$\endgroup\$
    – ND523
    Commented Feb 26, 2020 at 0:25
  • 2
    \$\begingroup\$ It's covered in the Box2D documentation and relevant tutorials much more thoroughly than I have either the experience or character space to explain in a comment. ;) \$\endgroup\$
    – DMGregory
    Commented Feb 26, 2020 at 0:35
  • 1
    \$\begingroup\$ @DMGregory haha, works for me. thanks, dawg \$\endgroup\$
    – ND523
    Commented Feb 26, 2020 at 0:35
  • 1
    \$\begingroup\$ could you add a link on one of these tutorials, either you or @DMGregory \$\endgroup\$ Commented Feb 26, 2020 at 6:50

1 Answer 1

0
\$\begingroup\$

A switch case would be a great start. And you could try looking at the use of an overlap function of the axis-aligned bounding box of the sword to that of the wall, then push the difference from the player.

\$\endgroup\$
1
  • 2
    \$\begingroup\$ This answer would be better if it explained in more detail how to use these tools to accomplish this effect. \$\endgroup\$
    – DMGregory
    Commented Feb 29 at 11:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .