Results 1 to 14 of 14

Thread: [RESOLVED] [VB6] - Building games with API functions

Threaded View

  1. #3
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: [VB6] - Building games with API functions

    (Continued from above post) Also just to let ya know, if the collision isn't a sprite and its a tile in a tile engine, I do something differently but with the same algorithm. Most people would make the fatal mistake of looping through the whole map one tile at a time to check if a collision took place with the sprite. But if your map is gigantic in size, this would be sluggish. The faster way would be to check all 9 sides of the sprite to see if it collided into the tile surrounding it. Why check all 9 sides and not just the sprite itself? Cause if you are approaching the walls at an angle or move into a corner, you end up going through the walls! So for example, if you use rigid body collision, you do this, assuming you got a tile engine going:

    vb Code:
    1. Public Function Collision_Detection() As Boolean
    2.    
    3.     Dim Vertex_List(4) As Vector
    4.     Dim Vertex_List2(4) As Vector
    5.     Dim Boundry(8) As Vector
    6.     Dim Position As Vector
    7.     Dim I As Long
    8.    
    9.     Vertex_List(0) = Vector_New(0, 0)
    10.     Vertex_List(1) = Vector_New(TILE_SIZE, 0)
    11.     Vertex_List(2) = Vector_New(TILE_SIZE, TILE_SIZE)
    12.     Vertex_List(3) = Vector_New(0, TILE_SIZE)
    13.    
    14.     Vertex_List2(0) = Vector_New(0, 0)
    15.     Vertex_List2(1) = Vector_New(TILE_SIZE, 0)
    16.     Vertex_List2(2) = Vector_New(TILE_SIZE, TILE_SIZE)
    17.     Vertex_List2(3) = Vector_New(0, TILE_SIZE)
    18.    
    19.     With Player
    20.    
    21.         Boundry(0).X = .Coordinates.X - 1: Boundry(0).Y = .Coordinates.Y
    22.         Boundry(1).X = .Coordinates.X:     Boundry(1).Y = .Coordinates.Y - 1
    23.         Boundry(2).X = .Coordinates.X + 1: Boundry(2).Y = .Coordinates.Y
    24.         Boundry(3).X = .Coordinates.X:     Boundry(3).Y = .Coordinates.Y + 1
    25.         Boundry(4).X = .Coordinates.X:     Boundry(4).Y = .Coordinates.Y
    26.         Boundry(5).X = .Coordinates.X - 1:     Boundry(5).Y = .Coordinates.Y - 1
    27.         Boundry(6).X = .Coordinates.X + 1:     Boundry(6).Y = .Coordinates.Y - 1
    28.         Boundry(7).X = .Coordinates.X - 1:      Boundry(7).Y = .Coordinates.Y + 1
    29.         Boundry(8).X = .Coordinates.X + 1:     Boundry(8).Y = .Coordinates.Y + 1
    30.    
    31.         For I = 0 To 8
    32.             If Boundry(I).X <= 0 Then Boundry(I).X = 0
    33.             If Boundry(I).Y <= 0 Then Boundry(I).Y = 0
    34.             If Boundry(I).X >= Map.Width - 1 Then Boundry(I).X = Map.Width - 1
    35.             If Boundry(I).Y >= Map.Height - 1 Then Boundry(I).Y = Map.Height - 1
    36.        
    37.             Position.X = Map.Collision_Map.Vertex_List(Boundry(I).X, Boundry(I).Y).X
    38.             Position.Y = Map.Collision_Map.Vertex_List(Boundry(I).X, Boundry(I).Y).Y
    39.             .Collided = Collide(Vertex_List2(), Vertex_List(), 4, 4, Vector_Subtract(.Position, Position), .NColl, .DColl)
    40.            
    41.             If .Collided = True Then
    42.                 If Map.Collision_Map.Response(Boundry(I).X, Boundry(I).Y) = COLLISION_WALL Then
    43.                     Collision_Detection = True
    44.                     .Position = Vector_Subtract(.Position, Vector_Multiply2(.NColl, (.DColl * 1.01)))
    45.                 End If
    46.             End If
    47.         Next I
    48.        
    49.     End With
    50.  
    51. end function

    with sprite coordinates being the tile position of where the sprite is at on the map. To convert a position into tile coordinates you do this:

    vb Code:
    1. Public Sub Convert_Position_To_Coordinates(Sprite As Sprite_Type)
    2.  
    3.     Sprite.Coordinates.X = Int(Sprite.Position.X / TILE_SIZE)
    4.     Sprite.Coordinates.Y = Int(Sprite.Position.Y / TILE_SIZE)
    5.  
    6. End Sub

    For Box to Box however, its similar but a little different for collision response:

    vb Code:
    1. Public Function Collision_Detection2(ByVal Overlap As Long)
    2.  
    3.     Const NO_COLLISION As Long = 0
    4.     Const COL_LEFT As Long = 1
    5.     Const COL_RIGHT As Long = 2
    6.     Const COL_UP As Long = 3
    7.     Const COL_DOWN As Long = 4
    8.  
    9.     Dim Boundry(8) As Vector
    10.     Dim Side As Long
    11.     Dim I As Long
    12.  
    13.     With Player
    14.    
    15.         Boundry(0).X = .Coordinates.X - 1: Boundry(0).Y = .Coordinates.Y
    16.         Boundry(1).X = .Coordinates.X:     Boundry(1).Y = .Coordinates.Y - 1
    17.         Boundry(2).X = .Coordinates.X + 1: Boundry(2).Y = .Coordinates.Y
    18.         Boundry(3).X = .Coordinates.X:     Boundry(3).Y = .Coordinates.Y + 1
    19.         Boundry(4).X = .Coordinates.X:     Boundry(4).Y = .Coordinates.Y
    20.         Boundry(5).X = .Coordinates.X - 1:     Boundry(5).Y = .Coordinates.Y - 1
    21.         Boundry(6).X = .Coordinates.X + 1:     Boundry(6).Y = .Coordinates.Y - 1
    22.         Boundry(7).X = .Coordinates.X - 1:      Boundry(7).Y = .Coordinates.Y + 1
    23.         Boundry(8).X = .Coordinates.X + 1:     Boundry(8).Y = .Coordinates.Y + 1
    24.        
    25.         For I = 0 To 8
    26.        
    27.             If Boundry(I).X <= 0 Then Boundry(I).X = 0
    28.             If Boundry(I).Y <= 0 Then Boundry(I).Y = 0
    29.             If Boundry(I).X >= Map.Width - 1 Then Boundry(I).X = Map.Width - 1
    30.             If Boundry(I).Y >= Map.Height - 1 Then Boundry(I).Y = Map.Height - 1
    31.  
    32.             Side = Collision_Box_To_Box2(Player.Position.X, Player.Position.Y, TILE_SIZE, TILE_SIZE, Boundry(I).X * TILE_SIZE, Boundry(I).Y * TILE_SIZE, TILE_SIZE, TILE_SIZE)
    33.            
    34.             If Side <> 0 Then
    35.                 If Map.Collision_Map.Response(Boundry(I).X, Boundry(I).Y) = COLLISION_WALL Then
    36.                     Collision_Detection2 = True
    37.                     .Collided = True
    38.                     Select Case Side
    39.                         Case COL_LEFT: .Position.X = .Position.X + Overlap
    40.                         Case COL_RIGHT:: .Position.X = .Position.X - Overlap
    41.                         Case COL_UP:: .Position.Y = .Position.Y + Overlap
    42.                         Case COL_DOWN:: .Position.Y = .Position.Y - Overlap
    43.                     End Select
    44.                 End If
    45.             End If
    46.            
    47.         Next I
    48.        
    49.     End With
    50.    
    51. End Function

    Heres a project I uploaded for ya. Although it's an AStar sample I'm working on, it has the tile collision code I was talking about. Have fun
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width