Jump to content
  • Advertisement
Sign in to follow this  
PeterVercauteren

minigolf issue - ball movement

This topic is 2650 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

[font="Arial, Helvetica, sans-serif"][color="#333333"]
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 SubEnd Class


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[/font]

Share this post


Link to post
Share on other sites
Advertisement
Well this seems odd... If gameBall.Location.Y > Me.Height - gameBall.Size.Height Then gameBall.Location = New Point(gameBall.Location.X, Me.ClientSize.Height - gameBall.Size.Height)You check for one condition, then set it to some other size... why not always use Me.Height? or Me.ClientSize.Height?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!