0

I'm using the function

acc = (float) Math.sqrt(x * x + y * y + z * z) - GRAVITY_EARTH;

to calculate the initial acceleration when a ball is being thrown. X, y and z are the values from the Android accelerometer.

I also have a counter that counts up when the throwing motion exceeds the minimum required acceleration, and I'm trying to update the height of a virtual ball and output its position and speed, using this formula:

vel = acc + (-GRAVITY_EARTH * Float.parseFloat(timeCounter));

But this only continuously applies the accelerometer force, how can I get the initial throwing speed and then calculate the height of the ball in real-time?

Accelerometer function below:

/* Get values from the built in accelerometer while they are being updated. */
@Override
public void onSensorChanged(SensorEvent event) {
    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {

        float x = event.values[0];
        float y = event.values[0];
        float z = event.values[0];

        acc = (float) Math.sqrt(x * x + y * y + z * z) - GRAVITY_EARTH;
        String a = String.format("%.3f", acc);
        a = a.replace(",", ".");
        currentAccText.setText(String.valueOf(a));

        /* Is the current acc larger than set minimum value? */
        if (acc >= minAcc) {

            /* Has the timer not been started yet? */
            if (timeCounter.equals("0.0")) {

                timer.start();
                a = String.format("%.3f", acc);
                a = a.replace(",", ".");
                thrownAccText.setText(a);

                String hr = String.format("%.3f", heightRecord);
                hr = hr.replace(",", ".");
                recordHeightText.setText(hr);

            }

        }

        vel = acc + (-GRAVITY_EARTH * Float.parseFloat(timeCounter));


        height += vel;
        heightBar.setProgress((int) height);

        if (vel < 0.01f && vel > -0.01f) sound.start();
        if (height > heightRecord) heightRecord = height;

        if (height < 0.01f) height = 0.f;
        String h = String.format("%.3f", height);
        h = h.replace(",", ".");
        heightText.setText(h);

        if (height > 0.f) throwOrGroundText.setText("Throw!");
        else throwOrGroundText.setText("On the ground.");

    }

}
OKL
  • 11
  • 3
  • 1
    I'd advise going through the [various](https://stackoverflow.com/q/7829097/295004) [questions](https://stackoverflow.com/q/4609309/295004) [about](https://stackoverflow.com/q/27959573/295004) [accelerometer](https://stackoverflow.com/q/36247535/295004) [accuracy](https://stackoverflow.com/q/10223565/295004) on StackOverflow. Short version: average historical values to smooth, but if you check multiple devices, you'll see that accuracy varies but that may be good enough for your use case. Good luck. – Morrison Chang Mar 02 '19 at 18:44
  • in the first acc= ... formula, how do you take the direction of gravitation into account? To get the total speed increase, how many accelerometer readings do you have between when the hand starts to move the ball until the ball leaves the hand? How much variation in acceleration during that short time frame? Is the adroid accelerometer inside that ball? What speed accuracy do you aim for? – Mats Lind Mar 22 '19 at 14:51

0 Answers0