Forums

Topic: How do I make 4 way movement with array collision detection?

Posts 1 to 4 of 4

MyLegGuy

I made a character that moves in 4 different directions. Before moving, lets say left, it checks if its x value divided by 8 - 1 = 1 meaning if to the left of the player is there a wall? I put 1 for a wall in my array and 0 for nothing. However when moving left or up it detects a collision to soon, about 1 tile to soon. So, can anybody help me? Or give me example code or something? Thanks! Heres my code:

ACLS:CLEAR
PX=8
PY=8

@GAME
ACLS
RESTORE @MAPDATA
DIM MAP(32,23)

FOR Y=0 TO 22
FOR X=0 TO 31
READ MAP(X,Y)
NEXT
NEXT

FOR Y=0 TO 22
FOR X=0 TO 31
LOCATE X,Y
IF MAP(X,Y)==0 THEN BGPUT 0,X,Y,1,0,0,0
IF MAP(X,Y)==1 THEN BGPUT 0,X,Y,4,0,0,0
NEXT
NEXT

SPSET 0,8,0,0,0,0,8,8
SPOFS 0,8,8

@MAPDATA
Here I put a giant array that's 32x23 all in data statments, like this:
DATA 1,1,1,1
DATA 1,0,0,1
DATA 1,1,1,1
That's not my actual array above /

'note, I made it @LOP and not @LOOP on putpose
@LOP
wait 1
SPREAD(0),PX,PY

IF BUTTON()==4 THEN GOSUB @LEFT
IF BUTTON()==1 THEN GOSUB @UP
IF BUTTON()==2 THEN GOSUB @DOWN
IF BUTTON()==8 THEN GOSUB @RIGHT
GOTO @LOP

@LEFT
IF MAP(PX/8-1, PY/8)==1 THEN RETURN
SPOFS 0,PX-1,PY
RETURN

@RIGHT
IF MAP(PX/8+1, PY/8)==1 THEN RETURN
SPOFS 0,PX+1,PY
RETURN

@UP
IF MAP(PX/8, PY/8-1)==1 THEN RETURN
SPOFS 0,PX,PY-1
RETURN

@DOWN
IF MAP(PX/8, PY/8+1)==1 THEN RETURN
SPOFS 0,PX,PY+1
RETURN

Hello.

Nintendo Network ID: MyLegGuy2

nintendyagmr

What programming language are you using? Python?

Faster than snanic

Discostew

@nintendyagmr

It's SmileBASIC, used in Petit Computer.

@MyLegGuy

This is what I'd call a "boundary" problem. The sprite's origin point is on the top-left of the sprite. Imagine a sprite at <0,0>. If the sprite is 8 pixels wide, it actually occupies from its x position to x+7 (because x is actually x+0 and counts as a pixel), making 8 pixels. When you calculate collision when going right/down, you end up going 1 pixel beyond the boundary the sprite occupies, which is "(PX+8)/8" (or "PX/8+1" as you have it). When check on the left-top boundaries, you'll need to check one pixel off of those rather than a full 8 like you did with going right/down. So, instead of "PX/8-1 (which examines 8 pixels to the left), use "(PX-1)/8", which will examine just one pixel to the left. Do the same for UP using PY.

Discostew

3DS Friend Code: 4425-1477-0127 | Nintendo Network ID: Discostew

MyLegGuy

Thanks, you have helped me once again!

Hello.

Nintendo Network ID: MyLegGuy2

  • Page 1 of 1

This topic has been archived, no further posts can be added.