Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arreglar juego con brújula #35

Closed
martinlaizg opened this issue May 13, 2019 · 2 comments
Closed

Arreglar juego con brújula #35

martinlaizg opened this issue May 13, 2019 · 2 comments
Assignees
Labels
fix To fix
Milestone

Comments

@martinlaizg
Copy link
Owner

La brújula sólo se actualiza cuándo se mueve el usuario.
Se debería de calcular la orientación del usuario en cada movimiento, pero después rotar la imagen a la vez que se rota el dispositivo móvil.

@martinlaizg martinlaizg added the fix To fix label May 13, 2019
@martinlaizg martinlaizg added this to the v0.1 milestone May 13, 2019
@martinlaizg martinlaizg self-assigned this May 13, 2019
@martinlaizg
Copy link
Owner Author

Pospuesto para una siguiente versión

package com.martinlaizg.geofind.views.fragment.play;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.martinlaizg.geofind.R;

import butterknife.BindView;
import butterknife.ButterKnife;

public class PlayCompassFragment
		extends PlayTourFragment
		implements SensorEventListener {

	private static final String TAG = PlayCompassFragment.class.getSimpleName();

	private final float[] accelerometerReading = new float[3];
	private final float[] magnetometerReading = new float[3];

	@BindView(R.id.navigation_image)
	ImageView navigation_image;
	private SensorManager sensorManager;

	@Nullable
	@Override
	public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
			@Nullable Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_play_compass, container, false);
		ButterKnife.bind(this, view);
		sensorManager = (SensorManager) requireActivity().getSystemService(Context.SENSOR_SERVICE);
		return view;
	}

	@Override
	public void onSensorChanged(SensorEvent event) {
		if(usrLocation != null && placeLocation != null) {
			if(event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
				System.arraycopy(event.values, 0, accelerometerReading, 0,
				                 accelerometerReading.length);
			} else if(event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
				System.arraycopy(event.values, 0, magnetometerReading, 0,
				                 magnetometerReading.length);
			}

			// Rotation matrix based on current readings from accelerometer and magnetometer.
			final float[] rotationMatrix = new float[9];
			SensorManager.getRotationMatrix(rotationMatrix, null, accelerometerReading,
			                                magnetometerReading);

			// Express the updated rotation matrix as three orientation angles.
			final float[] orientationAngles = new float[3];
			SensorManager.getOrientation(rotationMatrix, orientationAngles);
			Log.i(TAG, "rotation=" + orientationAngles[0]);
		}
	}

	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {
		// DO NOTHING
	}

	@Override
	protected String TAG() {
		return TAG;
	}

	@Override
	public void onResume() {
		super.onResume();
		// Get updates from the accelerometer and magnetometer at a constant rate.
		// To make batch operations more efficient and reduce power consumption,
		// provide support for delaying updates to the application.
		//
		// In this example, the sensor reporting delay is small enough such that
		// the application receives an update before the system checks the sensor
		// readings again.
		Sensor accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
		if(accelerometer != null) {
			sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL,
			                               SensorManager.SENSOR_DELAY_UI);
		}
		Sensor magneticField = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
		if(magneticField != null) {
			sensorManager.registerListener(this, magneticField, SensorManager.SENSOR_DELAY_NORMAL,
			                               SensorManager.SENSOR_DELAY_UI);
		}
	}

	@Override
	public void onPause() {
		super.onPause();
		sensorManager.unregisterListener(this);
	}

	@Override
	void updateView() {
		/*
		  In compass view we do not update the view with de location updates
		  We update the view with the onSensorChanged method
		 */
	}
}

@martinlaizg martinlaizg modified the milestones: v0.1, Next version May 14, 2019
@martinlaizg
Copy link
Owner Author

fix-compass-tour

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fix To fix
Projects
None yet
Development

No branches or pull requests

1 participant