Viped 0 Junior Poster in Training

Hi, I am trying to make basic breakout clone for android. I want that the paddle moves as player rotates phone. I am getting data from magnetic field sensor and it changes as I rotate my phone, but it changes to same direction always. So if I have my phone top to up sensor gives me 0degrees if I rotate my phone to clockwise 45degrees it gives me 45 degrees and same for counterclockwise still 45degrees (so I want this value to be -45degrees) so what I am doing wrong here.

package net.viped.breakout;

import java.util.ArrayList;

import net.viped.breakout.R.drawable;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class MainGamePanel extends SurfaceView implements SurfaceHolder.Callback, Runnable {

    Bitmap brick;
    Brick b;

    int bricksWidth;
    int bricksHeight;
    float orientation[] = {0,0,0};
    double do1 = 0;

    ArrayList<Brick> bricks = new ArrayList<Brick>();

    private Thread thread;

    public MainGamePanel(Context context, SensorManager sensorManager) {
        super(context);
        getHolder().addCallback(this);
        brick = BitmapFactory.decodeResource(getResources(), R.drawable.brick);
        bricksWidth = 15;
        bricksHeight = 10;

        SensorEventListener sensorListener = new SensorEventListener() {

            @Override
            public void onSensorChanged(SensorEvent event) {

                // TODO Auto-generated method stub
                if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
                    mGravity = event.values;
                }
                if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
                    mGeomagnetic = event.values;
                }

                if (mGravity != null && mGeomagnetic != null) {
                    float R[] = new float[9];
                    float I[] = new float[9];

                    boolean success = SensorManager.getRotationMatrix(R, I, mGravity, mGeomagnetic);
                    if (success) {

                        SensorManager.getOrientation(R, orientation);
//                      do1 = Math.toDegrees(orientation[1]);
                        // Log.v("AXIS", Math.toDegrees(orientation[1])+90 +
                        // "");
                    }
                }
            }

            @Override
            public void onAccuracyChanged(Sensor sensor, int accuracy) {

            }
        };

        sensorManager.registerListener(sensorListener, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_GAME);
        sensorManager.registerListener(sensorListener, sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD), SensorManager.SENSOR_DELAY_GAME);

        initBricks();
        start();
    }

    public void initBricks() {
        for (int y = 0; y < bricksHeight; y++) {
            for (int x = 0; x < bricksWidth; x++) {
                Brick b = new Brick(x * 32, y * 20, brick);
                bricks.add(b);
            }
        }
    }

    float[] mGravity;
    float[] mGeomagnetic;

    public void tick() {

            double orientationNow = Math.toDegrees(orientation[1])+90;
            do1 = do1 - orientationNow;

            Log.v("do1", orientationNow + "");



            for (Brick b : bricks) {
                b.x += Math.toRadians(orientationNow);
            }

            do1 = orientationNow;
    }

    public void onSensorChanged(SensorEvent event) {

    }

    Paint blackPaint = new Paint();

    public void onDraw(Canvas canvas) {
        blackPaint.setColor(Color.rgb(0, 0, 0));
        blackPaint.setStrokeWidth(1);
        int screenWidth = this.getWidth();
        int screenHeight = this.getHeight();
        // Log.v("RESOLUTION", "WIDTH=" + screenWidth + ", HEIGHT=" +
        // screenHeight);
        canvas.drawRect(0, 0, screenWidth, screenHeight, blackPaint);
        for (Brick b : bricks) {
            b.render(canvas);
        }
    }

    public void start() {
        thread = new Thread(this);
        thread.start();
    }

    public void run() {
        Canvas canvas;
        SurfaceHolder surfaceHolder = this.getHolder();
        Log.d("TAG", "Starting game loop");
        while (true) {
            canvas = null;
            try {
                canvas = surfaceHolder.lockCanvas();

                synchronized (surfaceHolder) {
                    if (canvas != null) {
                        tick();
                        onDraw(canvas);
                        try {
                            thread.sleep(20);
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }
            } finally {
                if (canvas != null) {
                    surfaceHolder.unlockCanvasAndPost(canvas);
                }
            }

        }
    }

    @Override
    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
        // TODO Auto-generated method stub

    }

    @Override
    public void surfaceCreated(SurfaceHolder arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder arg0) {
        // TODO Auto-generated method stub

    }

}

EDIT:

I have forced portrait mode. If my game would use landscape mode it would work fine. So maybe if I some how could set rotation to 0 in portrait mode it might work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.