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!
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
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
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
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
-- 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.
-- 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?