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!

2 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