Skip to content

Instantly share code, notes, and snippets.

@adamnejm
Created December 17, 2023 22:33
Show Gist options
  • Save adamnejm/b1166e1740c503684e177aed763c6848 to your computer and use it in GitHub Desktop.
Save adamnejm/b1166e1740c503684e177aed763c6848 to your computer and use it in GitHub Desktop.
[GMod] Car Physics Jitter Fix Test
--@name Mini Car Test
--@author Name
--@server
local owner, client, chip, world = owner(), player(), chip(), entity(0)
-------------------------------------------
local custom_forces = true -- Set this to `true` to workaround a physics jitter bug
local size = Vector(32, 20, 2)
local body_mass = 100
local convexes = {
{
-- Vector(0, 0, 20),
Vector(size.x / 2, -size.y / 2, -size.z / 2), Vector(-size.x / 2, -size.y / 2, -size.z / 2), Vector(size.x / 2, size.y / 2, -size.z / 2), Vector(-size.x / 2, size.y / 2, -size.z / 2),
Vector(size.x / 2, -size.y / 2, size.z / 2), Vector(-size.x / 2, -size.y / 2, size.z / 2), Vector(size.x / 2, size.y / 2, size.z / 2), Vector(-size.x / 2, size.y / 2, size.z / 2),
}
}
local body = prop.createCustom(chip:getPos() + chip:getUp() * 22, chip:getAngles(), convexes, true)
local body_phys = body:getPhysicsObject()
body_phys:setMass(body_mass)
-------------------------------------------
local wheels = {
{ origin = Vector( size.x / 2, size.y / 2, -size.z / 2), holo = hologram.create(Vector(), Angle(), "models/holograms/cube.mdl", Vector(0.2)) }, -- Front Left
{ origin = Vector( size.x / 2, -size.y / 2, -size.z / 2), holo = hologram.create(Vector(), Angle(), "models/holograms/cube.mdl", Vector(0.2)) }, -- Front Right
{ origin = Vector(-size.x / 2, size.y / 2, -size.z / 2), holo = hologram.create(Vector(), Angle(), "models/holograms/cube.mdl", Vector(0.2)) }, -- Rear Left
{ origin = Vector(-size.x / 2, -size.y / 2, -size.z / 2), holo = hologram.create(Vector(), Angle(), "models/holograms/cube.mdl", Vector(0.2)) }, -- Rear Right
}
local suspension_length = 20
local suspension_rest_length = 10
local suspension_strength = 150
local suspension_damping = 10
-------------------------------------------
-- hook.add("Tick", "", function() -- works as well
body:setPhysicsUpdateListener(function()
local force_linear, force_torque = Vector(), Vector()
local suspension_direction = body:getUp()
for _, wheel in ipairs(wheels) do
local origin = body:localToWorld(wheel.origin)
local ray = trace.line(origin, origin - suspension_direction * suspension_length, body)
wheel.holo:setPos(ray.HitPos)
if ray.Hit then
local suspension_velocity = suspension_direction:dot(body_phys:getVelocityAtPoint(origin))
local suspension_offset = suspension_rest_length - suspension_length * ray.Fraction
local suspension_force = (suspension_offset * suspension_strength) - (suspension_velocity * suspension_damping)
local linear, torque = body_phys:calculateForceOffset(suspension_direction * suspension_force, origin)
force_linear = force_linear + linear
force_torque = force_torque + torque
end
end
if custom_forces then
body:setCustomPropForces(force_torque * 10, force_linear * 100, 4)
else
force_torque = body_phys:localToWorldVector(force_torque)
body_phys:applyForceCenter(force_linear)
body_phys:applyTorque(force_torque)
end
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment