RTL_Shadow
1020 posts
|
What is the best way to handle attacking in a platformer? Assume that the player has a “sword” and you want to hit test that.
The only idea I can think of is to just animate the swinging of the sword in the Player MovieClip, and then have a hitbox for it. Then you can just swap out the weapon you want in the correct position. The only problem with this is if there are more than 1 animations for the attacking, the player MC could get rather messy.
Any others??
|
|
|
Senekis93
4090 posts
|
Separate display from logic. Don’t “hittest” the sword, test against a line from the sword’s handle to the edge of it, unless your swords aren’t sraight, then adapt it to whatever suits your game.
Or simply animate with code and use a pixel perfect method.
|
|
|
qwerberism
56 posts
|
use a mathematic representation of the sword geometry
|
|
|
RTL_Shadow
1020 posts
|
The testing a line theory is a good one- that’s what I’m going to do. Any idea how to go about it?
|
|
|
qwerberism
56 posts
|
calculate the positions of the end points of the segment at each frame and model it as a traveling point (start from one point, travel to the other). sweep this model (using a point vs AABB sweep algorithm) across all enemy bounding boxes and look for the positive return values.
|
|
|
RTL_Shadow
1020 posts
|
Originally posted by qwerberism:
calculate the positions of the end points of the segment at each frame and model it as a traveling point (start from one point, travel to the other). sweep this model (using a point vs AABB sweep algorithm) across all enemy bounding boxes and look for the positive return values.
Would it be worthwhile to run a distance check on each enemy to minimize calculations? It’s worthless to run a check on an enemy 200+px away.
|
|
|
Moshdef
1020 posts
|
You can also have the weapon animation just following your player around, so that it doesn’t interfere with anything on the player mc and can be used no matter what you are doing (running, jumping, crouching). I’m not sure if this causes lag or anything, and I’m a big noob, but this is a VERY simple way of doing it and also makes it very easy to switch between weapons and make the hittest whatever you want (sword blade, axe blade, arrow, etc.). Sorry if this is really stupid/bad advice, but this is how I recently got mine working like a charm and I thought it might help.
|
|
|
qwerber
4717 posts
|
Originally posted by Moshdef:
You can also have the weapon animation just following your player around, so that it doesn’t interfere with anything on the player mc and can be used no matter what you are doing (running, jumping, crouching). I’m not sure if this causes lag or anything, and I’m a big noob, but this is a VERY simple way of doing it and also makes it very easy to switch between weapons and make the hittest whatever you want (sword blade, axe blade, arrow, etc.). Sorry if this is really stupid/bad advice, but this is how I recently got mine working like a charm and I thought it might help.
try not to use the hittest function :)
|
|
|
OctaBech
23 posts
|
I’ve never made a game of this type, so take what I’m going to say with a grain of salt. :)
A guy under the alias DeanoC (If I remember correctly) from Team Ninja who did Heavenly Sword, said that only fighting games like Tekken and Street Fighter use precise hit detection because the outcome of a battle should depend on player skills. Action Adventure games like Heavenly Sword, God of War, Devil May Cry and Ninja Gaiden use hit boxes or proximity which is less process demanding and less infuriating for the player.
the hitbox is quite simple, each unit (friend and foe) is given a hitbox containing 4 points xMin, xMax, yMin and yMax. The weapon is just a point (ie the tip of a sword) and all the game does is checking if the point is within a hitbox. Depending on weapon type, the hitbox can be de/increased.
public function isHit(unit:Unit, point:Point, weaponRadious):Boolean
{
if (point.x < unit.xMin – weaponRadious)
return false;
if (point.x > unit.xMax + weaponRadious)
return false;
if (point.y < unit.yMin – weaponRadious)
return false;
if (point,y > unit.yMax + weaponRadious)
return false;
return true;
}
Originally posted by Draco18s:
weaponRadious
Radius. R-A-D-I-U-S. Radius.
For game balancing purpose, make the player’s hitbox smaller then the sprite (even with pixel perfect hit detection people will feel they got hit due to an error with the game) and make the enemy hitboxes bigger than their sprites. :)
|
|
|
Draco18s
6860 posts
|
weaponRadious
Radius. R-A-D-I-U-S. Radius.
|
|
|
UnknownGuardian
8130 posts
|
What I did:

Player is green square in center of screen holding the sword. You can see red big box where the sword is in. That is the collision box. I just checked if something was inside that box each frame and dealt a tiny amount of damage if so. Since it checked each frame, the damage was realistic. If you are getting hit for the full swing, you get a lot of damage, but if you just barely hit the edge, you’ll only get a little bit of damage.
This also allowed for a variety of melee weapons to be used by simply adjusting the hit box. If you changed to ranged weapons, then of course the whole system would change.
|
|
|
OctaBech
23 posts
|
Originally posted by Draco18s:
weaponRadious
Radius. R-A-D-I-U-S. Radius.
Thanks :D
|
|
|
RTL_Shadow
1020 posts
|
Yeah- I think what I’m going to do is have a standard attack animation and then a spot where any weapon can go. Only downside is that all weapons will be held the same and the attack animations will be the same- but whatever :/
code-wise for hitting:
// Run for each enemy
// Check each enemies distance. If <96px away, continue
// Check point, middle, and bottom of sword's blade. If hitting, deal damage, etc.
|
|
|
OctaBech
23 posts
|
it could be an array of points.
point = pointArray[weapon.frameNo];
or a 2D array if multiple points should be checked per frame.
EDIT:
Sorry that was awkward code :)
The weapon class could hold info on where the point(s) are for each frame of the attack animation.
point = weapon.getPoint(frameNo);
If you want multiple points to be checked per frame, the getPoint function could return an array of points. That should allow for some diversity in weapon types.
|
|
|
RTL_Shadow
1020 posts
|
Originally posted by OctaBech:
it could be an array of points.
point = pointArray[weapon.frameNo];
or a 2D array if multiple points should be checked per frame.
That’s actually exactly what I’ll do- the only problem is that the blade will be moving.
|
|
|
OctaBech
23 posts
|
yeah, I changed it to a function called from the weapon object instead, so the points can’t be calculated from the weapon’s position and direction before returned. :)
|
|
|
RTL_Shadow
1020 posts
|
EDIT: Scratched hitpoint problem. Sigh- I really don’t know how to go about this.
|
|
|
MoonlaughMaster
6660 posts
|
Originally posted by UnknownGuardian:
What I did:
Player is green square in center of screen holding the sword. You can see red big box where the sword is in. That is the collision box. I just checked if something was inside that box each frame and dealt a tiny amount of damage if so. Since it checked each frame, the damage was realistic. If you are getting hit for the full swing, you get a lot of damage, but if you just barely hit the edge, you’ll only get a little bit of damage.
This also allowed for a variety of melee weapons to be used by simply adjusting the hit box. If you changed to ranged weapons, then of course the whole system would change.
On an unrelated note, what do you think of Flashpunk vs. Flixel?
|
|
|
qwerber
4717 posts
|
I’m not using either because of the powerful stage3d feature in newer fp versions, but Flashpunk seems to offer more flexibility.
|