Issues with dt and multiple windows

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
M3OW
Prole
Posts: 4
Joined: Fri Aug 08, 2014 3:47 pm

Issues with dt and multiple windows

Post by M3OW »

I'm trying to make my charecter jump with the following code:

Code: Select all

function love.update(dt)
[...]
if love.keyboard.isDown("w") then 
	dTotal = dTotal + dt
	meow.y = meow.y - (meow.gravity *25* dt)
	if (meow.gravity > 2.1) then meow.gravity = meow.gravity - 0.8 end
	if (meow.gravity < 2) then meow.gravity = 1  end
end
[...]
end
where meow.y is the Y value of the sprite and meow.gravity is initially 50.

However, if I have multiple windows or something going on in the background, my character jumps a LOT higher that its supposed to. I read the wiki and it seems like dt is just the amount of time from the previous frame update, so how should I program this jump so that it lasts for the same amount of time on all machines?
Attachments
TESTPOSTPLSIGNORE.love
(40.67 KiB) Downloaded 147 times
0x29a
Prole
Posts: 27
Joined: Sun Jul 06, 2014 10:22 pm

Re: Issues with dt and multiple windows

Post by 0x29a »

Hmm...
dt is the most common shorthand for delta-time, which is usually passed through love.update to represent the amount of time which has passed since it was last called. It is in seconds, but because of the speed of modern processors is usually smaller than 1 (values like 0.01 are common).
Maybe you should just hard-code it?
Edit: or put some boundaries on it, so if dt is different from what is expected, your game would use fixed value?
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Issues with dt and multiple windows

Post by micha »

Hi M3ow,

you are on the right track, but you also have to multiply the change of velocity by dt:

Code: Select all

if love.keyboard.isDown("w") then
   dTotal = dTotal + dt
   meow.y = meow.y - (meow.gravity *25* dt)
   if (meow.gravity > 2.1) then 
      meow.gravity = meow.gravity - 0.8 * 60 * dt
   end
   if (meow.gravity < 2) then
      meow.gravity = 1
   end
end
This makes the behavior almost independent from dt and should be enough for your purposes. If you need an even higher accuracy, then have a look at higher order integration scheme, for example here.

And a small side-note: What you call gravity in your code, is in fact velocity. I suggest you rename all occurrences of "gravity" to "velocity". The number you subtract from velocity (here 0.8) is gravity.
M3OW
Prole
Posts: 4
Joined: Fri Aug 08, 2014 3:47 pm

Re: Issues with dt and multiple windows

Post by M3OW »

Thanks a lot Mincha, it works.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 4 guests