1
\$\begingroup\$

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

enter image description here

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

enter image description here

\$\endgroup\$
0
\$\begingroup\$

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

enter image description here

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.

\$\endgroup\$
  • \$\begingroup\$ 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 \$\endgroup\$ – JuanBonnett Feb 14 '15 at 1:51
  • \$\begingroup\$ 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 \$\endgroup\$ – Pip Feb 14 '15 at 1:56
0
\$\begingroup\$

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

enter image description here

\$\endgroup\$
0
\$\begingroup\$

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.

enter image description here

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.