[SOLVED] How can i stop updating whilst the window is being moved?

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
User avatar
unixfreak
Citizen
Posts: 77
Joined: Thu Oct 15, 2015 6:25 am
Location: Bristol, UK
Contact:

[SOLVED] How can i stop updating whilst the window is being moved?

Post by unixfreak » Fri Apr 15, 2016 6:45 pm

I have a problem with some basic collisions, that when the window is dragged across the desktop, collisions are failing either causing the player to die as the window is dragged or many objects falling through platforms.

How can i combat this?


pseudo code example of what i want to achieve:

Code: Select all

if not love.window.isMoving() then
    love:update()
end
Basically, pausing everything whilst the window is being moved across the desktop. Is this possible, or do i need to find another method?
Last edited by unixfreak on Mon Apr 18, 2016 10:53 pm, edited 2 times in total.

User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: How can i stop updating whilst the window is being moved?

Post by s-ol » Fri Apr 15, 2016 7:25 pm

Usually this happens because the window is not being updated while you drag it, and as a result the dt is very high after it is dropped. Try putting dt = math.min(dt, 1/30) at the top of love.update and see if that fixes it / whether thats whats going on.

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end

User avatar
HugoBDesigner
Party member
Posts: 403
Joined: Mon Feb 24, 2014 6:54 pm
Location: Somewhere in Brazil
Contact:

Re: How can i stop updating whilst the window is being moved?

Post by HugoBDesigner » Sat Apr 16, 2016 2:26 am

I was going to suggest the "math.min" thing for dt, but if you want the deltaTime to be accurate but also prevent that, just do a simple check on the "length" of it:

Code: Select all

function love.update(dt)
    if dt >= 1 then
        return
    end
    --Your normal update code here--
end
HugoBDesigner - my Twitter
HugoBDesigner - my Blog

User avatar
substitute541
Party member
Posts: 484
Joined: Fri Aug 24, 2012 9:04 am
Location: Southern Leyte, Visayas, Philippines
Contact:

Re: How can i stop updating whilst the window is being moved?

Post by substitute541 » Sat Apr 16, 2016 11:59 am

Alternatively:

Code: Select all

function love.load()
  ACCUMULATED_DELTA_TIME = 0
end

function love.update(dt)
  ACCUMULATED_DELTA_TIME = ACCUMULATED_DELTA_TIME + dt

  if ACCUMULATED_DELTA_TIME > 0.016666 then -- approx 1 / 60
    ACCUMULATED_DELTA_TIME = 0

    -- run stuffs
  end
end
or:

Code: Select all

function love.load()
  ACCUMULATED_DELTA_TIME = 0
  MAX_UPDATE_LOOPS = 3
end

function love.update(dt)
  ACCUMULATED_DELTA_TIME = ACCUMULATED_DELTA_TIME + dt
  local num_update_loops = 0

  while ACCUMULATED_DELTA_TIME > 0.016666 and num_update_loops < MAX_UPDATE_LOOPS do -- approx 1 / 60
    ACCUMULATED_DELTA_TIME = ACCUMULATED_DELTA_TIME - 0.016666
    num_update_loops = num_update_loops + 1
    
    -- run stuffs
  end
end



The first rate-limits the update loop to 60 fps. If the dt happens to be larger than 1 / 60, the code within the if statement will run only once. This does mean if your game happens to run below that, the code within the `if` statement will run half the time.

The second is a slightly more smarter approach using a while loop. It runs as many update steps if necessary. Example: if your game happens to run in 30 fps, the code within the while loop will run two times. Note that the MAX_UPDATE_LOOPS is kinda necessary; if you hold the window for too long, the while loop is gonna run far longer than necessary and probably crash your game. Modify if necessary.
Currently designing themes for WordPress.

Sometimes lurks around the forum.

User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: How can i stop updating whilst the window is being moved?

Post by s-ol » Sat Apr 16, 2016 2:38 pm

If you are running accumulated dt I would put that into love.run though instead of hacking love.update.

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end

User avatar
unixfreak
Citizen
Posts: 77
Joined: Thu Oct 15, 2015 6:25 am
Location: Bristol, UK
Contact:

Re: How can i stop updating whilst the window is being moved?

Post by unixfreak » Mon Apr 18, 2016 10:46 pm

s-ol wrote:Usually this happens because the window is not being updated while you drag it, and as a result the dt is very high after it is dropped. Try putting dt = math.min(dt, 1/30) at the top of love.update and see if that fixes it / whether thats whats going on.
Thanks for the suggestions all, i've gone with s-ol's as it seems more simple.

I'm already using some code based on the second snippet here to cap the fps manually outside of vsync (in game option can toggle vsync on/off)

so i used in love.update()

Code: Select all

	dt = math.min(dt, 1/max_fps)
Which seems to be a fix for anything that max_fps is set to, aswell as working with vsync. Nothing seems to fail a collision now. Thanks. Hopefully it shouldn't cause other problems :awesome:

Also here's the fps related code i now have, in case anyone else might find it useful (or if someone can see any issues with it).
Just adjust max_fps to any value you need.

Code: Select all

function love.load()
  max_fps = 120
  min_dt = 1/max_fps
  next_time = love.timer.getTime()
end


function love.update(dt)
  dt = math.min(dt, min_dt)
  next_time = next_time + min_dt

  --[[ update code

  --]]
end


function love.draw()

  --[[ draw code


  --]]

  local cur_time = love.timer.getTime()
  if next_time <= cur_time then
    next_time = cur_time
    return
   end
  love.timer.sleep(next_time - cur_time)
end

Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests