0

I am trying to create a player in love2D, and I have a lerp function that brings the players speed to 0 , but I know I am not using the lerp function correctly because it never reaches the end value and starts acting funky and crazy, how do I use the lerp function correctly.

function love.load()
    moon = require "Library/moon_light"
    position = {x = 0, y = 0}
    H = 0
    V = 0
    new_H = 0
    new_V = 0
    position["x"] = moon.Mid_point(0,0,1280,720)[1]
    position["y"] = moon.Mid_point(0,0,1280,720)[2]
    accel = 8
    max_speed = 80
    fps = 60
    friction = 6
end

function love.update(dt)
    if love.keyboard.isDown('escape') then
        love.event.push('quit')
    end

    if love.keyboard.isDown("d") then
        H = 1
    elseif love.keyboard.isDown("a") then
        H = -1
    else
        H = 0
    end

    if love.keyboard.isDown("w") then
        V = -1
    elseif love.keyboard.isDown("s") then
        V = 1
    else
        V = 0
    end
    if H ~= 0 then -- use a new value and give it acceleration
        new_H = H * accel * dt * fps
    end
    if H == 0 then -- Check if the player isnt moving lerp it to 0 so stopping the movement
        new_H = moon.Lerp(new_H,0,friction * dt)
    end
    if V ~= 0 then
        new_V = V * accel * dt * fps
    end
    if V == 0 then
        new_V = moon.Lerp(new_V,0,friction * dt)
    end

    position["x"] = position["x"] + new_H -- Set the motion to the position
    position["y"] = position["y"] + new_V
    
end

function love.draw()
    love.graphics.rectangle("fill",position["x"] - 25,position["y"] - 25,50,50)
    love.graphics.print("Horiztonal value : " .. H .. "\nVertical value : " .. V,100,400)
    love.graphics.print("new_H value : " .. new_H .. "\nnew_V value : " .. new_V,100,440)

end

The lerp function, I think nothing is wrong with it, but just in case.

function moon_light.Lerp(start_v,end_v,percent)
    return start_v + (end_v - start_v) * percent
end

EDIT I changed the lerp to be time based, from the suggestion and I also made a new variable called motion it seems setting the lerp to its self caused a weird issue, making it never reach 0:

if H ~= 0 then -- use a new value and give it acceleration
    H_counter = 0
    new_H = H * accel * dt * fps
    motion_H = new_H
end
if H == 0 then -- Check if the player isnt moving lerp it to 0 so stopping the movement
    if H_counter < slow_down_duration then
        motion_H = moon.Lerp(new_H,0,H_counter/slow_down_duration)
        H_counter = H_counter + dt
    else
        new_H = 0
        motion_H = 0
    end
end


if V ~= 0 then
    V_counter = 0
    new_V = V * accel * dt * fps
    motion_V = new_V
end
if V == 0 then
    if V_counter < slow_down_duration then
        motion_V = moon.Lerp(new_V,0,V_counter/slow_down_duration)
        V_counter = V_counter + dt
    else
        new_V = 0
        motion_V = 0
    end
end

position["x"] = position["x"] + motion_H -- Set the motion to the position
position["y"] = position["y"] + motion_V
Dragon20C
  • 307
  • 2
  • 11
  • without going to your code, you#re using an imprecise lerp function that is not guaranteed to reach the target value. try math.lerp instead https://love2d.org/wiki/General_math – Piglet Jan 19 '22 at 11:52
  • For it to hit 0, `percent` needs to be equal to 1, but `friction * dt` is never going to be 1 (unless you have a dt spike). You definitely want to use a different value. A better method might be to set a time interval (do you want the player to stop moving after 1 second? 2 seconds?), then track how much of that interval has passed with each frame. Use the percentage of the interval to lerp. – Optimum Jan 19 '22 at 14:20
  • okay so I have taken into account what Optimum has said and I have added a duration and a counter that goes towards the duration and I do counter/duration at the percent that at first didnt fix it but that wasnt the issue, futher investigating I found that (new_H = moon.Lerp(new_H,0,friction * dt)) was causing weird issues it seems each frame new_H keeps changing and sometimes would completely miss the target value, so I added a new variable and set it to that and that seemed to fix the issue. edited post to show new changes – Dragon20C Jan 19 '22 at 16:56

0 Answers0