Solved

vb.net minigolf issues - bouncing ball and direction

Posted on 2011-09-14
6
347 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

747 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

12 Experts available now in Live!

Get 1:1 Help Now