Also, you could use built-in BitmapData.hitTest method:
"Performs pixel-level hit detection between one bitmap image and a point, rectangle or other bitmap image. No stretching, rotation, or other transformation of either object is considered when doing the hit test.
If an image is an opaque image, it is considered a fully opaque rectangle for this method. Both images must be transparent images to perform pixel-level hit testing that considers transparency. When you are testing two transparent images, the alpha threshold parameters control what alpha channel values, from 0 to 255, are considered opaque."
Move circles with arrow keys. Notice how they wont move if any of the circles collides with any rectangle.
This is how it works:
*circles are squares are separate mcs
*from both mcs bitmapdata is created
*when arrow key is pressed bitmap.hittest is performed between 2 bitmaps moving starting point in correct direction by 1 pixel until speed value is reached or hittest returns true.
which method do you think you would use. this bitMap data hitTest seems better than getColorBoundsRect. But i noticed in your other post, i doesn't support rotation
I suppose it depends what you need. With getColorBoundsRect you can actually get the coordinates of the point colliding too. With bitmap.hitTest you only get true or false. But hitTest method is much easier to use.
If you want to rotate mcs, then you need to create new bitmapdata after rotation and draw mc into bitmapdata again.
ahhh, i see. So using the getColorBoundsRect method is kinda like shape flag hitTesting, i don't remember ever needing corrdinates from a hitTest, so this bitMapData hitTest is probably the way to go. Just another question Tonypa. Since you are the master of tiles (in my opinion). With the new scrolling engines, do you think a tileBased engine (one which sets graphics, collisions based on mathematics) will soon become obsolete. I mean, you will still use tile based methods, like recording Array positions, but hitTesting will be different. This bitMap data looks pixel perfect, i'd be interseted to know if it still operates at high speeds.
I dont think math based collision will ever be obsolete no matter what engine you use. I have not tested any of this for speed so I dont know really how fast the bitmap vs hittest performs. Plus, with math you can also get the point of collision which can be very important if your game involves physics like balls rolling down on slopes because of gravity. It all depends very much on what type of game you are building, most games dont need perfect collision detection, but some cant live without it.
is this only possible with bitmaps, or will it work with vectors?
It will work with both. You can convert any movie clip containing both bitmaps and vectors into bitmapdata object. The example with bitmapData.hitTest method actually uses only vectors, those circles and rectangles are all drawn in Flash. But like I said, you could use bitmaps (gif, png) too.
That's interesting. this is the first time I read a good reason to upgrade to flash 8.
I guess this collision detection is CPU intensive. A benchmark would be very instructive.
Using bitmap and (if(mc.hittest(_mousex, _mousey){} are practically the same thing I've noticed. If you look at my example (one that i've found) you'll see that it amounts to the same thing.
Using bitmap and (if(mc.hittest(_mousex, _mousey){} are practically the same thing I've noticed. If you look at my example (one that i've found) you'll see that it amounts to the same thing.
Not really...
You are testing an exact point with the mc.hittest...(or in your example's case, you're testing a total of 8 points) while BitmapData, you can test a shape versus a shape.
EDIT: Actually, I just notice Tonypa's example pretty much shows this:
No, but it is possible to make a copy of the vectors into a bitmapData layer for hit testing purposes only.
It is basically what Tonypa is doing.
Download his example (see above) and play around with the two movie clips.
Add and remove shapes, draw different shapes.
The only conditions (for the circle movie clip anyhow) is that the shapes can not be positioned beyond the 0,0 point (I.E. you can draw something which goes from -20 to 20 because it will only test from 0 to 20).
Yes, its true, Inglor. Whenever you change the content (animation, tween etc) you would need to create new bitmapdata from the object. Turning bitmap caching on actually does exactly this, it creates bitmapdata object from the object then draws that bitmap instead of real mc.
But I honestly have seen very few games where collision is done with real animations. Most games simplify the collision to circle or rectangle which rarely changes through the game. If you want to detect collision between complex animation then what options do you have? The bitmapdata is only way to do it.
Circles and rectangles don't require hitTest to check if they hit stuff, you can just use math.
I do realise that BitmapData is currently probably the only reasonable option for this. The problem is that this rarely works in complex games with animation which is a disappointment.