Untitled conversation
- Userlua
- Assistant
local EntityLastDamage = {}
local DAMAGE_TYPE = {
BULLET = DMG_BULLET,
BLAST = DMG_BLAST,
SLASH = DMG_SLASH,
FIRE = DMG_BURN,
CHEMICAL = DMG_ACID,
EXPLOSION = DMG_BLAST_SURFACE,
FALL = DMG_FALL,
GENERIC = DMG_GENERIC,
}
local CreatedRagdolls = {}
hook.Add("EntityTakeDamage", "StoreEntityLastDamageType", function(target, dmginfo)
if not IsValid(target) then return end
if target:IsPlayer() or target:IsNPC() then
EntityLastDamage[target:EntIndex()] = dmginfo:GetDamageType()
EntityLastDamage[target:EntIndex() .. "_attacker"] = dmginfo:GetAttacker()
end
end)
local function CreateRealisticRagdoll(victim, position, angles, model, dmgType, attacker)
if not IsValid(victim) then return end
local rag = ents.Create("prop_ragdoll")
if not IsValid(rag) then return end
rag:SetModel(model)
rag:SetPos(position)
rag:SetAngles(angles)
rag:SetNWBool("IsCustomRagdoll", true)
rag:Spawn()
local forceDir = Vector(0, 0, 0)
if bit.band(dmgType, DAMAGE_TYPE.BULLET) ~= 0 then
if IsValid(attacker) then
forceDir = (rag:GetPos() - attacker:GetPos()):GetNormalized()
else
forceDir = Vector(0, 0, 1)
end
end
local forceVector = Vector(0, 0, 0)
if bit.band(dmgType, DAMAGE_TYPE.BLAST) ~= 0 or bit.band(dmgType, DAMAGE_TYPE.EXPLOSION) ~= 0 then
forceVector = forceDir * 500 + Vector(0, 0, 300)
elseif bit.band(dmgType, DAMAGE_TYPE.BULLET) ~= 0 then
forceVector = forceDir * 600 + Vector(0, 0, 400)
elseif bit.band(dmgType, DAMAGE_TYPE.FIRE) ~= 0 or bit.band(dmgType, DAMAGE_TYPE.CHEMICAL) ~= 0 then
forceVector = Vector(0, 0, 250)
elseif bit.band(dmgType, DAMAGE_TYPE.SLASH) ~= 0 then
forceVector = forceDir * 400 + Vector(0, 0, 150)
else
forceVector = Vector(0, 0, 200)
end
timer.Simple(0.05, function()
if not IsValid(rag) then return end
for i = 0, rag:GetPhysicsObjectCount() - 1 do
local phys = rag:GetPhysicsObjectNum(i)
if IsValid(phys) then
local variation = Vector(math.Rand(-50, 50), math.Rand(-50, 50), math.Rand(-50, 50))
phys:ApplyForceCenter(forceVector + variation)
end
end
end)
timer.Simple(300, function() if IsValid(rag) then rag:Remove() end end)
end
local function RemoveDefaultRagdoll(pos)
local closestRagdoll = nil
local minDistance = 100
for _, ent in ipairs(ents.FindByClass("prop_ragdoll")) do
if not ent:GetNWBool("IsCustomRagdoll", false) then
local distance = ent:GetPos():Distance(pos)
if distance < minDistance then
closestRagdoll = ent
minDistance = distance
end
end
end
if IsValid(closestRagdoll) then closestRagdoll:Remove() end
end
hook.Add("PlayerDeath", "CreateRealisticCorpse_Player", function(victim, infl, attacker)
if not IsValid(victim) then return end
if CreatedRagdolls[victim:EntIndex()] then return end
CreatedRagdolls[victim:EntIndex()] = true
local model = victim:GetModel()
local pos = victim:GetPos()
local ang = victim:EyeAngles()
local dmgType = EntityLastDamage[victim:EntIndex()] or DAMAGE_TYPE.GENERIC
local lastAttacker = EntityLastDamage[victim:EntIndex() .. "_attacker"]
CreateRealisticRagdoll(victim, pos, ang, model, dmgType, lastAttacker)
timer.Simple(0.1, function()
if not IsValid(victim) then return end
RemoveDefaultRagdoll(pos)
end)
end)
hook.Add("OnNPCKilled", "CreateRealisticCorpse_NPC", function(npc, attacker, inflictor)
if not IsValid(npc) then return end
if CreatedRagdolls[npc:EntIndex()] then return end
CreatedRagdolls[npc:EntIndex()] = true
local model = npc:GetModel()
local pos = npc:GetPos()
local ang = npc:GetAngles()
local dmgType = EntityLastDamage[npc:EntIndex()] or DAMAGE_TYPE.GENERIC
local lastAttacker = EntityLastDamage[npc:EntIndex() .. "_attacker"]
CreateRealisticRagdoll(npc, pos, ang, model, dmgType, lastAttacker)
timer.Simple(0.1, function()
if not IsValid(npc) then return end
RemoveDefaultRagdoll(pos)
end)
end)
hook.Add("OnEntityRemoved", "CleanUpEntityDamageData", function(ent)
if not IsValid(ent) then return end
EntityLastDamage[ent:EntIndex()] = nil
EntityLastDamage[ent:EntIndex() .. "_attacker"] = nil
CreatedRagdolls[ent:EntIndex()] = nil
end)
hook.Add("PlayerDisconnected", "CleanUpPlayerDamageData", function(ply)
if not IsValid(ply) then return end
EntityLastDamage[ply:EntIndex()] = nil
EntityLastDamage[ply:EntIndex() .. "_attacker"] = nil
CreatedRagdolls[ply:EntIndex()] = nil
end)
Улучши физику рэгдоллов, чтобы они были иммерсивнее и лучше.
To enhance the realism and immersion of ragdoll physics in your Garry's Mod script, you can implement several improvements. These enhancements focus on applying more nuanced forces, adding torque for rotational motion, utilizing hit positions to determine force direction, and refining the overall physics behavior of the ragdolls.
Below is the improved version of your script with detailed explanations of the changes made:
local EntityLastDamage = {}
local DAMAGE_TYPE = {
BULLET = DMG_BULLET,
BLAST = DMG_BLAST,
SLASH = DMG_SLASH,
FIRE = DMG_BURN,
CHEMICAL = DMG_ACID,
EXPLOSION = DMG_BLAST_SURFACE,
FALL = DMG_FALL,
GENERIC = DMG_GENERIC,
}
local CreatedRagdolls = {}
hook.Add("EntityTakeDamage", "StoreEntityLastDamageType", function(target, dmginfo)
if not IsValid(target) then return end
if target:IsPlayer() or target:IsNPC() then
EntityLastDamage[target:EntIndex()] = {
dmgType = dmginfo:GetDamageType(),
attacker = dmginfo:GetAttacker(),
damagePos = dmginfo:GetDamagePosition() or target:GetPos()
}
end
end)
local function CreateRealisticRagdoll(victim, position, angles, model, dmgInfo)
if not IsValid(victim) then return end
local rag = ents.Create("prop_ragdoll")
if not IsValid(rag) then return end
rag:SetModel(model)
rag:SetPos(position)
rag:SetAngles(angles)
rag:SetNWBool("IsCustomRagdoll", true)
rag:Spawn()
-- Calculate force direction based on damage position
local forceDir = Vector(0, 0, 1) -- Default upward force
if victim:GetPos() ~= dmgInfo.damagePos then
forceDir = (rag:GetPos() - dmgInfo.damagePos):GetNormalized()
elseif IsValid(dmgInfo.attacker) then
forceDir = (rag:GetPos() - dmgInfo.attacker:GetPos()):GetNormalized()
end
-- Determine force magnitude and type based on damage type
local forceVector = Vector(0, 0, 0)
local torqueVector = Vector(0, 0, 0)
if bit.band(dmgInfo.dmgType, DAMAGE_TYPE.EXPLOSION) ~= 0 then
forceVector = forceDir * 800 + Vector(0, 0, 500)
torqueVector = Vector(math.Rand(-300, 300), math.Rand(-300, 300), math.Rand(-300, 300))
elseif bit.band(dmgInfo.dmgType, DAMAGE_TYPE.BLAST) ~= 0 then
forceVector = forceDir * 700 + Vector(0, 0, 400)
torqueVector = Vector(math.Rand(-250, 250), math.Rand(-250, 250), math.Rand(-250, 250))
elseif bit.band(dmgInfo.dmgType, DAMAGE_TYPE.BULLET) ~= 0 then
forceVector = forceDir * 600 + Vector(0, 0, 450)
torqueVector = Vector(math.Rand(-200, 200), math.Rand(-200, 200), math.Rand(-200, 200))
elseif bit.band(dmgInfo.dmgType, DAMAGE_TYPE.FIRE) ~= 0 or bit.band(dmgInfo.dmgType, DAMAGE_TYPE.CHEMICAL) ~= 0 then
forceVector = Vector(0, 0, 300)
torqueVector = Vector(math.Rand(-150, 150), math.Rand(-150, 150), math.Rand(-150, 150))
elseif bit.band(dmgInfo.dmgType, DAMAGE_TYPE.SLASH) ~= 0 then
forceVector = forceDir * 500 + Vector(0, 0, 350)
torqueVector = Vector(math.Rand(-180, 180), math.Rand(-180, 180), math.Rand(-180, 180))
else
forceVector = Vector(0, 0, 400)
torqueVector = Vector(math.Rand(-100, 100), math.Rand(-100, 100), math.Rand(-100, 100))
end
-- Apply force and torque with a slight random delay for realism
timer.Simple(0.05, function()
if not IsValid(rag) then return end
for i = 0, rag:GetPhysicsObjectCount() - 1 do
local phys = rag:GetPhysicsObjectNum(i)
if IsValid(phys) then
-- Apply central force
local variation = Vector(math.Rand(-30, 30), math.Rand(-30, 30), math.Rand(-30, 30))
phys:ApplyForceCenter(forceVector + variation)
-- Apply torque for rotational movement
phys:AddAngleVelocity(torqueVector)
-- Optional: Adjust individual physics properties for more realism
phys:SetMass(math.Clamp(phys:GetMass(), 5, 100)) -- Ensure mass is within a realistic range
phys:EnableGravity(true)
phys:EnableDrag(true)
end
end
end)
-- Optional: Add collision handling for more interaction
rag:SetCollisionGroup(COLLISION_GROUP_WEAPON)
-- Automatically remove the ragdoll after a longer period for performance
timer.Simple(600, function()
if IsValid(rag) then rag:Remove() end
end)
end
local function RemoveDefaultRagdoll(pos)
local closestRagdoll = nil
local minDistance = 100
for _, ent in ipairs(ents.FindByClass("prop_ragdoll")) do
if not ent:GetNWBool("IsCustomRagdoll", false) then
local distance = ent:GetPos():Distance(pos)
if distance < minDistance then
closestRagdoll = ent
minDistance = distance
end
end
end
if IsValid(closestRagdoll) then closestRagdoll:Remove() end
end
hook.Add("PlayerDeath", "CreateRealisticCorpse_Player", function(victim, infl, attacker)
if not IsValid(victim) then return end
if CreatedRagdolls[victim:EntIndex()] then return end
CreatedRagdolls[victim:EntIndex()] = true
local model = victim:GetModel()
local pos = victim:GetPos()
local ang = victim:EyeAngles()
local dmgInfo = EntityLastDamage[victim:EntIndex()] or { dmgType = DAMAGE_TYPE.GENERIC, attacker = nil, damagePos = pos }
CreateRealisticRagdoll(victim, pos, ang, model, dmgInfo)
timer.Simple(0.1, function()
if not IsValid(victim) then return end
RemoveDefaultRagdoll(pos)
end)
end)
hook.Add("OnNPCKilled", "CreateRealisticCorpse_NPC", function(npc, attacker, inflictor)
if not IsValid(npc) then return end
if CreatedRagdolls[npc:EntIndex()] then return end
CreatedRagdolls[npc:EntIndex()] = true
local model = npc:GetModel()
local pos = npc:GetPos()
local ang = npc:GetAngles()
local dmgInfo = EntityLastDamage[npc:EntIndex()] or { dmgType = DAMAGE_TYPE.GENERIC, attacker = nil, damagePos = pos }
CreateRealisticRagdoll(npc, pos, ang, model, dmgInfo)
timer.Simple(0.1, function()
if not IsValid(npc) then return end
RemoveDefaultRagdoll(pos)
end)
end)
hook.Add("OnEntityRemoved", "CleanUpEntityDamageData", function(ent)
if not IsValid(ent) then return end
EntityLastDamage[ent:EntIndex()] = nil
CreatedRagdolls[ent:EntIndex()] = nil
end)
hook.Add("PlayerDisconnected", "CleanUpPlayerDamageData", function(ply)
if not IsValid(ply) then return end
EntityLastDamage[ply:EntIndex()] = nil
CreatedRagdolls[ply:EntIndex()] = nil
end)
Enhanced Damage Information Storage:
EntityLastDamage table now stores the position where the damage was inflicted (damagePos). This allows for more accurate force direction based on where the entity was hit.dmgType, attacker, and damagePos for better data management.Force Direction Based on Damage Position:
dmgInfo.damagePos). This ensures that ragdolls react more realistically to the impact location.Application of Torque:
ApplyForceCenter), torque (AddAngleVelocity) is now applied to each physics object within the ragdoll. This introduces rotational motion, making the ragdoll tumble and spin naturally.Adjusted Force Magnitudes and Variations:
Physics Properties Refinement:
Collision Group Adjustment:
COLLISION_GROUP_WEAPON to allow it to interact appropriately with the environment and other entities without causing excessive physics calculations.Hitgroup-Based Force Application: For even greater realism, consider determining the specific hitgroup (e.g., head, torso, limbs) where the entity was hit and adjusting the force application accordingly. This can create more varied and believable ragdoll reactions.
Custom Ragdoll Models: If possible, use custom ragdoll models with properly configured physics bones to ensure that the physics simulation behaves as expected.
Physics Constraints: Implementing physics constraints between bones can help maintain the structural integrity of the ragdoll, preventing limbs from unnaturally separating or overlapping.
Environmental Interactions: Enhance interactions between ragdolls and the environment (e.g., collisions with objects, terrain) to add to the immersion.
By incorporating these improvements, your ragdoll physics will exhibit more natural and immersive behaviors, enhancing the overall experience within your game.
Not yet included in a published release.
Extended Ragdoll Lifetime:
Code Cleanup and Optimization:
PlayerDeath and OnNPCKilled hooks to retrieve structured damage information.