Harry89pl
Harry89pl

Reputation: 2435

Checking collision circle with rectangle with c# XNA 4.0

Like in Title i trying to check collision circle with rectangle with XNA.

But have no idea how to check it simple.

I found something like this tutorial

but i wonder is there any existing solution for my problem in XNA?

I attach the image which showing what i'm trying to check if someone not fallow What I'm trying to do

EDIT: I'm making game for Mobile Windows Phone 7. It should not overload CPU too much.

Thanks for advance:)

Upvotes: 1

Views: 9788

Answers (2)

user4506906
user4506906

Reputation:

There is a simpler solution in XNA "Platformer" demo;

    public bool Intersects(Rectangle rectangle)
    {
        Vector2 v = new Vector2(MathHelper.Clamp(Center.X, rectangle.Left, rectangle.Right),
                                MathHelper.Clamp(Center.Y, rectangle.Top, rectangle.Bottom));

        Vector2 direction = Center - v;
        float distanceSquared = direction.LengthSquared();

        return ((distanceSquared > 0) && (distanceSquared < Radius * Radius));
    }

Upvotes: 0

Keldon Alleyne
Keldon Alleyne

Reputation: 2130

Here is an answer to a similar question: Circle-Rectangle collision solution.

For reference, here's a thread on Moving Collision Detection with a programmer from Griptonite Games (Mike0801). It might be worth reading what he has to say on the maths. They've produced games for power starved machines like the Gameboy, so he gives lots of insight on doing so quickly.

Upvotes: 2

Related Questions