# Collision and Gravity Simple How To?

0

I_hope_I_can_change_this_later 2020-03-07 22:45 (Edited)

I am trying to make a simple platformer game. I somehow managed to scrape together some sort of collision, but it doesn't work very well, and i have no gravity. Eventually, I would like to add multiple levels, each with their own theme.

Mainly what i need is a good collision and gravity system. Once I know what commands to use and how to use them, i can tweak it to my liking. But right now, I am completely stuck on this.

If someone could be kind enough to help me with this, I would greatly appreciate it. :)

I_hope_I_can_change_this_later 2020-03-07 22:47 (Edited)

Here is the code, I also posted it on my account as well.

X=60
Y=112

DO
CLS

BG COPY 0,0,20,16 TO 0,0

'COLLISION
TOP=CELL.C((X+4)/8,(Y)/8)
LT=CELL.C((X)/8,(Y+8)/8)
LF=CELL.C((X+8)/8,(Y+8)/8)
DWN=CELL.C((X+4)/8,(Y+8)/8)

SPRITE 1,X,Y,1

'MOVE
IF UP(0) AND TOP=0 THEN Y=Y-3
IF DOWN(0) AND DWN=0 THEN Y=Y+3
IF LEFT(0) AND LT=0 THEN X=X-3
IF RIGHT(0) AND LF=0 THEN X=X+3

WAIT VBL
LOOP

#1:MAIN PALETTES
003F303300383400003C0C00003F3C00
003F2A15003F2A15003F2A15003F2A15

#2:MAIN CHARACTERS
00000000000000000000000000000000
FFFFFFFFFFFFFFFF0000000000000000
0000000000000000FFFFFFFFFFFFFFFF
000000FFFFFFFFFF000000FFFFFFFFFF
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF

#3:MAIN BG
00001410020002000200020002000200
02000200020002000200020002000200
02000200020002000200020002000000
00000000000000000000000000000000
00000000000000000000000000000300
00000200020000000000000000000000
00000000000000000000000000000000
00000000000013000000020002000000
00000000000000000000020002000000
00000200020002000000000002000200
02000200020000000000000000000000
00000000000000000000000000000000
00000000000000000000020002000000
00000200020002000200000000000000
00000000000000000000000000000000
00000200020000000000000000000000
00000000000000000000000000000200
02000200000000000000020002000200
02000000000000000000020002000200
02000200000000000000000000000000
00000200020000000000000000000000
00000000000000000000000000000000
00000000000002000200020002000000
00000000020002000200020002000200
02000200000000000000000000000000
00000200020000000000000000000000
00000000000000000000000000000000
02000200000000000000020002000200
02000000000000000000000000000000
00000000000000000000000000000000
00000200020000000000000000000000
00000000000000000200020002000000
00000000000000000000020002000000
00000000020002000200000000000000
00000000000000000000000000000000
00000200020000000000000000000000
00000000000000000000000000000000
02000200020000000000020002000200
02000200020002000200020002000200
02000200020002000200020002000200
02000200

I_hope_I_can_change_this_later 2020-03-08 05:16

Also, I'm not sure if sprite-on-cell or cell-on-cell collision is better, I'm open to both, but would prefer sprite-on-cell so I could make additional levels easier

was8bit 2020-03-08 07:12

I am really tired right now...my next moment of alertness with extra time and i will help out :)

Timo 2020-03-08 15:07

Here is an example, but I didn't check the code. https://lowresnx.inutilis.com/topic.php?id=303

I_hope_I_can_change_this_later 2020-03-08 16:19

@was8bit Ok, thanks for taking any extra time!

@Timo ok, thanks, i'll check it out :)

was8bit 2020-03-08 16:26 (Edited)

I have posted your fixed code on your game post, but for the benefit of others I will post here to...

Code taken from TIMO's game....

https://lowresnx.inutilis.com/topic.php?id=318

R=0
POSX=40
POSY=96
SPRITE 1,POSX,POSY,1

BG COPY 0,0,20,16 TO 0,0

GFORCE=0

DO

'MOVE PLAYER
IF LEFT(0) THEN
CALL CHECKCOLL(POSX-1,POSY,R)
IF R THEN POSX=POSX-1
END IF
IF RIGHT(0) THEN
CALL CHECKCOLL(POSX+1,POSY,R)
IF R THEN POSX=POSX+1
END IF
IF UP(0) THEN
CALL CHECKCOLL(POSX,POSY-1,R)
IF R THEN POSY=POSY-1
END IF
IF DOWN(0) THEN
CALL CHECKCOLL(POSX,POSY+1,R)
IF R THEN POSY=POSY+1
END IF

'SIMPLE GRAVITY
GFORCE=GFORCE+0.1
IF GFORCE>1 THEN GFORCE=1
CALL CHECKCOLL(POSX,POSY+GFORCE,R)
IF R THEN POSY=POSY+GFORCE

'SIMPLE JUMP
IF BUTTON(0,0) THEN
CALL CHECKCOLL(POSX,POSY+1,R)
IF NOT R THEN GFORCE=-2
END IF

SPRITE 1,POSX,POSY,

WAIT VBL
LOOP

'X,Y: PLAYER POSITION IN PIXELS
'R IS SET TO TRUE IF NO COLLISION WITH MAP
SUB CHECKCOLL(X,Y,R)
'CHECK ALL CORNERS
R=-1
CALL CHECKCELL(X/8,Y/8,R)
CALL CHECKCELL((X+7)/8,Y/8,R)
CALL CHECKCELL(X/8,(Y+7)/8,R)
CALL CHECKCELL((X+7)/8,(Y+7)/8,R)
END SUB

SUB CHECKCELL(X,Y,R)
C=MCELL.C(X,Y)
IF C<>0 THEN
R=0
END IF
END SUB

#1:MAIN PALETTES
003F303300383400003C0C00003F3C00
003F2A15003F2A15003F2A15003F2A15

#2:MAIN CHARACTERS
00000000000000000000000000000000
FFFFFFFFFFFFFFFF0000000000000000
0000000000000000FFFFFFFFFFFFFFFF
000000FFFFFFFFFF000000FFFFFFFFFF
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF

#3:MAIN BG
00001410020002000200020002000200
02000200020002000200020002000200
02000200020002000200020002000000
00000000000000000000000000000000
00000000000000000000000000000300
00000200020000000000000000000000
00000000000000000000000000000000
00000000000013000000020002000000
00000000000000000000020002000000
00000200020002000000000002000200
02000200020000000000000000000000
00000000000000000000000000000000
00000000000000000000020002000000
00000200020002000200000000000000
00000000000000000000000000000000
00000200020000000000000000000000
00000000000000000000000000000200
02000200000000000000020002000200
02000000000000000000020002000200
02000000000000000000000000000000
00000200020000000000000000000000
00000000000000000000000000000000
00000000000002000200020002000000
00000000020002000200020002000200
02000200000000000000000000000000
00000200020000000000000000000000
00000000000000000000000000000000
02000200000000000000020002000200
02000000000000000000000000000000
00000000000000000000000000000000
00000200020000000000000000000000
00000000000000000200020002000000
00000000000000000000020002000200
02000200020002000200020002000200
02000200020002000200020002000200
02000200000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000