Minor bone and ragdoll E2 fixes (#2896)

* 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
This commit is contained in:
Denneisk 2023-12-23 18:41:42 -05:00 committed by GitHub
parent 95de2a81cc
commit 0653171f96
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 10 deletions

View File

@ -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
@ -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

View File

@ -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"

View File

@ -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)
@ -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
@ -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)
@ -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)