Skip to content

Commit 2004f0c

Browse files
fix: kind of works on android! yeah!
1 parent d6dcb6a commit 2004f0c

File tree

3 files changed

+79
-38
lines changed

3 files changed

+79
-38
lines changed

cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxAccelerometer.java

Lines changed: 52 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ public class Cocos2dxAccelerometer implements SensorEventListener {
4747
private final Context mContext;
4848
private final SensorManager mSensorManager;
4949
private final Sensor mAccelerometer;
50+
private final Sensor mCompass;
5051
private final int mNaturalOrientation;
52+
final float[] accelerometerValues = new float[3];
53+
final float[] magneticFieldValues = new float[3];
5154

5255
// ===========================================================
5356
// Constructors
@@ -58,6 +61,7 @@ public Cocos2dxAccelerometer(final Context context) {
5861

5962
this.mSensorManager = (SensorManager) this.mContext.getSystemService(Context.SENSOR_SERVICE);
6063
this.mAccelerometer = this.mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
64+
this.mCompass = this.mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
6165

6266
final Display display = ((WindowManager) this.mContext.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
6367
this.mNaturalOrientation = display.getOrientation();
@@ -67,20 +71,26 @@ public Cocos2dxAccelerometer(final Context context) {
6771
// Getter & Setter
6872
// ===========================================================
6973

74+
public void enableAll() {
75+
this.mSensorManager.registerListener(this, this.mAccelerometer, SensorManager.SENSOR_DELAY_GAME);
76+
this.mSensorManager.registerListener(this, this.mCompass, SensorManager.SENSOR_DELAY_GAME);
77+
}
78+
7079
public void enable() {
80+
this.mSensorManager.registerListener(this, this.mCompass, SensorManager.SENSOR_DELAY_GAME);
7181
this.mSensorManager.registerListener(this, this.mAccelerometer, SensorManager.SENSOR_DELAY_GAME);
7282
}
7383

74-
public void setInterval(float interval) {
75-
// Honeycomb version is 11
76-
if(android.os.Build.VERSION.SDK_INT < 11) {
84+
public void setInterval(float interval) {
85+
// Honeycomb version is 11
86+
if(android.os.Build.VERSION.SDK_INT < 11) {
7787
this.mSensorManager.registerListener(this, this.mAccelerometer, SensorManager.SENSOR_DELAY_GAME);
7888
} else {
7989
//convert seconds to microseconds
8090
this.mSensorManager.registerListener(this, this.mAccelerometer, (int)(interval*100000));
8191
}
8292
}
83-
93+
8494
public void disable() {
8595
this.mSensorManager.unregisterListener(this);
8696
}
@@ -91,38 +101,46 @@ public void disable() {
91101

92102
@Override
93103
public void onSensorChanged(final SensorEvent sensorEvent) {
94-
if (sensorEvent.sensor.getType() != Sensor.TYPE_ACCELEROMETER) {
95-
return;
104+
if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
105+
106+
float x = sensorEvent.values[0];
107+
float y = sensorEvent.values[1];
108+
final float z = sensorEvent.values[2];
109+
110+
/*
111+
* Because the axes are not swapped when the device's screen orientation
112+
* changes. So we should swap it here. In tablets such as Motorola Xoom,
113+
* the default orientation is landscape, so should consider this.
114+
*/
115+
final int orientation = this.mContext.getResources().getConfiguration().orientation;
116+
117+
if ((orientation == Configuration.ORIENTATION_LANDSCAPE) && (this.mNaturalOrientation != Surface.ROTATION_0)) {
118+
final float tmp = x;
119+
x = -y;
120+
y = tmp;
121+
} else if ((orientation == Configuration.ORIENTATION_PORTRAIT) && (this.mNaturalOrientation != Surface.ROTATION_0)) {
122+
final float tmp = x;
123+
x = y;
124+
y = -tmp;
125+
}
126+
127+
Cocos2dxGLSurfaceView.queueAccelerometer(x,y,z,sensorEvent.timestamp);
128+
129+
this.accelerometerValues[0] = x;
130+
this.accelerometerValues[1] = y;
131+
this.accelerometerValues[2] = z;
132+
133+
/*
134+
if(BuildConfig.DEBUG) {
135+
Log.d(TAG, "x = " + sensorEvent.values[0] + " y = " + sensorEvent.values[1] + " z = " + pSensorEvent.values[2]);
136+
}
137+
*/
96138
}
97-
98-
float x = sensorEvent.values[0];
99-
float y = sensorEvent.values[1];
100-
final float z = sensorEvent.values[2];
101-
102-
/*
103-
* Because the axes are not swapped when the device's screen orientation
104-
* changes. So we should swap it here. In tablets such as Motorola Xoom,
105-
* the default orientation is landscape, so should consider this.
106-
*/
107-
final int orientation = this.mContext.getResources().getConfiguration().orientation;
108-
109-
if ((orientation == Configuration.ORIENTATION_LANDSCAPE) && (this.mNaturalOrientation != Surface.ROTATION_0)) {
110-
final float tmp = x;
111-
x = -y;
112-
y = tmp;
113-
} else if ((orientation == Configuration.ORIENTATION_PORTRAIT) && (this.mNaturalOrientation != Surface.ROTATION_0)) {
114-
final float tmp = x;
115-
x = y;
116-
y = -tmp;
117-
}
118-
119-
Cocos2dxGLSurfaceView.queueAccelerometer(x,y,z,sensorEvent.timestamp);
120-
121-
/*
122-
if(BuildConfig.DEBUG) {
123-
Log.d(TAG, "x = " + sensorEvent.values[0] + " y = " + sensorEvent.values[1] + " z = " + pSensorEvent.values[2]);
139+
else if (sensorEvent.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
140+
this.magneticFieldValues[0] = sensorEvent.values[0];
141+
this.magneticFieldValues[1] = sensorEvent.values[1];
142+
this.magneticFieldValues[2] = sensorEvent.values[2];
124143
}
125-
*/
126144
}
127145

128146
@Override

cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxHelper.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -616,10 +616,29 @@ public static int setLowPowerMode(boolean enable) {
616616
//Enhance API modification end
617617
public static float[] getSensorRotationMatrix() {
618618
float[] rotationM = new float[16];
619-
float[] inclinationM = new float[16];
620-
float[] gravs = new float[3];
621-
float[] geoMags = new float[3];
622-
SensorManager.getRotationMatrix(rotationM, inclinationM, gravs, geoMags);
619+
Log.d("carlos", "accel x = " +
620+
Cocos2dxHelper.sCocos2dxAccelerometer.accelerometerValues[0]
621+
+ " y = " +
622+
Cocos2dxHelper.sCocos2dxAccelerometer.accelerometerValues[1]
623+
+ " z = " +
624+
Cocos2dxHelper.sCocos2dxAccelerometer.accelerometerValues[2]);
625+
Log.d("carlos", "mag x = " +
626+
Cocos2dxHelper.sCocos2dxAccelerometer.magneticFieldValues[0]
627+
+ " y = " +
628+
Cocos2dxHelper.sCocos2dxAccelerometer.magneticFieldValues[1]
629+
+ " z = " +
630+
Cocos2dxHelper.sCocos2dxAccelerometer.magneticFieldValues[2]);
631+
if (!SensorManager.getRotationMatrix(rotationM,
632+
null,
633+
Cocos2dxHelper.sCocos2dxAccelerometer.accelerometerValues,
634+
Cocos2dxHelper.sCocos2dxAccelerometer.magneticFieldValues))
635+
{
636+
for (int i=0; i<16; i++) {
637+
rotationM[i] = 0;
638+
}
639+
// identity matrix if error
640+
rotationM[0] = rotationM[5] = rotationM[10] = rotationM[15] = 1;
641+
}
623642
return rotationM;
624643
}
625644
}

cocos/vr/CCVRGenericHeadTracker.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
#include "vr/CCVRGenericHeadTracker.h"
3131
#include "platform/CCPlatformMacros.h"
32+
#include "platform/CCDevice.h"
3233

3334
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
3435
#import <CoreMotion/CoreMotion.h>
@@ -146,13 +147,16 @@ void VRGenericHeadTracker::startTracking()
146147
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
147148
_deviceToDisplay = getRotateEulerMatrix(0.f, 0.f, -90.f);
148149
_worldToInertialReferenceFrame = getRotateEulerMatrix(-90.f, 0.f, 90.f);
150+
Device::setAccelerometerEnabled(true);
149151
#endif
150152
}
151153

152154
void VRGenericHeadTracker::stopTracking()
153155
{
154156
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
155157
[(CMMotionManager*)_motionMgr stopDeviceMotionUpdates];
158+
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
159+
Device::setAccelerometerEnabled(false);
156160
#endif
157161
}
158162

0 commit comments

Comments
 (0)