Jumping and Gravity
Root / Programming Questions / [.]
JacboyXCreated:
Here's a simple demo:
VAR X,Y'Position XY VAR VX,VY'Velocity for each XY. Unlike acceleration, this is the actual movement speed of the player. VAR AX,AY'Acceleration for each XY. This will be reset every frame. VAR MVSP=0.3'Movement speed. Zero point three pixels per frame of button press. VAR FRCTN=0.9'Think of this as "how much velocity do I want to preserve after the character strikes a wall?" VAR BN'Button variable WHILE 1 BN=BUTTON() PHYSICS COLLISION RENDER VSYNC WEND DEF PHYSICS INC AY,0.5'Simulates gravity IF BN AND 1 THEN INC AY,-MVSP IF BN AND 2 THEN INC AY,MVSP IF BN AND 4 THEN INC AX,-MVSP IF BN AND 8 THEN INC AX,MVSP INC VX,AX INC VY,AY AX=0 AY=0 END DEF COLLISION IF(X<0)OR(X>400)THEN X=MIN(MAX(X,0),400):VX=-VX*FRCTN IF(Y<0)OR(Y>240)THEN Y=MIN(MAX(Y,0),240):VY=-VY*FRCTN END DEF RENDER GCLS GPSET X,Y ENDDon't shoot me if it doesn't work or doesn't look good. I can't quite test it atm, but this is the generic setup I use when dealing with a game's physics: Physics, collision, then render.
heres a kind of demo code thing:
'main loop while death==0 spset 0,0 spofs 0,x,y 'movement left right if button(0)==4 then dec x if button(0)==8 then inc x 'gravity inc y,2 'ground stopping fall if y<(number of the ground NO PARENTHESIS!) then y=(number of ground STILL NO PARENTHESIS!) 'jump if button(0)==16 then dec y,10 wait 1 wendbut idk if this will work just got it off the top of my head.