# Unity flash collision question

This topic is 3335 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hola. Posted this in the web development section, but got no replies. Need an answer to get working on this game. Post: "I'm making a flash game with a lot of movieclips that need to register collisions. I need to know how I can get collisions to register off of just select pieces of visual data, or specify the bounding box for the movieclip some other way. Here's an example of what i mean. I have a turret, and each turret has a ranch, which is a circle around the turret. I want the turret to be a single object, but have the collision only be detected on the turret itself, not the circle. I'd like to just use the built in flash collision detection, but if rewriting collisions isn't out of the question. Just want to know if that's doable easily in flash. PS: Why doesn't flash have a built in Vector2D or Vector3D class?" Link: http://www.gamedev.net/community/forums/topic.asp?topic_id=551131

##### Share on other sites
hello

are you working on AS2 or AS3? I am assuming you are working on AS2

first if turrets are always in a circle, why don't you use two circle collision detection?

If you want to check collision detection on pixel level first you need to draw all objects to a bitmapdata. after that you should draw the object that you want to check for collision into another bitmapdata.

after that for each pixel in the second bitmap data you need to if it has > 0 alpha and pixel on the scene also has > 0 alpha. if both is true there is a collision

here is an example, I hope it will help you to understand what am I trying to say. my explanation sucks =)

swf file

I should mention that you will not be able to do much testing using this method. as2 is very slow, and even as3 will not work with high number of objects.

##### Share on other sites
there is no vector class in as2 and as3 (as far as I know, I didn't use as3 that much) but there is a point class but it is not that usefull

and it is in 2D. probably macromedia is never intented to make flash work on 3D =)

##### Share on other sites
Quote:
 Original post by shultayshelloare you working on AS2 or AS3? I am assuming you are working on AS2first if turrets are always in a circle, why don't you use two circle collision detection?If you want to check collision detection on pixel level first you need to draw all objects to a bitmapdata. after that you should draw the object that you want to check for collision into another bitmapdata.after that for each pixel in the second bitmap data you need to if it has > 0 alpha and pixel on the scene also has > 0 alpha. if both is true there is a collisionhere is an example, I hope it will help you to understand what am I trying to say. my explanation sucks =)swf fileI should mention that you will not be able to do much testing using this method. as2 is very slow, and even as3 will not work with high number of objects.

I'm working in AS3. Circle to Circle would work fine, but I'm guessing I'd have to make my own Collision system then yes?

##### Share on other sites
yes, it won't be that hard if you work on circles. good luck

##### Share on other sites
Quote:
 Original post by shultaysyes, it won't be that hard if you work on circles. good luck

Alrighty. That will work fine for the most part. Is there a way to get the bounding box for a movieclip (I have a large square movieclip that needs collision detection and a circle wouldn't work for this one) so I can do circle/square collision?

I think the way AS3 does it is just getting the bounds of the visible movieclip to make a bounding box anyways. Is there a way to get that bounding box so I might be able to do a mix of both?

##### Share on other sites
Heya, here's a crash course on circle and box collision

Circle vs Circle test:

1) get distance between the centers of each circle.
2) if the distance is less than the 2 radiuses added together, the circles have collided.

To make it more efficient, you can work with squared distances as appropriate to not have to do the square root operation.

Circle vs Box test (assuming the 2d box is an axis aligned bounding box aka AABB and that this is 2d):

1) Find the closest point on the box to the circle's center by doing the below:  a) X = CircleCenterX  b) if X less than BoxLeft then X = BoxLeft  c) else if X greater than BoxRight then X = BoxRight  d) Y = CircleCenterY  e) if Y less than BoxTop then Y = BoxTop  f) else if Y greater than BoxBottom then Y = BoxBottom2) Calculate the distance from the closest point on the box to the center of the circle.3) If the distance is less than the radius of the circle you have a collision.

And hopefully you can figure out box vs box test (:

Hope this helps!

##### Share on other sites
Quote:
 Original post by Atrix256Heya, here's a crash course on circle and box collision Circle vs Circle test:1) get distance between the centers of each circle.2) if the distance is less than the 2 radiuses added together, the circles have collided.To make it more efficient, you can work with squared distances as appropriate to not have to do the square root operation.Circle vs Box test (assuming the 2d box is an axis aligned bounding box aka AABB and that this is 2d):*** Source Snippet Removed ***And hopefully you can figure out box vs box test (:Hope this helps!

What do you mean by "To make it more efficient, you can work with squared distances as appropriate to not have to do the square root operation."

Where was the Square root Operation in Circle vs. Circle? or do you mean when you calculate the distance between the two points? sorry, your wording just confused me.

##### Share on other sites
yeah you got it...

for instance if you wanted to know if a point was inside a circle, you would do something like this...

Distance = sqrt(square(PointX-CircleX)+square(PointY-CircleY));

then if the radius was 10, you would say...

if(Distance < 10) there is a collision!

but if you "square both sides of the equation" you get this...

Distance = (square(PointX-CircleX)+square(PointY-CircleY));

if(Distance < 100) there is a collision!

it saves you the square root operation, which is nice.

if this is weird to you, get it working first with the square root in there, and then if you want to, you can optimize your test by getting it working without the square root.

##### Share on other sites
DisplayObject.getBounds(DisplayObject.stage) will get you the bounding box of the display object relative to the stage.

If you want rectangle to rectangle, use (NOTE: these are example variables) myBoundingBox.intersects(otherBoundingBox) to find whether they are intersecting.

Even better, you could skip a step and use DisplayObject.hitTestObject(obj:DisplayObject) to find whether their bounding boxes are intersecting.

If you want to have circle to circle collisions, just have a radius number in each object and do a distance check between each one.

1. 1
2. 2
Rutin
18
3. 3
4. 4
5. 5

• 26
• 11
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633701
• Total Posts
3013445
×