Wednesday, May 9, 2018

physics - How do I detect collision between movie clips?


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?



  1. 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)

  2. 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).

  3. Check if there is a collision between the mc's rectangles. If not, stop.

  4. Check if there is a pixel perfect collision between the bitmapData objects using the API bitmapData.hitTest() and if not, stop.


  5. 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.


Two shapes


Trace their outlines into two separate point vectors in advance.


Trace outline


If there was no collision between their containing rectangles, we could stop here and save processing time.


Rectangle check


Time to check pixel perfect and outlines.


Now we check the outlines



No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...