Search code examples
javaandroidaccelerometer

moving a ball with help of accelerometer in android


I had written a code to move a ball with the help of accelerometer in android such that if it touches any of 4 edges it reappears on opposide edge my problems are

1.ball is not reappearing on opposite edge 2.orientation changes to lanscape

here is my code

 public class Accelerate extends Activity implements SensorEventListener {

float x, y, sensorX, sensorY, a, b, centerX, centerY;
Bitmap ball;
SensorManager sm;
NixSurface ourSurfaceView;

public class NixSurface extends SurfaceView implements Runnable {

    SurfaceHolder ourHolder;
    Thread ourThread = null;
    boolean isRunning = false;

    public NixSurface(Context context) {
        super(context);
        ourHolder = getHolder();

    }

    public void pause() {
        isRunning = false;
        while (true) {
            try {
                ourThread.join();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            break;
        }
        ourThread = null;
    }

    public void resume() {
        isRunning = true;
        ourThread = new Thread(this);
        ourThread.start();
    }

    public void run() {
        // TODO Auto-generated method stub
        while (isRunning) {
            if (!ourHolder.getSurface().isValid())
                continue;

            Canvas canvas = ourHolder.lockCanvas();

            canvas.drawRGB(255, 255, 255);
            centerX = canvas.getWidth() / 2;
            centerY = canvas.getHeight() / 2;
            x += sensorX;
            y += sensorY;
            a = centerX + x;
            b = centerY + y;

            if (a > canvas.getWidth())
                a = 0;
            if (b > canvas.getHeight())
                b = 0;
            if (a < 0)
                a = canvas.getWidth();
            if (b < 0)
                b = canvas.getHeight();

            canvas.drawBitmap(ball, a, b, null);
            ourHolder.unlockCanvasAndPost(canvas);
        }
    }

}

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    if (sm.getSensorList(Sensor.TYPE_ACCELEROMETER).size() != 0) {
        Sensor s = sm.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);
        sm.registerListener(this, s, SensorManager.SENSOR_DELAY_NORMAL);
    }
    x = y = sensorY = sensorX = 0;
    ball = BitmapFactory.decodeResource(getResources(), R.drawable.nix);
    ourSurfaceView = new NixSurface(this);
    ourSurfaceView.resume();
    setContentView(ourSurfaceView);
}

@Override
protected void onStop() {
    // TODO Auto-generated method stub
    sm.unregisterListener(this);
    super.onStop();
}

public void onAccuracyChanged(Sensor sensor, int accuracy) {
    // TODO Auto-generated method stub
    // nothing to do here
}

public void onSensorChanged(SensorEvent event) {
    // TODO Auto-generated method stub

    try {
        Thread.sleep(16);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    sensorX = -event.values[0];
    sensorY = event.values[1];
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}

}


Solution

  • I guess your problem is that you assume that the sensor origin (x=0,y=0) is the screen center. The sensor origin is the top left corner of the screen (X pointing left and Y pointing down), so comparing a & b to canvas.getWidth() & canvas.getHeight() is not correct since you already added centerX & centerY. Also, negating sensorX will cause the condition a > canvas.getWidth() to never occur since X is never negative.