Skip to content

Minor bone and ragdoll E2 fixes #2896

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

Merged
merged 4 commits into from
Dec 23, 2023
Merged
Show file tree
Hide file tree
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
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()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do E2 arrays support 0 index?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also should _origina be documented

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They can use them but they won't work on foreach. This is a table anyway. Numerical indices are already being used for the bones themselves.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also should _origina be documented

It's kind of an internal thing but I guess I could. Somewhere.

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