Log In  

So I'm relatively new to coding, so I thought Pico-8 would be good to fiddle around with.
So after playing a few games and messing with a few of the demo's code, I found a problem.
Sometimes, in the bounce demo which I modified, when you bounce the velocity y of the ball
would suddenly stay at 0 mid-air. I don't know why can anyone help?

-- bouncy ball demo
-- by zep
-- modified by help_plz

size  = 10
ballx = 64  
bally = 80
floor_y = 100
bounce_per_sec = 0  
-- starting velocity
velx =0
vely =0 
frame=0
lag=0
offset=3
bounce=false
collision=false

function _draw()
 cls(1)
 map(0,0,0,100,16,1)
 print("press ❎ to bump",
       32,10, 6)

 --rectfill(0,floor_y,2227,127,8)
 --circfill(ballx,bally,size,clr)

 if vely < 5.5 and vely > -4 then
  lag=vely
 end

 spr(1,ballx-offset-velx, 
       bally-4-lag)
 spr(17,ballx-10,bally-10,2.75,2.75)

       print("x:"..ballx, 20,26)
       print("y:"..bally, 20,32)
       print("b_p/s"..bounce_per_sec, 20,38)
       print("vx:"..velx, 20,44)
       print("vy:"..vely, 20,50)
       print("col:"..(collision and 'true' or 'false'),20,56)
     --print("cx:"..camerax, 20+ballx,56,1)
     --print("cy:"..cameray, 20+ballx,62)
end

function _update60()

 offset=4

 collision=false

 if bally < 0 then
  cameray=flr(bally)-64-flr(vely)
 end

 if bally    > 10 then
  cameray=0
 end

 camera(camerax, cameray)

 frame+=1

 if frame==60 then
  bounce_per_sec = 0
 end

 if frame==60 then
  frame = 0
 end

 if ballx+velx < 0+size or
    ballx+velx > 128-size

 then
  -- bounce on side!
  sfx(1)
  velx *= -0.8
 else

  ballx += velx
 end
--==================--

 -- move ball up/down
 if bally+vely >= floor_y-size
  then
  -- bounce on floor
   bounce_per_sec+=1
   vely = vely * -0.8
   collision = true
   sfx(0)

  else
   bally += vely
 end

 --so the ball doesn't bounce forever, and instead stops 
 if bounce_per_sec >= 5.5 then
  vely = 0
 end

 -- gravity
 if vely > 0 or vely < 0 then
  vely += 0.2
 end

--====================--
 if btn(0) then
  offset=8
 end

 if btn(1) then
  offset=-1
 end

 if (btnp(5)) then
  vely = 5
  sfx(2)

  if btn(0) then
   velx=-4
  end

  if btn(1) then
   velx=4   
  end
 end
end
P#57026 2018-09-24 00:31 ( Edited 2018-09-24 06:56)

Your problem is around here

 --so the ball doesn't bounce forever, and instead stops 
 if bounce_per_sec >= 5.5 then
  vely = 0
 end

 -- gravity
 if vely > 0 or vely < 0 then
  vely += 0.2
 end

Your gravity check misses out 0, so if vely is 0 gravity is not applied. So I'd guess you want to check for a while number in bounces_per_sec instead of a decimal since it does look like in that gif the ball does bounce five and a half times before stopping.
When I do gravity I use an 'on_ground' Boolean to control if gravity is applied or not, I would recommend doing that instead of 0 having odd behaviour

P#57027 2018-09-24 02:33 ( Edited 2018-09-24 06:33)

So yeah I'd have something like

  • apply gravity only if on_ground is false.
  • start with on_ground as true, then when the bouncing starts set it to false
  • have some kind of 'bounces_till_stop' variable and every time the ball hits the ground reduce it by 1
  • when the ball hits the ground and 'bounces_till_stop' is 0, set on_ground to true again.

It'll need a little tweaking but something like that. You probably want some terminal velocity as well.

P#57028 2018-09-24 02:56 ( Edited 2018-09-24 06:56)

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-03-29 05:14:03 | 0.008s | Q:13