Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 10-08-2010, 10:40 AM   #1
Senior Member
Join Date: Aug 2006
Posts: 275
Default [AS3] which hittest

I have learnt some hitTest methods and I dont know what is best for games as in performance wise. There are so many collision tests and I am not sure what is quickest or slowest.

I have a player image which takes on many images like walking that I move with keyboard.
I use bitmaps and test bitmapData using Bitmapdata.hitTest.
I want to test against an enemy image where it again is an animated image.

My idea is to test the player image and enemy image with a simple rectangle test to see if they overlap and if they do then do a points array test on the image to see if any point hits on a pixel test.

I have read hitTest ,HittestPoint and Hittest with bitmapdata,spriteMovieClip, and testing bitmapData with rectangles, points or another bitmapdata.

For a game is/is not acceptable?
jagguy is offline   Reply With Quote
Old 10-09-2010, 09:58 AM   #2
Senior Member
henke37's Avatar
Join Date: Mar 2009
Location: Sweden
Posts: 10,887
Send a message via Skype™ to henke37

You are missing out a large section of collision detection algorithms, the ones not built into the player. Math and geometry can be used for a lot of stuff.
Signature: I wrote a pair of articles about the timeline.
henke37 is offline   Reply With Quote
Old 10-09-2010, 05:40 PM   #3
tadster's Avatar
Join Date: Feb 2009
Location: Texas
Posts: 2,111

One of the simplest hit tests would be a radius based hit test where you assume both objects have a middle and/or are square.
ActionScript Code:
public static function radiusHitTest(thiser:IHitable, otherObject:IHitable):Boolean {             //simple hit detection, pythagorian theroum                         var Radius:Number = (thiser.width>>1) + (otherObject.width>>1);             var differenceX:Number = thiser.middleX - otherObject.middleX;             var differenceY:Number = thiser.middleY - otherObject.middleY;             var Xdistances:Number = differenceX * differenceX;             var Ydistances:Number = differenceY * differenceY;             var distance:int = Xdistances + Ydistances;             return (distance < Radius*Radius);         }

But general collision detection is the least of your worries, especially when talking about performance.
One of the things you must do to maximize performance is having some sort of broad phase before doing any hit testing.. so you are generally onto the right idea when you said you would first do a overlap test then go into the pixels.
That's kind of the idea of a broad phase, you try to determine what needs to be checked for a collision and then only check those things.

Take for instance my invaders from the darkness we call space game. Do you think that each bullet fired is being hit tested against? That would slow the game way down, since you fire at a rate of 1 bullet every 33 milliseconds, and therefore there are normally 50 or more bullets being displayed, and that's just the ships bullets, let alone the enemy bullets.
So to account for this and to allow fast fireing of many bullets I created bullet partitions.

The ship bullet partition holds each of the ships bullets as they are fired.
So I only need to do pixel level hit testing on the bullet partition as a whole, not each individual bullet.
When a hit is detected on the partition then I do a simple distance hit test to determine which child of the partition is responsible for the hit.

And..well saying it that way, you may be like, what? First you did pixel level detection then a simple one on each child...so there's still a good sized for loop associated with that. But, the whole game is partitioned in this way, so, there are only ever, and always only 5 objects that require pixel level detection (as apposed to many 100s)

The ships bullet partition, the bricks partition, the enemy partition, the enemy bullets partition, and the power ups partition.
And that is one of the keys to the efficiency of that game.

The other big key is that the whole game operates on just one loop, everything, the seconds based timer that keeps time, the movement of the ship, all movement, everything related to the game happens off of one single loop.
There are only two loops looping, the main game loop, and the background is on its own Timer as well.

So, there you have it, two big things that will drastically improve performance, regardless of the way you detect specific hits.
A broad phase first for your hit detection.
And limiting the number of constant loops (try to get it down to just 1)

Beyond that a generally good pixel level hit detection routine was made by Grant Skinner, then ported to AS3 then a guy called Tink did another AS3 version of it that I think is pretty good.
www.actiontad.com - ActionScript and JavaScript sitting in a tree...
tadster is offline   Reply With Quote

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

All times are GMT. The time now is 10:31 PM.

Follow actionscriptorg on Twitter


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.