PDA

View Full Version : The age-old question of circle collisions



Esoltas
January 18th, 2007, 02:50 PM
Hello,

As my title implies, I am stuck with the very popular question, "How do I get 2 circles to bounce off eachother correctly??". I am currently making an air hockey game and have been looking for an answer for this for a few months now. I have seen great physics explanations, and great swf's, but I can't seem to do it myself. I know how to get the puck to bounce off the walls, and that code is very stable/simple, but the hitter-against-puck collision is very elusive. Bit-101 has some great swf's on flashkit forums about the exact same thing I am looking for. I have heard of somebody on super samurai giving a great explanation. I have seen a physics student's explanation. It seems to be a lot of code involving the radii of the circles, and the only thing I really understand is this:

r1 = radius of puck | r2 = radius of hitter | d = distance between them

if r1+r2 > d, then they have collided.

I know, I know, what you're saying is,"well duh". If somebody could explain to me how to write this code (you don't have to give the code) that would be great.

Here is my puck-against-wall code:

onClipEvent (load) {
speed = 0;
dist = 0;
dx = 1;
dy = 1;
}
onClipEvent (enterFrame) {

xspeed = 5;
yspeed = 5;

if (_x<47.6) {
_x = 47.6;

dx *= -1;

} else if (_x>562) {
_x = 562;

dx *= -1;

}
if (_y<82) {
_y = 82;

dy *= -1;

}
if (_y>318.1) {
_y = 318.1;

dy *= -1;

}

_x = _x-(xspeed*dx);
_y = _y-(yspeed*dy);
}


So anyway, obviously that code is inside of the puck. Help please if you know how.

thank you!:hugegrin:

Austastic
January 18th, 2007, 06:31 PM
I would do it by fist getting the width of the circle(diameter), which, if you are just storing a circle in the movieclip and nothing else, should be the width of the movieclip. You want to assign a variable to width, such as:

onClipEvent(load) {
_root.puckWidth = this._width; //Gets the width
_root.puckWidth /= 2; //Divides the width by two to give you the radius
}

To calculate the distance :

_root.xdist = Math.round(_root.ball1._x-this._x);
_root.ydist = Math.round(_root.ball1._y-this._y);
_root.distance = Math.round(Math.sqrt((_root.xdist*_root.xdist)+(_r oot.ydist*_root.ydist)))


Then put it all together with the formula you have up top!
if((_root.puckWidth + _root.paddleWidth) >= _root.distance) {
BOUNCE LIKE A MOTHAFUGGA!
}

Dragonist
January 18th, 2007, 06:55 PM
Sorry to intrude but what does "ball1" refer to in your code? I know "paddle" refers to the hitter that you hold, so is "ball1" the puck?

See ya!

-Dragonist

Dragonist
January 18th, 2007, 07:25 PM
maybe its just because Im not sure what ball1 represents, but I tried the code & it didn't work. There weren't any syntax errors :P<|Hooray!| But when i tried this nothing happened:

if ((_root.puckWidth + _root.hitterWidth) >= _root.distance) {
trace ("Yay! It worked!");
}


so um...just so you know I put the above code inside of a onClipEvent (enterFrame) {

Hooray!
Dragonist

Austastic
January 18th, 2007, 09:36 PM
Sorry about the confusion. If my code were pasted inside the puck, then ball1 would be the paddle, or whatever ball you wanted to track the distance from. And that code wont just run. He didn't ask me to code for him, just help him out. If he needs help on other parts he can ask. The code won't work as a standalone is what I'm saying...although I haven't tried it yet. Getting on that...

SacrificialLamb
January 19th, 2007, 12:18 AM
I think the problem was the angle it was bouncing off at not the collision itself

TheCanadian
January 19th, 2007, 02:11 AM
http://www.kirupa.com/forum/showthread.php?t=247382