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. 🙂
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. 🙂