How to handle circle penetration
Posted
by
Kaertserif
on Game Development
See other posts from Game Development
or by Kaertserif
Published on 2012-04-06T15:17:11Z
Indexed on
2012/04/06
17:43 UTC
Read the original article
Hit count: 198
I've been working on cirlce to circle collision and have gotten the intersection method working correctly, but I'm having problems using the returned values to actually seperate the circles from one another.
This is the method which calculates the depth of the circle collision
public static Vector2 GetIntersectionDepth(Circle a, Circle b)
{
float xValue = a.Center.X - b.Center.X;
float yValue = a.Center.Y - b.Center.Y;
Vector2 depth = Vector2.Zero;
float distance = Vector2.Distance(a.Center, b.Center);
if (a.Radius + b.Radius > distance)
{
float result = (a.Radius + b.Radius) - distance;
depth.X = (float)Math.Cos(result);
depth.Y = (float)Math.Sin(result);
}
return depth;
}
This is where I'm trying to apply the values to actually seperate the circles.
Vector2 depth = Vector2.Zero;
for (int i = 0; i < circlePositions.Count; i++)
{
for (int j = 0; j < circlePositions.Count; j++)
{
Circle bounds1 = new Circle(circlePositions[i], circle.Width / 2);
Circle bounds2 = new Circle(circlePositions[j], circle.Width / 2);
if(i != j)
depth = CircleToCircleIntersection.GetIntersectionDepth(bounds1, bounds2);
if (depth != Vector2.Zero)
{
circlePositions[i] = new Vector2(circlePositions[i].X + depth.X, circlePositions[i].Y + depth.Y);
}
}
}
If you can offer any help in this I would really appreciate it.
© Game Development or respective owner