# collisiom

• 03-15-2004, 11:20 AM
tomkat42
collisiom
Its been quite sometime since I posted here; BUT!

Need to know how (if possible) when 2 objects collide, for one object to bounce off the other? Such as in when to pool balls collide or like a bowling pin when hit by the ball. Is it even possible in 3dfa?
• 03-17-2004, 11:06 AM
blanius
Sure but only if they are moving using velocity. Simplest is to reverse the velocity..

To really do it right you want to calculate the direction of the collision and change velocity based on that. (beyond my meager math skills but look in the flash tutorials it's there)

oh and you might want to use my circleCollide() function if the objects are round other wise than collide before they look like they do because the "bounding rectangle" is bigger in the corners.

My function library is available:
HERE
• 03-18-2004, 11:15 AM
zoranvedek
Somewhere at flashkit here, is an entire thread about the physics of a pool table, with links to enough resources to keep you busy for a long long time. Try searching the general forums for pool balls, or something similiar.

It's well worth hunting down if want to get it straight............

-J
• 03-18-2004, 03:30 PM
tomkat42
thanks guys...and sorry for the typo in the subject...should have said collision LOL.

Anyway I did search the flashkit boards and did find some really interesting and oh so complicated math for the collisions which was for flash 4 and flash 5, which at this time are way over my head.

Any suggestions to getting me started simple....one stationary object and one moving object collide, then move according to the angle and velocity of the moving object? Which I would assume is no simple task at all.

Any suggestion from Bret or Ed? I personally consider you guys as the gurus of 3dfa LOL is why i turn to you.
• 03-18-2004, 03:33 PM
tomkat42
Oh and guru zoranvedek as well!
• 03-18-2004, 04:01 PM
blanius
:)
Ah shucks! :domo:

Any start here: the following code from my function script
PHP Code:

``` //************************************* //Circle Collide uses distance test function //*************************************      function circleCollide(element1,element2)     {     //get width of both elements     width1=(element1.getBounds().xMax-element1.getBounds().xMin)-6     width2=(element2.getBounds().xMax-element2.getBounds().xMin)-6     //get distance      a=element1.x - element2.x;     b=element1.y - element2.y;     dist= sqrt (a*a+b*b);     //account for different sizes     width=(width1/2)+(width2/2);     //check distance and size          //return t/f     return dist<=width     }      //*************************************     //Get the Angle for rotation between two objects // //************************************* function angleTo(myelement,myelement2)     {     angle=atan2((myelement.x-myelement2.x),(myelement.y-myelement2.y))     return angle-90     }  ```
From this you can figure out the distance and the angle. The circleCollide works for any symetrical element, it basically calculates the distance between two objects and if it's less than halve the combine sizes of the two elements they must be colliding.

You could then get the angle from A to B using the angleTo function...

The trick then is to figure out what to do with the velocities.

Have to play with it a bit, but why don't you start there and for starters just invert both the x and y velocity.

I don't think I have the math for the rest. I'll look into it though
• 03-19-2004, 11:00 AM
tomkat42
Thanks Bret...I got the boundry of the circle, but using the help files and reading the threads I cant figure out how to make objects bounce of each other without directly adding object.velocity.x or y for each object
I am trying to figure out how to do something similar to the example at

Circle Collision

But cant releate the code used there to the scripts used in 3dfa
• 03-19-2004, 11:29 AM
blanius
Seen that one before, it might be where I got the collision of circles code myself.

Yeah that's tough code, It's pretty old for version 4 of Flash so much of it is not usfull for even Flash MX

Looks like what he's done here is to SWAP the velocities which might work.

try doing that by Making a function like

PHP Code:

``` function swapVelocity(element1,element2){    holdx=element1.velocity.x;    holdy=element1.velocity.y;    element1.velocity.x=element2.velocity.x;    element1.velocity.y=element2.velocity.y;    element2.velocity.x=holdx;    element2.velocity.y=holdy; }  ```
Yep this works reasonably well
http://bretlanius.com/flash/bounce.html
http://bretlanius.com/flash/bounce.movie
• 03-20-2004, 09:03 AM
blanius
One thing I noticed here with the collision detection I used is that you have to be careful about the fps of your flash movie. If you use a high low fps the elements can overlap a bit before detected, and sometimes it misses a slight collision.

So either use higher fps than about 15+ or tweek the collision detection or at least call it more times per frame in your script.
• 04-01-2004, 10:48 AM
blanius
Ok Got it
After tearing out a bit of the sparse hair on my head I finally figured it out.

There are a few problems still. Sometimes balls get stuck overlapping I tweeked the circle collision routine to try to fix it but they still can get stuck.

PHP Code:

``` for (x=0;x<maxballs;x++){     for (loop=x+1;loop<maxballs;loop++){ //this is the trick! so as not to repeate collisions             if (root.circleCollide(balls[x],balls[loop])&&loop!=x){             trace (balls[x].name+" hit "+balls[loop].name)                          root.swapVelocity(balls[x],balls[loop]);             }          } }  ```

Bounce balls
The Source
• 04-01-2004, 01:18 PM
lopez1_de
good job bret
• 04-06-2004, 07:20 PM
ForumNewbie
It may be a bit late to post this, and a little off topic, but the following thread has a few collision detection type routines in.

Other Thread In particular, there is some stuff in here about how to avoid your balls getting stuck to the walls. I noticed that this happens in the above example.

Also, I attach a movie that uses some more complex stuff to attempt to do a Newton's Cradle type model, with lots of collision detection, between multiple objects. It would be better if I'd used a detection method like Brett's in this case, rather than standard 3DFA collision detection, but maybe this would be a useful example. It looks best when exported for some reason.

Morgan.