PDA

View Full Version : HitTesting.. but without the HitTesting?



four4swords
June 18th, 2010, 06:16 AM
Hi,

I'm trying to make a game kinda like Zelda.. Where there is a character that swings their sword and when it hits an enemy.. the enemy gets hurt etc..

Normally.. I would use movieclips for everything and I could easily determine a "hit" between the sword and the enemy by just running a hitTestObject between the two..

However, this time I want to go at this project another way by blitting everything onto a bitmap. My character will be stored as a series of images.. and they will be blitted onto the bitmap when needed. This would therefore not require the use of any movieclips at all and would greatly enhance the speed of the game..

But the problem I'm facing now is.. I'm not too sure on how I would be able to detect hitTests..

How do other games do it?

jMilstead
June 18th, 2010, 03:45 PM
actually the bitmapData class has its own hitTest that is pixel perfect:
http://help.adobe.com/en_US/AS3LCR/Flash_10.0/flash/display/BitmapData.html#hitTest%28%29

you could use that and it will work way better than if you had used movie clips :)

dandylion13
June 18th, 2010, 05:55 PM
BitmapData.hitTest is a little slow, but necessary if you want to do pixel-perfect collision. A math-based approach is faster.

The first place to start is a simple distance test:



vx = objectB.x – objectA.x;
vy = objectB.y – objectA.y;

distance = Math.sqrt(vx * vx + vy * vy);

//Check whether the distance between the objects is less than
//their combined half-widths:

if(Math.abs(distance) < (objectA.width / 2 + objectB.width / 2))
{
//Collision
}



This will work with circles or squares. For rectangles you'll need a bit more math do deal with differing heights and widths, but the concept will be the same.

four4swords
June 20th, 2010, 06:31 AM
Thanks for the suggestions guys!

lol I didn't know bitMap had it's own hitTest.. Thanks for the info!

But I'm not too sure how well it'll hold up when there are heaps of objects to hitTest against the stage.. So I think math-based is the way to go!

dandylion13, I like your way of checking for the distance between 2 characters... But how do you suppose I would go about checking if a sword on a character hits the enemy?

The sword Swing animation would have probably 3 or so frames.. The first where the sword is barely drawn.. another when it's fully outstretched.. and the last frame where it retracts back into the character.. This means the sword's hitarea is not always constant and changes depending on the animation..

And since this is all a bitmap.. I would not be able to distinguish character from sword..

How would I go about testing for collision between an enemy and a changing part of the character( the sword )?

TOdorus
July 1st, 2010, 01:01 PM
I've been wrestling with the same idea. I've seen a graphic of Street Fighter 2 where they define it with rectangles. Several rectangles per frame define the areas that do damage, block, or are hittable. This is cumbersome and you'll probably have to write an editor to create the rectangles per frame. On the other hand it is quite efficient (you use axis parallel to your worlds axis) and you've got some control. Also it can be used to define any pose.

I hope I can find the graphic when I get home.