Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

local delta = 0.

0166
local wish_dir = 1
local wish_dirMagnitude = 1

--local m_vecOldVelocity = 0
local m_vecVelocity = 0
local mylt = 40
local frictionx = 8

local projected_speed = m_vecVelocity

function accelerate()

projected_speed = m_vecVelocity
local speed_remaining = (12 * wish_dirMagnitude) - projected_speed
--if speed_remaining <= 0 then return end

local accel_final = wish_dir * speed_remaining

local function ode(t, n)


return n * (delta * mylt * t)
end

local function rk4(f, tn, yn, h)

local k1 = f(tn, yn)


local k2 = f(tn + h/2, yn + h/2 * k1)
local k3 = f(tn + h/2, yn + h/2 * k2)
local k4 = f(tn + h, yn + h * k3)

m_vecVelocity = m_vecVelocity + h/6 * (k1 + 2 * k2 + 2 * k3 + k4)


end

rk4(
ode, -- f
speed_remaining, -- tn
wish_dir, -- yn
delta -- h
)
--m_vecVelocity = m_vecVelocity + delta * accel_fin
--[[
local vel = m_vecVelocity - m_vecOldVelocity
m_vecOldVelocity = m_vecVelocity
m_vecVelocity = m_vecVelocity + vel + accel_final * delta*delta
]]
end

function clamp(num, x, y)
return math.max(x, math.min(y, num))
end

function friction()

local current_speed = math.sqrt(m_vecVelocity * m_vecVelocity)--


m_vecVelocity.Magnitude

if(current_speed < 0.1) then


m_vecVelocity = 0
return
end

local friction_curve = clamp(current_speed, 14, math.huge)


local speed_loss = friction_curve * frictionx * delta
local speed_scalar = clamp(current_speed - speed_loss, 0, math.huge)
speed_scalar = speed_scalar / clamp(current_speed, 1, math.huge)

m_vecVelocity = m_vecVelocity * speed_scalar


end

for o = 0, 1000 do
accelerate()
friction()

print(o, m_vecVelocity)
end

You might also like