Skip to content

Commit

Permalink
Merge pull request #299 from GMLambda/ragdoll-spawn
Browse files Browse the repository at this point in the history
Fix ragdoll spawn from vehicles being offset
  • Loading branch information
ZehMatt authored Jun 8, 2024
2 parents 41575fc + db6c7eb commit c93a52b
Showing 1 changed file with 42 additions and 10 deletions.
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

0 comments on commit c93a52b

Please sign in to comment.