(XNA) beginner code questions

This topic is 4162 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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

Share on other sites
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.

1. 1
Rutin
44
2. 2
3. 3
4. 4
5. 5

• 13
• 10
• 12
• 10
• 13
• Forum Statistics

• Total Topics
632997
• Total Posts
3009781
• Who's Online (See full list)

There are no registered users currently online

×