Update Towbar Height

This commit is contained in:
2026-02-12 11:07:04 -05:00
parent 34b8f09533
commit 14f7b7bd4a

View File

@@ -8,6 +8,21 @@ TowBarMod.Utils.tempVector2 = Vector3f.new()
--- Util functions --- Util functions
--------------------------------------------------------------------------- ---------------------------------------------------------------------------
--- Compute the attachment Y offset for a vehicle so the towbar sits just
--- above the wheels (i.e. a fixed distance off the ground) regardless of
--- how the vehicle model is configured.
local function computeAttachmentHeight(vehicle)
local script = vehicle:getScript()
if not script then return -0.5 end
local wheelCount = script:getWheelCount()
if wheelCount > 0 then
return script:getWheel(0):getOffset():y() + 0.1
end
return -0.5
end
function TowBarMod.Utils.isTrailer(vehicle) function TowBarMod.Utils.isTrailer(vehicle)
return string.match(string.lower(vehicle:getScript():getName()), "trailer") return string.match(string.lower(vehicle:getScript():getName()), "trailer")
end end
@@ -88,6 +103,19 @@ function TowBarMod.Utils.updateAttachmentsForRigidTow(towingVehicle, towedVehicl
towedAttachment:setUpdateConstraint(false) towedAttachment:setUpdateConstraint(false)
towedAttachment:setZOffset(0) towedAttachment:setZOffset(0)
-- Dynamic height: compute Y from wheel offset so the towbar never clips the floor.
local towingHeight = computeAttachmentHeight(towingVehicle)
local towedHeight = computeAttachmentHeight(towedVehicle)
-- Store and update the towing vehicle's attachment Y.
local towingModData = towingVehicle:getModData()
if towingModData["towBarOriginalTowingOffsetY"] == nil then
towingModData["towBarOriginalTowingOffsetY"] = towingAttachment:getOffset():y()
towingModData["towBarOriginalTowingAttachmentId"] = attachmentA
end
local towingOffset = towingAttachment:getOffset()
towingAttachment:getOffset():set(towingOffset:x(), towingHeight, towingOffset:z())
local towedModData = towedVehicle:getModData() local towedModData = towedVehicle:getModData()
local spacingDistance = 1.0 local spacingDistance = 1.0
if TowBarMod.Config and tonumber(TowBarMod.Config.rigidTowbarDistance) ~= nil then if TowBarMod.Config and tonumber(TowBarMod.Config.rigidTowbarDistance) ~= nil then
@@ -114,22 +142,35 @@ function TowBarMod.Utils.updateAttachmentsForRigidTow(towingVehicle, towedVehicl
local zDirection = baseZ >= 0 and 1 or -1 local zDirection = baseZ >= 0 and 1 or -1
local zShift = zDirection * spacingDistance local zShift = zDirection * spacingDistance
towedAttachment:getOffset():set(baseX, baseY, baseZ + zShift) towedAttachment:getOffset():set(baseX, towedHeight, baseZ + zShift)
towedModData["isChangedTowedAttachment"] = true towedModData["isChangedTowedAttachment"] = true
towedModData["towBarChangedAttachmentId"] = attachmentB towedModData["towBarChangedAttachmentId"] = attachmentB
towedModData["towBarChangedOffsetZShift"] = zShift towedModData["towBarChangedOffsetZShift"] = zShift
towedVehicle:transmitModData() towedVehicle:transmitModData()
towingVehicle:transmitModData()
end end
function TowBarMod.Utils.updateAttachmentsOnDefaultValues(towingVehicle, towedVehicle) function TowBarMod.Utils.updateAttachmentsOnDefaultValues(towingVehicle, towedVehicle)
local towingAttachmentId = towingVehicle:getTowAttachmentSelf() local towingModData = towingVehicle:getModData()
local towingAttachmentId = towingModData["towBarOriginalTowingAttachmentId"]
or towingVehicle:getTowAttachmentSelf()
local towingAttachment = towingVehicle:getScript():getAttachmentById(towingAttachmentId) local towingAttachment = towingVehicle:getScript():getAttachmentById(towingAttachmentId)
if towingAttachment ~= nil then if towingAttachment ~= nil then
towingAttachment:setUpdateConstraint(true) towingAttachment:setUpdateConstraint(true)
local zOffset = (towingAttachmentId == "trailer") and -1 or 1 local zOffset = (towingAttachmentId == "trailer") and -1 or 1
towingAttachment:setZOffset(zOffset) towingAttachment:setZOffset(zOffset)
-- Restore the original Y offset that was overridden by dynamic height.
local originalY = tonumber(towingModData["towBarOriginalTowingOffsetY"])
if originalY ~= nil then
local off = towingAttachment:getOffset()
towingAttachment:getOffset():set(off:x(), originalY, off:z())
end end
end
towingModData["towBarOriginalTowingOffsetY"] = nil
towingModData["towBarOriginalTowingAttachmentId"] = nil
towingVehicle:transmitModData()
local towedModData = towedVehicle:getModData() local towedModData = towedVehicle:getModData()
local changedAttachmentId = towedModData["towBarChangedAttachmentId"] or towedVehicle:getTowAttachmentSelf() local changedAttachmentId = towedModData["towBarChangedAttachmentId"] or towedVehicle:getTowAttachmentSelf()