Qbasicnews.com

Full Version: Code art
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

Anonymous

this is how i do 8*8 collision on 16*16 tiles.

Code:
Function Tru_walk ( o As _char_type, dirtemp As Integer, layout As Integer Ptr Ptr, x2 As uInteger, y2 As uInteger, psfing As Byte = 0 )


  Dim As Integer layercheck, x_offset, y_offset, x_tile, y_tile, mx, my, x, y
  Dim As Integer x_assist, y_assist
  Dim As Integer crawl_factor


  unpack_1x3 ( x2, mx, x )  
  unpack_1x3 ( y2, my, y )  
                          

  Dim As Integer mx2 = ( mx \ 2 ), my2 = ( my \ 2 )
                          

  x_tile_2 = o.x \ mx2
  y_tile_2 = o.y \ my2

  x_offset_2 = o.x Mod mx2
  y_offset_2 = o.y Mod my2
  
  walk_grids_x = o.real_x \ mx2  
  walk_grids_y = o.real_y \ my2  

  If x_offset_2 <> 0 Then walk_grids_x += 1 Else x_jmp = 1
  If y_offset_2 <> 0 Then walk_grids_y += 1 Else y_jmp = 1


  Dim layerscan( 2 ) As Byte => { 1, 1, 1 }

  For layercheck = 0 To 2


    Select Case dirtemp Mod 2
    
      Case 0
      
        crawl_factor = walk_grids_x
  
      Case 1
  
        crawl_factor = walk_grids_y
        
    End Select
    
  
    For crawl = 0 To crawl_factor - 1


      Select Case dirtemp
      
          
            Case 0
    
              y_assist = ( y_tile_2 - y_jmp )
              x_assist = ( x_tile_2 + crawl )
    
            Case 1
    
              x_assist = ( walk_grids_x - 1 ) + x_tile_2 + x_jmp
              y_assist = ( y_tile_2 + crawl )

            Case 2
            
              x_assist = ( x_tile_2 + crawl )
              y_assist = ( walk_grids_y - 1 ) + y_tile_2 + y_jmp

            Case 3
    
              x_assist = ( x_tile_2 - x_jmp )
              y_assist = ( y_tile_2 + crawl )
            

          
      End Select

      '' what tile
      crawltile = ( _
                    ( _
                      y_assist * my2 _  ''
                    ) \ my _
                  ) * x _
              +         _ '' add
                  (     _
                    ( _
                      x_assist * mx2 _
                    ) \ mx _
                  )
      
      '' what quad inside the tilespace u hit.           1, 2
        walk_index = ( _ ''                              3, 4
                       ( _
                         y_assist Mod 2 _
                       ) * 2  _
                     ) _
                +      _ '' add
                     ( _
                       x_assist Mod 2 _
                     ) + 1
      

      '' store this result
      layerscan( layercheck ) And = Not( _
                                         Bit( layout[layercheck][crawltile], 16 - walk_index ) _
                                       )
  
    Next
  
  Next


  Dim As Integer tile_free = layerscan( 0 ) And layerscan( 1 ) And layerscan( 2 )

  Function = tile_free
  

End Function

Anonymous

refined. (i dont care about double posting)

Code:
Function tru_walk ( o As _char_type, d As Integer, layout As Integer Ptr Ptr, x2 As uInteger, y2 As uInteger )


  Dim layerscan( 2 ) As Byte => { 1, 1, 1 }


  Dim As Integer tile_x, tile_y, room_x, room_y

    unpack_1x3 ( x2, tile_x, room_x )  
    unpack_1x3 ( y2, tile_y, room_y )  

    '' x2 / y2 are packed values. first byte is the (x / y) tile size,
    '' last 3 bytes hold the (x / y) size of the map area in tiles.

    ''  4     1      3 .................... # of bytes
    '' x2  tile_x  room_x                        names

    ''  4     1      3 .................... # of bytes
    '' y2  tile_y  room_y                        names      


    '' o.x = object x location
    '' o.y = object y location

    '' o.real_x = object x size
    '' o.real_y = object y size


    '' layout is basically a 2d array. (layers, tiles per layer)
    '' arranged from the top left (0,0), increasing by column, until
    '' the last column is reached, then row is incremented, column reset.
    
    '' the layout is stored one integer per tile. the first byte of the integer
    '' is the value of the tile, (0-255) 256 in a tileset.
    '' the walkability of each tile is stored in bits 12-15.
    '' tiles are divided into quadrants, like so:
    
    ''  1 2
    ''  3 4
    
    '' (16 - n) = quadrant(n)'s bit.
    
    


  Dim As Integer x_offset_2, y_offset_2, x_tile_2, y_tile_2, quads_x, quads_y, x_aligned, y_aligned
  Dim As Integer tile_x_2 = ( tile_x \ 2 ), tile_y_2 = ( tile_y \ 2 )
                          
    x_tile_2 = o.x \ tile_x_2
    y_tile_2 = o.y \ tile_y_2
  
    x_offset_2 = o.x Mod tile_x_2
    y_offset_2 = o.y Mod tile_y_2
    
    quads_x = o.real_x \ tile_x_2  
    quads_y = o.real_y \ tile_y_2  
  
    If x_offset_2 <> 0 Then quads_x += 1 Else x_aligned = 1
    If y_offset_2 <> 0 Then quads_y += 1 Else y_aligned = 1

  
  
  Dim As Integer layer
  Dim As Integer crawl_axis
  Dim As Integer x_opt, y_opt

    For layer = 0 To 2
  
  
      Select Case d Mod 2
      
        Case 0
        
          crawl_axis = quads_x
    
        Case 1
    
          crawl_axis = quads_y
          
      End Select
      
    
      For crawl = 0 To crawl_axis - 1
  
  
        Select Case d
        
            
              Case 0
      
                y_opt = ( y_tile_2 - y_aligned )
                x_opt = ( x_tile_2 + crawl )
      
              Case 1
      
                x_opt = ( quads_x - 1 ) + x_tile_2 + x_aligned
                y_opt = ( y_tile_2 + crawl )
  
              Case 2
              
                x_opt = ( x_tile_2 + crawl )
                y_opt = ( quads_y - 1 ) + y_tile_2 + y_aligned
  
              Case 3
      
                x_opt = ( x_tile_2 - x_aligned )
                y_opt = ( y_tile_2 + crawl )
              
  
            
        End Select
  
        layerscan( layer ) And = Not( _
                                           Bit( layout[layer][ _       '' what tile
                                                               ( ( y_opt * tile_y_2 ) \ tile_y ) * room_x + ( ( x_opt * tile_x_2 ) \ tile_x ) _
                                                             ], 16 - ( _   '' what quad inside the tilespace u hit.    
                                                                       ( ( y_opt Mod 2 ) * 2 ) + ( x_opt Mod 2 ) + 1 _
                                                                     ) _
                                              ) _
                                     )
    
      Next
    
    Next
  

  Dim As Integer tile_free = layerscan( 0 ) And layerscan( 1 ) And layerscan( 2 )

    Function = tile_free
  

End Function