Domanda

I'm making a simple animation where a ball bounces repeatedly. The equation for the bounce is h = rt / 16t^2, where h is height, t is time in seconds, and r is initial speed. The problem is that the ball bounces up-side-down. I've been playing around with the equation, but I just can't get it right. Can anyone see what is wrong with this?

function move_ball()
  count = count + 0.3
  local h = (ints*count)-(16*(math.pow(count,2)))
  if (h < 0) then
    count = 0
    move_ball()
    collision()
  else
    ball.y = h / scale
  end
  if (ball.x - ball.rad < 0) then
    ball.dir = ball.speed
    collision()
  elseif (ball.x + ball.rad > length) then
    ball.dir = -ball.speed
    collision()
  end
  ball.x = ball.x + ball.dir
end
È stato utile?

Soluzione

Perhaps you need something like:

ball.y = height - (h / scale)

with a test to be sure that ball.y doesn't go negative.

Altri suggerimenti

Your equation is h = r t - 16 t^2, assuming that positive h is up and gravity pulls down (I would have thought 4 not 16 but that has nothing to do with your question). Your code seems correct. However it may be that the screen coordinates have positive y going down. Try negating h: h = -h right after you compute h, and check h > 0 instead.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top