Skip to content

Instantly share code, notes, and snippets.

@CheezusChrust
Last active March 31, 2022 16:00
Show Gist options
  • Save CheezusChrust/e9f9834afb626ee8766761477f0958fe to your computer and use it in GitHub Desktop.
Save CheezusChrust/e9f9834afb626ee8766761477f0958fe to your computer and use it in GitHub Desktop.
Simple test to confirm GMod's inertia unit (it's kg*m^2)
if SERVER then
util.AddNetworkString("TorqueTest")
util.AddNetworkString("TorqueTestEntity")
local ply = me --If you aren't running this with luapad set this to your player entity
local e = ents.Create("prop_physics")
e:SetModel("models/props_junk/wood_crate001a.mdl")
e:SetPos(ply:GetPos() + Vector( 0, 0, 100 ))
e:Spawn()
e:SetCollisionGroup(COLLISION_GROUP_WORLD)
if CPPI then e:CPPISetOwner(ply) end
local phys = e:GetPhysicsObject()
phys:EnableDrag(false)
phys:EnableGravity(false)
phys:SetMass(500)
local inertia = phys:GetInertia().z
local lastAngVel = 0
hook.Add("Think", "TorqueTestThink", function()
if not IsValid(e) or not e:IsValid() then
hook.Remove("Think", "TorqueTestThink")
return
end
--39.3701 = 1 meter in units
--Force applied is a total of 10 newtons at a distance of 1 meter from the center of the entity
--Rotation is around the Z axis
phys:ApplyForceOffset(phys:LocalToWorldVector(Vector(0, 1, 0)) * 5 * 39.3701 * FrameTime(), e:LocalToWorld(Vector(39.3701, 0, 0)))
phys:ApplyForceOffset(phys:LocalToWorldVector(Vector(0, -1, 0)) * 5 * 39.3701 * FrameTime(), e:LocalToWorld(Vector(-39.3701, 0, 0)))
local angVel = phys:GetAngleVelocity().z / 57.2958 --Convert to rad/sec
local angAccel = (angVel - lastAngVel) / FrameTime()
lastAngVel = angVel
--Torque (Nm) = Inertia (kg*m^2) * AngularAcceleration (rad/s^2)
--https://www.softschools.com/formulas/physics/torque_formula/59/
local torque = math.abs(inertia * angAccel)
net.Start("TorqueTest")
net.WriteFloat(math.abs(angVel))
net.WriteFloat(math.abs(angAccel))
net.WriteFloat(math.abs(torque))
net.Broadcast()
end)
timer.Simple(0.1, function()
net.Start("TorqueTestEntity")
net.WriteEntity(e)
net.Broadcast()
end)
undo.Create("prop")
undo.AddEntity(e)
undo.SetPlayer(ply)
undo.Finish()
else
local e
local angVel = 0
local angAccel = 0
local torque = 0
net.Receive("TorqueTest", function()
angVel = net.ReadFloat()
angAccel = net.ReadFloat()
torque = net.ReadFloat()
end)
net.Receive("TorqueTestEntity", function()
e = net.ReadEntity()
hook.Add("HUDPaint", "TorqueTestHud", function()
if not e or not e:IsValid() then
hook.Remove("HUDPaint", "TorqueTestHud")
return
end
draw.SimpleText("AngVel: " .. math.Round(angVel, 2) .. " rad/s", "DermaLarge", 32, ScrH() / 2, Color(255, 255, 255, 255), TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER)
draw.SimpleText("AngAccel: " .. math.Round(angAccel, 2) .. " rad/s/s", "DermaLarge", 32, ScrH() / 2 + 32, Color(255, 255, 255, 255), TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER)
draw.SimpleText("Torque: " .. math.Round(torque, 2) .. " Nm / " .. math.Round(torque * 0.737562, 2) .. " ft-lbs", "DermaLarge", 32, ScrH() / 2 + 64, Color(255, 255, 255, 255), TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER)
end)
end)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment