# Game physics / 2D Collision detection AS3

I know there are some methods you can use like hittestPoint and so on, but I want to see where my movieclip colliedes with another another movieclip. Any other methods I can use?

by any chance does somebody know some a good introduction to game physics? Im asking because I coded a small engine and pretty much the whole code is spagetti code thats why I would like to know how you can setup something like this properly

-
Searching for "collision detection" (or clicking this tag) on this site should give you a lot of results: gamedev.stackexchange.com/questions/tagged/collision-detection We can't really recommend a particular method, because you didn't say anything concrete about your problem. – Markus von Broady Oct 9 '12 at 18:18

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

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.

-
I appreciate your effort! – Jery Oct 10 '12 at 5:31

Jery, the truth is that structuring code is a function of the developer's understanding of the concept at hand (in your case, collision detection and physics). If you don't really understand the concepts, your code will be a mess.

So, better-structured code comes with an improved understanding of the problem. That means you will have to do your own research. This site, gpwiki.org, gamedev.net, are all worthwhile sources, among countless others, especially (I find) devblogs. When it comes to anything new and advanced like a particular AI technique, entity-component systems, collision detection, physics, 3D to name just a few -- you are going to have to take the initiative to do a bit of your own research.

Just don't be intimidated. Look at a lot of sources. If one source doesn't make sense, don't waste much time, move onto another. Good sources are hard to find, and maybe that's what you're asking for here, but I will tell you that looking through many sources leads to a broader understanding and capability, as you piece together the puzzle that is that particular concept. Mastery doesn't come from a single source. And finding the right sources is a major skill in itself, for any developer.

-
Yes youre right I was looking for good resources and I totally agree with you. I´ve been improving over time and sometimes devblogs come back to my mind when I start to tackle a problem I never really encounterd before, thx for the good vibe =) – Jery Oct 10 '12 at 5:31

There are several ways to detect a colision in AS3, and you have to choose the one that fits better with your needs.

ActionScript3 provides two native methods for testing colisions:

• obj1.hitTestObject( obj2 );

This function will return true if the bounds of the objects are colliding. Be careful, this will cause fake colisions if you are using shapes like circles, because the function will test the area of the shapes, not the pixels.

• obj1.hitTestPoint( obj2.x, obj2.y, true );

This function will return true if a coordinate colides with the shape of an object. Be careful you can only test one point, you may want to determine several points of colision in a shape, then test all of them to a better result.

Of course these are only natives functions, if you want to save you some time, you can import and use a colision kit. They can be very useful, since they have a lot of features and examples. A good one can be found here.

About your other question, tonypa have a great tutorial about vectors, that can be very useful when developing phisic games. The tutorials are written in AS3, and provides a lot of graphics representations. These can be found here.

If you do not want to write the full code, you can learn how to use boxD2, that provides a full environment whith phisics. But maybe you will want to learn some theory before using a phisic library.