Bénédicte Lagouge - 5 months ago 38

Java Question

I am getting the 3dimensional values of a grove pi module,

**acc_vector = (accX, accY, accZ)**

each 100 milliseconds .

What I want is, using java, diplaying the movement that I make with the module...but my problem is not how to display it :p

For this I need to compute the value of the position

**position_vector = (x,y,z)**

corresponding to the acceleration values. But I have no clue about how to do it.

Here is what I've tried, but it always gives odd results (initial position, speed and acceleration are (0,0,0) ):

`position_vector = 0.5f*acc_vector*t;`

`position_vector += acc_vector;`

`position_vector = acc_vector*(t-t_just_before);`

Answer

It will depend on how you want to interpolate the acceleration. The easiest (but likely least accurate) would be to assume constant acceleration between samples. In that situation, your code for the `x`

component should resemble

```
position[0] += velocity[0] * timeStep + 0.5 * acceleration[0] * timeStep * timeStep;
velocity[0] += acceleration[0] * timeStep;
```

You would have to do the same for the `y`

and `z`

components.

To get more accurate results, you may need more sophisticated techniques. You can look up numerical methods such as Runge-Kutta.