I am currently creating the classic Pong game in VB.Net. I have a few questions regarding angle of approach and rebound.
For example if the ball hits the wall at roughly a 30 degree angle then it should roughly bounce off the wall at ~30 degrees. This is my first query, how would I achieve this as I cannot figure out how. It is currently bouncing the ball off the wall by altering ballXspeed and ballYspeed by 5 or -5.
My second query is that of the ball hitting the paddle, if the ball is to hit to top 0% - 5% of the paddle it will bounce off at a different angle opposed to if the ball hit the paddle in the 15% - 20% zone of the paddle.
Here is my code in its entirety:
Public Class Form1 Dim ballXSpeed As Single Dim ballYSpeed As Single Dim aScore As Integer Dim bScore As Integer Dim level As Single = 1 Dim bSpeed As Single Dim aPaddleHeight As Integer = 80 Dim bPaddleHeight As Integer = 80 Dim eRectionDistance As Integer = 30 Dim ePaddleSpeed As Integer = 4 Dim lives As Integer = 2 Dim playing As Boolean = True Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ballXSpeed = 5 ballYSpeed = -5 End Sub Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick If playing = True Then Startgame() ElseIf playing = False Then Application.Exit() End If End Sub Private Sub Startgame() 'To Do 'Make the ball bounce off the paddle according to where the ball hit the paddle. 'e.g if it hits the top half of the paddle, the ball will bounce off at a different angle than if it hit the bottom. 'Fix the balls bounce action when hits the wall 'Initiate ball movement ball.Location = New Point(ball.Location.X + Math.Round(ballXSpeed), ball.Location.Y + Math.Round(ballYSpeed)) 'Detect collision with player paddle If ball.Right > playerPaddle.Right Then If ball.Top < playerPaddle.Bottom And ball.Bottom > playerPaddle.Top And ball.Left < playerPaddle.Right Then ballXSpeed = 5 ballYSpeed = New Random(DateTime.Now.Millisecond).Next(-10, 10) End If End If 'Detect collision with enemy paddle If ball.Right >= enemyPaddle.Left Then If ball.Top < enemyPaddle.Bottom And ball.Bottom > enemyPaddle.Top And ball.Left < enemyPaddle.Left Then ballXSpeed = -5 ballYSpeed = New Random(DateTime.Now.Millisecond).Next(-10, 10) End If End If 'Collisions with walls If ball.Location.X < 0 Then ballXSpeed = 5 End If If ball.Location.X >= Me.Width Then ballXSpeed = -5 End If If ball.Location.Y < 0 Then ballYSpeed = 5 End If If ball.Location.Y >= Me.Height Then ballYSpeed = -5 End If 'Collision with friendly net If ball.Left = Net1.Right Then bScore += 1 ballXSpeed = 5 ElseIf ball.Right = Net2.Left Then aScore += 1 ballXSpeed = -5 End If 'Increment Score Label2.Text = aScore Label4.Text = bScore 'CPU Paddle logic If ball.Bottom < enemyPaddle.Top + eRectionDistance Then 'Move Up enemyPaddle.Top -= ePaddleSpeed End If If ball.Top > enemyPaddle.Bottom - eRectionDistance Then 'Move Down enemyPaddle.Top += ePaddleSpeed End If If aScore = bScore + 1 Then NewLevel() ElseIf bScore = aScore + 1 Then EnemyWin() End If LevelNo.Text = level lblRemainingLives.Text = lives End Sub Private Sub NewLevel() 'Increment level by one level += 1 If aScore = bScore + 1 Then playerPaddle.Height = aPaddleHeight - 10 ePaddleSpeed += 0.2 aScore = 0 bScore = 0 End If If level = 3 Then lives += 1 End If End Sub Private Sub EnemyWin() 'Check to see if the enemy beat player by 5 points ' This will reduce the enemies paddle size by 10 and increase the player's paddle by 10 ' Furthermore, the enemies paddle speed will be reduced by 0.5 If bScore = aScore + 1 Then enemyPaddle.Height = bPaddleHeight - 10 playerPaddle.Height = aPaddleHeight + 10 ePaddleSpeed -= 0.5 aScore = 0 bScore = 0 End If 'Reduce the players lives by 1 when they lose. When their lives reach zero a message box is shown lives -= 1 If lives = 0 Then EndGame() End If End Sub Private Sub EndGame() If MessageBox.Show("Do you want to play again", "Yes", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then playing = True Application.Restart() Else playing = False End If End Sub 'CONTROL PLAYER'S PADDLE Private Sub pongMain_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove 'e = Cursor If e.Y > 5 And e.Y < Me.Height - playerPaddle.Height Then playerPaddle.Location = New Point(playerPaddle.Location.X, e.Y) End If End Sub End Class
I understand that you cannot do the work for me and I don't expect you to. Would it be possible for help with the pseudo-logic or a particular area that will help better my understanding on how to do this.
EDIT: I forgot to mention that the ball currently bounces off the paddles at a random angle. Hence:
ballXSpeed = -5 ballYSpeed = New Random(DateTime.Now.Millisecond).Next(-10, 10)
This post has been edited by Jezzabeanz: 08 October 2011 - 12:31 PM