- Home /
How do I get the acceleration of an object accurately?
I am making a simulated Inertial Measurement Unit (IMU) which is a device that detects acceleration (linear and angular but lets focus on linear first).
I found this script and based mine on it but converted to c#: http://wiki.unity3d.com/index.php/IMU_Inertial_Measurement_Unit
Basically it just records the position for each step and uses the difference divided by deltatime to get speed, and then records the speed and uses the difference divided by deltatime to get acceleration.
Problem is that some frames seem produce inaccurate numbers. For example if I let an object fall freely, the script doesn't report a smoothly increasing Y value which would give a smoothly increasing speed and a constant acceleration. Instead, the Y value is slightly off every time it records, so sometimes the velocity increases twice as much as it did the last step, and sometimes it even goes backwards for one step. This of course completely messes up the acceleration calculation. So basically, the position is slightly inaccurate, which causes a much bigger inaccuracy in its derivation, which causes and even bigger inaccuracy in that derivation.
So what's a way to get more accurate position values for the exact time? What am I doing wrong?
Answer by Grix · Apr 03, 2018 at 07:29 PM
Actually I think I fixed it myself, I just had to move the code from Update() to FixedUpdate(), that massively improved the results
the code you are looking at seems a bit much to simply get acceleration. you should be able to simply subtract the distance moved and the last frames distance moved to get your result.
public float acceleration;
public float distancemoved=0;
public float lastdistancemoved=0;
public Vector3 last = transform.position;
void Update(){
distancemoved = Vector3.Distance (last, transform.position);
distancemoved *= Time.deltaTime;
acceleration = distancemoved - lastdistancemoved;
lastdistancemoved = distancemoved;
last = transform.position;
}
That's basically what it does, but most of the code is for also calculating angular acceleration, not just linear. And also I needed acceleration as a vector with direction, not just a float. But I have compressed the script by subtracting whole vectors directly ins$$anonymous$$d of component by component.
if you need to separate the vectors it would be even easier
public Vector3 acceleration;
public Vector3 distancemoved=Vector3.zero;
public Vector3 lastdistancemoved=Vector3.zero;
public Vector3 last = transform.position;
void Update(){
distancemoved = (transform.position - last) * Time.deltaTime ;
acceleration = distancemoved - lastdistancemoved;
lastdistancemoved = distancemoved;
last = transform.position;
}
hello. it's been a while about your question. if you sccussed making your virtual IMU sensor , i want to know about your idea.
I think that i can get position data according to time, and then differential the position data i can get acceleromter.
give me some hint... i want to make virtual sensor as you.
Your answer
Follow this Question
Related Questions
Change position of camera on scene load? 1 Answer
Create a straight gradient equation with a grid of 3d objects 1 Answer
Following another object's position/rotation like parent/child relationship? 4 Answers
How do you set the initial value of a variable to an objects current position in unity? 1 Answer
Create an animation with variables? 1 Answer