Kocik
Kocik

Reputation: 11

C# collision detection sphere sphere

I have some basic collision detection and its Precise collision accuracy: 55.000000% and i need 100% and dont know how to do it. I read many sites and dont come up wit any idea. So I woud apriciate some help.

I have some simulation where are some static asteroids and flying asteroids with Vector3 position Vector3 LinerVelocity and radius

I got this:

        public float PreciseCollision(WorldObject a, WorldObject b)
    {
        Vector3 vecs = a.Position + b.Position; // vector between the centers of each sphere
        Vector3 vecv = a.LinearVelocity - b.LinearVelocity; // relative velocity between spheres
        double q = vecv.Dot(vecs);

        if (b.BoundingBox.Intersects(a.BoundingBox) | q >= 0)
            return 0;

        return float.PositiveInfinity;
    }

when is or will be collision its return 0 and its not than float.PositiveInfinity

Upvotes: 1

Views: 419

Answers (1)

Chuck
Chuck

Reputation: 2102

If you're looking to do spheres you could just check for the tangent condition, which would be the sum of the two radii. I don't know why your function is returning a float of either 0 or float.PositiveInfinity when this seems like you're after a bool (colliding or not colliding), but if it were a bool it'd look like:

public bool PreciseCollision(WorldObject a, WorldObject b)
{
    float distance = (a.Position - b.Position).magnitude;
    float tangentDistance = a.Radius + b.Radius;
    return distance <= tangentDistance;
}

You get true if the two spheres are touching or overlapping and false if they're not.

Upvotes: 2

Related Questions