Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Back again from my last question. Now I was curious I use a foreach statement to use the same shot class. How would I go about doing collison detection.

I used the tutorial here on how to shoot a fireball
XNA Development: Game Development for the masses.

I tried to put in several places a foreach to look at all of them to see if they have reached the borders of my sprite hero but doesn't seem to do anything. If again some one might know of a good site that has tutorials to explain collision detection a little bit better that would be appriecated.

share|improve this question

1 Answer 1

up vote 0 down vote accepted

The fireball is a 'box' of width and height 200. And it's at a given position. Add a rectangle

Rectangle boundingRect = new Rectangle(position.X, position.Y, 200, 200); 

to your fireball class.

Make sure to update the rectangle with the new position of the fireball every frame (so in the fireball's update or draw method)

Do the same thing for your player class.

Now you can check if the fireball has hit the player like this in your game's update method:

if(fireball.boundingRect.Intersects(player.boundingRect)) {
    //hit
}
share|improve this answer
    
I have tried that before with no results. Where would I put the rectangle portion and where would I put the if statement. –  user5476 Feb 15 '11 at 20:26
    
add the rectangle portion to your fireball class (now you fireball has a rectangle, which will follow the position of your fireball image) You can add the if statement wherever in the update statement though i do mine towards the end –  Spooks Feb 15 '11 at 23:00
    
I mean in which function of the class and do I also add a rectangle for my hero? Also which function for the update cause i have a hereo, enemy, spirte, and fireball calss along wiht the game1 class. –  user5476 Feb 15 '11 at 23:21
    
Hey Bradley, I explicitly told you to make a rectangle in both your hero class and fireball class, however you could also add one in your enemey class. If your hero, enemey and fireball all derive from the same class, then you could add it in that base class instead, make sure that you update the position of the rectangle every frame (you have todo this in the update or draw method of the entity that has the rectangle). Now the collision checking part needs to be done somewhere where you have access to all the items, usually in the game1.Update(..) method. –  Roy T. Feb 16 '11 at 7:04
    
Thank you again for the help. Finally got it after thinking aobut it yet again. I had to add a rectangle for the player that was for his fireball and him, then I added a rectangle for the enemy and his fireball. Of course every update I had if jumping or moving and for the firball being shot. Then in the game update of game1.cs I just added an if statement to test it and then it would close the game if it ever became true. –  user5476 Feb 17 '11 at 0:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.