Calculating acceleration is way too "noisy"/varied

Hello,

I’m currently working on a custom vehicle system. I’ve been wanting to tinker with camera manipulation while in first-person to simulate the character shifting backwards/forward/side-to-side as a result of G-force like in real life; braking hard will have you pushed forward, accelerating will pull you into your seat. I’m recreating this effect with just the camera.

Problems immediately arose when trying to get the acceleration and deceleration of my test car as I needed those values to plug into the camera manipulation. The values returned from my equation are way too “noisy”/varied.

While accelerating, the returned values are as high as 50 and as low as 36, before jumping back up and down again the frame after. This noisy return ends up jittering the camera way too much to be comfortable. While it’s not really visible in the example video, it can be felt in my live game with another vehicle of differing stats.

When the vehicle stops accelerating and the speed caps out, the returned values drops back down to near 0, but it’s still a noisy decimal mess. I also had to incorporate a minimum speed check because while the vehicle was seemingly stationary, it would eventually start shooting out the noisy values again, disrupting the camera.

local lastSpeed = 0
local acceleration = 0
local speedThreshold = 5 -- Threshold to determine if the vehicle is stationary
local accelCheck = 0.1

while wait(accelCheck) do
	local currentSpeed = Seat.AssemblyLinearVelocity.Magnitude

	if math.abs(currentSpeed) > speedThreshold then
		-- Calculate acceleration as the change in speed since last frame
		acceleration = (currentSpeed - lastSpeed) / accelCheck
	else
		acceleration = 0
	end

	lastSpeed = currentSpeed

	print("Current Acceleration: " .. acceleration)
end

Does anyone know how I can improve the equation to make it return smoother values, or any advice at all to generally improve it? I’ve surprisingly found nothing online, even looking as far as Unity forums. Math and physics have never been my strong suit so I thought I’d ask here.

Thanks in advance.

Your loop doesn’t necessarily run frame-by-frame. You should try incorporating RunService and deltaTime and see if that smooths it out.

RunService.RenderStepped:Connect(function(deltaTime)
    local currentSpeed = Seat.AssemblyLinearVelocity.Magnitude
    
    acceleration = (currentSpeed - lastSpeed) / deltaTime
    lastSpeed = currentSpeed
end)
1 Like

I should’ve mentioned that it was originally in a RenderStepped loop but I moved the equation to a while loop prior to posting for better output readability. The same problems arise.

This is just what happens when you use math, I did have to use an average smoothing system when I was working with acceleration for past projects.

There’s a technique called mean-smoothing, which is done by taking the values of the past few frames (best to store them in an array) and averaging it. The more frames you use, the more frames you average, and the smoother it gets.

But that read-out is accurate frame-by-frame.

1 Like

This method sounds promising, I’ll give it a shot tomorrow and relay the results. Thank you.