1

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:)

Harry89pl
  • 2,415
  • 12
  • 42
  • 63
  • You want to have a precise collision check, or a box check? if you want a precise you should take a look at `per pixel collision`. There are a lot of tutorials and algorithms about this technique in the web, but be aware, its a little too costly for the cpu – Gustavo Maciel Dec 27 '11 at 14:55
  • I want to check not 100% precise pixel but when background of texture is transparent it shouldn't collision return true. Maybe is some solution for sliced texture for little rectangle or something like this? – Harry89pl Dec 27 '11 at 15:07
  • Thats what per pixel collision is. It check the intersecting pixels of both sprites, if at least one couple of these pixels have a alpha greater than 0.1f(or other value you provided), that is, its not transparent, then the collision occurs. Also, there's a lot of ways to check collision. Some are: bounding box collision(each sprite have a box of its size, then tests collision on these boxes. not so precise, but very good), sphere collision (like bounding box, but you use spheres instead), etc etc – Gustavo Maciel Dec 27 '11 at 15:10
  • and how working the box check solution if u can explain that to me. I will be very greatfull:) – Harry89pl Dec 27 '11 at 15:12
  • Well you'll have 2 options. The per pixel, or combine sphere with bounding boxes, depending on your project objects. – Gustavo Maciel Dec 27 '11 at 15:13
  • I think sphere bounding boxes wil be enough:) Thanks:) – Harry89pl Dec 27 '11 at 15:16
  • Do you have a good mathematic experience? this should help you a lot: http://mathworld.wolfram.com/Circle-LineIntersection.html and by the way, this is a C implementation of this: http://www.ics.uci.edu/~arvo/code/BoxSphereIntersect.c it is the 3D logic, but the 2D isnt that different. just simpler (: – Gustavo Maciel Dec 27 '11 at 15:22
  • I think I have medium mathematic experience;) I will try this and will see what happend :P – Harry89pl Dec 27 '11 at 15:24

2 Answers2

2

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.

Community
  • 1
  • 1
Keldon Alleyne
  • 2,103
  • 16
  • 23
0

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));
    }