add dampening, fix weight

This commit is contained in:
fnicon 2026-03-22 14:44:17 +09:00
parent 03c0f489b8
commit eae97741d3
3 changed files with 29 additions and 24 deletions

View File

@ -21,32 +21,30 @@ local default = {
max_orientation_speed = 1.0,
velocity = vm.vec2(0.0, 0.0),
max_speed = 1.0,
min_speed = -1.0,
max_speed = 0.1,
min_speed = -0.1,
inertia = vm.vec2(0.0, 0.0),
accel = 0.1,
brake = 0.1,
grip = 0.0,
grip = 0.1,
steer = 0.1,
mass = 0.0,
streamline = 0.0
mass = 1,
streamline = 0.1
}
local magic_w = {
tire = 0.0,
floor = 0.0,
tire = 0.01,
floor = 0.1,
forward = 0.01,
steer = 0.1,
drag_boost = 0.0,
drag_halt = 0.0,
steer = 0.01,
drag_boost = 0.01,
drag_halt = 0.1,
inertia = 0.0,
centrifugal = 0.0,
centrifugal = 0.1,
dampening = 0.1
}
local debug_index = 1
function world:load(entity, data)
self.e = entity
self.data = data or default
@ -73,14 +71,21 @@ local function rotate_vector(x0, y0, theta)
return vm.vec2(x1, y1)
end
local function forward_velocity(dt, force, mass, velocity, max_speed, min_speed)
local function forward_velocity(dt, force, mass, velocity, max_speed, min_speed, dampening)
if (mass == 0) then
mass = 1
end
local a = force[1] * dt / mass
local new_velocity = velocity[1] + a
if (a == 0 and new_velocity > 0.1) then
new_velocity = velocity[1] - dampening * dt / mass
elseif (a == 0 and new_velocity < -0.1) then
new_velocity = velocity[1] + dampening * dt / mass
elseif (a == 0) then
new_velocity = 0
end
new_velocity = vm.clamp(new_velocity, min_speed, max_speed)
print(new_velocity)
return new_velocity
end
@ -88,7 +93,7 @@ local function orientation_velocity(dt, force, mass, orientation_speed, orientat
if (mass == 0) then
mass = 1
end
local a = force[2] * dt / mass
local a = force[2] / mass
local new_speed = orientation_speed + a
new_speed = vm.clamp(new_speed, min_orientation, max_orientation)
local new_orientation = rotate_vector(orientation[1], orientation[2], new_speed)
@ -115,11 +120,11 @@ function world:update_race(dt)
magic_w.drag_boost, magic_w.drag_halt,
magic_w.inertia, magic_w.centrifugal
)
local f_v = forward_velocity(dt, force, mass, velocity, max_speed, min_speed)
local f_v = forward_velocity(dt, force, mass, velocity, max_speed, min_speed, magic_w.dampening)
local o, o_v = orientation_velocity(dt, force, mass, self.data.orientation_speed, self.data.orientation, self.data.max_orientation_speed, self.data.min_orientation_speed)
self.data.velocity[1] = f_v
self.data.orientation_speed = o_v
-- self.data.orientation_speed = o_v
self.data.orientation = o
-- if (self.data.velocity[1] ~= 0) then
self.data.pos = self.data.pos + o * f_v

View File

@ -31,8 +31,8 @@ local entities_default = {
max_orientation_speed = 1.0,
velocity = vm.vec2(0.0, 0.0),
max_speed = 1.0,
min_speed = -1.0,
max_speed = 0.3,
min_speed = -0.3,
inertia = vm.vec2(0.0, 0.0),
accel = 0.1,

View File

@ -84,7 +84,7 @@ end
local function add_centrifugal(v, mass, inertia_force, weight_centrifugal)
local centrifugal = centrifugal_force(mass, inertia_force[1], inertia_force[2])
local new_v
local new_v = v
if (v[2] > 0) then
new_v = v - vm.vec2(0.0, 1.0) * centrifugal * (weight_centrifugal or 1.0)
elseif (v[2] < 0) then
@ -140,12 +140,12 @@ local function force_workflow(
weight_centrifugal
)
local val = add_desire(desire_forward, desire_steer, weight_forward, weight_steer)
if (val[1] ~= 0 and val[2] ~= 0) then
-- if (val[1] ~= 0 and val[2] ~= 0) then
val = add_friction(val, grip, friction, weight_tire, weight_floor)
val = add_drag(val, drag, streamline, weight_drag_boost)
val = add_drag(val, drag_movement, streamline, weight_drag_halt)
val = add_centrifugal(val, mass, inertia_force, weight_centrifugal)
end
-- end
val = add_inertia(val, inertia_force, weight_inertia)
return val
end