开发者

Very basic question about collision detections. XNA Beginner

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 w开发者_运维知识库ant these collisions to be in my Game1 class. In what class people often check collision detections and how?


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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜