(XNA) beginner code questions

Started by
0 comments, last by Days 16 years, 10 months ago
I'm writing a pong clone as my first game and everything is fine up to this point. My first question is..I have 3 images, two paddles and a ball. I need to check a collision for a maximum Y coordinate that the images can move to. Since my paddles are different sizes than my balls, does this mean I have to create seperate variables..for example this is what I have.

int maxY = Window.ClientBounds.Height - 90;
int ballMaxY = Window.ClientBounds.Height - 50;
is there anyway where I can just have one variable and somehow determine how much to subtract from the Window.ClientBounds.Height? My second question has to do with the ball movement and collision. I did write a pong program in Blitz Basic if any of you have used that, but I forgot how I did it. I remember I had to use ballXVelocity or something in the math part. Can someone give me a quick template to look at? Thanks!! Mark
Advertisement
Your best bet is going to be creating what is called a bounding box.
In a 2d game you are looking at 2 2d vectors describing the min and max values for the game objects. These are members of your object class and are used when testing for collisions.

For instance

struct BBox
{
Vector2 min;
Vector2 max;
}

The min is the offset from the center of your object to the lower left corner, and the max is the offset from the center of your object to the upper right. (Or if you have flipped your axis ie. your Y value increases as you go down the screen, you can reverse the min and max to make more sense).

After that, you will simply test collisions by using the X,Y position of your object + the bounding offsets. This makes for less code and less headaches trying to keep it straight which side of the object you should be testing.

This topic is closed to new replies.

Advertisement