collision problems

Subscribe to collision problems 11 posts, 7 voices

 
avatar for Acidlava Acidlava 69 posts

I use the hitTest code to detect collision, but the problem is, if you use that code with a circle, it collides with the edges of the circle as if it was a square, how do i make it collide as if it was a circle, not a square

 
avatar for BigCheese BigCheese 367 posts
You can test a point to the circle, or any other shape, using
if(circle_mc.hitTest(point._x, point._y,true){
}

If you are trying to hitTest two non-rectangular shapes, you have to do some math.

 
avatar for Dealmaster13 Dealmaster13 54 posts

Just for future reference, let’s say you had a star, how do you do collision detection with that and another random object?

 
avatar for BigCheese BigCheese 367 posts

It depends. Since it is a star, you could split it up into line segments and test each segment against the other object, or you could test the five points on the outside of the star using hitTest, similar to above.

 
avatar for Dealmaster13 Dealmaster13 54 posts

I see, thanks a lot, and if you test for a series of points’ collisions would you have to create new objects that stick onto the object or another way?

 
avatar for BigCheese BigCheese 367 posts

You could manually add movieclips onto the shapes (like you said) or you could calculate the coordinates for each point in relation to the center of the shape. (ie. The top point on a star might be -20 vertically from the center or the star)

 
avatar for FunkyPear FunkyPear 104 posts

Just to add a small note about the star example… you can’t just test 5 points, incase something goes in between them. You’d at the minimum have to test the 5 inside angles as well. Depending on its size, you’d probably have to test points inbetween the 10 points already mentioned.

 
avatar for BigCheese BigCheese 367 posts

Yeah, I was thinking that if something hits the inside points, it would have probably hit the outside points first. Unless it is really thin.

 
avatar for Lysis Lysis 320 posts

You could just test the 5 points if the object to be collided was big enough not to be able to touch the inner angles.

If the star were regular, you could conceptually rotate it by a multiple of (360degrees/number of points), then you’d only have to test for collision vs the near points. May be a win.

Other alternatives are testing as if both objects were circles (compare distance between to sum of radii) and rendering the overlap to a bitmap and checking that.

However you do it, you probably should test using a quick method first – use the generic hittest.

 
avatar for Haddock Haddock 103 posts

dont bother with any of that point stuff – go here: Grant Skinner’s Site and download this class, it has a perfect hitTest method that works perfectly and easily every time.

 
avatar for talrnu talrnu 176 posts

It’s a pretty ingenious way to detect collisions between two objects with pixel-perfect precision. First, it checks to see if the boxes surrounding those objects are overlapping. If they are, then it copies both objects in place, converts each object into a single color with no border, and applies a filter that combines the colors of overlapping objects where they overlap (like holding two pieces of differently colored film together and looking through them). Then it checks for the appropriate color that results from that combination; if it exists, even if only in a single pixel, then those two objects are colliding.

If you’re making a more technical game, there’s another decent collision detection package available at http://www.coreyoneil.com/Flash/CDK/index.html. This kit not only allows pixel-perfect collision detection between two objects, but it also detects collisions between many objects at once, AND it can provide other information about each collision, e.g. the angle between the colliding objects, or the amount of overlap between the colliding objects. Cool stuff.