Object freezes when stops

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
Twinkle942910
Prole
Posts: 2
Joined: Mon Jun 18, 2018 5:56 pm

Object freezes when stops

Post by Twinkle942910 »

Hi! I'm new to GD and I am trying to implement smooth sliding of a block and simulate friction to stop object slowly. The problem is, when object almost completely stops, you can notice freezes in his movement. What can cause it? Thank you!

Here's the code.

Code: Select all

push = require 'push'

WINDOW_WIDTH = 1280
WINDOW_HEIGHT = 720

VIRTUAL_WIDTH = 256
VIRTUAL_HEIGHT = 144

BLOCK_SIZE = 10

GRAVITY = 10

function love.load()
  love.graphics.setDefaultFilter('nearest', 'nearest')

  push:setupScreen(VIRTUAL_WIDTH, VIRTUAL_HEIGHT, WINDOW_WIDTH, WINDOW_HEIGHT, {
    fullscreen = false,
    resizable = false,
    vsync = true
  })

  x = VIRTUAL_WIDTH / 2 - BLOCK_SIZE
  y = VIRTUAL_HEIGHT / 2 - BLOCK_SIZE

  dx = 0
  dy = 0

  jumping = true
end

function love.update(dt)
  if love.keyboard.isDown('d') then
      dx = dx + 50
  end

  if love.keyboard.isDown('a') then
      dx = dx - 50
  end

  if love.keyboard.isDown('w') and jumping == false then
    dy = -200
    jumping = true
  end

  dy = dy + GRAVITY

  x = x + dx * dt
  y = y + dy * dt

  --friction
  dx = dx * 0.9
  --dy = dy * 0.9

  if(y > VIRTUAL_HEIGHT - BLOCK_SIZE) then
    y = VIRTUAL_HEIGHT - BLOCK_SIZE
    dy = 0
    jumping = false
  end

  if(x > VIRTUAL_WIDTH) then
    x = 0
  elseif (x + BLOCK_SIZE < 0) then
    x = VIRTUAL_WIDTH
  end
end

function displayFPS()
    love.graphics.setColor(0, 255, 0, 255)
    love.graphics.print('FPS: ' .. tostring(love.timer.getFPS()), 10, 10)
end

function love.draw()
  push:apply('start')
  --love.graphics.setColor(123, 43, 15, 255)
    love.graphics.setColor(0, 255, 0, 255)
  love.graphics.rectangle('fill', x, y, BLOCK_SIZE, BLOCK_SIZE)
  displayFPS()
  push:apply('end')
end
DarkShroom
Citizen
Posts: 86
Joined: Mon Jul 17, 2017 2:07 pm

Re: Object freezes when stops

Post by DarkShroom »

you should really not make others to find the libraries:
https://github.com/Ulydev/push/blob/master/push.lua

i noticed not dt applied to friction

i could not debug you sorry, good luck, bump lol
DarkShroom
Citizen
Posts: 86
Joined: Mon Jul 17, 2017 2:07 pm

Re: Object freezes when stops

Post by DarkShroom »

never came across the issue as i use the box 2D, also lerping wouldn't have such behaviour as it always makes the sum from start to finish

sorta want to now, this code exhibits less issue, i think it might be a known thing to oldschool vid game devs :)

Code: Select all

x = 30
y = 30

x_vel = 300

function love.update(dt)
    x = x + (dt * x_vel)
    x_vel = x_vel * 0.99
    if x_vel < 20 then --simply added a point of rest
        x_vel = 0
    end
end

function love.draw()
    love.graphics.rectangle('fill', x, y, 50, 50)
end
<shouts> FORUM!!!
DarkShroom
Citizen
Posts: 86
Joined: Mon Jul 17, 2017 2:07 pm

Re: Object freezes when stops

Post by DarkShroom »

hows this smooth your boat, yeah i know that looks a bit weird but surely you can do the rest, i will keep this for myself now, i need a custom physics comparison for my framework

Code: Select all

x = 30
y = 30

x_vel = 300

x_acceleration = -100

function love.update(dt)
    x = x + (dt * x_vel)
    -- x_vel = x_vel * 0.99 --friction
    if x_vel < 1 then --simply added a point of rest
        -- x_vel = 0
        x_acceleration = 0
        x_vel = 0
    end
    
    x_vel = x_vel + x_acceleration * dt
end

function love.draw()
    love.graphics.rectangle('fill', x, y, 50, 50)
end
someone really needs to hire me (yeah like maybe just to actually make sense of the answer sure...)

edit: so i think your code was MATH correct, mine is applying a constant deceleration, which doesn't screw up when it is applied frame by frame, or something like that

edit2: my cv:
beer
telesales
User avatar
Link
Prole
Posts: 19
Joined: Tue Jun 19, 2018 4:09 am

Re: Object freezes when stops

Post by Link »

You may be interested in this great library: https://github.com/kikito/tween.lua

Code: Select all

-- make some text fall from the top of the screen, bouncing on y=300, in 4 seconds
local label = { x=200, y=0, text = "hello" }
local labelTween = tween.new(4, label, {y=300}, 'outBounce')
labelTween:update(dt)
It provides Tweening/Easing/Interpolating functions, allowing you to move between 2 values gradually over a specified time duration, using an easing function for different effects. I use it within my game for gradual deceleration like you describe.
DarkShroom
Citizen
Posts: 86
Joined: Mon Jul 17, 2017 2:07 pm

Re: Object freezes when stops

Post by DarkShroom »

Link wrote: Tue Jun 19, 2018 4:17 am You may be interested in this great library: https://github.com/kikito/tween.lua

Code: Select all

-- make some text fall from the top of the screen, bouncing on y=300, in 4 seconds
local label = { x=200, y=0, text = "hello" }
local labelTween = tween.new(4, label, {y=300}, 'outBounce')
labelTween:update(dt)
It provides Tweening/Easing/Interpolating functions, allowing you to move between 2 values gradually over a specified time duration, using an easing function for different effects. I use it within my game for gradual deceleration like you describe.
perhaps he doesn't know the destination, or do you have some other idea on this?
Twinkle942910
Prole
Posts: 2
Joined: Mon Jun 18, 2018 5:56 pm

Re: Object freezes when stops

Post by Twinkle942910 »

Thank you, guys, that helped a lot!
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 1 guest