7 Replies - 2396 Views - Last Post: 10 December 2010 - 03:07 PM Rate Topic: -----

#1 redrin  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 46
  • Joined: 01-May 10

Need help with collisions/movement in VB.NET

Posted 09 December 2010 - 12:38 PM

basically, i want any object that is not a passable object to check for collision. it's just a matter of how, since i can't figure out what i'm doing wrong with just calling the sub mymap_keydown.

for example, i wanted to put all of this in one sub:
 Private Sub mymap_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        Dim Loc As Point


        Select Case e.KeyCode
            Case Keys.Up
                If Not myplayer.Location.Y - 5 < 0 And Not myplayer.Bounds.IntersectsWith(hedge1.Bounds) Then
                    Loc = New Point(myplayer.Location.X, myplayer.Location.Y - 5)
                    myplayer.Location = Loc
                End If
            Case Keys.Down
                If Not myplayer.Location.Y + 5 < 0 And Not myplayer.Bounds.IntersectsWith(hedge1.Bounds) Then
                    Loc = New Point(myplayer.Location.X, myplayer.Location.Y + 5)
                    myplayer.Location = Loc
                End If
            Case Keys.Left
                If Not myplayer.Location.X - 5 < 0 And Not myplayer.Bounds.IntersectsWith(hedge1.Bounds) Then
                    Loc = New Point(myplayer.Location.X - 5, myplayer.Location.Y)
                    myplayer.Location = Loc
                End If
            Case Keys.Right
                If Not myplayer.Location.X + 5 < 0 And Not myplayer.Bounds.IntersectsWith(hedge1.Bounds) Then
                    Loc = New Point(myplayer.Location.X + 5, myplayer.Location.Y)
                    myplayer.Location = Loc
                End If
        End Select

    End Sub

    Private Sub passable()
        Dim canpass As Boolean
        Dim pass(3) As String
        pass(0) = "bush"
        pass(1) = "rock"
        pass(2) = "wall"

        If pass.Contains(tile) Then
            canpass = False

        Else
            canpass = True

        End If
        If canpass = True And Me.hedge1.Bounds.IntersectsWith(myplayer.Bounds) Then
            'allow player movement
            Call mymap_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        ElseIf canpass = False And Me.hedge1.Bounds.IntersectsWith(myplayer.Bounds) Then
            'player is in non-passable tile and must return to position prior
        Else
            'player is not interacting with object
        End If
    End Sub



but this part:

 Call mymap_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown



doesn't work. i've tried some variations of the call

Call mymap_KeyDown(sender, e)

Call mymap_KeyDown()


and none are working. it says in the above, "expression expected" for ByVal and i don't know what the issue is. i did try to remove byval, tried byref too, and no go.


the full code (current), by the way:
Public Class mymap
    Dim tile As String
    Dim sender As System.Object
    Dim e As System.Windows.Forms.KeyEventArgs
    Dim Loc As Point

    Private Sub mymap_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown


        Select Case e.KeyCode
            Case Keys.Up
                If Not myplayer.Location.Y - 5 < 0 And Not myplayer.Bounds.IntersectsWith(hedge1.Bounds) Then
                    Loc = New Point(myplayer.Location.X, myplayer.Location.Y - 5)
                    myplayer.Location = Loc
                End If
            Case Keys.Down
                If Not myplayer.Location.Y + 5 < 0 And Not myplayer.Bounds.IntersectsWith(hedge1.Bounds) Then
                    Loc = New Point(myplayer.Location.X, myplayer.Location.Y + 5)
                    myplayer.Location = Loc
                End If
            Case Keys.Left
                If Not myplayer.Location.X - 5 < 0 And Not myplayer.Bounds.IntersectsWith(hedge1.Bounds) Then
                    Loc = New Point(myplayer.Location.X - 5, myplayer.Location.Y)
                    myplayer.Location = Loc
                End If
            Case Keys.Right
                If Not myplayer.Location.X + 5 < 0 And Not myplayer.Bounds.IntersectsWith(hedge1.Bounds) Then
                    Loc = New Point(myplayer.Location.X + 5, myplayer.Location.Y)
                    myplayer.Location = Loc
                End If
        End Select

    End Sub

    Private Sub passable()
        Dim canpass As Boolean
        Dim pass(3) As String
        pass(0) = "bush"
        pass(1) = "rock"
        pass(2) = "wall"

        If pass.Contains(tile) Then
            canpass = False

        Else
            canpass = True

        End If
        If canpass = True And Me.hedge1.Bounds.IntersectsWith(myplayer.Bounds) Then
            'allow player movement
            'call on movement script
                   
        ElseIf canpass = False And Me.hedge1.Bounds.IntersectsWith(myplayer.Bounds) Then
        'player is in non-passable tile and must return to position prior
        'script to do so
        Else
            'player is not interacting with object
            'call on movement sub
        End If
    End Sub

    Private Sub hedge1_Click(ByVal sender As System.Object, ByVal s As System.EventArgs) Handles hedge1.Click
        tile = "bush"
        Call passable()

    End Sub

End Class

This post has been edited by redrin: 09 December 2010 - 12:42 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Need help with collisions/movement in VB.NET

#2 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 1
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Need help with collisions/movement in VB.NET

Posted 10 December 2010 - 12:59 PM

I don't code in VB, so please forgive me if I've misread your code.

Unfortunately when checking for collisions as a sequence of if-else statements it will invariably go wrong. What you need to do is determine if any point in the geometry itself of the two objects themselves are intersecting. Visualize two cubes; if you rotate them in space in any random direction then it becomes near impossible using your method to determine of they collide.

The easiest, albeit messy, way to check for a collision would be to have your irregularly shaped geometry enclosed in a bounding sphere and check for collisions against the two spheres, such that the distance between the two origins should be less than or equal to the sum of the radii for a collision to have taken place. :)
Was This Post Helpful? 0
  • +
  • -

#3 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2268
  • View blog
  • Posts: 9,482
  • Joined: 29-May 08

Re: Need help with collisions/movement in VB.NET

Posted 10 December 2010 - 01:30 PM

I recommend that you separate the collision stuff into its class.
This is called a game engine, it task is really simple.

Given the current state of the game/world and the current inputs what is the next state of the world.

Though the task is simple the implementation maybe complex.

Would the next player move result in an object collision? Then Raise object collision event.
Was This Post Helpful? 0
  • +
  • -

#4 redrin  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 46
  • Joined: 01-May 10

Re: Need help with collisions/movement in VB.NET

Posted 10 December 2010 - 01:43 PM

View PostButchDean, on 10 December 2010 - 11:59 AM, said:

I don't code in VB, so please forgive me if I've misread your code.

Unfortunately when checking for collisions as a sequence of if-else statements it will invariably go wrong. What you need to do is determine if any point in the geometry itself of the two objects themselves are intersecting. Visualize two cubes; if you rotate them in space in any random direction then it becomes near impossible using your method to determine of they collide.

The easiest, albeit messy, way to check for a collision would be to have your irregularly shaped geometry enclosed in a bounding sphere and check for collisions against the two spheres, such that the distance between the two origins should be less than or equal to the sum of the radii for a collision to have taken place. :)

the game is 2D, so i don't think i could lay a bounding box over it (i know what you mean, having played with Blender 3D) but the idea of checking all points is a good one that i hadn't thought of. i could have it check the four major points-- top right, top left, bottom right, and bottom left. i just have to figure out how to code it. >_<

thank you.

View PostAdamSpeight2008, on 10 December 2010 - 12:30 PM, said:

I recommend that you separate the collision stuff into its class.
This is called a game engine, it task is really simple.

Given the current state of the game/world and the current inputs what is the next state of the world.

Though the task is simple the implementation maybe complex.

Would the next player move result in an object collision? Then Raise object collision event.

thank you for responding (i was honestly starting to wonder if anyone would respond to this thread, so this is great).

i was thinking of having the collision stuff in a separate form that is called on, but i am having serious problems with calling so much as the keydown sub.

maybe i'm organizing poorly? i used to always squash all my code into one long script, so i'm trying very hard to keep things tidy this time around. could you possibly give me an example of what you mean, in reference to my code posted above? not that i'm trying to get you to do it (i swear i'm not one of those sorts) but because i am really stuck on this and i don't want to go further with the code until i have a good feel of the organization, and what i already have working.

could you explain what you mean by the state of the game world? i'm still a little new to some terms. the world itself is a 2d top-down. i plan the map to be made from a tile grid (i am still working on the map engine, but it is slowly coming together) and each tile is given a variable name.

currently what i was trying to do, is have the collision and movement as one set engine. for every tile the character moves to, the engine will check if the tile name is in the collision array, and if it is, won't allow the character to move onto it.

there may be a much better way of doing this that i haven't figured out, though.

This post has been edited by redrin: 10 December 2010 - 01:47 PM

Was This Post Helpful? 0
  • +
  • -

#5 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 1009
  • View blog
  • Posts: 4,199
  • Joined: 14-February 08

Re: Need help with collisions/movement in VB.NET

Posted 10 December 2010 - 01:54 PM

View Postredrin, on 10 December 2010 - 07:43 PM, said:

the game is 2D, so i don't think i could lay a bounding box over it


You definitely could, this is one of the easiest ways to check for collisions, even easier is to use a bounding circle. Your bounding box will work in 2D if it has points in the x and y but not the z :)

A collision has occured between circles if the distance between their centres is less than the sum of their radii.
Was This Post Helpful? 0
  • +
  • -

#6 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 1
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Need help with collisions/movement in VB.NET

Posted 10 December 2010 - 03:01 PM

Similar to my bounding sphere. :)
Was This Post Helpful? 0
  • +
  • -

#7 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 1009
  • View blog
  • Posts: 4,199
  • Joined: 14-February 08

Re: Need help with collisions/movement in VB.NET

Posted 10 December 2010 - 03:05 PM

Nearly identical actually, in another dimension. >.<
Was This Post Helpful? 0
  • +
  • -

#8 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 1
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Need help with collisions/movement in VB.NET

Posted 10 December 2010 - 03:07 PM

Always wondered what the difference between a circle and sphere was! Thanks for the lesson! :tt2:

This post has been edited by ButchDean: 10 December 2010 - 03:08 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1