Object freezes when stops
Posted: Mon Jun 18, 2018 6:01 pm
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.
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