PDA

View Full Version : Circle collisions, need help here!



Parasite
March 8th, 2005, 05:28 AM
Hi there. I'm developing a game atm, and I need some help on the mathematics/coding of colliding circles (and their change in x/y speed). Didn't post this in the gameforum, as it is really not a game-related question.

What i need to know is how to calculate the direction and speed a circle has after colliding with a static (non-moving) circle. I know how to detect a collision between the two, but I'm not sure about the maths I need to give the circle the right speed and direction after the collision

http://www.you-me.no/forklaring.jpg
If i do know how far away the two circle-points (in the middle of each) are to each other (in both x and y coords) would it be easy for me to find the new speed and direction of the large circle in the example above? I know that I somehow should use the angles to pretend the circle hits a flat "wall" (as it only hits one point), and then calculate the new speed/direction. My only problem is that I'm not sure how to decide the angle of this flat "wall" and how x and y speeds should change when colliding with a wall that is angled. Does anyone here have any insight, links or tips on how I can du this?

Help would be much appreciated!

MichaelxxOA
March 8th, 2005, 12:44 PM
Here's what I do when dealing with just circles:

First off in this example i'm going to use circle1 and circle2 as the instance names:

_root.onEnterFrame = function() {
//pythagorean theorem
a = Math.abs(circle1._x - circle2._x);
b = Math.abs(circle1._y - circle2._y);
c = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));

if (c < (circle1._width/2 + circle2._width/2)) {
trace("this is where you change speed");
}
}


What this is doing is taking pythagorean's theorem to find the hypotenuse which in this case would be the distance from the registration point of circle1 to circle2 and checking if it is less then the half of each circle's width (Half because it's measuring from the center) because if it is less than half then they Have to be colliding, this is how I do it, there's a million other ways probably, as far as changing speed I would need to see how you are using it in code.
-Michael

jerez_z
March 8th, 2005, 12:53 PM
the math behind colliding circles and the resulting vectors is quite complicated. I have seen several methods and they are hard to implement and the math is extreme.

http://www.google.ca/search?hl=en&q=circle+collisions&meta=

It appears Gamasutra may have a good tutorial but I'm at school and can't get there...

MichaelxxOA
March 8th, 2005, 12:59 PM
I think that My Method is a little bit easier to implement than that as far as collision goes.

jerez_z
March 8th, 2005, 01:01 PM
for the collisions definatley, but calculating the new vectors is pretty crazy.

MichaelxxOA
March 8th, 2005, 01:10 PM
agreed! we should find an easier way then, I mean if it's going to be this complicated until someone finds a better way, why don't we?
-Michael:huh:

jerez_z
March 8th, 2005, 01:14 PM
hehe... I might have to write a function when I get home.