Page 1 of 1

### collision detection

Posted: Wed Nov 02, 2005 5:45 pm
Hello,
I am putting together a simple game using ascii characters and while I have movement, and limiting the area of movement down pat, I became stumped when I thought about putting in buildings and how I would go about coming to a halt when I hit them, rather than going through them. Here is a snippet of code in its simplest form. I am putting a simple building in as the first line of code so it is easier to spot and have numbered the movement lines for simplicity sake:

'building
LOCATE 13, 32: COLOR 4, 0: PRINT STRING\$(5, 219)
'movement
100 PX = 7: PY = 22
LOCATE PX, PY, 0: COLOR 7, 0: PRINT CHR\$(64)
101 K\$ = INKEY\$
102 K\$ = RIGHT\$(INKEY\$, 1): IF K\$ = "" THEN GOTO 102
IF K\$ = "2" OR K\$ = "P" THEN LOCATE PX, PY, 0: COLOR 8, 0: PRINT CHR\$(250); : PX = PX + 2: LOCATE PX, PY, 0: COLOR 7, 0: PRINT CHR\$(64)
IF K\$ = "8" OR K\$ = "H" THEN LOCATE PX, PY, 0: COLOR 8, 0: PRINT CHR\$(250); : PX = PX - 2: LOCATE PX, PY, 0: COLOR 7, 0: PRINT CHR\$(64)
IF K\$ = "4" OR K\$ = "K" THEN LOCATE PX, PY, 0: COLOR 8, 0: PRINT CHR\$(250); : PY = PY - 2: LOCATE PX, PY, 0: COLOR 7, 0: PRINT CHR\$(64)
IF K\$ = "6" OR K\$ = "M" THEN LOCATE PX, PY, 0: COLOR 8, 0: PRINT CHR\$(250); : PY = PY + 2: LOCATE PX, PY, 0: COLOR 7, 0: PRINT CHR\$(64)
IF PX > 39 THEN LOCATE PX, PY, 0: COLOR 8, 0: PRINT CHR\$(250): PX = 39: LOCATE PX, PY, 0: COLOR 7, 0: PRINT CHR\$(64): GOTO 101
IF PX < 7 THEN LOCATE PX, PY, 0: COLOR 8, 0: PRINT CHR\$(250): PX = 7: LOCATE PX, PY, 0: COLOR 7, 0: PRINT CHR\$(64): GOTO 101
IF PY > 77 THEN LOCATE PX, PY, 0: COLOR 8, 0: PRINT CHR\$(250): PY = 77: LOCATE PX, PY, 0: COLOR 7, 0: PRINT CHR\$(64): GOTO 101
IF PY < 22 THEN LOCATE PX, PY, 0: COLOR 8, 0: PRINT CHR\$(250): PY = 22: LOCATE PX, PY, 0: COLOR 7, 0: PRINT CHR\$(64): GOTO 101
IF K\$ = CHR\$(27) THEN CLS : END
GOSUB 101

What couuld I add to this in order to stop the character from walking through the building? Thank you in advance for any assistance with this.

Posted: Wed Nov 02, 2005 5:55 pm

Code: Select all

``psuedo: IF playerX < builingX then movechararcter``

Posted: Wed Nov 02, 2005 8:32 pm
I guess I don't understand that code.

Posted: Wed Nov 02, 2005 8:46 pm
Basicaly, you set it so when character's X, Y equals the Building's wall X, Y the program stops motion in that direction. Normaly to stop, you reverse the number of steps the character took b4 hitting the wall.

Code: Select all

``````PlayX = PlayX + 1

IF PlayX = BuildX THEN PlayX = PlayX - 1``````
/\ In it's simplist form....

Posted: Wed Nov 02, 2005 8:54 pm
Ratt, that works, but I find this to be more efficiant (smaller and faster)

Code: Select all

``IF PlayerX + 1 < TheThingUdontWannaHitX THEN PlayerX = PlayerX + 1``
Really, I find that just small things (like defining consts Left = 1, ect) is better so instead of that, I would use this. I know its bigger, but it looks WAY better. QB just needs inline subs...

Code: Select all

``````CONST Left = 1, Right = 2, Up = 3, Down = 4
...
IF Player.X > CollisionThing.X THEN MovePlayer(Left)

SUB MovePlayer(Dir as integer)
'its obvious what goes here! duh...[code]``````

Posted: Thu Nov 03, 2005 7:17 am
Ahhh, okay, I got it. Thanks guys.

Posted: Thu Nov 03, 2005 10:54 am
You could also use the SCREEN function to check what character is infront of the @, and if it's a wall, don't allow them the @ to move.

It's not good programming practice to do collisions straight from the screen like that, but if you have allot of walls it's easier to manage.

Code: Select all

``IF K\$ = "2" OR K\$ = "P" and SCREEN(PX+2,PY) <> [i]*WALL*[/i] THEN LOCATE PX, PY, 0: COLOR 8, 0: PRINT CHR\$(250); : PX = PX + 2: LOCATE PX, PY, 0: COLOR 7, 0: PRINT CHR\$(64) ``
matt