Calculating Acceleration Of A Part

Good day all!, I’m having an issue with calculating the acceleration of a part aka mimicking what an Accelerometer would do. I need to check the acceleration of a part more so a rocket or even a train to be able to then use this data for a PID Controller to control various useful systems.

PID stuff etc I have that all covered but calculating what should be simple is proving very flinky and Im finding the Vector3 values returned are rapidly changing with absurd numbers.

My issue is that I calculate the Acceleration with:

local lastVelocity = script.Part.AssemblyLinearVelocity
local RunService = game["Run Service"]

RunService.Stepped:Connect(function(_, delta)
local Acceleration = (script.Part.AssemblyLinearVelocity - lastVelocity) / delta
lastVelocity = script.Part.AssemblyLinearVelocity
end)

I get values that fluctuate extremely largely and seemingly inaccurate. I’m using a PID controller to then try to maintain a specfic Acceleration of which its applying force in said direction using a VectorForce constraint.

Mainly I’m just trying to figure out if there is any better or accurate ways of calculating acceleration like an Accelerometer would. Thanks!

3 Likes

you can just use .Velocity for baseparts, can you not?

example:

workspace.Part.Velocity -- returns a vector3 value
workspace.Part.Velocity.Magnitude -- returns a floating point number (decimals)
1 Like

You can extract a way to get a more consistant delta value from the solution here:

this should solve your fluctuations

1 Like

Thanks this seems to have solved the Acceleration issue, feels much more smoother.

Edit: just realized that this works more so locally rather than server side. Still getting abnormal changes in acceleration calculations

1 Like

@kckhem Is there anyway for us to get instantaneous Acceleration without all the noise that its exposed to rendering it useless when we use: Acceleration = (currentvelocity - lastvelocity) / deltatime

Having an Acceleration value like Velocity or some sorts that just shows the Acceleration is highly useful unless there is someone to accurately replicate an Accelerometer sensor to measure Acceleration.

Having an accurate Acceleration readout would help understand the physics of vehicles etc much better than current, one would be able to use the data to develop advanced systems like Vehicle autopilot etc and develop proper braking curves etc plus character development.

Example of noisy acceleration no matter the method of lerp, smoothdamp etc:
image

If the script is serverside, double check if the part you’re measuring is owned by the server or client. A difference in networkownership could be causing the fluctuations.

1 Like

Made sure to set the network ownership to client.

does this match your accelerometer script?

Yeah which is client side. Im not sure one can get rid of the noise so much regardless of client or server it just reduces a bit on the client vs server.

1 Like

You could try larger timesteps instead of per frame, but that’ll reduce the resolution of your data… it’s a balance.

1 Like

I see, might have to attempt that or shelve it for now till there is possibly a better way to accurately calculate it with much less noise as its really needed for a PID controller

At the moment we do not provide acceleration (or force and torque) data, but I can see how it would be useful. Thanks for this suggestion, we will think about ways to expose this information.

3 Likes

–Double precision floating point

1 Like

Thanks, will be very useful for quite a few specfic control systems

I know I’m late but I’d love to see this feature get added. I’ve succeeded in vertical Gs, but I can’t get a script to work when getting all 3 axes.

Yep, same issue for me I need the other Axis and a stable output in that for feeding some PID controllers to then control certain systems that counter things like Dutch rolling in an aircraft. But can’t calculate the Acceleration steady enough for other axis.

Seems like a recurring issue?

local FOV = scoped:Value(70)

local tmp0 = scoped:Tween(FOV,TweenInfo.new(0))
scoped:Hydrate(Camera) {
FieldOfView = tmp0
}

local function ChangeCameraCFrame()
Camera.CFrame = CurrentCamera.CFrame
local Acceleration = (OldVelocity-CurrentCamera.AssemblyLinearVelocity).Magnitude
OldVelocity = CurrentCamera.AssemblyLinearVelocity
FOV:set(70 * (1+Acceleration/100))
end

Before the tween I also tried using a function apart to get acceleration and then adding it but both ways it still happens the same thing.

Yeah same here, ive even tried a Kalman filter as well as a EuroFilter and both either dampen the values too far behind what is needed for the PID controller. Having the Angular Acceleration and Linear Acceleration of parts would be massive.

I havent really figured it out yet, I will get back to the project in a few weeks after rotation, but I have seen a module that is really smooth;
I should try to check out that module with my system, therefore its my physics step,
I can also try using the player character for acceleration but no idea, I will see and notify the thread

1 Like