9 Replies - 5009 Views - Last Post: 27 May 2009 - 05:19 PM Rate Topic: -----

#1 Lesco   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 55
  • Joined: 05-December 08

Movement and Physics

Posted 20 May 2009 - 07:49 PM

So i am redesigning a donkey kong game, and i have run into a roadblock. I currently have figured out movement using keydown events.

All was well until i had to figure out how to get the barrels to roll down the slanted ledges. It has to roll down one platform going right, then drop down to the next and roll to the left and so on.

But right now i would be happy if i could get someone to help me have it roll straight with no physics :P.

this is where im drawing the barrel and placing it on the form

Dim barrelLoc As String
Dim barrelFrame As Integer = 1
Dim xBarrel As Integer = 200
Dim yBarrel As Integer = 100

 Private Sub gameForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

		barrelLoc = "barrelroll1.gif"


		SetStyle(ControlStyles.DoubleBuffer, True)
		SetStyle(ControlStyles.AllPaintingInWmPaint, True)
	End Sub
  
  Private Sub gameForm_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
		Dim mario As New Bitmap(marioLoc)
		Dim barrel As New Bitmap(barrelLoc)

		'Bmp.MakeTransparent(Color.Lime)
		e.Graphics.DrawImage(New Bitmap(mario), xMario, yMario)
		e.Graphics.DrawImage(New Bitmap(barrel), xBarrel, yBarrel)
	End Sub



I have been trying to put movement into the gameForm_Load event and i have tried so many other ideas that have come to my head :P. Ive done a google search and searched the forums.

If you have a tutorial on object movement without the user activating it, basically random object movement id be grateful for a link, or if anyone could help me please do :D

Thanks,
Lesco

Is This A Good Question/Topic? 0
  • +

Replies To: Movement and Physics

#2 SixOfEleven   User is offline

  • Planeswalker
  • member icon

Reputation: 1055
  • View blog
  • Posts: 6,643
  • Joined: 18-October 08

Re: Movement and Physics

Posted 20 May 2009 - 09:23 PM

The best way for you to do it would be to set up a game loop. That you would call from Form1_Load. Then inside the loop you will update all of the objects, the barrels, mario, etc. Then you would draw all of the objects, process any events pending in the message queue, make sure to do a little timing so your game will run at the same speed on different computers. This is how you would set up a simple game loop:

Public Class Form1

	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		GameLoop()
	End Sub

	Private Sub GameLoop()
		Dim StartTickCount As Long

		Do While Me.Created
			StartTickCount = Environment.TickCount
			UpdateObjects()
			DrawObjects()
			Application.DoEvents()
			Do While Environment.TickCount - StartTickCount < 50
				' No code goes in this loop, it is purely for timing purposes
			Loop
		Loop
	End Sub

	Private Sub UpdateObjects()
		' Perform all of your game logic here
	End Sub

	Private Sub DrawObjects()
		' Perform all of your drawing here
		Me.Invalidate() 'Do not forget to call this after drawing your objects
	End Sub
End Class



There are others that do the same using a Timer control and set the timer interval to a specific value:

Public Class Form1

	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		Me.Timer1.Interval = 250
		Me.Timer1.Start()
	End Sub

	Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
		UpdateObjects()
		DrawObjects()
	End Sub

	Private Sub UpdateObjects()
		' Perform all of your game logic here
	End Sub

	Private Sub DrawObjects()
		' Perform all of your drawing here
		Me.Invalidate() 'Do not forget to call this after drawing your objects
	End Sub

End Class



Technically it is better practice to use the game loop than the Timer control. However, it is entirely up to you. Both approaches will work.
Was This Post Helpful? 0
  • +
  • -

#3 Lesco   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 55
  • Joined: 05-December 08

Re: Movement and Physics

Posted 21 May 2009 - 02:07 PM

when creating the DrawObjects sub i realized that to draw my image im going to need
  Private Sub DrawObjects(ByVal e As PaintEventArgs)
the painteventargs argument, but im having a problem coming up with an argument for where i am calling on the sub inside the GameLoop, any ideas?
Was This Post Helpful? 0
  • +
  • -

#4 SixOfEleven   User is offline

  • Planeswalker
  • member icon

Reputation: 1055
  • View blog
  • Posts: 6,643
  • Joined: 18-October 08

Re: Movement and Physics

Posted 21 May 2009 - 02:27 PM

View PostLesco, on 21 May, 2009 - 03:07 PM, said:

when creating the DrawObjects sub i realized that to draw my image im going to need
  Private Sub DrawObjects(ByVal e As PaintEventArgs)
the painteventargs argument, but im having a problem coming up with an argument for where i am calling on the sub inside the GameLoop, any ideas?


You do not need to pass that. You can create your own graphics object. Since you are using the form to draw to you can do this:

		Dim g As Graphics
		g = Me.CreateGraphics()
		g.DrawImage(New Bitmap(mario), marioX, marioY)
		g.DrawImage(New Bitmap(barrel), barrelX, barrelY)
		Me.Invalidate()



You can do the same with just about any control. This is the same thing doing a picture box:

		Dim g As Graphics
		g = PictureBox1.CreateGraphics()
		g.DrawImage(New Bitmap(mario), marioX, marioY)
		g.DrawImage(New Bitmap(barrel), barrelX, barrelY)
		Me.Invalidate()



*edit*
Feel free to PM me if you are having more problems

This post has been edited by SixOfEleven: 21 May 2009 - 02:30 PM

Was This Post Helpful? 1
  • +
  • -

#5 stayscrisp   User is offline

  • フカユ
  • member icon

Reputation: 1040
  • View blog
  • Posts: 4,325
  • Joined: 14-February 08

Re: Movement and Physics

Posted 22 May 2009 - 04:33 AM

View PostSixOfEleven, on 21 May, 2009 - 01:27 PM, said:

Feel free to PM me if you are having more problems


Could you keep it in the forum please, always good to read problems and how people fixed them :^:
Was This Post Helpful? 0
  • +
  • -

#6 SixOfEleven   User is offline

  • Planeswalker
  • member icon

Reputation: 1055
  • View blog
  • Posts: 6,643
  • Joined: 18-October 08

Re: Movement and Physics

Posted 22 May 2009 - 06:40 AM

View Poststayscrisp, on 22 May, 2009 - 05:33 AM, said:

View PostSixOfEleven, on 21 May, 2009 - 01:27 PM, said:

Feel free to PM me if you are having more problems


Could you keep it in the forum please, always good to read problems and how people fixed them :^:


Good point. Wasn't thinking at the moment. :D
Was This Post Helpful? 0
  • +
  • -

#7 Lesco   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 55
  • Joined: 05-December 08

Re: Movement and Physics

Posted 27 May 2009 - 01:54 PM

so ive got alot of the stuff to work lately but i cant seem to figure out how to make it so my sprite will stay on the form, (ie makes the top, bottom, left and right sides of the form like walls so he cant go through, but i dont know how to detect the collision with a sprite the is being drawn onto the form)

also i have to make the barrels roll down platforms, drop down to the next platform and roll the other direction just like in Donkey Kong but i cant seem to make this happen with my barrel movement (ie my platforms are all pictureboxes). I think this kind of relates to the previous question as it will probably have to do with collision detection, and some physics.

heres what i have so far
Private Sub level1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		marioLoc = "marioright1.gif"
		barrelloc = "barrelroll1.gif"

		SetStyle(ControlStyles.DoubleBuffer, True)
		SetStyle(ControlStyles.AllPaintingInWmPaint, True)

		barrelTimer.Interval = 90
		barrelTimer.Start()
	End Sub

 Private Sub rollBarrel()
		Dim g As Graphics
		g = Me.CreateGraphics()

		If barrelRoll Then
			Dim barrel As New Bitmap(barrelloc)
			g.DrawImage(New Bitmap(barrel), xBarrel, yBarrel)
		End If
	End Sub

	Private Sub barrelTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles barrelTimer.Tick
		rollBarrel()
		If barrel <= 200 Then
			barrelRoll = True
			xBarrel += 4
			yBarrel += 0.7
			barrel += 1
			barrelloc = "barrelroll1.gif"
			barrelTimer.Enabled = True
			Me.Invalidate()

			barrel = barrel + 1
			barrelFrame += 1
			If barrelFrame = 4 Then barrelFrame = 1
			barrelloc = "barrelroll" & barrelFrame & ".gif"
		Else
			barrelTimer.Enabled = False
			barrel = 0
			xBarrel = 0
			yBarrel = 0
			barrelRoll = False
		End If
	End Sub



also if anyone has a tutorial on physics or collision detection using sprites drawn through the graphics engine i would greatly appreciate a link :D
Was This Post Helpful? 0
  • +
  • -

#8 stayscrisp   User is offline

  • フカユ
  • member icon

Reputation: 1040
  • View blog
  • Posts: 4,325
  • Joined: 14-February 08

Re: Movement and Physics

Posted 27 May 2009 - 03:00 PM

Hey there

Well I don't know about VB.NET but this can be achieved by something like this
//pseudo code

if sprites x value is more than 0 or less than the screen(or form in your case) width - sprite width

allow movement

else 

don't allow movement

// same for y values
if sprites y value is more than 0 or less than the screen(or form in your case) height - sprite height

allow movement 

else

don't allow movement



This should work, I'm sorry I cannot provide you with a syntax correct answer but hey I'm sure you can work it out from this.
Was This Post Helpful? 1
  • +
  • -

#9 SixOfEleven   User is offline

  • Planeswalker
  • member icon

Reputation: 1055
  • View blog
  • Posts: 6,643
  • Joined: 18-October 08

Re: Movement and Physics

Posted 27 May 2009 - 04:39 PM

Staycrisp was right on the money. Here is a little snippet of how you could accomplish this in VB.NET:

		If (xValue < 0) Then
			xValue = 0
		End If
		If (yValue < 0) Then
			yValue = 0
		End If
		If (xValue + bWidth > Me.ClientRectangle.Width) Then
			xValue = Me.ClientRectangle.Width - bWidth
		End If
		If (yValue + bHeight > Me.ClientRectangle.Height) Then
			yValue = Me.ClientRectangle.Height - bHeight
		End If



xValue would be the x coordinate of the sprite. yValue would be the y coordinate of the sprite. bWidth and bHeight would be the height and width of the sprite. You can get the height and the width of the form using Me.ClientRectangle.

Now, about how you are drawing the levels. Are you using a back ground image or are you using a tile system? You will probably want to use a tile system. If you aren't using a tile system, leave a note and I will show you how to use a tile system.
Was This Post Helpful? 1
  • +
  • -

#10 Lesco   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 55
  • Joined: 05-December 08

Re: Movement and Physics

Posted 27 May 2009 - 05:19 PM

No i'm not using a tile system. Im using a bg image and picture boxes XD
But if you'd be willing to show me how i would greatly appreciate it!

Edit: Oh and thank you very much both of you for the help on the other part, its working perfectly now!

This post has been edited by Lesco: 27 May 2009 - 05:24 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1