0
votes

Alright, so currently now, I have my collision detections in my Game1 class (Main class). For example: rectangle collision between my player and ball. I don't want these collisions to be in my Game1 class. In what class people often check collision detections and how?

Thanks in advanced.

1
I would define what area of the widget would be a collision within the widget class. I would then check for a collision within the code that moves the player. This way you can have different widgets that would have different collision areas.Security Hound
@Ramhound I didn't really understand what you said, maybe it's because of my bad English, but let me explain you again what I'm asking anyways, : Where do I suppose to check the collision.Rich Porter
@Ramhound If I have to check collision between ball and player, in what class should I implement the code? and how?Rich Porter

1 Answers

4
votes

I would make a class called 'CollisionManager', This way you can code all your collisions in this class and use them in your Game class. You can basically call it what you want though since it's personal preference. Whatever you seem fit to call it.

An example of collision detection can be found here: example. Credit goes to the writer of this article though and not myself.

Even more resources can be found at the App Hub, you can find much more information about XNA there, not only about collisions.

Hope it helped!

Edit:

Basically you have the CollissionManager class which has all the functions you need for your game regarding collisions. For example you asked about a paddle and a ball. So you can write a function with the Paddle and Ball as parameter. Then you can check in that function if they collide.

protected boolean PaddleBallCollision(Rectangle paddle, Rectangle ball)
{
 if (paddle.Intersects(ball)) return paddleHit= true;
}

In your Game class you can just do the following:

using projectname.CollisionManager;

And in the Update (I think atleast, it's been a year since I used XNA) you can check if they collide by calling your function.

boolean paddleHit =  CollisionManager.PaddleBallCollision(paddle,ball)

if (paddleHit == true)
{
   //TODO when hit
}

Something like this I believe. Since a ball is round a Rectangle wouldn't be best for it but I can't say the perfect thing from the top of my head. As I said it's been a while since I used this but this should give you a general idea!