diff --git a/game/love_src/src/world/top_down_race/component/race.lua b/game/love_src/src/world/top_down_race/component/race.lua index 7166826..5033e7f 100644 --- a/game/love_src/src/world/top_down_race/component/race.lua +++ b/game/love_src/src/world/top_down_race/component/race.lua @@ -10,7 +10,9 @@ components.dict = { decel = "race.decel", drag = "race.drag", mass = "race.mass", - inertia = "race.inertia" + inertia = "race.inertia", + inertia_dir = "race.inertia_dir", + streamline = "race.streamline" } function components.velocity (c, x) @@ -49,8 +51,16 @@ function components.mass (c, x) c.data = x end -function components.inertia (c, x, y) +function components.inertia (c, x) + c.data = x +end + +function components.inertia_dir (c, x, y) c.data = {x, y} end +function components.streamline (c, x) + c.data = x +end + return components 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 587468a..16ff825 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 @@ -1,6 +1,7 @@ local system_constructor = require("love_src.wrapper.Concord.system") local race = require("love_src.src.world.top_down_race.component.race") local racing_phy = require("love_src.src.system.racing_phy") +local racing_force = require("love_src.src.system.racing_force") local component = require("love_src.wrapper.Concord.component") @@ -19,8 +20,10 @@ system.pool = { race.dict.steer, race.dict.velocity, race.dict.inertia, + race.dict.inertia_dir, race.dict.drag, - race.dict.mass + race.dict.mass, + race.dict.streamline } } @@ -32,8 +35,10 @@ system.components = { [race.dict.steer] = race.steer, [race.dict.velocity] = race.velocity, [race.dict.inertia] = race.inertia, + [race.dict.inertia_dir] = race.inertia_dir, [race.dict.drag] = race.drag, - [race.dict.mass] = race.mass + [race.dict.mass] = race.mass, + [race.dict.streamline] = race.streamline } function system.new() @@ -58,7 +63,9 @@ local function get(e) e[race.dict.velocity].data, e[race.dict.drag].data, e[race.dict.inertia].data, - e[race.dict.mass].data + e[race.dict.mass].data, + e[race.dict.inertia_dir].data, + e[race.dict.streamline].data end local function draw(text, x, y, sx, sy, angle, image, origin) @@ -117,98 +124,6 @@ local frictions = { } } -local function accel_force(up, down, accel, brake, inertia) - local force - if (up) then - force = accel - inertia - elseif(down) then - force = - brake + inertia - else - force = inertia - end - return force -end - -local function steer_force(left, right, steer, inertia) - if (left) then - force = -steer + inertia - elseif(right) then - force = steer - inertia - else - force = inertia - end - return force -end - -local function grip_force(grip, friction) - return friction * grip -end - -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 + friction_direction * tire_c * f_grip - friction_direction * floor_c * f_grip - elseif (force < 0) then - force = force + friction_direction * tire_c * f_grip - friction_direction * floor_c * f_grip - else - force = force - end - return force -end - -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 - force = force + drag - else - force = force - end - return force -end - -local function add_centrifugal(force, centrifugal) - if (force > 0) then - force = force - centrifugal - elseif (force < 0) then - force = force + centrifugal - else - force = force - end - return force -end - -- x1 y1 -- (x1 + w1) y1 -- x2 y2 -- x1 (y1 + h1) -- (x1 + w1) (y1 + h1) @@ -231,76 +146,94 @@ 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 = 1000.0, - scale_y = 1000.0, - centrifugal_r = 1.0, - magic_slowdown = 10.0, - magic_boost = 10000.0, - magic_slow_drift = 10.0 +local magic_w = { + tire = 1.0, + floor = 1.5, + forward = 1.0, + steer = 1.0, + drag_boost = 1.0, + drag_halt = 1.5, + inertia = 1.0, + centrifugal = 1.0, + -- magic_slowdown = 10.0, + -- magic_boost = 10000.0, + -- magic_slow_drift = 10.0 } +local drag_dir = vm.vec2(1.0, 0.0) + +local function handle_input(accel, brake, steer) + local up = love.keyboard.isDown("up") + local down = love.keyboard.isDown("down") + local left = love.keyboard.isDown("left") + local right = love.keyboard.isDown("right") + + local desire_forward = 0 + local desire_steer = 0 + if (up) then + desire_forward = accel + elseif (down) then + desire_forward = - brake + end + if (left) then + desire_steer = - steer + elseif (right) then + desire_steer = steer + end + return desire_forward, desire_steer +end + function system:update(dt) for _, e in ipairs(self.pool) do - local accel, brake, grip, max_speed, steer, velocity, drag, inertia, mass = get(e) + local accel, brake, grip, max_speed, steer, velocity, drag, inertia, mass, inertia_dir, streamline = get(e) - local up = love.keyboard.isDown("up") - local down = love.keyboard.isDown("down") - local left = love.keyboard.isDown("left") - local right = love.keyboard.isDown("right") local pos_x, pos_y = e["race.pos"].data[1], e["race.pos"].data[2] local friction = get_friction(pos_x, pos_y, frictions) - local f_forward = accel_force(up, down, accel, brake, inertia[1]) - local f_lateral = steer_force(left, right, steer, inertia[2]) + local desire_forward, desire_steer = handle_input(accel, brake, steer) - 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 + local force = racing_force( + desire_forward, desire_steer, + grip, friction, + drag, streamline, + inertia, + mass, + magic_w.forward, magic_w.steer, + magic_w.tire, magic_w.floor, + magic_w.drag_boost, magic_w.drag_halt, + magic_w.inertia, magic_w.centrifugal + ) - 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 (not up) then + -- force = force - magic_n.magic_slowdown + -- elseif (up and velocity == 0) then + -- force = force + magic_n.magic_boost + -- end if (mass == 0) then mass = 1 end + local a = force * dt / mass - 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 * magic_n.centrifugal_r, max_speed) - f_lateral = add_centrifugal(f_lateral, centrifugal) + -- local new_velocity = racing_phy.accelerate(dt, 1, velocity, max_speed, f_forward*dt/mass, 0) - if (not left and not right) then - if (not (left) and f_lateral > 0) then - f_lateral = f_lateral - (magic_n.magic_slow_drift) - if (f_lateral < 0) then - f_lateral = 0 - end - elseif (not right and f_lateral < 0) then - f_lateral = f_lateral + (magic_n.magic_slow_drift) - if (f_lateral > 0) then - f_lateral = 0 - end - end - end + -- if (not left and not right) then + -- if (not (left) and f_lateral > 0) then + -- f_lateral = f_lateral - (magic_n.magic_slow_drift) + -- if (f_lateral < 0) then + -- f_lateral = 0 + -- end + -- elseif (not right and f_lateral < 0) then + -- f_lateral = f_lateral + (magic_n.magic_slow_drift) + -- if (f_lateral > 0) then + -- f_lateral = 0 + -- end + -- end + -- end - e[race.dict.inertia].data = {f_forward * dt, f_lateral * dt} + e[race.dict.inertia].data = force local new_angle = e["race.angle"].data + f_lateral * dt/mass e["race.angle"].data = new_angle