Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ragdoll spawn from vehicles being offset #299

Merged
merged 2 commits into from
Jun 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 42 additions & 10 deletions entities/entities/lambda_ragdollmanager.lua
Original file line number Diff line number Diff line change
Expand Up @@ -211,30 +211,60 @@ function ENT:HandleRagdollCollision(ragdoll, data)
end
end

-- HACKHACK: Compensate for issue https://github.com/Facepunch/garrysmod-issues/issues/5894
local function CompensateRotation(ply)
local res = Angle(0, 0, 0)
local vehicle = ply:GetVehicle()
if not IsValid(vehicle) then
return res
end

local vehicleMdl = vehicle:GetModel()
if vehicleMdl == "models/buggy.mdl" then
res = Angle(0, 85, 0)
elseif vehicleMdl == "models/airboat.mdl" then
res = Angle(0, 85, 0)
end

return res
end

local function GetPlayerVelocity(ply)
local res = ply:GetVelocity()
local vehicle = ply:GetVehicle()
if not IsValid(vehicle) then
return res
end
return vehicle:GetVelocity()
end

function ENT:CreateRagdoll(dmgForce, gibPlayer, didExplode)
DbgPrint(self, "CreateRagdoll", dmgForce, gibPlayer, didExplode)
local ent = self:GetOwner()

local ent = self:GetOwner()
if not IsValid(ent) then
DbgPrint("No valid owner for ragdoll manager")

return
end

local mdl = ent:GetModel()

if mdl == nil then
DbgPrint("Player has no valid model for ragdoll manager")

return
end

-- HACKHACK: Compensate for issue https://github.com/Facepunch/garrysmod-issues/issues/5894
local rotation = CompensateRotation(ent)
local vel = GetPlayerVelocity(ent)

local spawnPos = ent:GetPos()

self:RemoveRagdoll()
if gibPlayer == true then return self:GibPlayer(dmgForce, gibPlayer, didExplode) end
local ragdoll = ents.Create("prop_ragdoll")
ragdoll:SetModel(mdl)
ragdoll:SetPos(ent:GetPos())
ragdoll:SetAngles(ent:GetAngles())
ragdoll:SetPos(spawnPos)
ragdoll:SetAngles(Angle(0, 0, 0))
ragdoll:SetColor(ent:GetColor())
ragdoll:SetSkin(ent:GetSkin())

Expand All @@ -253,24 +283,26 @@ function ENT:CreateRagdoll(dmgForce, gibPlayer, didExplode)

ragdoll.LastRagdollImpact = 0
ragdoll:SetNWBool("IsReviving", false)

ragdoll:AddCallback("PhysicsCollide", function(e, data)
if IsValid(self) then
self:HandleRagdollCollision(e, data)
end
end)

local vel = ent:GetVelocity()

local centralPos = ragdoll:GetPos()
for i = 0, ragdoll:GetPhysicsObjectCount() - 1 do
local bone = ragdoll:GetPhysicsObjectNum(i)

if IsValid(bone) then
local bp, ba = ent:GetBonePosition(ragdoll:TranslatePhysBoneToBone(i))

if bp and ba then
local relativePos = bp - centralPos
relativePos:Rotate(rotation)
bp = centralPos + relativePos

bone:SetPos(bp)
bone:SetAngles(ba)
bone:SetAngles(ba + rotation)
end

bone:SetVelocity(vel)
Expand Down