Sign in to follow this  
Ryanq

C# XNA 2D collision detection

Recommended Posts

Ryanq    100
hi i have been learning xna lately and am trying to create a 2d game.

i've made a game where a player controls a sprite and other sprites appear and fire at the player, but!, (this was from an online lesson) I have no collision detection!

i'm mad that my lesson negated to explain this, as I thought I had the basic reusable code for loading, moving and launching things from sprites. Which are controlled by classes for each that I can familirazie myself with, understand and can modify.

so I googled xna sprite collision, and with my limited knowledge the examples I do not know how to apply to my game. Is there a collision class I can download and then use to add the objects in my game for? then study and learn the code that way and modify etc?

Share this post


Link to post
Share on other sites
BierbyteZ    115
The simplest way would be to implement a collision method in your game class, should work for the beginning.

This method will be able to use your player and bullet or enemy object.
Then you can create a method on each of the gameobject classes to create and return a rectangle. This rectangle can be used as a boundingbox.

In the collision method you can then check if these boxes are colliding.
The rectangle class has methods to return the coordinates of each side (Top, Left, Right, Bottom).

http://www.xnaresources.com/pages.asp?pageid=8
Take a look at this site for more information

Thats what I have done in my first game.

Share this post


Link to post
Share on other sites
Brain    18906
This works, you can use a bounding box for collision but if you use this on a non-square object like for example a space ship or a missile, it will appear to collide before it actually makes contact.

There are several extra things you can do either AFTER the two sprites are found to be intersecting each others bounding boxes, or BEFORE if you are looking ahead.

See this post on the XNA forums for example code.

Share this post


Link to post
Share on other sites
Ryanq    100
hi BierbyteZ

thanks for the link to that site, that does look like a cool tutorial to do and learn from, i'm doing it now. although i'm stuck on one thing and wondering if you know why since you made it yourself. i'm at the end of part 2 "foundations"

and I get a compiler error saying it can't find the explosions file:

spriteBatch = new SpriteBatch(GraphicsDevice);

Explosion = new AnimatedSprite(
Content.Load<Texture2D>(@"Textures\explosions"),
0, 0, 64, 64, 16);
Explosion.X = 0;
Explosion.Y = 0;

I have a folder called Textures with explosions.png showing in their in solution explorer, but it doesn't seem to find it?

Share this post


Link to post
Share on other sites
BjsAust    116
Quote:
Original post by Ryanq
I have a folder called Textures with explosions.png showing in their in solution explorer, but it doesn't seem to find it?


Is the Textures folder under your Content folder (correct) or your game folder (incorrect)? Also double check the obvious such as spelling/etc.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this