[SOLVED] Creating, equipping and swinging a weapon

I'm making a top-down arcade style game that has melee combat (among other things). What I'd like to do is:

1. Have the weapon facing (or at least, it's swing arc) depend on the character facing. So when I rotate my character, the weapon's potential swing arc will rotate with it.

2. When the character attacks, have the weapon swing around a specified point within the circular body of the character holding it. It should have a limited arc, eg. it might swing from -45 degrees to +45 degrees of where the character is facing, thus:

/ start swing (-45 degrees)...
__ /
/o .\ /_____________CHARACTER FACING DIRECTION (0 degrees)
\o__/ \
character \
\ ...end swing (+45 degrees).

3. In regards to the physics, simulate the weapon only when the character is in the process of swinging it. I don't want any weapon bodies participating in physics AT ALL until they're absolutely needed. This will coincide with the weapon's sensor ability so that the weapon can only hit other characters when it's swinging. The body will be in existence, but inactive as part of the simulation, until an attack is made.


Right, so in order to do the above, here's what I'm thinking (and this is what I need your help with, 'cause I'm new to Box2d):

1. Create a weapon body + associated user data (eg. damage rating, swing arc limits). A character has (a) reference(s) to this Weapon + Body pair.

2. When the character equips a weapon, set the weapon body inactive. Create a revolute joint between it and the character body. Set limits of eg. +45 and -45 degrees (as example above) on this joint. As the body is not to collide with others, but rather to detect hits against the bodies of other characters, set it to be a sensor (plus any peripheral tasks).

3. On attack, activate the weapon body. Set the angle of the weapon to one extreme of the arc (eg. +45 degrees) so that it's now ready to begin its swing arc. Then apply torque impulse. When the weapon reaches the end of it's arc (eg. -45 degrees), set it inactive (which I believe should not only remove it from collision testing but also terminate its motion). Any collisions it makes with enemies during its arc will obviously be resolved in terms of damage done etc. Also, it is necessary that the weapon body does not record collisions with the player (is there a way to easily do this for joints or do I just use standard filtering?).


Another option I considered was setting up a fixture on the player body to act as the swinging weapon, but I don't know if you can independently move fixtures inside a body anyway and it didn't seem the right™ choice; I got the impression that joints was a better idea, if someone could confirm this also, I'd be happier.

One small issue I have is that I'm using JBox2D which presently doesn't seem to support BodyDef's active/inactive property. Are there any other options for me to eliminate the weapon from hit detection when it's "idle", or will I just have to use collision filtering? (I assume removing and re-adding the weapon body + joint would be slow if I had to do it every time some entity wanted to attack.)

Your thoughts, suggestions would be appreciated. 🙂
FYI this is resolved now... I had extra problems due to the fact that JBox2D has no kinematic bodies at present.

I did the following to create a single attack sequence:

-Create a new weapon body. In my case I set it as a sensor so no collisions.

-Create a revolute joint between entity body and weapon body. Rotate the sword's body def to the starting angle for the swing plus the angle of the character body's current facing; then simply setting the upper limit for the revolute joint to 90 degrees (the full swing arc) and leave the lower limit and the reference angle alone.

-Apply setMotorSpeed and enable the motor on the joint to begin the swing.

-Fire an event after the swing duration is up... I used a timer because relying on "when joint angle equals upper limit angle" did not work for me to tell when the swing was complete, due to numeric inaccuracies. When the event is received, destroy weapon body+joint.

Recreating the body each time a new attack is performed seemed the best way. Since this what happens for missile fire in any case, the overhead can't be that much larger, although the difference with the melee weapon is that the joint needs to be recreated too (destroying one body in a joint destroys the joint). The benefitt to doing things this way is one can immediately reset the weapon arc to zero which is something you otherwise cannot do without kinematic bodies... okay you could set a really high velocity back in the other direction, but this means you have to wait an extra world step before it gets back to its start pos, which I don't like -- I want timings to be exact as possible for each attack, without backswing overhead.

You can also do this simply by using destroyShape instead of destroyBody... I had this going but the backswing issue was annoying me. The benefit to this approach is that (a) it's lighter on processing to simply add and destroy shapes, rather than recreating both body and joint every time.

But if you're able to use kinematic bodies and also to set bodies inactive (neither of which I can do in JBox2D right now), I'd recommend it over the above options: you could immediately reset your swing to 0 radians when the swing is complete rather than waiting that extra world step, and you could inactivate the sword between swings (instead of destroying the sword body or shape) so there's no physics processing done on it.

Original thread: https://box2d.org/forum/viewtopic.php?t=5167