I know there are some methods you can use, like hittestPoint
and so on, but I want to see where my movie clip collides with another another movie clip. Are there any other methods I can use?
I have had trouble finding a good introduction to game physics, and I would like to know how to something like this, properly.
Answer
"I want to see where my movieclip colliedes with another another movieclip."
Actionscript offers pixel perfect collision detection between bitmapData objects. It does not offer straight out of the box a tool to detect the exact points where the two objects overlap.
How to detect where two objects are 'touching' on another?
- Create two bitmapData objects and .draw() the movieclips, each one to a separate bitmapData object using the `.draw() function. only needs to be done once unless you rotate or scale the movie clips later)
- Create 2
Vector(typed array) of type Point
and trace the outline of the bitmap data objects. Do this by running in a loop over all pixels in the bitmapData. If a pixel's alpha is 0 or low enough to be invisible to the eye and it is next to a visible pixel, add it to the vector as point of the pixel position in (x,y). - Check if there is a collision between the mc's rectangles. If not, stop.
- Check if there is a pixel perfect collision between the bitmapData objects using the API bitmapData.hitTest() and if not, stop.
- Create a 2d-vector of type Boolean. Trace the first outline into the 2-vector. Now run over the points in the second outline. If any of them is already 'true' it means the two outlines collide in that point. You can return all points that meet this condition as a result for the function.
Here are two sample shapes.
Trace their outlines into two separate point vectors in advance.
If there was no collision between their containing rectangles, we could stop here and save processing time.
Time to check pixel perfect and outlines.
No comments:
Post a Comment