• Advertisement
Sign in to follow this  

Code Help - Pong

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey fellow aspiring gamedevs,

I am having a weird problem with my Pong game, I am sure my it's something wrong with my update but I don't know what. The ball goes along the same path and it doesn't bounce when it hits a wall. This is is my ball.cs Class and I am trying to get it to bounce of the walls and the player.cs Class.

[source lang="csharp"] public void CheckForCollisonWithWall()
{
if (position.X < 0)
{
position.X = 0;
motion.Y *= -1;
}
if (position.X + texture.Width > screenBounds.Width)
{
position.X = screenBounds.Width - texture.Width;
motion.Y *= -1;
}
if (position.Y < 0)
{
position.Y = 0;

}
if (position.Y + texture.Height > screenBounds.Height)
{
position.Y = screenBounds.Height - texture.Height;
}
}[/source]
-----------------------------------------------------------------
More of my Code
[source lang="csharp"] public void CheckForCollisionWithPaddle(Rectangle paddleLocation)
{
Rectangle ballLocation = new Rectangle(
(int)position.X, (int)position.Y, texture.Width, texture.Height);
if (paddleLocation.Intersects(ballLocation))
{
position.Y = paddleLocation.Y - texture.Height;
motion.Y *= -1;
}
}[/source]

Anyone have any ideas on what I am doing wrong?

Share this post


Link to post
Share on other sites
Advertisement
I see a 'motion' vector which I would take to mean your velocity.

Merely altering the position of the ball on collision will do nothing in changing the velocity (which determines the moving direction) of the ball.

I'm unclear on how you're determining how the ball moves through the game world. Are you simply adding to position every update tick or are you doing proper movement with distance = velocity * time (assuming constant velocity since this is pong)?

When the ball collides with a surface, you will need to [b]reflect[/b] the velocity vector. Merely changing the sign from negative to positive or vice versa will give you weird results. You would notice this any time the ball is going in a diagonal motion. XNA vectors provide a [url="http://msdn.microsoft.com/en-us/library/bb975900%28v=xnagamestudio.20%29.aspx"]Reflect method[/url] to do just that. Edited by caldiar

Share this post


Link to post
Share on other sites
When you're checking for collision with X variables (the sides of the box) you should reverse the X motion, currently you're reversing the Y motion. This means that if the ball hits either side all that will happen is that the ball bounces back and forth up and down as it continues to move off screen to the left or right.

You haven't changed the velocity of the ball for collision with the top or bottom of the screen at all, so there won't be any effect there.

Based on your code I'm thinking the solution should look something like this:

[CODE]
public void CheckForCollisonWithWall()
{
if (position.X < 0)
{
position.X = 0;
motion.X *= -1;
}
if (position.X + texture.Width > screenBounds.Width)
{
position.X = screenBounds.Width - texture.Width;
motion.X *= -1;
}
if (position.Y < 0)
{
position.Y = 0;
motion.Y *= -1;

}
if (position.Y + texture.Height > screenBounds.Height)
{
position.Y = screenBounds.Height - texture.Height;
motion.Y *= -1;

}
}
[/CODE]

Assuming that the paddles are on the top and bottom of the screen, then you're correctly reversing the Y speed. If the paddles are on the left and right then you need to reverse the X speed. Looking at it I presume the paddles are on the top and bottom and you didn't include collision with the top and bottom of the screen since that would mean someone has scored a point. Though I don't know why you are resetting the position of the ball in that case.

[quote name='caldiar' timestamp='1349461061' post='4987196']
I'm unclear on how you're determining how the ball moves through the game world. Are you simply adding to position every update tick or are you doing proper movement with distance = velocity * time (assuming constant velocity since this is pong)?

When the ball collides with a surface, you will need to reflect the velocity vector. Merely changing the sign from negative to positive or vice versa will give you weird results.
[/quote]

I don't think he's using a constant speed vector. I'm assuming that he's using two variables for X and Y speed and incrementing them onto the ball for each tick to move the ball. If that's true then just reversing the sign of the appropriate variable would produce correct motion.

Hopefully the assumptions I've made are close to true and something here helps. Let us know how you get on.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement