Update racer system
This commit is contained in:
parent
8f67a4de8b
commit
08521535fe
@ -50,12 +50,11 @@ end
|
||||
---@param f vec2 input
|
||||
---@param drag number|vec2 drag
|
||||
---@param streamline number car's streamline
|
||||
---@param weight_drag_boost number? magic weight
|
||||
---@param weight_drag_halt number? magic weight
|
||||
---@param weight_drag number? magic weight
|
||||
---@return vec2 f
|
||||
local function add_drag(v, drag, streamline, weight_drag_boost, weight_drag_halt)
|
||||
local function add_drag(v, drag, streamline, weight_drag)
|
||||
local f_drag = drag_force(drag, streamline)
|
||||
return v + f_drag * ((weight_drag_boost or 1.0) - (weight_drag_halt or 0.0))
|
||||
return v + f_drag * (weight_drag or 1.0)
|
||||
end
|
||||
|
||||
---add inertia into f
|
||||
@ -112,6 +111,7 @@ end
|
||||
---@param grip number car's grip
|
||||
---@param friction number outside's friction
|
||||
---@param drag number|vec2 outside's drag
|
||||
---@param drag_movement number drag from moving
|
||||
---@param streamline number car's streamline
|
||||
---@param inertia_force vec2 last's force
|
||||
---@param mass number car's mass
|
||||
@ -128,7 +128,7 @@ local function force_workflow(
|
||||
-- friction
|
||||
grip, friction,
|
||||
-- drag
|
||||
drag, streamline,
|
||||
drag, drag_movement, streamline,
|
||||
-- inertia
|
||||
inertia_force,
|
||||
-- centrifugal
|
||||
@ -141,7 +141,8 @@ local function force_workflow(
|
||||
)
|
||||
local val = add_desire(desire_forward, desire_steer, weight_forward, weight_steer)
|
||||
val = add_friction(val, grip, friction, weight_tire, weight_floor)
|
||||
val = add_drag(val, drag, streamline, weight_drag_boost, weight_drag_halt)
|
||||
val = add_drag(val, drag, streamline, weight_drag_boost)
|
||||
val = add_drag(val, drag_movement, streamline, weight_drag_halt)
|
||||
val = add_inertia(val, inertia_force, weight_inertia)
|
||||
val = add_centrifugal(val, mass, inertia_force, weight_centrifugal)
|
||||
|
||||
|
||||
@ -1,3 +1,5 @@
|
||||
local vm = require("lib.vornmath")
|
||||
|
||||
local components = {}
|
||||
|
||||
components.dict = {
|
||||
@ -7,16 +9,13 @@ components.dict = {
|
||||
grip = "race.grip",
|
||||
steer = "race.steer",
|
||||
brake = "race.brake",
|
||||
decel = "race.decel",
|
||||
drag = "race.drag",
|
||||
mass = "race.mass",
|
||||
inertia = "race.inertia",
|
||||
inertia_dir = "race.inertia_dir",
|
||||
streamline = "race.streamline"
|
||||
streamline = "race.streamline",
|
||||
}
|
||||
|
||||
function components.velocity (c, x)
|
||||
c.data = x
|
||||
function components.velocity (c, x, y)
|
||||
c.data = vm.vec2(x, y)
|
||||
end
|
||||
|
||||
function components.max_speed (c, x)
|
||||
@ -39,24 +38,12 @@ function components.brake (c, x)
|
||||
c.data = x
|
||||
end
|
||||
|
||||
function components.decel (c, x)
|
||||
c.data = x
|
||||
end
|
||||
|
||||
function components.drag (c, x)
|
||||
c.data = x
|
||||
end
|
||||
|
||||
function components.mass (c, x)
|
||||
c.data = x
|
||||
end
|
||||
|
||||
function components.inertia (c, x)
|
||||
c.data = x
|
||||
end
|
||||
|
||||
function components.inertia_dir (c, x, y)
|
||||
c.data = {x, y}
|
||||
function components.inertia (c, x, y)
|
||||
c.data = vm.vec2(x, y)
|
||||
end
|
||||
|
||||
function components.streamline (c, x)
|
||||
|
||||
@ -31,15 +31,16 @@ function wrapper:load(_args)
|
||||
{
|
||||
assemblage = racer.assemble,
|
||||
data = {
|
||||
accel = 200.0,
|
||||
brake = 100.0,
|
||||
grip = 1.0,
|
||||
max_speed = 50.0,
|
||||
steer = 5.0,
|
||||
velocity = 0.0,
|
||||
velocity = {0.0, 0.0},
|
||||
max_speed = 10.0,
|
||||
|
||||
accel = 10.0,
|
||||
brake = 10.0,
|
||||
grip = 10.0,
|
||||
steer = 10.0,
|
||||
inertia = {0.0, 0.0},
|
||||
drag = 1.0,
|
||||
mass = 1.0
|
||||
mass = 10.0,
|
||||
streamline = 1.0
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
@ -13,30 +13,27 @@ system.__index = system
|
||||
|
||||
system.pool = {
|
||||
pool = {
|
||||
race.dict.max_speed,
|
||||
race.dict.velocity,
|
||||
race.dict.accel,
|
||||
race.dict.brake,
|
||||
race.dict.grip,
|
||||
race.dict.max_speed,
|
||||
race.dict.steer,
|
||||
race.dict.velocity,
|
||||
race.dict.inertia,
|
||||
race.dict.inertia_dir,
|
||||
race.dict.drag,
|
||||
race.dict.mass,
|
||||
race.dict.streamline
|
||||
}
|
||||
}
|
||||
|
||||
system.components = {
|
||||
[race.dict.max_speed] = race.max_speed,
|
||||
[race.dict.velocity] = race.velocity,
|
||||
|
||||
[race.dict.accel] = race.accel,
|
||||
[race.dict.brake] = race.brake,
|
||||
[race.dict.grip] = race.grip,
|
||||
[race.dict.max_speed] = race.max_speed,
|
||||
[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.streamline] = race.streamline
|
||||
}
|
||||
@ -55,16 +52,14 @@ end
|
||||
|
||||
local function get(e)
|
||||
return
|
||||
e[race.dict.max_speed].data,
|
||||
e[race.dict.velocity].data,
|
||||
e[race.dict.accel].data,
|
||||
e[race.dict.brake].data,
|
||||
e[race.dict.grip].data,
|
||||
e[race.dict.max_speed].data,
|
||||
e[race.dict.steer].data,
|
||||
e[race.dict.velocity].data,
|
||||
e[race.dict.drag].data,
|
||||
e[race.dict.inertia].data,
|
||||
e[race.dict.mass].data,
|
||||
e[race.dict.inertia_dir].data,
|
||||
e[race.dict.streamline].data
|
||||
end
|
||||
|
||||
@ -124,6 +119,12 @@ local frictions = {
|
||||
}
|
||||
}
|
||||
|
||||
local drags = {
|
||||
{
|
||||
drag = vm.vec2(1.0, 0.0)
|
||||
}
|
||||
}
|
||||
|
||||
-- x1 y1 -- (x1 + w1) y1
|
||||
-- x2 y2
|
||||
-- x1 (y1 + h1) -- (x1 + w1) (y1 + h1)
|
||||
@ -155,13 +156,12 @@ local magic_w = {
|
||||
drag_halt = 1.5,
|
||||
inertia = 1.0,
|
||||
centrifugal = 1.0,
|
||||
drag_movement = 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")
|
||||
@ -185,19 +185,19 @@ end
|
||||
|
||||
function system:update(dt)
|
||||
for _, e in ipairs(self.pool) do
|
||||
local accel, brake, grip, max_speed, steer, velocity, drag, inertia, mass, inertia_dir, streamline = get(e)
|
||||
|
||||
local max_speed, velocity, accel, brake, grip, steer, inertia, mass, streamline = get(e)
|
||||
|
||||
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 drag = drags[1].drag
|
||||
|
||||
local desire_forward, desire_steer = handle_input(accel, brake, steer)
|
||||
|
||||
local force = racing_force(
|
||||
desire_forward, desire_steer,
|
||||
grip, friction,
|
||||
drag, streamline,
|
||||
drag, magic_w.drag_movement, streamline,
|
||||
inertia,
|
||||
mass,
|
||||
magic_w.forward, magic_w.steer,
|
||||
@ -217,6 +217,8 @@ function system:update(dt)
|
||||
end
|
||||
local a = force * dt / mass
|
||||
|
||||
e[race.dict.velocity].data = e[race.dict.velocity].data + a
|
||||
|
||||
-- local new_velocity = racing_phy.accelerate(dt, 1, velocity, max_speed, f_forward*dt/mass, 0)
|
||||
|
||||
-- if (not left and not right) then
|
||||
@ -235,15 +237,15 @@ function system:update(dt)
|
||||
|
||||
e[race.dict.inertia].data = force
|
||||
|
||||
local new_angle = e["race.angle"].data + f_lateral * dt/mass
|
||||
e["race.angle"].data = new_angle
|
||||
e[race.dict.velocity].data = new_velocity
|
||||
-- local new_angle = e["race.angle"].data + f_lateral * dt/mass
|
||||
-- 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, magic_n.scale_x, magic_n.scale_y)
|
||||
-- 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
|
||||
e["race.pos"].data[2] = goal_y
|
||||
-- e["race.pos"].data[1] = goal_x
|
||||
-- e["race.pos"].data[2] = goal_y
|
||||
end
|
||||
end
|
||||
|
||||
@ -261,8 +263,8 @@ function system:draw()
|
||||
local image = e["race.image"].data.image
|
||||
local origin = e["race.image"].data.origin
|
||||
--draw(velocity, x, y, sx, sy, angle, image, origin)
|
||||
local vx = 0
|
||||
local vy = 0
|
||||
local vx = e[race.dict.velocity].data[1]
|
||||
local vy = e[race.dict.velocity].data[2]
|
||||
test.set_sphere(x, y, vx, vy)
|
||||
end
|
||||
end
|
||||
|
||||
@ -3,26 +3,28 @@ local race = require("love_src.src.world.top_down_race.component.race")
|
||||
local template = {}
|
||||
|
||||
template.default_data = {
|
||||
velocity = {0.0, 0.0},
|
||||
max_speed = 10.0,
|
||||
|
||||
accel = 10.0,
|
||||
brake = 10.0,
|
||||
grip = 10.0,
|
||||
max_speed = 10.0,
|
||||
steer = 10.0,
|
||||
velocity = 0.0,
|
||||
inertia = {0.0, 0.0},
|
||||
drag = 2.0,
|
||||
mass = 10.0
|
||||
mass = 10.0,
|
||||
streamline = 1.0
|
||||
}
|
||||
function template.assemble(e, data)
|
||||
e:give(race.dict.max_speed, data.max_speed)
|
||||
e:give(race.dict.velocity, data.velocity[1], data.velocity[2])
|
||||
|
||||
e:give(race.dict.accel, data.accel)
|
||||
e:give(race.dict.brake, data.brake)
|
||||
e:give(race.dict.grip, data.grip)
|
||||
e:give(race.dict.max_speed, data.max_speed)
|
||||
e:give(race.dict.steer, data.steer)
|
||||
e:give(race.dict.velocity, data.velocity)
|
||||
e:give(race.dict.inertia, data.inertia[1], data.inertia[2])
|
||||
e:give(race.dict.drag, data.drag)
|
||||
e:give(race.dict.mass, data.mass)
|
||||
e:give(race.dict.streamline, data.streamline)
|
||||
end
|
||||
|
||||
return template
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user