Skip to content

Commit

Permalink
Minor bone and ragdoll E2 fixes (#2896)
Browse files Browse the repository at this point in the history
* Fix GetBones would not fully set up
Fix ragdollSetPose(table, number) with 0 would not rotate properly

* E2 Descriptions

* Fix nil table count
Add dynamic perf

* Change GetBones
  • Loading branch information
Denneisk authored Dec 23, 2023
1 parent 95de2a8 commit 0653171
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
12 changes: 5 additions & 7 deletions lua/entities/gmod_wire_expression2/core/bone.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ local Clamp = math.Clamp

local rad2deg = 180 / math.pi

function getBone(entity, index)
local function getBone(entity, index)
if not entity2bone[entity] then entity2bone[entity] = {} end
local bone = entity2bone[entity][index]
if not bone then
Expand All @@ -47,13 +47,11 @@ end
E2Lib.getBone = getBone

local function GetBones(entity)
if not entity2bone[entity] then
entity2bone[entity] = {}
for i = 0, entity:GetPhysicsObjectCount() - 1 do
getBone(entity, i)
end
local bone_count = entity:GetPhysicsObjectCount()
for i = 0, bone_count - 1 do
getBone(entity, i)
end
return entity2bone[entity] or { }
return entity2bone[entity] or {}
end
E2Lib.GetBones = GetBones

Expand Down
3 changes: 3 additions & 0 deletions lua/entities/gmod_wire_expression2/core/custom/cl_prop.lua
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ E2Helper.Descriptions["setPos(b:v)"] = "Sets the position of a bone."
E2Helper.Descriptions["setAng(b:a)"] = "Set the rotation of a bone."
E2Helper.Descriptions["ragdollSetPos(e:v)"] = "Sets the position of a ragdoll while preserving pose."
E2Helper.Descriptions["ragdollSetAng(e:a)"] = "Set the rotation of a ragdoll while preserving pose."
E2Helper.Descriptions["ragdollGetPose(e:)"] = "Gets a specially built table containing the pose of the ragdoll. See ragdollSetPose."
E2Helper.Descriptions["ragdollSetPose(e:t)"] = "Sets the pose of a ragdoll while preserving position and angle. See ragdollGetPose."
E2Helper.Descriptions["ragdollSetPose(e:tn)"] = "Sets the pose of a ragdoll while preserving position. Setting rotate to 0 makes the ragdoll use the pose's original angle. See ragdollGetPose."
E2Helper.Descriptions["setEyeTarget(e:v)"] = "For NPCs, sets the eye target to the world position. For ragdolls, sets the eye target to the local eye position"
E2Helper.Descriptions["setEyeTargetLocal(e:v)"] = "Sets the eye target to the local eye position"
E2Helper.Descriptions["setEyeTargetWorld(e:v)"] = "Sets the eye target to the world position"
Expand Down
14 changes: 11 additions & 3 deletions lua/entities/gmod_wire_expression2/core/custom/prop.lua
Original file line number Diff line number Diff line change
Expand Up @@ -706,8 +706,8 @@ e2function table entity:ragdollGetPose()
local pose = E2Lib.newE2Table()
local bones = GetBones(this)
local originPos, originAng = bones[0]:GetPos(), bones[0]:GetAngles()
local size = 0

-- We want to skip bone 0 as that will be the reference point
for k, bone in pairs(bones) do
local value = E2Lib.newE2Table()
local pos, ang = WorldToLocal(bone:GetPos(), bone:GetAngles(), originPos, originAng)
Expand All @@ -720,9 +720,12 @@ e2function table entity:ragdollGetPose()

pose.n[k] = value
pose.ntypes[k] = "t"
size = size + 1
end

pose.size = #pose.n
pose.stypes._origina = "a"
pose.s._origina = bones[0]:GetAngles()
pose.size = size + 1
return pose
end

Expand All @@ -734,9 +737,12 @@ e2function void entity:ragdollSetPose(table pose, rotate)
if rotate ~= 0 then
originAng = bones[0]:GetAngles()
else
originAng = this:GetForward():Angle()
local stype = pose.stypes._origina
originAng = stype and stype == "a" and pose.s._origina or angle_zero
end

self.prf = self.prf + pose.size * 2

for k, v in pairs(pose.n) do
local pos, ang = LocalToWorld(v.n[1], v.n[2], originPos, originAng)
setAng(bones[k], ang)
Expand All @@ -753,6 +759,8 @@ e2function void entity:ragdollSetPose(table pose)
local bones = GetBones(this)
local originPos, originAng = bones[0]:GetPos(), bones[0]:GetAngles() -- Rotate by default.

self.prf = self.prf + pose.size * 2

for k, v in pairs(pose.n) do
local pos, ang = LocalToWorld(v.n[1], v.n[2], originPos, originAng)
setAng(bones[k], ang)
Expand Down

0 comments on commit 0653171

Please sign in to comment.