collision detection

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!
Post Reply
benjamin

collision detection

Post by benjamin »

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.
Guest
Veteran
Posts: 128
Joined: Sun Aug 14, 2005 8:33 pm
Location: Forest Lake, MN
Contact:

Post by Guest »

Code: Select all

psuedo: IF playerX < builingX then movechararcter
Last edited by Guest on Wed Nov 02, 2005 8:46 pm, edited 1 time in total.
benjamin

Post by benjamin »

<scratches head>
I guess I don't understand that code.
:?
Rattrapmax6
Veteran
Posts: 1055
Joined: Sun Jan 02, 2005 2:11 pm
Location: At my computer
Contact:

Post by Rattrapmax6 »

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.... :wink:
-Kevin (aka:Rattra)
(x.t.r.GRAPHICS)
User avatar
{Nathan}
Veteran
Posts: 1169
Joined: Thu Aug 19, 2004 6:08 pm
Location: The wetlands of central Ohio, USA
Contact:

Post by {Nathan} »

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]
Image
benjamin

Post by benjamin »

Ahhh, okay, I got it. Thanks guys.
:D
User avatar
matt2jones
Veteran
Posts: 80
Joined: Sat Feb 19, 2005 8:29 am
Location: elsewhere
Contact:

Post by matt2jones »

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
Do not mistake Apathy for feeling Content.

http://www.disjointed.cjb.net - Short Storys
http://matt2jones.deviantart.com - Random Art
http://www.freewebs.com/matt2jones - WebComic
Post Reply