0
\$\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
    Feb 26, 2020 at 0:15
  • \$\begingroup\$ @DMGregory never heard of it; what all does it entail? \$\endgroup\$
    – ND523
    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
    Feb 26, 2020 at 0:35
  • 1
    \$\begingroup\$ @DMGregory haha, works for me. thanks, dawg \$\endgroup\$
    – ND523
    Feb 26, 2020 at 0:35
  • 1
    \$\begingroup\$ could you add a link on one of these tutorials, either you or @DMGregory \$\endgroup\$ Feb 26, 2020 at 6:50

0

You must log in to answer this question.

Browse other questions tagged .