Skip to content

Commit 760dc97

Browse files
authored
Add entities.setRagdollPos (#2226)
* Add entities.transformSkeleton * Fix the nil case * rename to setRagdollPos
1 parent 412f504 commit 760dc97

File tree

1 file changed

+40
-2
lines changed

1 file changed

+40
-2
lines changed

lua/starfall/libs_sv/entities.lua

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,14 +194,14 @@ local checkvector = SF.CheckVector
194194

195195
return function(instance)
196196
local checkpermission = instance.player ~= SF.Superuser and SF.Permissions.check or function() end
197-
local Ent_AddCallback,Ent_DrawShadow,Ent_Extinguish,Ent_Fire,Ent_GetChildren,Ent_GetClass,Ent_GetCreationID,Ent_GetForward,Ent_GetFriction,Ent_GetMoveType,Ent_GetParent,Ent_GetPhysicsObject,Ent_GetRight,Ent_GetTable,Ent_GetUp,Ent_GetVar,Ent_Ignite,Ent_IsConstraint,Ent_IsPlayer,Ent_IsPlayerHolding,Ent_IsScripted,Ent_IsValid,Ent_IsVehicle,Ent_IsWorld,Ent_OBBMaxs,Ent_OBBMins,Ent_PhysicsInit,Ent_PhysicsInitSphere,Ent_Remove,Ent_RemoveCallback,Ent_SetAngles,Ent_SetCollisionBounds,Ent_SetCollisionGroup,Ent_SetElasticity,Ent_SetFriction,Ent_SetLightingOriginEntity,Ent_SetLocalAngles,Ent_SetLocalPos,Ent_SetMoveType,Ent_SetNotSolid,Ent_SetPreventTransmit,Ent_SetPos,Ent_SetSolid,Ent_SetVelocity,Ent_TestPVS,Ent_Use = ENT_META.AddCallback,ENT_META.DrawShadow,ENT_META.Extinguish,ENT_META.Fire,ENT_META.GetChildren,ENT_META.GetClass,ENT_META.GetCreationID,ENT_META.GetForward,ENT_META.GetFriction,ENT_META.GetMoveType,ENT_META.GetParent,ENT_META.GetPhysicsObject,ENT_META.GetRight,ENT_META.GetTable,ENT_META.GetUp,ENT_META.GetVar,ENT_META.Ignite,ENT_META.IsConstraint,ENT_META.IsPlayer,ENT_META.IsPlayerHolding,ENT_META.IsScripted,ENT_META.IsValid,ENT_META.IsVehicle,ENT_META.IsWorld,ENT_META.OBBMaxs,ENT_META.OBBMins,ENT_META.PhysicsInit,ENT_META.PhysicsInitSphere,ENT_META.Remove,ENT_META.RemoveCallback,ENT_META.SetAngles,ENT_META.SetCollisionBounds,ENT_META.SetCollisionGroup,ENT_META.SetElasticity,ENT_META.SetFriction,ENT_META.SetLightingOriginEntity,ENT_META.SetLocalAngles,ENT_META.SetLocalPos,ENT_META.SetMoveType,ENT_META.SetNotSolid,ENT_META.SetPreventTransmit,ENT_META.SetPos,ENT_META.SetSolid,ENT_META.SetVelocity,ENT_META.TestPVS,ENT_META.Use
198197
local function Ent_IsNPC(ent) return dgetmeta(ent)==NPC_META end
199198
local function Ent_IsPlayer(ent) return dgetmeta(ent)==PLY_META end
200199
local function Ent_IsVehicle(ent) return dgetmeta(ent)==VEH_META end
201200
local function Ent_IsWeapon(ent) return dgetmeta(ent)==WEP_META end
202201

202+
local Ent_AddCallback,Ent_DrawShadow,Ent_Extinguish,Ent_Fire,Ent_GetAngles,Ent_GetChildren,Ent_GetClass,Ent_GetCreationID,Ent_GetForward,Ent_GetFriction,Ent_GetMoveType,Ent_GetParent,Ent_GetPhysicsObject,Ent_GetPhysicsObjectCount,Ent_GetPhysicsObjectNum,Ent_GetPos,Ent_GetRight,Ent_GetTable,Ent_GetUp,Ent_GetVar,Ent_Ignite,Ent_IsConstraint,Ent_IsPlayerHolding,Ent_IsScripted,Ent_IsValid,Ent_IsWorld,Ent_OBBMaxs,Ent_OBBMins,Ent_PhysicsInit,Ent_PhysicsInitSphere,Ent_Remove,Ent_RemoveCallback,Ent_SetAngles,Ent_SetCollisionBounds,Ent_SetCollisionGroup,Ent_SetElasticity,Ent_SetFriction,Ent_SetLightingOriginEntity,Ent_SetLocalAngles,Ent_SetLocalPos,Ent_SetMoveType,Ent_SetNotSolid,Ent_SetPos,Ent_SetPreventTransmit,Ent_SetSolid,Ent_SetVelocity,Ent_TestPVS,Ent_Use = ENT_META.AddCallback,ENT_META.DrawShadow,ENT_META.Extinguish,ENT_META.Fire,ENT_META.GetAngles,ENT_META.GetChildren,ENT_META.GetClass,ENT_META.GetCreationID,ENT_META.GetForward,ENT_META.GetFriction,ENT_META.GetMoveType,ENT_META.GetParent,ENT_META.GetPhysicsObject,ENT_META.GetPhysicsObjectCount,ENT_META.GetPhysicsObjectNum,ENT_META.GetPos,ENT_META.GetRight,ENT_META.GetTable,ENT_META.GetUp,ENT_META.GetVar,ENT_META.Ignite,ENT_META.IsConstraint,ENT_META.IsPlayerHolding,ENT_META.IsScripted,ENT_META.IsValid,ENT_META.IsWorld,ENT_META.OBBMaxs,ENT_META.OBBMins,ENT_META.PhysicsInit,ENT_META.PhysicsInitSphere,ENT_META.Remove,ENT_META.RemoveCallback,ENT_META.SetAngles,ENT_META.SetCollisionBounds,ENT_META.SetCollisionGroup,ENT_META.SetElasticity,ENT_META.SetFriction,ENT_META.SetLightingOriginEntity,ENT_META.SetLocalAngles,ENT_META.SetLocalPos,ENT_META.SetMoveType,ENT_META.SetNotSolid,ENT_META.SetPos,ENT_META.SetPreventTransmit,ENT_META.SetSolid,ENT_META.SetVelocity,ENT_META.TestPVS,ENT_META.Use
203203
local Ply_InVehicle = PLY_META.InVehicle
204-
local Phys_AddAngleVelocity,Phys_AddVelocity,Phys_ApplyForceCenter,Phys_ApplyForceOffset,Phys_ApplyTorqueCenter,Phys_EnableDrag,Phys_EnableGravity,Phys_EnableMotion,Phys_GetAngleVelocity,Phys_GetMass,Phys_GetMaterial,Phys_IsMoveable,Phys_IsValid,Phys_SetContents,Phys_SetInertia,Phys_SetMass,Phys_Wake = PHYS_META.AddAngleVelocity,PHYS_META.AddVelocity,PHYS_META.ApplyForceCenter,PHYS_META.ApplyForceOffset,PHYS_META.ApplyTorqueCenter,PHYS_META.EnableDrag,PHYS_META.EnableGravity,PHYS_META.EnableMotion,PHYS_META.GetAngleVelocity,PHYS_META.GetMass,PHYS_META.GetMaterial,PHYS_META.IsMoveable,PHYS_META.IsValid,PHYS_META.SetContents,PHYS_META.SetInertia,PHYS_META.SetMass,PHYS_META.Wake
204+
local Phys_AddAngleVelocity,Phys_AddVelocity,Phys_ApplyForceCenter,Phys_ApplyForceOffset,Phys_ApplyTorqueCenter,Phys_EnableDrag,Phys_EnableGravity,Phys_EnableMotion,Phys_GetAngleVelocity,Phys_GetAngles,Phys_GetMass,Phys_GetMaterial,Phys_GetPos,Phys_IsMoveable,Phys_IsValid,Phys_SetAngles,Phys_SetContents,Phys_SetInertia,Phys_SetMass,Phys_SetPos,Phys_Wake = PHYS_META.AddAngleVelocity,PHYS_META.AddVelocity,PHYS_META.ApplyForceCenter,PHYS_META.ApplyForceOffset,PHYS_META.ApplyTorqueCenter,PHYS_META.EnableDrag,PHYS_META.EnableGravity,PHYS_META.EnableMotion,PHYS_META.GetAngleVelocity,PHYS_META.GetAngles,PHYS_META.GetMass,PHYS_META.GetMaterial,PHYS_META.GetPos,PHYS_META.IsMoveable,PHYS_META.IsValid,PHYS_META.SetAngles,PHYS_META.SetContents,PHYS_META.SetInertia,PHYS_META.SetMass,PHYS_META.SetPos,PHYS_META.Wake
205205

206206
local owrap, ounwrap = instance.WrapObject, instance.UnwrapObject
207207
local ents_methods, ent_meta, ewrap, eunwrap = instance.Types.Entity.Methods, instance.Types.Entity, instance.Types.Entity.Wrap, instance.Types.Entity.Unwrap
@@ -590,6 +590,44 @@ function ents_methods:setLocalAngles(ang)
590590
Ent_SetLocalAngles(ent, aunwrap1(ang))
591591
end
592592

593+
--- Sets the entity's ragdoll position and angles without modifying the skeleton pose
594+
-- @param Vector? pos New position or use nil to only set angles
595+
-- @param Angle? ang New angles or use nil to only set position
596+
-- @param number? bone The origin bone to use or nil to use the entity origin
597+
function ents_methods:setRagdollPos(pos, ang, bone)
598+
local ent = getent(self)
599+
checkpermission(instance, ent, "entities.setPos")
600+
601+
local nbones = Ent_GetPhysicsObjectCount(ent)
602+
if nbones < 2 then SF.Throw("Expected a ragdoll entity or entity with physics bones!", 2) end
603+
604+
local originpos, originang
605+
if bone ~= nil then
606+
checkluatype(bone, TYPE_NUMBER)
607+
if bone < 0 or bone >= nbones then SF.Throw("Invalid Bone! "..bone, 2) end
608+
local phys = Ent_GetPhysicsObjectNum(ent, bone)
609+
if not Phys_IsValid(phys) then SF.Throw("Invalid Bone! "..bone, 2) end
610+
originpos = Phys_GetPos(phys)
611+
originang = Phys_GetAngles(phys)
612+
else
613+
originpos = Ent_GetPos(ent)
614+
originang = Ent_GetAngles(ent)
615+
end
616+
617+
if pos == nil then pos = originpos else pos = SF.clampPos(vunwrap1(pos)) end
618+
if ang == nil then ang = originang else ang = aunwrap1(ang) end
619+
620+
for i = 0, nbones-1 do
621+
local phys = Ent_GetPhysicsObjectNum(ent, i)
622+
if Phys_IsValid(phys) then
623+
local lpos, lang = WorldToLocal(Phys_GetPos(phys), Phys_GetAngles(phys), originpos, originang)
624+
local npos, nang = LocalToWorld(lpos, lang, pos, ang)
625+
Phys_SetPos(phys, npos)
626+
Phys_SetAngles(phys, nang)
627+
end
628+
end
629+
end
630+
593631
--- Sets the entity's linear velocity. Physics entities, use physobj:setVelocity
594632
-- @param Vector vel New velocity
595633
function ents_methods:setVelocity(vel)

0 commit comments

Comments
 (0)