I forgot to mention that to be more physically correct and consistent, you have to use
instead of
so yeah, timing will be off unless you fix this line. Also, the following discussion will assume the above:
The timing should (now) be relatively the same across all machines because it's adjusted to the variable dt. The problem is either in changing the acceleration to 1, or in neglecting to change the initial velocity to compensate for the reduced acceleration. Currently, since the guy isn't accelerating down as fast, so his speed doesn't change as much every timestep. That means he'll stay going fast for awhile.
Code: Select all
velocity = 400 -- Changed to the maximum height of the screen from his jump. He never gets this high.
So the above comment isn't true. You have to look at some kinematics equations to find maximum height of jump. So some bla bla math physics:
v^2 = v_0 ^2 + 2a(delta s) = 0 (find where speed is 0, which is where maximum height occurs)
==> delta s = -v_0^2 / (2a)
And substituting in current values of v_0 = 400 and a = -1 (negative because accelerating in opposite direction) gives:
delta s = 80000 pixels
...which is pretty far off. Anyways, to find a v_0 you would want to use, solve for v_0:
v_0 = sqrt( 2a(delta s) )
so let's assume we want a gravitational acceleration of 200 px / sec^2 (it actually isn't that large), and the stick guy to rise 100 px:
v_0 = sqrt( 2 * 200 * (100) ) = 200
so setting v_0 = 200, a = 200 should make the stick guy rise 100 px. Of course, _still_, since you're using the euler method of integration, there's going to be a small % error (like... miniscule, but deadly if it's for some crazy hardcore engineering application) euler method is an
approximation of a physical system. The % error depends on how small your dt is, for the most part, so this should work fine.
Anyways, resulting code from my wall-of-text:
Code: Select all
-- Make a dude jump up and land. Also allow the movement left and right.
local G_ACCEL = 200
local V_NAUGHT = 200
local V_DOUBLE_JUMP_CHECK = 100
function load()
local f = love.graphics.newFont(love.default_font, 14)
dude = love.graphics.newImage("dude.png")
x = 300
y = 400
speed = 100
jump = 0
velocity = 0
accel = 0
love.graphics.setPointSize(2)
love.graphics.setColor(255,0,0)
love.graphics.setFont(f)
end
function update(dt)
if love.keyboard.isDown(love.key_right) then
x = x + (speed * dt)
elseif love.keyboard.isDown(love.key_left) then
x = x - (speed * dt)
end
if love.keyboard.isDown(love.key_up) then
if jump == 0 then
jump = 1
accel = G_ACCEL -- Changed to slow his jump speed.
velocity = V_NAUGHT -- Changed to the maximum height of the screen from his jump. He never gets this high.
elseif jump == 1 then -- DOUBLE JUMP! WOO!
if velocity <= V_DOUBLE_JUMP_CHECK then
jump = 2
velocity = V_NAUGHT
end
end
end
if jump ~= 0 then
velocity = velocity - accel * dt
y = y - velocity * dt
if y >= 400 then -- Changed to ONLY greater than 400.
jump = 0
velocity = 0
accel = 0
y = 400
end
end
end
function draw()
love.graphics.draw(dude,x,y)
love.graphics.point(x,y)
love.graphics.draw("Dude is at (" .. x .. "," .. y .. ")",50,50)
end