# XNA - How to use One Rectangle in sprites for collisions

I'm new to XNA and game development in general. If I want to make an object solid so my character cannot pass through it, I would use several Rectangles to make the collision more "realistic"

What I do is to use the method Intersects() with both;

This are two Rectangles that will be solid. (Highlighted in Red):

And I wonder if there is a way to use one rectangle to achieve something like this:

Unfortunately, there is not. By definition, a rectangle has four points, and the new figure has six.

This is something to do with the library, however you could most likely create a wrapper around a few rectangles that has a similar API to the XNA rectangle class. This, I think, is the only way to accomplish something like this excepting @SteveH's solution.

• Ball (circle) or Round corners collision use other similar methods? are they built in XNA? For example, if I have a Race game, and a track where you can collide with the round corners – JuanBonnett Feb 14 '15 at 1:51
• There's a Sphere class for 3D, however natively XNA does not support circles. There is a library called Monogame that aims to be the open source, multiplatform version of XNA that is contemplating adding a circle class, however. That can be seen here: github.com/mono/MonoGame/pull/3419 – Pip Feb 14 '15 at 1:56

The closest you can do something like this:

If(not in red rectangle)
return;//no collision
else
if(not in blue rectangle)
//run collision code


Here's another trick that can be helpful:

If you plan to set up a large group of these, you can first test a rectangle that encompasses the component rectangles, then test the interior rectangles individually.

For P[1] we check whether it intersects R[ab]. Since it doesn't we don't have to check R[a] or R[b].

P[2] intersects R[ab], so we have to check R[a] and R[b].

P[3] intersects R[ab], so we check R[a] and R[b] and find R[b] intersects it.

If R contained a lot of rectangles, we could save a lot of time by checking the union first.

• Yes, I'm now making a new class called "SolidSprite" public SolidSprite(SpriteBatch spriteBatch, Rectangle[] collisionRectangles) to implement this idea – JuanBonnett Feb 14 '15 at 4:02