?
Solved

vb.net minigolf issues - bouncing ball and direction

Posted on 2011-09-14
6
Medium Priority
?
377 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
CHALLENGE LAB: Troubleshooting Connectivity Issues

Goal: Fix the connectivity issue in the lab's AWS environment so that you can SSH into the provided EC2 instance.  

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

777 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