tile collisions

Game development specific discussions.
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

tile collisions

Post by BasicCoder2 »

This is some demo code I wrote about 2 years ago when trying to figure out tile collisions.
The collision rectangle is in green the tiles it overlaps are in red.
You keep the bird aloft with the up arrow key and control its direction with the left/right arrow keys.
First you need some tiles so run this,

Code: Select all

screenres 640,580,32
color rgb(0,0,0),rgb(255,255,255)
cls
dim as any ptr tileBlock
tileBlock = imagecreate(32*16,32*16)

for j as integer = 0 to 15
    for i as integer = 0 to 15
        
        line (0,0)-(7,7),rgb(255,255,255),bf  'clear
        draw string (0,0),chr(i+j*16)
        
        'enlarge
        for y as integer = 0 to 7
            for x as integer = 0 to 7
                line tileBlock,(x*4+i*32,y*4+j*32)-(x*4+3+i*32,y*4+3+j*32),point(x,y),bf
            next x
        next y
        
    next i
next j
put (0,0),tileBlock,pset

bsave "ascTiles32.bmp",tileBlock
sleep
And then you need some sprites,

Code: Select all

const WINW = 640   'displayed window size view of bitmap
const WINH = 480

screenres WINW,WINH,32      'screen set to window size

'=========  LOAD BITMAP BLOCK OF SPRITES ====

dim shared as any ptr spriteBlock
spriteBlock = imagecreate(128,128)

dim as string datum,word

'flying sprite
for k as integer = 0 to 3
    for j as integer = 0 to 31
        read datum
        for i as integer = 0 to 47
            word = mid(datum,i+1,1)
            if word = "#" then
                pset spriteBlock,(i+k*32,j),rgb(0,0,0)
                pset spriteBlock,(32-i+k*32,j+32),rgb(0,0,0)
            end if
            if word = " " then
                pset spriteBlock,(i+k*32,j),rgb(255,0,255)
                pset spriteBlock,(32-i+k*32,j+32),rgb(255,0,255)
            end if
            if word = "@" then
                pset spriteBlock,(i+k*32,j),rgb(255,127,39)
                pset spriteBlock,(32-i+k*32,j+32),rgb(255,127,39)
            end if
            if word = "." then
                pset spriteBlock,(i+k*32,j),rgb(255,242,0)
                pset spriteBlock,(32-i+k*32,j+32),rgb(255,242,0)
            end if
            if word = "*" then
                pset spriteBlock,(i+k*32,j),rgb(0,162,232)
                pset spriteBlock,(32-i+k*32,j+32),rgb(0,162,232)
            end if
            if word = "$" then
                pset spriteBlock,(i+k*32,j),rgb(153,217,234)
                pset spriteBlock,(32-i+k*32,j+32),rgb(153,217,234)
            end if
        next i
    next j
next k
'walking sprite
for k as integer = 0 to 3
    for j as integer = 0 to 31
        read datum
        for i as integer = 0 to 47
            word = mid(datum,i+1,1)
            if word = "#" then
                pset spriteBlock,(i+k*32,j+64),rgb(0,0,0)
                pset spriteBlock,(32-i+k*32,j+96),rgb(0,0,0)
            end if
            if word = " " then
                pset spriteBlock,(i+k*32,j+64),rgb(255,0,255)
                pset spriteBlock,(32-i+k*32,j+96),rgb(255,0,255)
            end if
            if word = "@" then
                pset spriteBlock,(i+k*32,j+64),rgb(255,127,39)
                pset spriteBlock,(32-i+k*32,j+96),rgb(255,127,39)
            end if
            if word = "." then
                pset spriteBlock,(i+k*32,j+64),rgb(255,242,0)
                pset spriteBlock,(32-i+k*32,j+96),rgb(255,242,0)
            end if
            if word = "*" then
                pset spriteBlock,(i+k*32,j+64),rgb(0,162,232)
                pset spriteBlock,(32-i+k*32,j+96),rgb(0,162,232)
            end if
            if word = "$" then
                pset spriteBlock,(i+k*32,j+64),rgb(153,217,234)
                pset spriteBlock,(32-i+k*32,j+96),rgb(153,217,234)
            end if
        next i
    next j
next k

put (0,0),spriteBlock,pset
bsave "BIRD32x32.bmp",spriteBlock

sleep

DATA "                                "
DATA "                                "
DATA "  #                 #           "
DATA " #*#####           #$###        "
DATA "  #*****#####       #$$$#       "
DATA " ##**********#      #$$$$#      "
DATA "  #***********####  #$$##$#     "
DATA "   #####*********####$#..#$###  "
DATA "  ###***#*********#$$$#.##$#@@# "
DATA " #*****************#$$#.##$#@@@#"
DATA "  ##****************#$#..#$#####"
DATA "    ##***####*******#$$##$$#@@# "
DATA "     #####**********#$$$$$#@@#  "
DATA "       ###*********#$$$$$$###   "
DATA "       #$##*******#$$$$$$#      "
DATA "       #$$$#######$$$$$##       "
DATA "       #$$$$$$$$$$$$$##         "
DATA "       #$$$$$$$$$$$$$#          "
DATA "       #$$$$$$$$$$$$$#          "
DATA "        #$$$$$$$$$$$#           "
DATA "        #$$$$$$$$$$$#           "
DATA "         #$$$$$$$$$#            "
DATA "      #############             "
DATA "      ###  ###                  "
DATA "       #######                  "
DATA "       ######                   "
DATA "      ####                      "
DATA "     ###                        "
DATA "                                "
DATA "                                "
DATA "                                "
DATA "                                "

DATA "                                "
DATA "       ####                     "
DATA "  #   #****##       #           "
DATA " #*#  #******#     #$###        "
DATA "  #*#  #******#     #$$$#       "
DATA "  #**# ##******#    #$$$$#      "
DATA "   #**#$###*****#   #$$##$#     "
DATA "   #**###*##****#####$#..#$###  "
DATA "  ##***#*********#$$$$#.##$#@@# "
DATA " #*****#**********#$$$#.##$#@@@#"
DATA "  #######*********#$$$#..#$#####"
DATA "       ###********#$$$$##$$#@@# "
DATA "       #$#####****#$$$$$$$#@@#  "
DATA "       #$$##******#$$$$$$$###   "
DATA "       #$$#*******#$$$$$$#      "
DATA "       #$$##*****##$$$$##       "
DATA "       #$$$$####$$$$$##         "
DATA "       #$$$$$$$$$$$$$#          "
DATA "       #$$$$$$$$$$$$$#          "
DATA "        #$$$$$$$$$$$#           "
DATA "        #$$$$$$$$$$$#           "
DATA "         #$$$$$$$$$#            "
DATA "       ############             "
DATA "       ###  ###                 "
DATA "        #######                 "
DATA "        ######                  "
DATA "       ####                     "
DATA "      ###                       "
DATA "                                "
DATA "                                "
DATA "                                "
DATA "                                "

DATA "                                "
DATA "                                "
DATA "  #                 #           "
DATA " #*#####           #$###        "
DATA "  #*****#####       #$$$#       "
DATA " ##**********#      #$$$$#      "
DATA "  #***********####  #$$##$#     "
DATA "   #####*********####$#..#$###  "
DATA "  ###***#*********#$$$#.##$#@@# "
DATA " #*****************#$$#.##$#@@@#"
DATA "  ##****************#$#..#$#####"
DATA "    ##***####*******#$$##$$#@@# "
DATA "     #####**********#$$$$$#@@#  "
DATA "       ###*********#$$$$$$###   "
DATA "       #$##*******#$$$$$$#      "
DATA "       #$$$#######$$$$$##       "
DATA "       #$$$$$$$$$$$$$##         "
DATA "       #$$$$$$$$$$$$$#          "
DATA "       #$$$$$$$$$$$$$#          "
DATA "        #$$$$$$$$$$$#           "
DATA "        #$$$$$$$$$$$#           "
DATA "         #$$$$$$$$$#            "
DATA "      #############             "
DATA "      ###  ###                  "
DATA "       #######                  "
DATA "       ######                   "
DATA "      ####                      "
DATA "     ###                        "
DATA "                                "
DATA "                                "
DATA "                                "
DATA "                                "

DATA "                                "
DATA "                                "
DATA "  #                 #           "
DATA " #*#               #$###        "
DATA "  #*#               #$$$#       "
DATA "  #**#              #$$$$#      "
DATA "   #**#    #######  #$$##$#     "
DATA "   #**## ##$$$$$$$###$#..#$###  "
DATA "  ##****#$$$$#####$$$$#.##$#@@# "
DATA " #******#$$$#*****#$$$#.##$#@@@#"
DATA "  ######$$$$#******#$$#..#$#####"
DATA "       #$$$#*******#$$$##$$#@@# "
DATA "       #$$$#*******#$$$$$$#@@#  "
DATA "       #$$#********#$$$$$$###   "
DATA "       #$#*********#$$$$$#      "
DATA "       #$#*********#$$$##       "
DATA "       ##******#***#$##         "
DATA "       ##******#***#$#          "
DATA "       ##******####$$#          "
DATA "        #***#***##$$#           "
DATA "       #***##***#$$$#           "
DATA "       #***#****#$$#            "
DATA "       #***#****###             "
DATA "       #***##**#                "
DATA "       #***##*#                 "
DATA "       #**## ##                 "
DATA "        #*#                     "
DATA "         #                      "
DATA "                                "
DATA "                                "
DATA "                                "
DATA "                                "


DATA "                                "
DATA "                                "
DATA "  #                 #           "
DATA " #*#               #$###        "
DATA "  #*#               #$$$#       "
DATA "  #**#              #$$$$#      "
DATA "   #**#    #######  #$$##$#     "
DATA "   #**## ##$$$$$$$###$#..#$###  "
DATA "  ##****#$$$######$$$$#.##$#@@# "
DATA " #******#$$#******##$$#.##$#@@@#"
DATA "  ######$$#*********#$#..#$#####"
DATA "       #$#**********#$$##$$#@@# "
DATA "       #$#**********#$$$$$#@@#  "
DATA "       #$#******#***#$$$$$###   "
DATA "       #$#******#**#$$$$$#      "
DATA "       #$#******#**#$$$##       "
DATA "       #$#******#*#$$##         "
DATA "       #$#***#**##$$$#          "
DATA "       ###***#**##$$$#          "
DATA "        ##***###$$$$#           "
DATA "        #$###$$$$$$$#           "
DATA "         #$$$$$$$$$#            "
DATA "          #########             "
DATA "            ####                "
DATA "           ### ##               "
DATA "    ####  ##    ### ####        "
DATA "    #######      ######         "
DATA "      ####        ###           "
DATA "      ###        ###            "
DATA "       ######  #####            "
DATA "        ####                    "
DATA "                                "

DATA "                                "
DATA "                                "
DATA "  #                 #           "
DATA " #*#               #$###        "
DATA "  #*#               #$$$#       "
DATA "  #**#              #$$$$#      "
DATA "   #**#    #######  #$$##$#     "
DATA "   #**## ##$$$$$$$###$#..#$###  "
DATA "  ##****#$$$######$$$$#.##$#@@# "
DATA " #******#$$#******##$$#.##$#@@@#"
DATA "  ######$$#*********#$#..#$#####"
DATA "       #$#**********#$$##$$#@@# "
DATA "       #$#**********#$$$$$#@@#  "
DATA "       #$#******#***#$$$$$###   "
DATA "       #$#******#**#$$$$$#      "
DATA "       #$#******#**#$$$##       "
DATA "       #$#******#*#$$##         "
DATA "       #$#***#**##$$$#          "
DATA "       ###***#**##$$$#          "
DATA "        ##***###$$$$#           "
DATA "        #$###$$$$$$$#           "
DATA "         #$$$$$$$$$#            "
DATA "          #########             "
DATA "              ###               "
DATA "             ##  ###            "
DATA "             ## ###             "
DATA "           #######              "
DATA "          ######                "
DATA "             ###                "
DATA "          #########             "
DATA "         ###########            "
DATA "                                "

DATA "                                "
DATA "                                "
DATA "  #                 #           "
DATA " #*#               #$###        "
DATA "  #*#               #$$$#       "
DATA "  #**#              #$$$$#      "
DATA "   #**#    #######  #$$##$#     "
DATA "   #**## ##$$$$$$$###$#..#$###  "
DATA "  ##****#$$$######$$$$#.##$#@@# "
DATA " #******#$$#******##$$#.##$#@@@#"
DATA "  ######$$#*********#$#..#$#####"
DATA "       #$#**********#$$##$$#@@# "
DATA "       #$#**********#$$$$$#@@#  "
DATA "       #$#******#***#$$$$$###   "
DATA "       #$#******#**#$$$$$#      "
DATA "       #$#******#**#$$$##       "
DATA "       #$#******#*#$$##         "
DATA "       #$#***#**##$$$#          "
DATA "       ###***#**##$$$#          "
DATA "        ##***###$$$$#           "
DATA "        #$###$$$$$$$#           "
DATA "         #$$$$$$$$$#            "
DATA "          #########             "
DATA "            ####                "
DATA "           ### ##               "
DATA "    ####  ##    ### ####        "
DATA "    #######      ######         "
DATA "      ####        ###           "
DATA "      ###        ###            "
DATA "       ######  #####            "
DATA "        ####                    "
DATA "                                "

DATA "                                "
DATA "                                "
DATA "  #                 #           "
DATA " #*#               #$###        "
DATA "  #*#               #$$$#       "
DATA "  #**#              #$$$$#      "
DATA "   #**#    #######  #$$##$#     "
DATA "   #**## ##$$$$$$$###$#..#$###  "
DATA "  ##****#$$$######$$$$#.##$#@@# "
DATA " #******#$$#******##$$#.##$#@@@#"
DATA "  ######$$#*********#$#..#$#####"
DATA "       #$#**********#$$##$$#@@# "
DATA "       #$#**********#$$$$$#@@#  "
DATA "       #$#******#***#$$$$$###   "
DATA "       #$#******#**#$$$$$#      "
DATA "       #$#******#**#$$$##       "
DATA "       #$#******#*#$$##         "
DATA "       #$#***#**##$$$#          "
DATA "       ###***#**##$$$#          "
DATA "        ##***###$$$$#           "
DATA "        #$###$$$$$$$#           "
DATA "         #$$$$$$$$$#            "
DATA "          #########             "
DATA "              ###               "
DATA "             ##  ###            "
DATA "             ## ###             "
DATA "           #######              "
DATA "          ######                "
DATA "             ###                "
DATA "          #########             "
DATA "         ###########            "
DATA "                                "
And the demo code.

Code: Select all

screenres 940,480,32
color rgb(0,0,0),rgb(255,255,255)
cls
dim shared as integer mx,my,mb
dim shared as integer tileID
dim shared as integer onFloor

const TILEW = 32   'measured in pixels
const TILEH = 32

const FLOOR = &HB2

dim shared as any ptr tileBlock32
tileBlock32 = imagecreate(TILEW*16,TILEH*16)
bload "ascTiles32.bmp",tileBlock32

const MAPW = 80   'measured in tiles
const MAPH = 12   '


dim shared as integer WINW,WINH,WINX,WINY  'display window of bitmap
WINX = 0      'measured in PIXELS not tiles
WINY = 0
WINW = 640   
WINH = 380

dim shared as integer TMAP(MAPW,MAPH)   'map array of tiles

dim shared as integer tileX,tileY       'tile position of hero in tile array

dim shared as integer BMAPW,BMAPH  'in pixels
BMAPW = MAPW*TILEW
BMAPH = MAPH*TILEH

dim shared as any ptr background
background = imagecreate(BMAPW,BMAPH)

'=========  LOAD BITMAP BLOCK OF SPRITES ====
dim shared as any ptr spriteBlock
spriteBlock = imagecreate(128,128)   '*** WIDER BLOCK
bload "bird32x32.bmp",spriteBlock
'box the sprite
for j as integer = 0 to 3
    for i as integer = 0 to 3
        line spriteBlock,(i*32,j*32)-(i*32+31,j*32+31),rgb(0,255,0),b
        line spriteBlock,(i*32+1,j*32+1)-(i*32+30,j*32+30),rgb(0,255,0),b
    next i
next j


' === READ TILE ID DATA INTO TMAP ==
dim as string datum,word
dim as integer ii
for j as integer = 0 to MAPH-1
    ii = 1
    read datum
    for i as integer = 0 to MAPW-1
        word = "&H" + mid(datum,ii,2)
        ii = ii + 2
        TMAP(i,j)= val(word)
    next i
next j

' ================== create a sprite ========
type SPRITE
    as integer x
    as integer y
    as integer w
    as integer h
    as integer dx    'left = -dx, right = +dx not moving = 0
    as integer dy    'up/down
    as integer d     'last direction
    as integer f     'frame number for animated sprites images
    as integer c     'timer counter for animation
end type

'create sprite
dim shared as SPRITE hero

' ----------- initialize sprite data --------------
hero.w = 32
hero.h = 32
hero.x = 63 
hero.y = 63
hero.dx = 0
hero.dy = 0

    
sub tileMapToBitMap()
    dim as integer tileID,x,y
    for j as integer = 0 to MAPH-1
        for i as integer = 0 to MAPW-1
            tileID = TMAP(i,j)
            y = int(tileID\16)   'location in block of tileID image
            x = tileID - (y*16)
            put background,(i*TILEW,j*TILEH),tileBlock32,(x*TILEW,y*TILEH)-(x*TILEW+TILEW-1,y*TILEH+TILEH-1),pset
        next i
    next j
    for j as integer = 0 to MAPH-1
        for i as integer = 0 to MAPW-1
            line background,(i*TILEW,j*TILEH)-(i*TILEW+TILEW-1,j*TILEH+TILEH-1),rgb(27,127,127),b
        next i
    next j
end sub

function overLap(x1 as integer,y1 as integer,x2 as integer,y2 as integer) as integer
    dim as integer hit
    hit = 0
    'top left corner
    if x1>=x2 and x1<(x2+31) then
        if y1>=y2 and y1<(y2+31) then
            hit = 1
        end if
    end if
    if x1>=x2 and x1<(x2+31) then
        if (y1+31)>=y2 and (y1+31)<(y2+31) then
            hit = 1
        end if
    end if
    if (x1+31)>=x2 and (x1+31)<(x2+31) then
        if y1>=y2 and y1<(y2+31) then
            hit = 1
        end if
    end if
    if (x1+31)>=x2 and (x1+31)<(x2+31) then
        if (y1+31)>=y2 and (y1+31)<(y2+31) then
            hit = 1
        end if
    end if
    return hit
end function

function testForHit() as integer
    dim as integer TileX,TileY
    dim as integer hit1
    hit1 = 0
    for j as integer = 0 to 1
        for i as integer = 0 to 1
            TileX = hero.x\32+i
            TileY = hero.y\32+j
            if overLap(hero.x,hero.y,TileX*32,TileY*32)<>0 then
                'was it a reward tile?
                if TMAP(TileX,TileY)=3 or TMAP(TileX,TileY)=4 then
                    TMAP(TileX,TileY)=0
                    tileMapToBitMap()  'update change to bitmap
                end if
                if TMAP(TileX,TileY)<>0 then
                    hit1 = 1  'change to hit1 = i+j*2+1 to return tile number
                end if
            end if
        next i
    next j
    return hit1
end function

sub update()
    
    dim as integer TileX,TileY,nx,ny
    
    TileX = hero.x\32
    TileY = hero.y\32
    
    if TMAP(tileX+1,TileY+2) = 178 then onFloor = 1 else onFloor = 0
    
    screenlock()
    
    put (0,0),background,(WINX,WINY)-(WINX+WINW-1,WINY+WINH-1),pset
    'draw character sprite

        if onFloor = 0 then  'flying
            if hero.d = 1 then
                'flying moving right
                put (hero.x-WINX,hero.y-WINY),spriteBlock,(hero.f*hero.w,0)-(hero.f*hero.w+hero.w-1,31),trans
            else
                'flying moving left
                put (hero.x-WINX,hero.y-WINY),spriteBlock,(hero.f*hero.w,32)-(hero.f*hero.w+hero.w-1,63),trans
            end if
        else  'walking
            if hero.dx = 0 then 'then not walking
                if hero.d = 1 then  'looking right
                    put (hero.x-WINX,hero.y-WINY),spriteBlock,(hero.w,64)-(hero.w+hero.w-1,95),trans
                else
                    put (hero.x-WINX,hero.y-WINY),spriteBlock,(hero.w,96)-(hero.w+hero.w-1,127),trans
                end if
            else
                if hero.d = 1 then  'walking right
                    put (hero.x-WINX,hero.y-WINY),spriteBlock,(hero.f*hero.w,64)-(hero.f*hero.w+hero.w-1,95),trans
                else
                    put (hero.x-WINX,hero.y-WINY),spriteBlock,(hero.f*hero.w,96)-(hero.f*hero.w+hero.w-1,127),trans
                end if
            end if
        end if
        
  

    'display tiles in contact with sprite
    for j as integer = 0 to 1
        for i as integer = 0 to 1
            TileX = hero.x\32+i
            TileY = hero.y\32+j
            line (TileX*32-WINX,TileY*32-WINY)-(TileX*32+31-WINX,TileY*32+31-WINY),rgb(255,0,0),b
        next i
    next j

    screenunlock()
end sub

tileMapToBitMap()  'generates a bitmap from the tileBlock and tileMap
update()


dim as double now1
now1 = timer
dim as integer sx,sy

do
    if timer > now1 + 0.05 then
        now1 = timer
        
        hero.dx = 0  'only moving if key down
        'MOVE RIGHT
        if multikey(&H4D) then
                hero.x = hero.x + 4
                if testForHit()<>0 then
                    hero.x = hero.x - 4
                end if
                hero.d  = 1
                hero.dx = 1  'is moving
            hero.dx = 1
        end if
        
        'MOVE LEFT
        if multikey(&H4B) then
                hero.x = hero.x - 4
                if testForHit()<>0 then
                    hero.x = hero.x + 4
                end if
                hero.d = -1
                hero.dx = -1 'is moving
        end if

        'GRAVITY MOVE DOWN

            hero.y = hero.y + 4
            if testForHit()<>0 then
                hero.y = hero.y - 4
            end if

        end if
        
        'MOVE UP
        if multikey(&H48) then

                hero.y = hero.y - 8
                if testForHit()<>0 then
                    hero.y = hero.y + 8
                end if

        end if
        
        WINX = hero.x - (WINW\2)
        if WINX < 0 then WINX = 0
        if WINX > (BMAPW - WINW) then WINX = (BMAPW - WINW)
        WINY = hero.y - (WINH\2)
        if WINY < 0 then WINY = 0
        if WINY > (BMAPH - WINH) then WINY = (BMAPH - WINH)

'        if bird not moving AND bird on floor then 
            hero.c = hero.c + 1
            if hero.c > 8 then
                hero.c = 0
                hero.f = hero.f + 1
                if hero.f = 4 then hero.f=0
            end if
'        end if

    update()
    sleep 2
loop until multikey(&H01)

'==========  TILE MAP DATA ============
DATA "B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2"
DATA "B20000000000000000000000000000000000000400040004000000000000000000000000000000000D000D000000000000000000000000000000000000000000000000000000000000000000000000B2"
DATA "B2000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000B2"
DATA "B20000000000000000000000000000000000B2B2B2B2B2B2B2B20000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000B2"
DATA "B20000000000000000000000030000000000000000000000000000000000000000000000000000B2B2B2B200000000B2B2B2B2B200000000B2B2B2B2B2000000000000000000000000000000000000B2"
DATA "B2DBDB000000000000000000000000000000000000000000000000000200000000000000000000B1B1B1B100000000B1B1B1B1B100000000B1B1B1B1B10000000000000000000000000000DBDBDB00B2"
DATA "B2DBDB0000000000000000B2B2B2B20000000000000000B2B2B2B2B2B2B20000000000B2B2B2B2B1B1B1B100000000B1B1B1B1B100000000B1B1B1B1B10000000000000000000000000000DBDBDB00B2"
DATA "B2DBDB0000000000000000B1B1B1B10000000000000000B1B1B1B1B1B1B10000000000B1B1B1B1B1B1B1B100000000B1B1B1B1B100000000B1B1B1B1B100000000B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2"
DATA "B200000000000000000000000000000000000000000000B1B1B1B1B1B1B10000020000B1B1B1B1B1B1B1B100000000B1B1B1B1B100000000B1B1B1B1B100000000B1B1B1B1B1B1B1B1B1B1B1B1B1B1B1"
DATA "B2B2B2B2B2B2B2B2B20000000000000000B2B2B2000000B1B1B1B1B1B1B1B2B2B2B2B2B1B1B1B1B1B1B1B100000000B1B1B1B1B100000000B1B1B1B1B100000000B1B1B1B1B1B1B1B1B1B1B1B1B1B1B1"
DATA "B1B1B1B1B1B1B1B1B10000000000000000B1B1B1000000B1B1B1B1B1B1B1B1B1B1B1B1B1B1B1B1B1B1B1B100000000B1B1B1B1B100000000B1B1B1B1B100000000B1B1B1B1B1B1B1B1B1B1B1B1B1B1B1"
DATA "B1B1B1B1B1B1B1B1B11E1E1E1E1E1E1E1EB1B1B11E1E1EB1B1B1B1B1B1B1B1B1B1B1B1B1B1B1B1B1B1B1B11E1E1E1EB1B1B1B1B11E1E1E1EB1B1B1B1B11E1E1E1EB1B1B1B1B1B1B1B1B1B1B1B1B1B1B1"
Last edited by BasicCoder2 on May 12, 2017 19:31, edited 1 time in total.
thesanman112
Posts: 538
Joined: Jul 15, 2005 4:13

Re: tile collisions

Post by thesanman112 »

are you looking for constructive criticism...I removed the sleep function and made it full screen to see why movement seemed to be machined like...or like bit..bit..bit.. instead of smooth...it seems like maybe your program is remaining nested to drawing primitives,,,like putting every single block or sprite every screenflip or refresh....the key to smoothness lies with get and put....like if player moves 1 pixel or 4 pixels....that's what you clip of your drawing screen, and add to other side of drawing screen with one put....the whole time having shifted the drawing screen right or left with a single get command...
you use 2 put statements every screen cycle...draw your whole world on one bitmap on a buffer layer....and dechipher where to get from along the line and put that to current draw page or buffer.
thesanman112
Posts: 538
Joined: Jul 15, 2005 4:13

Re: tile collisions

Post by thesanman112 »

back in qb days you couldn't get enough memory reserved for video to be able to blitz a whole screen and have a sensible refresh rate...hahahaha
but during the time of Pentium 4 running DIRECTQB...that was another story...it was like GOD was plopping QUARTERS in to your screen like an arcade game!!!!

I wrote a whole 3d editor/processor program with FUTURE.BI....
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: tile collisions

Post by BasicCoder2 »

thesanman112 wrote:are you looking for constructive criticism...
Always welcome even if not always heeded :)
The post was in response to an exchange about tile collisions in another thread.
This code was written a couple of years ago and I never really looked at again before posting. A quick look now I realize I would probably code it differently. The temp bitmap for example is redundant in this case and can be removed and just display the background.
It was going to be a game but I lost interest. All too silly. In the last version there were flying bugs for the bird to eat and traps for the bird to avoid until it found its way out of the enclosure.
.
thesanman112
Posts: 538
Joined: Jul 15, 2005 4:13

Re: tile collisions

Post by thesanman112 »

I know exactly what you mean....there is always a better way...codeing is almost gone completely....with so many developers out there offering free compilier and engines to build games its almost useless to code anything anymore....and these engines are based on years of hard coding.....i remember building a 3d engine and spending weeks on optimising the code, trying to get higher screenrates for texturing....omg the hours and cups of coffee....heheheeh...alot of those developers where users of every language of programming available.....constantly looking for better ways of doing things faster....better blending abilities, better graphic enhancements and appearances....the graphics abilities always surpasses the processing power, till the next new chipsets!!!!
thesanman112
Posts: 538
Joined: Jul 15, 2005 4:13

Re: tile collisions

Post by thesanman112 »

I was watching the same messages as you about 2d collision.....its still interesting today to see different out takes on one persons perspective....by the way i like the demo....very cool!!!
leopardpm
Posts: 1795
Joined: Feb 28, 2009 20:58

Re: tile collisions

Post by leopardpm »

thesanman112 wrote:...like putting every single block or sprite every screenflip or refresh....
I always hate that 'lazy' (nothing personal BC!) method of redrawing the entire screen each frame, but BC's demo was to demonstrate something else entirely anyways so he gets a pass
thesanman112 wrote:the key to smoothness lies with get and put....like if player moves 1 pixel or 4 pixels....that's what you clip of your drawing screen, and add to other side of drawing screen with one put....the whole time having shifted the drawing screen right or left with a single get command...
the key to both smoothness AND speed is to only draw to the screen the things that have changed, if possible. I think this is what you are alluding to. My favorite way is to have a screen background buffer for the static graphics, and another buffer that holds a z-level attribute so that one is not stuck with drawing things in a certain order to get the desired 'overlap'/'depth' effect in isometric. I also use the z-level buffer to hold another attribute which indicates what exact objects 'owns' that particular pixel which makes for pixel perfect mouse picking. If you go a step further, you can take into account animation timings as well so that animated objects only get written when their frames actually change. I think this method is about the fastest possible (without using the 3D card etc) in FB. My goal is to always maintain a frame rate of 30 FPS for all animated/moving objects and then to allow the leftover time (which is dependent now on the amount of animated objects on screen) to be used by the rest of the program, but dished out in time slices. I didn't say that very clear, basically, with this method, the more objects on screen then the more time spent refreshing the screen which means that the amount of time given to other parts of the program will vary - dealing with this is an additional step (headache) which I think is worth it. I wish someone would write a full sprite/graphics library where all these things could be taken care of ... I have yet to get anything like this completed.
thesanman112 wrote:you use 2 put statements every screen cycle...draw your whole world on one bitmap on a buffer layer....and dechipher where to get from along the line and put that to current draw page or buffer.
I assume one of the put statements is used to erase or redraw the static stuff? I prefer not to erase then draw over the same pixel location if I can avoid it. I usually know the limits to the distance an agent/object can move and I incorporate that into the sprite frame size itself so that when it is drawn it also erases any parts that need be erased (actually not erased, but rather replaced with background or underlying sprite pixel info)

Even though machines are lickity-split fast these days and can easily erase/redraw the entire screen and still get decent FPS, I figure there is ALWAYS some use for spare machine cycles in other parts of the program, especially AI, so the quicker the frames can be dealt with, the better (in my book anyways!)
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: tile collisions

Post by BasicCoder2 »

leopardpm wrote:
thesanman112 wrote:...like putting every single block or sprite every screenflip or refresh....
I always hate that 'lazy' (nothing personal BC!) method of redrawing the entire screen each frame, but BC's demo was to demonstrate something else entirely anyways so he gets a pass
I think a blit is fast. The screen scrolls so unless you do something like shift and insert I think it is fast. I edited out the temp bitmap which served no purpose in this case.

You are always welcome to redo it your way to show how you would do it. This was old discarded code which happened to demo the four tiles involved in sprite to tile collisions which is why I posted it. With your isometric display idea perhaps when you get time you could demo it where it is not buried in a long thread?
.
Last edited by BasicCoder2 on May 12, 2017 19:51, edited 2 times in total.
leopardpm
Posts: 1795
Joined: Feb 28, 2009 20:58

Re: tile collisions

Post by leopardpm »

BC, I wasn't necessarily talking about this particular demo of yours - I made an assumption based on other demos you have made and you usually redraw the entire screen as a matter of practice, whether in a tile-based scenario, or using a background image.
I think a blit is fast.
Yup, it is... the problem of its speed comes up when blitting a bunch of smaller items, like sprites or map tiles...
There is a speed issue when you redraw the whole bitmap itself from say an array of tile values.
Yes, this is what I am referring to mostly.... no need to redraw every tile on the screen, only the ones that have changed (perhaps animated or moved).
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: tile collisions

Post by BasicCoder2 »

After posting the previous post your name kept coming up last when I tried to review it and I thought the post had failed. You had replied to the first one without me being aware of it. The result was I posted it three times (each time with a different edit).
.
leopardpm
Posts: 1795
Joined: Feb 28, 2009 20:58

Re: tile collisions

Post by leopardpm »

BasicCoder2 wrote:...With the large tile based bitmaps I usually waste memory creating the whole bitmap from the tile map and then just blit the section being displayed.
Yes, that sounds like what I was describing - just never have seen a demo from you doing such a thing (and, really, there is no need to do so as most of the demos we make are showing completely unrelated things and do not need the 'speed' of a more elaborate display process. BUT, my question to you then is how do you deal with the depth issue for isometric display? Making a big bitmap from tiles is good, but when sprites can 'go behind' things on the bitmap then there needs to be some additional routines...
BasicCoder2 wrote:I am not sure there is a faster way.
I think it is the fastest way as well.
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: tile collisions

Post by BasicCoder2 »

leopardpm wrote:
BasicCoder2 wrote:...With the large tile based bitmaps I usually waste memory creating the whole bitmap from the tile map and then just blit the section being displayed.
Yes, that sounds like what I was describing - just never have seen a demo from you doing such a thing.
Well I best smarten up my ways then :)
It is the only way I do it with scrolling views.
It usually looks like this with a window onto the background scene:

Code: Select all

put (0,0),background,(WINX,WINY)-(WINX+WINW-1,WINY+WINH-1),pset
In the path finding demo I notice I did remove the bitmap background and drew it each cycle but that can be fixed in seconds and would do so in a scrolling version. There are no tile images in that demo. I tend to concentrate on getting the algorithms working along with readability and worry about speed later.
.
thesanman112
Posts: 538
Joined: Jul 15, 2005 4:13

Re: tile collisions

Post by thesanman112 »

Well it really comes down to computing power as we all know to well....no matter how you slice it up....but there are times when tweaking a bit of code makes a hudge difference thats for sure, for eg.. calling pset 100 times or calling put 100 times is not exponentially equal,meaning, comparing one pset and one put,then comparing 25 pset's uses more power or time then a put statement that is blitting a 25x25 image, so at that point
625 pixels are being drawn, and probably using less power or time to be honest, then calling pset 25 times.

Im not 100 percent sure where the values/numbers fall, they are just for example.
thesanman112
Posts: 538
Joined: Jul 15, 2005 4:13

Re: tile collisions

Post by thesanman112 »

I like this demo...I also like the idea of using data strings to make bitmaps. I built a 3d world that used a similar method. It would load a text file and build walls and floors and what not from different characters located in the text file and relevant to position within file. It allowed you to make alot of rooms or areas fast. It supported a few different types of walls and floors as well as some moving floors and stairs.
leopardpm
Posts: 1795
Joined: Feb 28, 2009 20:58

Re: tile collisions

Post by leopardpm »

thesanman112 wrote:...I also like the idea of using data strings to make bitmaps.
I like the idea of using strings as well, except I hate how much space they take. So, I like to use a compression method to make the most use of the 8 bit characters... I think I was able to use 7 of the 8 bits for data yet still retain the 'string' format and ability to easily copy n paste into the forums and data statements.
Post Reply