Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

vb.net minigolf issues - bouncing ball and direction

Posted on 2011-09-14
6
Medium Priority
?
384 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 1000 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 86

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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 35

Assisted Solution

by:Robert Schutt
Robert Schutt earned 1000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
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…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

604 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