how to move an object around a picturebox !!
how to move an object around a picturebox !!
how to move an object around a picturebox !!
how to move an object around a picturebox !!
how to move an object around a picturebox !!
how to move an object around a picturebox !! how to move an object around a picturebox !! how to move an object around a picturebox !! how to move an object around a picturebox !! how to move an object around a picturebox !! how to move an object around a picturebox !! how to move an object around a picturebox !! how to move an object around a picturebox !!
how to move an object around a picturebox !! how to move an object around a picturebox !!
how to move an object around a picturebox !!
Go Back  Xtreme Visual Basic Talk > > > how to move an object around a picturebox !!


Reply
 
Thread Tools Display Modes
  #1  
Old 01-02-2011, 07:45 AM
head_math head_math is offline
Newcomer
 
Join Date: Jan 2011
Posts: 14
Default how to move an object around a picturebox !!


hey guys, im a new vb user, i have a project about making a small game about a player moving in 5 rooms with some enemies.

anyway im only facing a problem with how to make a collision between the player object and the wall which is a picturebox

i can move the player object (which is also a picture box) with the arrow keys and i've made it move inside the form border, but the porblem is how to make it stop when it touches the wall (picture box)

if im moving left and the left border of the player touched the right border of the wall then it should stop and only the up, down and right keys will be working!!!

thanks in advance guys

Last edited by Flyguy; 01-02-2011 at 11:10 AM. Reason: Please use default font and font size
Reply With Quote
  #2  
Old 01-02-2011, 12:24 PM
Frog's Avatar
Frog Frog is offline
Freshman
 
Join Date: Aug 2003
Location: Australia
Posts: 47
Default Collision

You can create two rectangles (bounding box) which we'll call "wall" and "player"
then use rectangle intersects:

Dim rectWall,rectPlayer As Rectangle

rectWall = New Rectangle(picboxWall.Left, picboxWall.Top,picboxWall.Width, picboxWall.Height);
rectPlayer = New Rectangle(picboxPlayer.Left, picboxPlayer.Top,picboxPlayer.Width, picboxPlayer.Height);

If(rectPlayer.IntersectsWith(rectWall) Then
MessageBox.Show("Wall collision detected")

End IF

As for Form extremities use picboxPlayer.Left and picboxPlayer.Top (topleft corner of picturebox) and calculate distance from edges stopping movement after that point
Reply With Quote
  #3  
Old 01-02-2011, 02:39 PM
head_math head_math is offline
Newcomer
 
Join Date: Jan 2011
Posts: 14
Default

well i tried it but this doesn't stop the player from passing over the wall :S i want when the collision happens to stop the player from moving on the side where it collides and to be able to move in the 3 other directions :S got my point?

if you know any other method to make the player stop when it collides and to be able to move in other direction i would be very thankful

thanks for your help !!!
Reply With Quote
  #4  
Old 01-02-2011, 06:04 PM
Frog's Avatar
Frog Frog is offline
Freshman
 
Join Date: Aug 2003
Location: Australia
Posts: 47
Default

Example 1
Moving player left towards a wall on left
Code:
 Private Sub Form1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown

       
        If e.KeyCode = Keys.Left Then
            rectWall = New Rectangle(PictureBox1.Left, PictureBox1.Top, PictureBox1.Width, PictureBox1.Height)
            rectPlayer = New Rectangle(PictureBox2.Left, PictureBox2.Top, PictureBox2.Width, PictureBox2.Height)
            PictureBox2.Left = PictureBox2.Left - 1
            If rectPlayer.IntersectsWith(rectWall) Then
                MessageBox.Show("Wall collision detected")
                PictureBox2.Left = PictureBox2.Left + 1

            End If
        ElseIf e.KeyCode = Keys.Right Then
            rectWall = New Rectangle(PictureBox1.Left, PictureBox1.Top, PictureBox1.Width, PictureBox1.Height)
            rectPlayer = New Rectangle(PictureBox2.Left, PictureBox2.Top, PictureBox2.Width, PictureBox2.Height)
            If PictureBox2.Left + PictureBox2.Width < Me.Width Then
                PictureBox2.Left = PictureBox2.Left + 1

            End If
        End If
    End Sub
Note you can make rectangles slightly bigger so player does not enter wall

Last edited by Flyguy; 01-03-2011 at 03:10 AM. Reason: Added [code] [/code] tags
Reply With Quote
  #5  
Old 01-03-2011, 10:36 AM
head_math head_math is offline
Newcomer
 
Join Date: Jan 2011
Posts: 14
Default

here what i've tired with the method u gave me

Code:
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
      Dim rectWall As Rectangle
      Dim rectPlayer As Rectangle
      Select Case e.KeyCode
         Case Keys.Up
            If (PictureBox2.Top > 0) Then
               rectWall = New Rectangle(PictureBox1.Left, PictureBox1.Top, PictureBox1.Width, PictureBox1.Height)
               rectPlayer = New Rectangle(PictureBox2.Left, PictureBox2.Top, PictureBox2.Width, PictureBox2.Height)
               If (rectPlayer.IntersectsWith(rectWall)) Then
                  PictureBox2.Top = PictureBox2.Top + 5
                  Exit Sub
               Else
                  PictureBox2.Top = PictureBox2.Top - 5
               End If
            End If
         Case Keys.Down
            If (PictureBox2.Top < Me.Height - 1.7 * PictureBox2.Height) Then
               rectWall = New Rectangle(PictureBox1.Left, PictureBox1.Top, PictureBox1.Width, PictureBox1.Height)
               rectPlayer = New Rectangle(PictureBox2.Left, PictureBox2.Top, PictureBox2.Width, PictureBox2.Height)
               If (rectPlayer.IntersectsWith(rectWall)) Then
                  PictureBox2.Top = PictureBox2.Top - 5
                  Exit Sub
               Else
                  PictureBox2.Top = PictureBox2.Top + 5
               End If
            End If
         Case Keys.Left
            If (PictureBox2.Left > 0) Then
               rectWall = New Rectangle(PictureBox1.Left, PictureBox1.Top, PictureBox1.Width, PictureBox1.Height)
               rectPlayer = New Rectangle(PictureBox2.Left, PictureBox2.Top, PictureBox2.Width, PictureBox2.Height)
               If (rectPlayer.IntersectsWith(rectWall)) Then
                  PictureBox2.Left = PictureBox2.Left + 5
                  Exit Sub
               Else
                  PictureBox2.Left = PictureBox2.Left - 5
               End If
            End If
         Case Keys.Right
            If (PictureBox2.Left < Me.Width - 1.5 * PictureBox2.Width) Then
               rectWall = New Rectangle(PictureBox1.Left, PictureBox1.Top, PictureBox1.Width, PictureBox1.Height)
               rectPlayer = New Rectangle(PictureBox2.Left, PictureBox2.Top, PictureBox2.Width, PictureBox2.Height)
               If (rectPlayer.IntersectsWith(rectWall)) Then
                  PictureBox2.Left = PictureBox2.Left - 5
                  Exit Sub
               Else
                  PictureBox2.Left = PictureBox2.Left + 5
               End If
            End If
      End Select
End Sub

it still not working :S when u touch it from down for example then left key will move it to right and vis-versa and also the up key will movie it down :S
Reply With Quote
  #6  
Old 01-03-2011, 01:10 PM
Frog's Avatar
Frog Frog is offline
Freshman
 
Join Date: Aug 2003
Location: Australia
Posts: 47
Default

Check your code to my code
after rectPlayer = New Rectangle(PictureBox2.Left, PictureBox2.Top, PictureBox2.Width, PictureBox2.Height) you missed original move
First move left 1 and if hit wall move back 1
To move left on a form you use minus from X,right plus on X
To move up is minus, down is plus

Last edited by Frog; 01-03-2011 at 01:13 PM. Reason: more info
Reply With Quote
  #7  
Old 01-03-2011, 03:25 PM
ZaCkOX's Avatar
ZaCkOX ZaCkOX is offline
Contributor
 
Join Date: May 2006
Location: CA
Posts: 645
Default

It might be beneficial to read this thread:

VB2008 - Space Invader Collision Detection

I talk about writting your own collision for an object moving left. Should simplify things and then you can write with rects.
__________________
ZaCkO ... Who is your attitude?
Reply With Quote
  #8  
Old 01-04-2011, 04:47 AM
head_math head_math is offline
Newcomer
 
Join Date: Jan 2011
Posts: 14
Smile

guys i've tried and it's not working :S :S and the Lbound and the Ubound it's telling me :

Error 1 'LBound' is not a member of 'System.Windows.Forms.PictureBox'.
Error 2 'UBound' is not a member of 'System.Windows.Forms.PictureBox'.

here's what i've done in an attachment in a zip file i put the vb project
thanks all for ur help i really appreciate it

Last edited by head_math; 01-06-2011 at 07:14 PM.
Reply With Quote
  #9  
Old 01-04-2011, 04:56 AM
Flyguy's Avatar
Flyguyhow to move an object around a picturebox !! Flyguy is offline
Lost Soul

Super Moderator
* Guru *
 
Join Date: May 2001
Location: Vorlon
Posts: 19,165
Default

ZaCkOX mentioned in his post that the sample is VB6 code, not .Net.
It's about the idea, not to copy and paste without knowing what you are doing.
Reply With Quote
  #10  
Old 01-04-2011, 05:21 AM
Frog's Avatar
Frog Frog is offline
Freshman
 
Join Date: Aug 2003
Location: Australia
Posts: 47
Default

Code:
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
      Dim LocDoor1 As New Point(picDoor1.Location.X, picDoor1.Location.Y - picHero.Height - 1)
      Dim LocDoor2 As New Point(picDoor2.Location.X - picHero.Width - 1, picDoor2.Location.Y)
      Dim LocDoor3 As New Point(picDoor3.Location.X, picDoor3.Location.Y - picHero.Height - 1)
      Dim LocDoor4 As New Point(picDoor4.Location.X, picDoor4.Location.Y - picHero.Height - 1)
      Dim LocDoor5 As New Point(picDoor5.Location.X + picDoor5.Width + 1, picDoor5.Location.Y)
      Dim LocDoor6 As New Point(picDoor6.Location.X, picDoor6.Location.Y + picDoor6.Height + 1)

      Select Case e.KeyCode
         Case Keys.Up
            If (picHero.Top > 0) Then
                    picHero.Top -= 5

            End If
         Case Keys.Down
            If (picHero.Top < Me.Height - 2.8 * picHero.Height) Then
               picHero.Top += 5
            End If
         Case Keys.Left
            If (picHero.Left > 0) Then
                    picHero.Left -= 5
                    detectWall("Left")
            End If
         Case Keys.Right
            If (picHero.Left < Me.Width - 2 * picHero.Width) Then
               picHero.Left += 5
            End If
      End Select

      If (picHero.Bounds.IntersectsWith(picDoor1.Bounds)) Then
         picHero.Location = LocDoor1
      End If
      If (picHero.Bounds.IntersectsWith(picDoor2.Bounds)) Then
         picHero.Location = LocDoor2
      End If
      If (picHero.Bounds.IntersectsWith(picDoor3.Bounds)) Then
         picHero.Location = LocDoor3
      End If
      If (picHero.Bounds.IntersectsWith(picDoor4.Bounds)) Then
         picHero.Location = LocDoor4
      End If
      If (picHero.Bounds.IntersectsWith(picDoor5.Bounds)) Then
         picHero.Location = LocDoor5
      End If
      If (picHero.Bounds.IntersectsWith(picDoor6.Bounds)) Then
         picHero.Location = LocDoor6
      End If

      If (picHero.Bounds.IntersectsWith(picDoorEnd.Bounds)) Then
         MessageBox.Show("Congratulations !! You just finished level 1", "Finishing !")
      End If
    End Sub

    Sub detectWall(ByVal direction As String)
        For Each c As Control In Me.Controls
            If c.Name.Contains("picWall") Then

                If picHero.Bounds.IntersectsWith(c.Bounds) Then
                    Select Case direction
                        Case "Left"
                            picHero.Left += 5
                    End Select
                End If
            End If

        Next
    End Sub
Created a "sub detectwall" which when called checks for collision on your walls
We also pass string "Left" so that the sub can move player back 5
I have only created left so that YOU can create up,down and right

Look at what happens by using breakpoints

Last edited by Flyguy; 01-04-2011 at 06:16 AM. Reason: Added [code] [/code] tags
Reply With Quote
  #11  
Old 01-04-2011, 05:45 AM
head_math head_math is offline
Newcomer
 
Join Date: Jan 2011
Posts: 14
Default

Frog
Thanks a lottttttt finally it did work pretty well !! i really appreciate it!

i would never knew it, as the "Control" type i didn't know about it
thanks again

in case i need another help i guess i'll ask you hehehe thanks for your time

have a nice day
Reply With Quote
  #12  
Old 01-04-2011, 06:16 AM
Frog's Avatar
Frog Frog is offline
Freshman
 
Join Date: Aug 2003
Location: Australia
Posts: 47
Default

Hey you need to thank everyone that posted as they were all helping you

in "sub detectwall"
Case "Left"
picHero.Left += 5
Exit For

add "Exit For" to stop unnecessary looping
Reply With Quote
  #13  
Old 01-04-2011, 06:46 AM
head_math head_math is offline
Newcomer
 
Join Date: Jan 2011
Posts: 14
Default

sure i did thank them when they posted i wrote in 4th post :
"thanks all for ur help i really appreciate it"

here's another thanks for everyone really
Frog, ZaCkOX and Flyguy

and also everyone who viewed this post

i added the "Exit For"

Thanks again !!!
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
how to move an object around a picturebox !!
how to move an object around a picturebox !!
how to move an object around a picturebox !! how to move an object around a picturebox !!
how to move an object around a picturebox !!
how to move an object around a picturebox !!
how to move an object around a picturebox !! how to move an object around a picturebox !! how to move an object around a picturebox !! how to move an object around a picturebox !! how to move an object around a picturebox !! how to move an object around a picturebox !! how to move an object around a picturebox !!
how to move an object around a picturebox !!
how to move an object around a picturebox !!
 
how to move an object around a picturebox !!
how to move an object around a picturebox !!
 
-->