Solved

vb.net minigolf issues - bouncing ball and direction

Posted on 2011-09-14
6
350 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
DevOps Toolchain Recommendations

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

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
A short film showing how OnPage and Connectwise integration works.

930 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now