Solved

vb.net minigolf issues - bouncing ball and direction

Posted on 2011-09-14
6
354 Views
Last Modified: 2012-05-12
Hi,

So as stated in a previous question, I'm creating a basic minigolf game, as test.
So I managed to move the ball and bounce it off walls.

'create simple form with small picturebox as "gameBall" and timer as "gameTimer"

Public Class Main

    Dim pt As Point
    Dim speed As Double = 0.07
    Dim xVel As Double
    Dim yVel As Double

    Private Sub Main_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick
        xVel = (pt.X - gameBall.Location.X) * speed
        yVel = (pt.Y - gameBall.Location.Y) * speed
        gameTimer.Enabled = True
    End Sub

    Private Sub Main_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
        pt = New Point(e.X, e.Y)
        Me.Refresh()
    End Sub

    Private Sub Main_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        e.Graphics.DrawLine(Pens.Black, gameBall.Location.X,
                            gameBall.Location.Y, pt.X, pt.Y)
    End Sub

    Private Sub gameTimer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles gameTimer.Tick
        'move ball
        gameBall.Location = New Point(gameBall.Location.X + xVel,
                                      gameBall.Location.Y + yVel)

        ' Check for top wall.
        If gameBall.Location.Y < 0 Then
            gameBall.Location = New Point(gameBall.Location.X, 0)
            yVel = -yVel
        End If

        ' Check for bottom wall.
        If gameBall.Location.Y > Me.Height - gameBall.Size.Height Then
            gameBall.Location = New Point(gameBall.Location.X, Me.ClientSize.Height - gameBall.Size.Height)
            yVel = -yVel
        End If

        ' Check for left wall.
        If gameBall.Location.X < 0 Then
            gameBall.Location = New Point(0, gameBall.Location.Y)
            xVel = -xVel
        End If

        ' Check for right wall.
        If gameBall.Location.X > Me.Width - gameBall.Size.Width Then
            gameBall.Location = New Point(Me.ClientSize.Width - gameBall.Size.Width, gameBall.Location.Y)
            xVel = -xVel
        End If

        'stop ball
        If speed = 0 Then
            gameTimer.Enabled = False
        End If
    End Sub
End Class

Open in new window


But I come across small problems:

1. when you move the mouse a few pixels away from the corner at for example a tad more than 90° you'll see that the ball wont move or that it moves straight upwards or downwards and not a few degrees off.

2. There seems to be a problem with the bottom and right wall... the ball keeps bouncing off outside the form although i use the me.clientsize control. Any ideas why this is happening?


Regards
0
Comment
Question by:Mutsop
  • 2
  • 2
  • 2
6 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 250 total points
ID: 36537847
For #2...your initial check was using Width/Height instead of ClientSize.Width/ClientSize.Height:

        ' Check for bottom wall.
        If gameBall.Location.Y > Me.ClientSize.Height - gameBall.Size.Height Then
            gameBall.Location = New Point(gameBall.Location.X, Me.ClientSize.Height - gameBall.Size.Height)
            yVel = -yVel
        End If

        ' Check for right wall.
        If gameBall.Location.X > Me.ClientSize.Width - gameBall.Size.Width Then
            gameBall.Location = New Point(Me.ClientSize.Width - gameBall.Size.Width, gameBall.Location.Y)
            xVel = -xVel
        End If
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 36537849
Can you show a screencast for #1?
0
 
LVL 3

Author Comment

by:Mutsop
ID: 36560565
Sorry for late response, will be able to answer you by tomorrow.
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 35

Assisted Solution

by:Robert Schutt
Robert Schutt earned 250 total points
ID: 36563316
Problem #1 is caused by gameBall.Location being a Point, which is rounded off. 1 pixel difference gives an yVel of .07 and is never gonna show up when adding to an integer. So you can use a temp PointF for example:

    Dim gl As PointF

    Private Sub Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        gl = gameBall.Location
    End Sub

    Private Sub gameTimer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles gameTimer.Tick
        'move ball
        gl.X += xVel
        gl.Y += yVel

        ' Check for top wall.
        If gl.Y < 0 Then
            gl.Y = 0
            yVel = -yVel

            ' Check for bottom wall.
        ElseIf gl.Y > Me.ClientSize.Height - gameBall.Size.Height Then
            gl.Y = Me.ClientSize.Height - gameBall.Size.Height
            yVel = -yVel
        End If

        ' Check for left wall.
        If gl.X < 0 Then
            gl.X = 0
            xVel = -xVel

            ' Check for right wall.
        ElseIf gameBall.Location.X > Me.ClientSize.Width - gameBall.Size.Width Then
            gl.X = Me.ClientSize.Width - gameBall.Size.Width
            xVel = -xVel
        End If

        gameBall.Location = Point.Round(gl)

        'stop ball
        If speed = 0 Then
            gameTimer.Enabled = False
        End If
    End Sub

Open in new window


This has the added advantage of only setting the gameBall.Location once per execution.
0
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 36563393
oh, forgot to replace on line 29:

ElseIf gl.X > ...

Open in new window

0
 
LVL 3

Author Closing Comment

by:Mutsop
ID: 36565753
Oooh should have thought about this!!!! Thanks, this seems to work.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question