Skip to content

Instantly share code, notes, and snippets.

@CheezusChrust
Created June 5, 2021 01:04
Show Gist options
  • Save CheezusChrust/ffea7e5b77af20a5fc8e6b34bd813231 to your computer and use it in GitHub Desktop.
Save CheezusChrust/ffea7e5b77af20a5fc8e6b34bd813231 to your computer and use it in GitHub Desktop.
GForce display for each player when sitting in a seat - place this in autorun
if SERVER then
hook.Add("PlayerEnteredVehicle", "GForce::EnterVehicle", function(ply, veh)
ply.gforceLastPos = veh:GetPos()
ply.gforceLastVel = Vector()
end)
hook.Add("PlayerLeaveVehicle", "GForce::ExitVehicle", function(ply, Veh)
ply.gforceLastPos = nil
ply.gforceLastVel = nil
end)
local gravity = Vector(0, 0, -600)
hook.Add("InitPostEntity", "GForce::Init", function()
gravity = physenv.GetGravity() --physenv.GetGravity returns nil if ran too early
end)
hook.Add("Tick", "GForce::Tick", function()
for _, ply in pairs(player.GetHumans()) do
if ply:GetVehicle() and ply:GetVehicle():IsValid() then
local veh = ply:GetVehicle()
if not ply.gforceLastVel or not ply.gforceLastPos then
ply.gforceLastVel = Vector()
ply.gforceLastPos = veh:GetPos()
end
local vel = (veh:GetPos() - ply.gforceLastPos) / engine.TickInterval()
local gforce = (vel - ply.gforceLastVel) / engine.TickInterval() / gravity[3] - Vector(0, 0, 1)
--To calculate local acceleration relative to the vehicle, calculate the acceleration in world space,
--and then dot each axis with each normalized direction vector of the vehicle, for example the up/down gforce
--would be calculated by gforce:Dot(veh:GetUp())
--This is used for the clientside HUD below
ply.gforceLastVel = vel
ply.gforceLastPos = veh:GetPos()
ply:SetNWFloat("gx", gforce[1])
ply:SetNWFloat("gy", gforce[2])
ply:SetNWFloat("gz", gforce[3])
end
end
end)
else
local pos = {
x = ScrW() * 0.01,
y = ScrH() * 0.5
}
local max = 0
local maxAge = SysTime()
hook.Add("HUDPaint", "GForce::HUD", function()
local ply = LocalPlayer()
if not ply:InVehicle() or not ply:GetVehicle():IsValid() then return end
local veh = ply:GetVehicle()
local gforce = Vector(ply:GetNWFloat("gx", 0), ply:GetNWFloat("gy", 0), ply:GetNWFloat("gz", 0))
if SysTime() - maxAge > 5 then
max = 0
maxAge = SysTime()
end
if gforce:Length() > max then
max = gforce:Length()
maxAge = SysTime()
end
surface.SetDrawColor(0, 0, 0, 127)
surface.DrawRect(pos.x, pos.y - 2, 138, 102)
draw.DrawText("GForce Statistics", "ChatFont", pos.x + 5, pos.y)
draw.DrawText("Local X: " .. string.format("%.1f", gforce:Dot(-veh:GetForward())), "ChatFont", pos.x + 5, pos.y + 16)
draw.DrawText("Local Y: " .. string.format("%.1f", gforce:Dot(veh:GetRight())), "ChatFont", pos.x + 5, pos.y + 32)
draw.DrawText("Local Z: " .. string.format("%.1f", gforce:Dot(-veh:GetUp())), "ChatFont", pos.x + 5, pos.y + 48)
draw.DrawText("Overall: " .. string.format("%.1f", gforce:Length()), "ChatFont", pos.x + 5, pos.y + 64)
draw.DrawText("Max (5s): " .. string.format("%.1f", max), "ChatFont", pos.x + 5, pos.y + 80)
end)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment