From da9030fa90d188e245051f976ad236726061afe8 Mon Sep 17 00:00:00 2001 From: fnicon Date: Sat, 21 Mar 2026 11:18:58 +0900 Subject: [PATCH] putting together magic_number, fix friction, add magic boost and magic slowdown --- .../love_src/src/world/top_down_race/init.lua | 4 +- .../world/top_down_race/system/velocity.lua | 74 ++++++++++++++++--- 2 files changed, 66 insertions(+), 12 deletions(-) diff --git a/game/love_src/src/world/top_down_race/init.lua b/game/love_src/src/world/top_down_race/init.lua index b770e51..aaa5998 100644 --- a/game/love_src/src/world/top_down_race/init.lua +++ b/game/love_src/src/world/top_down_race/init.lua @@ -32,10 +32,10 @@ function wrapper:load(_args) assemblage = racer.assemble, data = { accel = 20.0, - brake = 1.0, + brake = 10.0, grip = 1.0, max_speed = 50.0, - steer = 1.0, + steer = 5.0, velocity = 0.0, inertia = {0.0, 0.0}, drag = 1.0, diff --git a/game/love_src/src/world/top_down_race/system/velocity.lua b/game/love_src/src/world/top_down_race/system/velocity.lua index f40bb10..b3abbb9 100644 --- a/game/love_src/src/world/top_down_race/system/velocity.lua +++ b/game/love_src/src/world/top_down_race/system/velocity.lua @@ -144,20 +144,50 @@ local function grip_force(grip, friction) return friction * grip end -local function add_friction(force, grip, friction, tire_c, floor_c) +local function add_friction(force, friction_direction, grip, friction, tire_c, floor_c) local f_grip = grip_force(grip, friction) if (force > 0) then -- tire - floor - force = force + tire_c * f_grip - floor_c * f_grip + force = force + friction_direction * tire_c * f_grip - friction_direction * floor_c * f_grip elseif (force < 0) then - force = force - tire_c * f_grip + floor_c * f_grip + force = force + friction_direction * tire_c * f_grip - friction_direction * floor_c * f_grip else force = force end return force end -local function add_drag(force, drag) +local function add_friction_lateral(force, grip, friction, tire_c, floor_c) + local f_grip = grip_force(grip, friction) + if (force > 0) then + -- tire - floor + force = force + tire_c * f_grip - floor_c * f_grip + if (force < 0) then + force = 0.1 + end + elseif (force < 0) then + force = force - tire_c * f_grip + floor_c * f_grip + if (force > 0) then + force = -0.1 + end + else + force = force + end + return force +end + +local function add_drag(force, drag_direction, drag) + if (force > 0) then + force = force + drag_direction * drag + elseif (force < 0) then + force = force + drag_direction * drag + else + force = force + end + return force +end + +local function add_drag_lateral(force, drag) if (force > 0) then force = force - drag elseif (force < 0) then @@ -201,6 +231,16 @@ local function get_friction(pos_x, pos_y, floors) return friction end +local magic_n = { + tire_c = 1.0, + floor_c = 1.5, + scale_x = 100000.0, + scale_y = 100000.0, + centrifugal_r = 1.0, + magic_slowdown = 10.0, + magic_boost = 5000.0 +} + function system:update(dt) for _, e in ipairs(self.pool) do local accel, brake, grip, max_speed, steer, velocity, drag, inertia, mass = get(e) @@ -217,18 +257,32 @@ function system:update(dt) local f_forward = accel_force(up, down, accel, brake, inertia[1]) local f_lateral = steer_force(left, right, steer, inertia[2]) - f_forward = add_friction(f_forward, grip, friction, 1.0, 0.5) - f_lateral = add_friction(f_lateral, grip, friction, 1.0, 0.5) + if (not up) then + f_forward = f_forward - magic_n.magic_slowdown + elseif (up and velocity == 0) then + f_forward = f_forward + magic_n.magic_boost + end - f_forward = add_drag(f_forward, drag) - f_lateral = add_drag(f_lateral, drag) + if (velocity > 0) then + f_forward = add_friction(f_forward, 1.0, grip, friction, magic_n.tire_c, magic_n.floor_c) + elseif (velocity < 0) then + f_forward = add_friction(f_forward, -1.0, grip, friction, magic_n.tire_c, magic_n.floor_c) + end + f_lateral = add_friction_lateral(f_lateral, grip, friction, magic_n.tire_c, magic_n.floor_c) + + if (velocity > 0) then + f_forward = add_drag(f_forward, -1.0, drag) + elseif (velocity < 0) then + f_forward = add_drag(f_forward, 1.0, drag) + end + f_lateral = add_drag_lateral(f_lateral, drag) if (mass == 0) then mass = 1 end local new_velocity = racing_phy.accelerate(dt, 1, velocity, max_speed, f_forward*dt/mass, 0) - local centrifugal = racing_phy.centrifugal_force(dt, mass, new_velocity, max_speed) + local centrifugal = racing_phy.centrifugal_force(dt, mass, new_velocity * magic_n.centrifugal_r, max_speed) f_lateral = add_centrifugal(f_lateral, centrifugal) e[race.dict.inertia].data = {f_forward * dt, f_lateral * dt} @@ -237,7 +291,7 @@ function system:update(dt) e["race.angle"].data = new_angle e[race.dict.velocity].data = new_velocity - local goal_x, goal_y = racing_phy.drift(dt, pos_x, pos_y, new_velocity, max_speed, new_angle, 100000, 100000) + local goal_x, goal_y = racing_phy.drift(dt, pos_x, pos_y, new_velocity, max_speed, new_angle, magic_n.scale_x, magic_n.scale_y) -- print("force_forward", f_forward, "f_lateral", f_lateral, "velocity", new_velocity, "angle", new_angle, "goal_x", goal_x, "goal_y", goal_y) e["race.pos"].data[1] = goal_x