1 Replies - 2285 Views - Last Post: 08 October 2011 - 12:45 PM Rate Topic: -----

#1 Jezzabeanz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 19-September 11

Angle of approach affects rebound [Pong game]

Posted 08 October 2011 - 12:28 PM

Hi,

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.

Thank you.

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


Is This A Good Question/Topic? 0
  • +

Replies To: Angle of approach affects rebound [Pong game]

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9579
  • View blog
  • Posts: 36,291
  • Joined: 12-June 08

Re: Angle of approach affects rebound [Pong game]

Posted 08 October 2011 - 12:45 PM

Why don't you simply flip the X movement's sign when doing the addition for determining the location?

Case in point - a ball is moving down and to the right... it hits the right wall... the downward movement is still wanting to occur.. plus you want to move away from the wall (reflection) so your math to determine the next x location is subtracting and not adding.

0,0 being top left..

the ball is at 50, 50... moving down and to the right... you are adding the set 1, 1 to the x, y
next turn it's 51, 51.
next turn it's 52, 52.
next turn it's 53, 53.
...
next turn it's 60, 60.
it hits a wall.
we still want to move down but we also want to move to the left.
the new addition set is -1, 1 for x, y
next turn it's 59, 61.
next turn it's 58, 62.
next turn it's 57, 63.
... etc


Pretty straight forward.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1