• Create Account

Code Help - Pong

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

3 replies to this topic

#1mistervirtue  Members

611
Like
0Likes
Like

Posted 05 October 2012 - 10:49 AM

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?

#2caldiar  Members

715
Like
0Likes
Like

Posted 05 October 2012 - 12:17 PM

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 reflect 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 Reflect method to do just that.

Edited by caldiar, 05 October 2012 - 12:18 PM.

#3PyroDragn  Members

404
Like
1Likes
Like

Posted 05 October 2012 - 12:43 PM

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:

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;

}
}


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.

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.

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.

#4mistervirtue  Members

611
Like
0Likes
Like

Posted 05 October 2012 - 01:36 PM

You fellow gamedevs are very helpful you have answered my question so quickly thanks.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.