Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualJosh Petrie

Posted 30 September 2012 - 10:11 PM

EDIT: I have solved my problem. All I needed was to change the send and third ifs to if elses. If anyone could tell me why this is, I'd love to know.

I've made a simple pong game, using Rectangle.Intersects(Rectangle) for hit detection. However, I'd like some more variability, so I want the ball to move differently depending on the spot on the paddle it was hit. [Ball movement has an XSpeed value and a YSpeed value, and each Update the ball moves along the X-axis a number of pixels equal to XSpeed; ditto for Y-axis and YSpeed.]

This is what is intended:
If the ball hits the top third of the paddle, YSpeed decreases by 1. If the ball hits the middle third of the paddle, YSpeed doesn't change. If the ball hits the bottom third of the paddle, YSpeed increases by 1. If the ball hits any part of the paddle, the XSpeed is reversed.

However, since I subdivided the paddles into three Rectangles, hit detection no longer functions properly. The ball changes direction only if the middle third of the paddle is hit, and the ball just flies through the top and bottom thirds.

This is really weird. I can not find the error. I've gone over the code several times, but I'm just stuck.


[source lang="csharp"]private void checkHit(){ Rectangle paddle1RectTop = new Rectangle(paddle1.X, paddle1.Y, paddleTexture.Width, paddleTexture.Height / 3); Rectangle paddle1RectMid = new Rectangle(paddle1.X, paddle1.Y + paddleTexture.Height / 3, paddleTexture.Width, paddleTexture.Height / 3); Rectangle paddle1RectBot = new Rectangle(paddle1.X, paddle1.Y + paddleTexture.Height * (2/3), paddleTexture.Width, paddleTexture.Height / 3); Rectangle paddle2RectTop = new Rectangle(paddle2.X, paddle2.Y, paddleTexture.Width, paddleTexture.Height / 3); Rectangle paddle2RectMid = new Rectangle(paddle2.X, paddle2.Y + paddleTexture.Height / 3, paddleTexture.Width, paddleTexture.Height / 3); Rectangle paddle2RectBot = new Rectangle(paddle2.X, paddle2.Y + paddleTexture.Height * (2/3), paddleTexture.Width, paddleTexture.Height / 3); Rectangle ballRect = new Rectangle(ball.X, ball.Y, ballTexture.Width, ballTexture.Height); Boolean hasHitPaddle1Top = paddle1RectTop.Intersects(ballRect); Boolean hasHitPaddle1Mid = paddle1RectMid.Intersects(ballRect); Boolean hasHitPaddle1Bot = paddle1RectBot.Intersects(ballRect); Boolean hasHitPaddle2Top = paddle2RectTop.Intersects(ballRect); Boolean hasHitPaddle2Mid = paddle2RectMid.Intersects(ballRect); Boolean hasHitPaddle2Bot = paddle2RectBot.Intersects(ballRect); if(hasHitPaddle1Top || hasHitPaddle2Top) { ball.XSpeed = -ball.XSpeed; ball.YSpeed = ball.YSpeed - 1; } if(hasHitPaddle1Mid || hasHitPaddle2Mid) { ball.XSpeed = -ball.XSpeed; } if(hasHitPaddle1Bot || hasHitPaddle2Bot) { ball.XSpeed = -ball.XSpeed; ball.YSpeed = ball.YSpeed + 1; }}[/source]

#5JDCAce

Posted 28 September 2012 - 11:27 PM

EDIT: I have solved my problem. All I needed was to change the send and third ifs to if elses. If anyone could tell me why this is, I'd love to know.

I've made a simple pong game, using Rectangle.Intersects(Rectangle) for hit detection. However, I'd like some more variability, so I want the ball to move differently depending on the spot on the paddle it was hit. [Ball movement has an XSpeed value and a YSpeed value, and each Update the ball moves along the X-axis a number of pixels equal to XSpeed; ditto for Y-axis and YSpeed.]

This is what is intended:
If the ball hits the top third of the paddle, YSpeed decreases by 1. If the ball hits the middle third of the paddle, YSpeed doesn't change. If the ball hits the bottom third of the paddle, YSpeed increases by 1. If the ball hits any part of the paddle, the XSpeed is reversed.

However, since I subdivided the paddles into three Rectangles, hit detection no longer functions properly. The ball changes direction only if the middle third of the paddle is hit, and the ball just flies through the top and bottom thirds.

This is really weird. I can not find the error. I've gone over the code several times, but I'm just stuck.


[source lang="csharp"]private void checkHit(){ Rectangle paddle1RectTop = new Rectangle(paddle1.X, paddle1.Y, paddleTexture.Width, paddleTexture.Height / 3); Rectangle paddle1RectMid = new Rectangle(paddle1.X, paddle1.Y + paddleTexture.Height / 3, paddleTexture.Width, paddleTexture.Height / 3); Rectangle paddle1RectBot = new Rectangle(paddle1.X, paddle1.Y + paddleTexture.Height * (2/3), paddleTexture.Width, paddleTexture.Height / 3); Rectangle paddle2RectTop = new Rectangle(paddle2.X, paddle2.Y, paddleTexture.Width, paddleTexture.Height / 3); Rectangle paddle2RectMid = new Rectangle(paddle2.X, paddle2.Y + paddleTexture.Height / 3, paddleTexture.Width, paddleTexture.Height / 3); Rectangle paddle2RectBot = new Rectangle(paddle2.X, paddle2.Y + paddleTexture.Height * (2/3), paddleTexture.Width, paddleTexture.Height / 3); Rectangle ballRect = new Rectangle(ball.X, ball.Y, ballTexture.Width, ballTexture.Height); Boolean hasHitPaddle1Top = paddle1RectTop.Intersects(ballRect); Boolean hasHitPaddle1Mid = paddle1RectMid.Intersects(ballRect); Boolean hasHitPaddle1Bot = paddle1RectBot.Intersects(ballRect); Boolean hasHitPaddle2Top = paddle2RectTop.Intersects(ballRect); Boolean hasHitPaddle2Mid = paddle2RectMid.Intersects(ballRect); Boolean hasHitPaddle2Bot = paddle2RectBot.Intersects(ballRect); if(hasHitPaddle1Top || hasHitPaddle2Top) { ball.XSpeed = -ball.XSpeed; ball.YSpeed = ball.YSpeed - 1; } if(hasHitPaddle1Mid || hasHitPaddle2Mid) { ball.XSpeed = -ball.XSpeed; } if(hasHitPaddle1Bot || hasHitPaddle2Bot) { ball.XSpeed = -ball.XSpeed; ball.YSpeed = ball.YSpeed + 1; }}[/source]

#4JDCAce

Posted 28 September 2012 - 11:13 PM

EDIT: I have solved my problem. All I needed was to change the send and third ifs to if elses.

I've made a simple pong game, using Rectangle.Intersects(Rectangle) for hit detection. However, I'd like some more variability, so I want the ball to move differently depending on the spot on the paddle it was hit. [Ball movement has an XSpeed value and a YSpeed value, and each Update the ball moves along the X-axis a number of pixels equal to XSpeed; ditto for Y-axis and YSpeed.]

This is what is intended:
If the ball hits the top third of the paddle, YSpeed decreases by 1. If the ball hits the middle third of the paddle, YSpeed doesn't change. If the ball hits the bottom third of the paddle, YSpeed increases by 1. If the ball hits any part of the paddle, the XSpeed is reversed.

However, since I subdivided the paddles into three Rectangles, hit detection no longer functions properly. The ball changes direction only if the middle third of the paddle is hit, and the ball just flies through the top and bottom thirds.

This is really weird. I can not find the error. I've gone over the code several times, but I'm just stuck.


[source lang="csharp"]private void checkHit(){ Rectangle paddle1RectTop = new Rectangle(paddle1.X, paddle1.Y, paddleTexture.Width, paddleTexture.Height / 3); Rectangle paddle1RectMid = new Rectangle(paddle1.X, paddle1.Y + paddleTexture.Height / 3, paddleTexture.Width, paddleTexture.Height / 3); Rectangle paddle1RectBot = new Rectangle(paddle1.X, paddle1.Y + paddleTexture.Height * (2/3), paddleTexture.Width, paddleTexture.Height / 3); Rectangle paddle2RectTop = new Rectangle(paddle2.X, paddle2.Y, paddleTexture.Width, paddleTexture.Height / 3); Rectangle paddle2RectMid = new Rectangle(paddle2.X, paddle2.Y + paddleTexture.Height / 3, paddleTexture.Width, paddleTexture.Height / 3); Rectangle paddle2RectBot = new Rectangle(paddle2.X, paddle2.Y + paddleTexture.Height * (2/3), paddleTexture.Width, paddleTexture.Height / 3); Rectangle ballRect = new Rectangle(ball.X, ball.Y, ballTexture.Width, ballTexture.Height); Boolean hasHitPaddle1Top = paddle1RectTop.Intersects(ballRect); Boolean hasHitPaddle1Mid = paddle1RectMid.Intersects(ballRect); Boolean hasHitPaddle1Bot = paddle1RectBot.Intersects(ballRect); Boolean hasHitPaddle2Top = paddle2RectTop.Intersects(ballRect); Boolean hasHitPaddle2Mid = paddle2RectMid.Intersects(ballRect); Boolean hasHitPaddle2Bot = paddle2RectBot.Intersects(ballRect); if(hasHitPaddle1Top || hasHitPaddle2Top) { ball.XSpeed = -ball.XSpeed; ball.YSpeed = ball.YSpeed - 1; } if(hasHitPaddle1Mid || hasHitPaddle2Mid) { ball.XSpeed = -ball.XSpeed; } if(hasHitPaddle1Bot || hasHitPaddle2Bot) { ball.XSpeed = -ball.XSpeed; ball.YSpeed = ball.YSpeed + 1; }}[/source]

#3JDCAce

Posted 28 September 2012 - 11:12 PM

EDIT: I have solved my problem. All I needed was to change the send and third ifs to if elses.

I've made a simple pong game, using Rectangle.Intersects(Rectangle) for hit detection. However, I'd like some more variability, so I want the ball to move differently depending on the spot on the paddle it was hit. [Ball movement has an XSpeed value and a YSpeed value, and each Update the ball moves along the X-axis a number of pixels equal to XSpeed; ditto for Y-axis and YSpeed.]

This is what is intended:
If the ball hits the top third of the paddle, YSpeed decreases by 1. If the ball hits the middle third of the paddle, YSpeed doesn't change. If the ball hits the bottom third of the paddle, YSpeed increases by 1. If the ball hits any part of the paddle, the XSpeed is reversed.

However, since I subdivided the paddles into three Rectangles, hit detection no longer functions properly. The ball changes direction only if the middle third of the paddle is hit, and the ball just flies through the top and bottom thirds.

This is really weird. I can not find the error. I've gone over the code several times, but I'm just stuck.


[source lang="csharp"]private void checkHit(){ Rectangle paddle1RectTop = new Rectangle(paddle1.X, paddle1.Y, paddleTexture.Width, paddleTexture.Height / 3); Rectangle paddle1RectMid = new Rectangle(paddle1.X, paddle1.Y + paddleTexture.Height / 3, paddleTexture.Width, paddleTexture.Height / 3); Rectangle paddle1RectBot = new Rectangle(paddle1.X, paddle1.Y + paddleTexture.Height * (2/3), paddleTexture.Width, paddleTexture.Height / 3); Rectangle paddle2RectTop = new Rectangle(paddle2.X, paddle2.Y, paddleTexture.Width, paddleTexture.Height / 3); Rectangle paddle2RectMid = new Rectangle(paddle2.X, paddle2.Y + paddleTexture.Height / 3, paddleTexture.Width, paddleTexture.Height / 3); Rectangle paddle2RectBot = new Rectangle(paddle2.X, paddle2.Y + paddleTexture.Height * (2/3), paddleTexture.Width, paddleTexture.Height / 3); Rectangle ballRect = new Rectangle(ball.X, ball.Y, ballTexture.Width, ballTexture.Height); Boolean hasHitPaddle1Top = paddle1RectTop.Intersects(ballRect); Boolean hasHitPaddle1Mid = paddle1RectMid.Intersects(ballRect); Boolean hasHitPaddle1Bot = paddle1RectBot.Intersects(ballRect); Boolean hasHitPaddle2Top = paddle2RectTop.Intersects(ballRect); Boolean hasHitPaddle2Mid = paddle2RectMid.Intersects(ballRect); Boolean hasHitPaddle2Bot = paddle2RectBot.Intersects(ballRect); if(hasHitPaddle1Top || hasHitPaddle2Top) { ball.XSpeed = -ball.XSpeed; ball.YSpeed = ball.YSpeed - 1; } if(hasHitPaddle1Mid || hasHitPaddle2Mid) { ball.XSpeed = -ball.XSpeed; } if(hasHitPaddle1Bot || hasHitPaddle2Bot) { ball.XSpeed = -ball.XSpeed; ball.YSpeed = ball.YSpeed + 1; }}[/source]

#2JDCAce

Posted 28 September 2012 - 11:12 PM

I've made a simple pong game, using Rectangle.Intersects(Rectangle) for hit detection. However, I'd like some more variability, so I want the ball to move differently depending on the spot on the paddle it was hit. [Ball movement has an XSpeed value and a YSpeed value, and each Update the ball moves along the X-axis a number of pixels equal to XSpeed; ditto for Y-axis and YSpeed.]

This is what is intended:
If the ball hits the top third of the paddle, YSpeed decreases by 1. If the ball hits the middle third of the paddle, YSpeed doesn't change. If the ball hits the bottom third of the paddle, YSpeed increases by 1. If the ball hits any part of the paddle, the XSpeed is reversed.

However, since I subdivided the paddles into three Rectangles, hit detection no longer functions properly. The ball changes direction only if the middle third of the paddle is hit, and the ball just flies through the top and bottom thirds.

This is really weird. I can not find the error. I've gone over the code several times, but I'm just stuck.


[source lang="csharp"]private void checkHit(){ Rectangle paddle1RectTop = new Rectangle(paddle1.X, paddle1.Y, paddleTexture.Width, paddleTexture.Height / 3); Rectangle paddle1RectMid = new Rectangle(paddle1.X, paddle1.Y + paddleTexture.Height / 3, paddleTexture.Width, paddleTexture.Height / 3); Rectangle paddle1RectBot = new Rectangle(paddle1.X, paddle1.Y + paddleTexture.Height * (2/3), paddleTexture.Width, paddleTexture.Height / 3); Rectangle paddle2RectTop = new Rectangle(paddle2.X, paddle2.Y, paddleTexture.Width, paddleTexture.Height / 3); Rectangle paddle2RectMid = new Rectangle(paddle2.X, paddle2.Y + paddleTexture.Height / 3, paddleTexture.Width, paddleTexture.Height / 3); Rectangle paddle2RectBot = new Rectangle(paddle2.X, paddle2.Y + paddleTexture.Height * (2/3), paddleTexture.Width, paddleTexture.Height / 3); Rectangle ballRect = new Rectangle(ball.X, ball.Y, ballTexture.Width, ballTexture.Height); Boolean hasHitPaddle1Top = paddle1RectTop.Intersects(ballRect); Boolean hasHitPaddle1Mid = paddle1RectMid.Intersects(ballRect); Boolean hasHitPaddle1Bot = paddle1RectBot.Intersects(ballRect); Boolean hasHitPaddle2Top = paddle2RectTop.Intersects(ballRect); Boolean hasHitPaddle2Mid = paddle2RectMid.Intersects(ballRect); Boolean hasHitPaddle2Bot = paddle2RectBot.Intersects(ballRect); if(hasHitPaddle1Top || hasHitPaddle2Top) { ball.XSpeed = -ball.XSpeed; ball.YSpeed = ball.YSpeed - 1; } if(hasHitPaddle1Mid || hasHitPaddle2Mid) { ball.XSpeed = -ball.XSpeed; } if(hasHitPaddle1Bot || hasHitPaddle2Bot) { ball.XSpeed = -ball.XSpeed; ball.YSpeed = ball.YSpeed + 1; }}[/source]

#1JDCAce

Posted 28 September 2012 - 10:26 PM

I've made a simple pong game, using Rectangle.Intersects(Rectangle) for hit detection. However, I'd like some more variability, so I want the ball to move differently depending on the spot on the paddle it was hit. [Ball movement has an XSpeed value and a YSpeed value, and each Update the ball moves along the X-axis a number of pixels equal to XSpeed; ditto for Y-axis and YSpeed.]

This is what is intended:
If the ball hits the top third of the paddle, YSpeed decreases by 1. If the ball hits the middle third of the paddle, YSpeed doesn't change. If the ball hits the bottom third of the paddle, YSpeed increases by 1. If the ball hits any part of the paddle, the XSpeed is reversed.

However, since I subdivided the paddles into three Rectangles, hit detection no longer functions properly. The ball changes direction only if the middle third of the paddle is hit, and the ball just flies through the top and bottom thirds.

This is really weird. I can not find the error. I've gone over the code several times, but I'm just stuck.


[source lang="csharp"]private void checkHit(){ Rectangle paddle1RectTop = new Rectangle(paddle1.X, paddle1.Y, paddleTexture.Width, paddleTexture.Height / 3); Rectangle paddle1RectMid = new Rectangle(paddle1.X, paddle1.Y + paddleTexture.Height / 3, paddleTexture.Width, paddleTexture.Height / 3); Rectangle paddle1RectBot = new Rectangle(paddle1.X, paddle1.Y + paddleTexture.Height * (2/3), paddleTexture.Width, paddleTexture.Height / 3); Rectangle paddle2RectTop = new Rectangle(paddle2.X, paddle2.Y, paddleTexture.Width, paddleTexture.Height / 3); Rectangle paddle2RectMid = new Rectangle(paddle2.X, paddle2.Y + paddleTexture.Height / 3, paddleTexture.Width, paddleTexture.Height / 3); Rectangle paddle2RectBot = new Rectangle(paddle2.X, paddle2.Y + paddleTexture.Height * (2/3), paddleTexture.Width, paddleTexture.Height / 3); Rectangle ballRect = new Rectangle(ball.X, ball.Y, ballTexture.Width, ballTexture.Height); Boolean hasHitPaddle1Top = paddle1RectTop.Intersects(ballRect); Boolean hasHitPaddle1Mid = paddle1RectMid.Intersects(ballRect); Boolean hasHitPaddle1Bot = paddle1RectBot.Intersects(ballRect); Boolean hasHitPaddle2Top = paddle2RectTop.Intersects(ballRect); Boolean hasHitPaddle2Mid = paddle2RectMid.Intersects(ballRect); Boolean hasHitPaddle2Bot = paddle2RectBot.Intersects(ballRect); if(hasHitPaddle1Top || hasHitPaddle2Top) { ball.XSpeed = -ball.XSpeed; ball.YSpeed = ball.YSpeed - 1; } if(hasHitPaddle1Mid || hasHitPaddle2Mid) { ball.XSpeed = -ball.XSpeed; } if(hasHitPaddle1Bot || hasHitPaddle2Bot) { ball.XSpeed = -ball.XSpeed; ball.YSpeed = ball.YSpeed + 1; }}[/source]

PARTNERS