Qbasicnews.com

Full Version: Help with collision detection
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Please give me some idea how to detect a collision in a Commander Keen like side-scroller game. For example when the man goes to a stone, the stone must stop him. But I know only the POINT(X,Y) method, that's not good... The tiles are 16x16 pixel like in Commander Keen and Dangerous Dave. Any ideas how to detect the collision? :-?
how are you storing your map?

Oz~
I'm storing it in a 2D array

By the way, the game isn't a side scroller as I said, it's with static screens like Dangerous Dave I... but I thing that the collision detection is the same in side scrollers, isn't it?
and what are you trying to find out if you collide into it? The map? sprites?

if its the map, assuming it is something like MAP(x, y), this is what I'd suggest:

Code:
--Presudoe code
if key left THEN
result = Map(x-1, y)
If result = clear_space THEN
   'go ahead, and change coords
END IF
end if

i don't have much time, so just do that sort of thing for every movement - reply if you have more trouble

Oz~
Depending on how the chracter x and ys are stored this cna be easy

If they are like the map array then you can compare the x and y's directly to the array and see if the coords are walkable
psuedocode:
Code:
IF map(guyx,guyy) = wall THEN dontmove

=P
Here is the source:

Code:
DECLARE SUB PutSprite (PosX%, PosY%, SpriteType$, WhatToPut%)
DECLARE SUB LoadGraphics ()
DECLARE SUB LoadLevel (lev%)
DECLARE SUB PutTile (PosX%, PosY%, WhatToPut%)

DEFINT A-Z

CONST TRUE = -1
CONST FALSE = NOT TRUE
CONST Wall = 0
CONST Ground = 1
CONST Stone = 2
CONST Box = 3
CONST NICKY = 0
CONST NICKY2 = 2
CONST Arrow = 0
CONST MaxLives = 5
CONST StartLives = 3
CONST LastLevel = 2
CONST StartLevel = 1
CONST StartScore = 0
CONST MaxScore = 100000
CONST LevelX = 320
CONST LevelY = 176
CONST VisualPage = 0
CONST BufferPage = 1
CONST LevelPage = 2

UpArrow$ = CHR$(0) + "H"
DownArrow$ = CHR$(0) + "P"
LeftArrow$ = CHR$(0) + "K"
RightArrow$ = CHR$(0) + "M"

TYPE Objects
     ObjType AS INTEGER
     ObjName AS STRING * 10
     ObjX AS INTEGER
     ObjY AS INTEGER
END TYPE

DIM SHARED Tiles%(65, 5)
DIM SHARED LevelArray(1 TO 20, 1 TO 12) AS Objects
DIM SHARED NickySprites%(65, 3)
DIM SHARED Sprites%(65, 1)
DIM SHARED Lives%, Level%, Score&
DIM SHARED NickyX%, NickyY%, StartX%, StartY%, EndX%, EndY%, CurrentFrame%, Direction%, Motion%
DIM SHARED ExitTheLoop%

Lives% = StartLives
Score& = StartScore
Level% = StartLevel
CurrentFrame% = 1
Direction% = 1
Motion% = 2

LoadGraphics
LoadLevel 1

ExitTheLoop% = FALSE

DO
Keyb$ = INKEY$
SELECT CASE UCASE$(Keyb$)
       CASE RightArrow$: Direction% = 1: GOSUB MoveNicky
       CASE LeftArrow$: Direction% = -1: GOSUB MoveNicky
END SELECT
LOOP

WHILE INKEY$ = "": WEND
SCREEN 0: WIDTH 80, 25

END

MoveNicky:
LINE (NickyX%, NickyY%)-(NickyX% + 15, NickyY% + 15), 0, BF
IF CurrentFrame% = 2 THEN CurrentFrame% = 1 ELSE CurrentFrame% = 2
PutSprite NickyX%, NickyY%, "Nicky", (CurrentFrame% - 1) * 2
PCOPY BufferPage, VisualPage
WAIT &H3DA, 8
RETURN

SUB LoadGraphics

DEF SEG = VARSEG(Tiles%(0, 0))
BLOAD "TILES.DAT", VARPTR(Tiles%(0, 0))
DEF SEG = VARSEG(NickySprites%(0, 0))
BLOAD "NICKY.DAT", VARPTR(NickySprites%(0, 0))
DEF SEG = VARSEG(Sprites%(0, 0))
BLOAD "SPRITES.DAT", VARPTR(Sprites%(0, 0))
DEF SEG

END SUB

SUB LoadLevel (lev%)

SCREEN 7, 0, LevelPage, VisualPage

l$ = RTRIM$(LTRIM$(STR$(lev%)))
OPEN "LEVEL" + l$ + ".DAT" FOR INPUT AS #1

FOR Row% = 1 TO 12
LINE INPUT #1, LevRow$
FOR Col% = 1 TO 20

Object$ = MID$(LevRow$, Col%, 1)
TempX% = (Col% - 1) * 16
TempY% = (Row% - 1) * 16
SELECT CASE Object$
       CASE "W": LevelArray(Col%, Row%).ObjName = "WALL": LevelArray(Col%, Row%).ObjType = 1: PutTile TempX%, TempY%, Wall
       CASE "G": LevelArray(Col%, Row%).ObjName = "GROUND": LevelArray(Col%, Row%).ObjType = 1: PutTile TempX%, TempY%, Ground
       CASE "S": LevelArray(Col%, Row%).ObjName = "STONE": LevelArray(Col%, Row%).ObjType = 1: PutTile TempX%, TempY%, Stone
       CASE "B": LevelArray(Col%, Row%).ObjName = "BOX": LevelArray(Col%, Row%).ObjType = 1: PutTile TempX%, TempY%, Box
       CASE " ": LevelArray(Col%, Row%).ObjName = "NOTHING": LevelArray(Col%, Row%).ObjType = 0
END SELECT
LevelArray(Col%, Row%).ObjX = TempX
LevelArray(Col%, Row%).ObjY = TempY
NEXT Col%
NEXT Row%

PCOPY LevelPage, BufferPage
SCREEN 7, 0, BufferPage, VisualPage
INPUT #1, StartX%
INPUT #1, StartY%
INPUT #1, StartDir$
INPUT #1, EndX%
INPUT #1, EndY%
INPUT #1, ArrowDir$
NickyX% = StartX%
NickyY% = StartY%
PutSprite StartX%, StartY%, "Nicky", NICKY
PutSprite EndX%, EndY%, "Other", Arrow
PCOPY BufferPage, VisualPage

END SUB

SUB PutSprite (PosX%, PosY%, SpriteType$, WhatToPut%)

IF SpriteType$ = "Nicky" THEN
   PUT (PosX%, PosY%), NickySprites%(0, WhatToPut% + 1), AND
   PUT (PosX%, PosY%), NickySprites%(0, WhatToPut%), OR
ELSE
   PUT (PosX%, PosY%), Sprites%(0, 1), AND
   PUT (PosX%, PosY%), Sprites%(0, 0), OR
END IF

END SUB

SUB PutTile (PosX%, PosY%, WhatToPut%)

PUT (PosX%, PosY%), Tiles%(0, WhatToPut%), PSET

END SUB
Quote:Depending on how the chracter x and ys are stored this cna be easy

If they are like the map array then you can compare the x and y's directly to the array and see if the coords are walkable
psuedocode:
Code:
IF map(guyx,guyy) = wall THEN dontmove

=P
The char's x and y are stored as screen coordinates, not like the map Sad
Ok, this is Mit.

I have recently done collision detecting.
I came up with a quick eight IF calcs per 2 pics.

All you need is the pixel Top and Left of each pic (on the screen), and the Length and Height of each pic (in pixels) and it will tell you if they're touching no matter what system of storing your using.

Do you want it? I'll post it here if you ask me to.
Please post it Smile
http://www.gamedev.net/reference/article...cle735.asp

Its c code, but you should understand a simple c-ish if(foo==bar) Wink
Pages: 1 2