Skip to content

Commit cbf5f64

Browse files
reading sensor from android
not compiling yet
1 parent 6ee0a3c commit cbf5f64

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ of this software and associated documentation files (the "Software"), to deal
4040
import android.util.DisplayMetrics;
4141
import android.view.Display;
4242
import android.view.WindowManager;
43+
import android.hardware.SensorManager;
4344

4445
import com.enhance.gameservice.IGameTuningService;
4546

@@ -49,6 +50,7 @@ of this software and associated documentation files (the "Software"), to deal
4950
import java.util.Map;
5051
import java.util.Set;
5152

53+
5254
public class Cocos2dxHelper {
5355
// ===========================================================
5456
// Constants
@@ -611,4 +613,12 @@ public static int setLowPowerMode(boolean enable) {
611613
}
612614
}
613615
//Enhance API modification end
616+
public static float[] getSensorRotationMatrix() {
617+
float[] rotationM = new float[16];
618+
float[] inclinationM = new float[16];
619+
float[] gravs = new float[3];
620+
float[] geoMags = new float[3];
621+
SensorManager.getRotationMatrix(rotationMatrix, inclimationMatrix, gravs, geoMags);
622+
return rotationM;
623+
}
614624
}

cocos/platform/android/jni/JniHelper.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,23 @@ class CC_DLL JniHelper
131131
return ret;
132132
}
133133

134+
template <typename... Ts>
135+
static jfloatArray callStaticFloatArrayMethod(const std::string& className,
136+
const std::string& methodName,
137+
Ts... xs) {
138+
jfloatArray ret;
139+
cocos2d::JniMethodInfo t;
140+
std::string signature = "(" + std::string(getJNISignature(xs...)) + ")[F";
141+
if (cocos2d::JniHelper::getStaticMethodInfo(t, className.c_str(), methodName.c_str(), signature.c_str())) {
142+
ret = (jfloatArray) t.env->CallStaticObjectMethod(t.classID, t.methodID, convert(t, xs)...);
143+
t.env->DeleteLocalRef(t.classID);
144+
deleteLocalRefs(t.env);
145+
} else {
146+
reportError(className, methodName, signature);
147+
}
148+
return ret;
149+
}
150+
134151
template <typename... Ts>
135152
static double callStaticDoubleMethod(const std::string& className,
136153
const std::string& methodName,

cocos/vr/CCVRGenericHeadTracker.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
3434
#import <CoreMotion/CoreMotion.h>
3535
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
36-
#include <android/sensor.h>
36+
#include <jni.h>
37+
#include "platform/android/jni/JniHelper.h"
3738
#endif
3839

3940
NS_CC_BEGIN
@@ -172,7 +173,11 @@ Mat4 VRGenericHeadTracker::getLocalRotation()
172173
return _deviceToDisplay * worldToDevice;
173174

174175
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
176+
static const std::string helperClassName = "org/cocos2dx/lib/Cocos2dxHelper";
177+
auto rotMatrix = JniHelper::callStaticFloatArrayMethod(helperClassName, "getSensorRotationMatrix");
175178

179+
Mat4 ret(rotMatrix);
180+
return ret;
176181
#else
177182
return Mat4::IDENTITY;
178183
#endif

0 commit comments

Comments
 (0)