xVxObliVioNxVx

Hi,

So far I have figured out how to get object moving on the screen. I have also figured out how to detect colissions at the edges of the screen. However, I am having trouble figuring out how to detect the collision between two objects. Say for example, two bouncing balls in a 2D game. How can I detect the collision of this two objects I imagine it has something to do with sine and cosine

Thanks,

Oblivion.



Re: XNA Game Studio Express Collision Detection in 2D Games

CodePfo

With spheres, it's really simple, subtract the centers and get the length of that vector. Then add the radii together. If the length < radii, intersection. Other shapes are a little more complicated. Note that this doesn't tell you where the intersection occurred either (which might be important).




Re: XNA Game Studio Express Collision Detection in 2D Games

Kurt Jaegers

The easiest type of collision detection would be bounding-box based. It's not pixel-perfect, but it's a start.

Basically, you should already have a way to determine the rectangle that the sprites occupy on the screen (after all, you need this rectangle to draw the sprites). All you need to do then is check to see if two rectangles overlap by comparing the X, Y, Width, and Height.

[code]

protected bool Intersects(Rectangle rectA, Rectangle rectB)
{
// Returns True if rectA and rectB contain any overlapping points
return (rectA.Right > rectB.Left && rectA.Left < rectB.Right &&
rectA.Bottom > rectB.Top && rectA.Top < rectB.Bottom);
}
[/code]

will return true if the two rectangles intersect. Just run through each of your sprites and test with each of the other sprites they can collide with.

That works great for simple detection, there are pixel-perfect methods to do it as well, one of which can be found here at ziggyware.com





Re: XNA Game Studio Express Collision Detection in 2D Games

George Clingerman

Don't forget about using the built in objects BoundingSphere and BoundingBox.

            BoundingBox aTank = new BoundingBox(new Vector3(12, 15, 0), new Vector3(24, 30, 0));
            BoundingBox aMissle = new BoundingBox(new Vector3(25, 50, 0), new Vector3(50, 100, 0));
 
            if (aTank.Intersects(aMissle) == true)
            {
                //Tank blows up!
            }

Basically, you define the box that surrounds the tank, missle (or whatever sprite you're dealing with). And then update those position in your Update code of course. Then just use the ".Instersects" method of the BoundingBox or BoundingSphere to see if that box/sphere has collided with the one you passed into the method.







Re: XNA Game Studio Express Collision Detection in 2D Games

Jim Perry

There's also this article.




Re: XNA Game Studio Express Collision Detection in 2D Games

xVxObliVioNxVx

Thanks everyone, that really helped get my head arround it.



Re: XNA Game Studio Express Collision Detection in 2D Games

CronosGod

When you have a lot of 2d objects in your scene then a more generic approach becomes worth-while. If you want to read up on it check "sweep and prune" collision detection algorithms.

These work with bounding boxes in a first step when they basically determine which objects might collide, and then depending on which sort of objects they are checks further for collisions.




Re: XNA Game Studio Express Collision Detection in 2D Games

Michael Morton

Nice to see people finding my articles useful :)

Let me know if you have any problems with the samples.






Re: XNA Game Studio Express Collision Detection in 2D Games

xVxObliVioNxVx

Hey Michael.

Thanks for the great tutorial on colission detection. That helped out a lot.

BTW, you have great collection of tutorials. However, most of them (the beta 1 versions) are rather difficult to use and learn from due to the changes form beta 1 to beta 2. Are you planning on converting those tutorials to the current version of XNA





Re: XNA Game Studio Express Collision Detection in 2D Games

Jim Perry

Yeah Ziggy, what's up with that!

I still haven't converted the couple of pieces I did, so I can't really talk.






Re: XNA Game Studio Express Collision Detection in 2D Games

NineOldMen

This helped me soooooo much, thanks for the info. I was trying to use a bounding box with sprites and had all sorts of problems going through the documentation and trying to figure out an efficient way to mix 2D and 3D vectors.....yeah, I made it waaaay to complicated. This is a great, simple to understand solution. Thanks again!