Skip to content

Commit

Permalink
Make mobility links into objects, also fix to obscure gearbox bug
Browse files Browse the repository at this point in the history
What it says on the tin, making a step into a mobility rewrite more accessible
As of right now they still function the same as before, but they are more centralized and at least a little bit easier to understand; these have accessor functions to modify certain parts of the links as well as obtain them
Eventually it might be a good idea to solely use these to transfer torque throughout the drivetrain as opposed to doing it per gearbox, and these have debugoverlay drawings to represent that transfer

Also a sort of bandaid fix for a bigger issue when it comes to splitting power from an engine to multiple gearboxes, only to then rejoin them afterwards to one wheel
  • Loading branch information
LiddulBOFH committed Oct 13, 2024
1 parent 5834296 commit 54b5b63
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 17 deletions.
47 changes: 47 additions & 0 deletions lua/acf/mobility/objects_sv/link.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
local ACF = ACF
local Meta = {}
local String = "Link [Source = %s, Target = %s, Origin = %s, Axis = %s]"
local Debug = ACF.Debug
local Objects = ACF.Mobility.Objects

function Objects.Link(Source, Target)
local Link = {
Vel = 0,
ReqTq = 0,

Source = Source,
Target = Target,
Origin = Vector(),
TargetPos = Vector(),
Axis = Vector(1, 0, 0),
IsGearbox = Target.IsACFGearbox or false
}

setmetatable(Link, Meta)

return Link
end

local red = Color(255, 0, 0)
local orange = Color(255, 127, 0)
function Meta:Transfer( Torque )
local P1 = self.Source:LocalToWorld(self.Origin)
local P2 = self.Target:LocalToWorld(self.TargetPos)
Debug.Line(P1, P2, 0.05, self.IsGearbox and red or orange, true)
Debug.Text(LerpVector(0.5, P1, P2), math.Round(Torque, 1) .. " Nm", 0.05, false)


end

function Meta:ToString()
return String:format(self.Source, self.Target, self.Origin, self.Axis)
end

AccessorFunc(Meta, "Source", "Source")
AccessorFunc(Meta, "Target", "Target")
AccessorFunc(Meta, "Origin", "Origin", FORCE_VECTOR)
AccessorFunc(Meta, "Axis", "Axis", FORCE_VECTOR)
AccessorFunc(Meta, "TargetPos", "TargetPos", FORCE_VECTOR)

Meta.__index = Meta
Meta.__tostring = Meta.ToString
16 changes: 11 additions & 5 deletions lua/entities/acf_engine/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ AddCSLuaFile("shared.lua")
include("shared.lua")

local ACF = ACF
local Mobility = ACF.Mobility
local MobilityObj = Mobility.Objects
local MaxDistance = ACF.LinkDistance * ACF.LinkDistance

--===============================================================================================--
Expand Down Expand Up @@ -69,11 +71,14 @@ do
Rope = constraint.CreateKeyframeRope(OutPos, 1, "cable/cable2", nil, Engine, Engine.Out, 0, Target, Target.In, 0)
end

local Link = {
Rope = Rope,
RopeLen = (OutPos - InPos):Length(),
ReqTq = 0
}
local Link = MobilityObj.Link(Engine, Target)

Link:SetOrigin(Engine.Out)
Link:SetTargetPos(Target.In)
Link:SetAxis(Direction)

Link.Rope = Rope
Link.RopeLen = (OutPos - InPos):Length()

Engine.Gearboxes[Target] = Link
Target.Engines[Engine] = true
Expand Down Expand Up @@ -781,6 +786,7 @@ function ENT:CalcRPM(SelfTbl)

-- Split the torque fairly between the gearboxes who need it
for Ent, Link in pairs(BoxesTbl) do
Link:Transfer(Link.ReqTq * AvailRatio * MassRatio)
Ent:Act(Link.ReqTq * AvailRatio * MassRatio, DeltaTime, MassRatio)
end
end
Expand Down
31 changes: 19 additions & 12 deletions lua/entities/acf_gearbox/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ include("shared.lua")

local ACF = ACF
local Contraption = ACF.Contraption
local Mobility = ACF.Mobility
local MobilityObj = Mobility.Objects
local Utilities = ACF.Utilities
local Clock = Utilities.Clock
local Clamp = math.Clamp
local abs = math.abs
local min = math.min
local max = math.max
local HookRun = hook.Run
local MaxDistance = ACF.LinkDistance * ACF.LinkDistance

Expand Down Expand Up @@ -158,7 +161,7 @@ do -- Spawn and Update functions -----------------------
if not next(Ropes) then return end

for Ent, Link in pairs(Ropes) do
local OutPos = Entity:LocalToWorld(Link.Output)
local OutPos = Entity:LocalToWorld(Link:GetOrigin())
local InPos = Ent.In and Ent:LocalToWorld(Ent.In) or Ent:GetPos()

-- make sure it is not stretched too far
Expand All @@ -168,7 +171,7 @@ do -- Spawn and Update functions -----------------------
end

-- make sure the angle is not excessive
local DrvAngle = (OutPos - InPos):GetNormalized():Dot((Entity:GetRight() * Link.Output.y):GetNormalized())
local DrvAngle = (OutPos - InPos):GetNormalized():Dot((Entity:GetRight() * Link:GetOrigin().y):GetNormalized())

if DrvAngle < 0.7 then
Entity:Unlink(Ent)
Expand Down Expand Up @@ -573,15 +576,15 @@ do -- Linking ------------------------------------------
local Phys = Target:GetPhysicsObject()
local Axis = Phys:WorldToLocalVector(Entity:GetRight())

return {
Side = Side,
Axis = Axis,
Rope = Rope,
RopeLen = (OutPosWorld - InPosWorld):Length(),
Output = OutPos,
ReqTq = 0,
Vel = 0
}
local Link = MobilityObj.Link(Entity, Target)
Link:SetOrigin(OutPos)
Link:SetTargetPos(InPos)
Link:SetAxis(Axis)
Link.Side = Side
Link.Rope = Rope
Link.RopeLen = (OutPosWorld - InPosWorld):Length()

return Link
end

local function LinkWheel(Gearbox, Wheel)
Expand Down Expand Up @@ -794,6 +797,8 @@ do -- Movement -----------------------------------------
local RClutch = SelfTbl.RClutch
local GearRatio = SelfTbl.GearRatio

if GearRatio == 0 then return 0 end

for Ent, Link in pairs(SelfTbl.GearboxOut) do
local Clutch = Link.Side == 0 and LClutch or RClutch

Expand Down Expand Up @@ -832,7 +837,7 @@ do -- Movement -----------------------------------------
if Link.Side == 0 then
Multiplier = min(0, Rate) + 1
else
Multiplier = -math.max(0, Rate) + 1
Multiplier = -max(0, Rate) + 1
end
end

Expand Down Expand Up @@ -872,6 +877,7 @@ do -- Movement -----------------------------------------
end

for Ent, Link in pairs(self.GearboxOut) do
Link:Transfer(Link.ReqTq * AvailTq)
Ent:Act(Link.ReqTq * AvailTq, DeltaTime, MassRatio)
end

Expand All @@ -883,6 +889,7 @@ do -- Movement -----------------------------------------
local WheelTorque = Link.ReqTq * AvailTq
ReactTq = ReactTq + WheelTorque

Link:Transfer(WheelTorque)
ActWheel(Link, Ent, WheelTorque, DeltaTime)
end
end
Expand Down

0 comments on commit 54b5b63

Please sign in to comment.