jPCT-AE - a 3d engine for Android > Support

[Tips] How to use the orientation sensors to set the JPCT camera to corrispond

(1/2) > >>

after a lot of trial & errors, I was able to use the orientation sensors to set the JPCT camera to correspond to the device movements.
This technique use Camera.setBack method, and it seems to work very well.
The solution comes from this interesting article:

The article is about how to use sensors directly with OpenGL API, so I had to adapt it in order to make it work with jPCT.

First of all, you must get the sensors' values:

--- Code: --- public void onSensorChanged(SensorEvent event) {
final int type = event.sensor.getType();
if (type == Sensor.TYPE_ACCELEROMETER) {
accelGData = event.values.clone();
if (type == Sensor.TYPE_MAGNETIC_FIELD) {
magnetData = event.values.clone();
if (type == Sensor.TYPE_ORIENTATION) {
orientationData = event.values.clone();
rootMeanSquareBuffer(bufferedAccelGData, accelGData);
rootMeanSquareBuffer(bufferedMagnetData, magnetData);
SensorManager.getRotationMatrix(rotationMatrix, null,
bufferedAccelGData, bufferedMagnetData);

--- End code ---

I omitted all the code needed to register the SensorManager listeners, because it isn't nothing new respect to the standard.
Note the use of the rootMeanSquareBuffer() function to smooth the device movement.
It's mandatory to obtain an 'stable' camera movement.

After that, in the onDrawFrame, you can use the rotationMatrix calculated in the previous step:

--- Code: --- public void onDrawFrame(GL10 gl) {

Camera cam = world.getCamera();

if (landscape) {
// in landscape mode first remap the
// rotationMatrix before using
// it with camera.setBack:
float[] result = new float[9];
rotationMatrix, SensorManager.AXIS_MINUS_Y,
SensorManager.AXIS_MINUS_X, result);
com.threed.jpct.Matrix mResult = new com.threed.jpct.Matrix();
copyMatrix(result, mResult);
} else {
// WARNING: This solution doesn't work in portrait mode
// See the explanation below
// ... Draw here your own 3D world
blitNumber(lfps, 5, 5);

private void copyMatrix(float[] src, com.threed.jpct.Matrix dest) {
dest.setRow(0, src[0], src[1], src[2], 0);    
dest.setRow(1, src[3], src[4], src[5], 0);    
dest.setRow(2, src[6], src[7], src[8], 0);    
dest.setRow(3, 0f, 0f, 0f, 1f);

--- End code ---

As you can read in the code above, this solution doesn't work when the screen is set in portrait mode, so you must insert the following declaration in the manifest file:

--- Code: ---        <activity android:name=".myActivity" ...

--- End code ---

Who is able to find the solution working also in portrait mode is invited to participate to the discussion  ;)

Please note that the coordinate system will be that one returned by SensorManager.getRotationMatrix method, as described in the Android API documentation:

--- Quote ---
* X is defined as the vector product Y.Z (It is tangential to the ground at the device's current location and roughly points East).
* Y is tangential to the ground at the device's current location and points towards the magnetic North Pole.

* Z points towards the sky and is perpendicular to the ground.
--- End quote ---

(See attached figure)
Take it in account when you move your objects in the scene.

Hope this will help someone  :D


P.S.: please forgive the grammatical errors coming from an Italian mother tongue  :-\

[attachment deleted by admin]

I'm sure this will be helpful to some people, so i made it sticky. Thanks for sharing.


I am using T-mobile....Speed is very slow.
what is the reason.
What mobile you people prefer for developing 3D application for android using this API??

Thank you for this, it needs some polishing on my side but it still works well :)

I've tried solving this problem in bit simplier way, using accelerometer values only.
In activity class, onSensorChanged method:

--- Code: ---
    public void onSensorChanged(SensorEvent event) {
        if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER)
         * record the accelerometer data, the event's timestamp as well as
         * the current time. The latter is needed so we can calculate the
         * "present" time during rendering. In this application, we need to
         * take into account how the screen is rotated with respect to the
         * sensors (which always return data in a coordinate space aligned
         * to with the screen in its native orientation).

        switch (mDisplay.getRotation()) {
            case Surface.ROTATION_0:
                mSensorX = event.values[0];
                mSensorY = event.values[1];
            case Surface.ROTATION_90:
                mSensorX = -event.values[1];
                mSensorY = event.values[0];
            case Surface.ROTATION_180:
                mSensorX = -event.values[0];
                mSensorY = -event.values[1];
            case Surface.ROTATION_270:
                mSensorX = event.values[1];
                mSensorY = -event.values[0];
        if (gameManager != null)
        gameManager.handleSensor(mSensorX, mSensorY);

--- End code ---
This method is mostly copied from Android sample ball game

gameManager is the instance of my game's class, which handles user input, game loop, creating game objects etc. gameManager.handleSensor(mSensorX, mSensorY) can be be replaced with your  method handling accelerometer values.

Then goes GameManager class:

--- Code: ---
final int TICKS_PER_SECOND = 50;
final int SKIP_TICKS = 1000 / TICKS_PER_SECOND;
* Game loop thread
* @author 32kda
protected class GameLoop extends Thread {

public GameLoop() {

public void run() {
long ticks = System.currentTimeMillis();
while (!isPaused()) {
long current = System.currentTimeMillis();
if (current - ticks < SKIP_TICKS) {
try {
sleep(SKIP_TICKS - (current - ticks));
} catch (InterruptedException e) {
updateGame(System.currentTimeMillis() - ticks); //Call update game method with some time interval
ticks = current;

public boolean isPaused () {
return false;


final SimpleVector initialVectorUp = new SimpleVector(0f,-1f,0f); //Initial vector pointing up
SimpleVector vectorUp = new SimpleVector(0f,-1f,0f); //Camera's up vector. We reinit & rotate it any time we change camera orientation to set camera up/down angle with it

private float sensorX; //Accelerometer X & Y values
private float sensorY;
private float initialY = Float.MIN_VALUE; //field for storing initial y accelerometer value
private SimpleVector cameraVector = new SimpleVector(0f,0f,1f);  //Camera horizontal orientation/azimuth vector
private SimpleVector orthoVector = new SimpleVector(); //Vector for rotating camera up/down. should be orthogonal to cameraVector when rotating
private Matrix cameraMatrix = new Matrix();  //Camera up vector rotation matrix
private float angle = 0; //Camera up/down rotation angle

*handleSensor method. Simply remember sensorX / sensorY val for future use
public void handleSensor(float mSensorX, float mSensorY) {
this.sensorX = mSensorX;
this.sensorY = mSensorY;

*This method is called from game loop & used to update our world, camera, etc.
protected void updateGame(long delta) {

if (Math.abs(sensorX) > 0.3) { //If x accelerometer value is greater than some threshold - rotate camera direction vector according to it
cameraVector.rotateY(-sensorX / 150);
Camera camera = world.getCamera();
if (initialY == Float.MIN_VALUE || initialY == 0.0) //Because it's not convenient to play with device placed strictly horizontally, I use some "initial" value
initialY = sensorY; //for vertical angle, and use it as position corresponding to zero vertical angle for camera
float yDiff = sensorY - initialY; //Calc the differnce
if (Math.abs(yDiff) > 0.3) {
angle -= yDiff / 200;
if (angle > 0.40f) //Limit vertical angle
angle = 0.40f;
if (angle < -0.40f)
angle = -0.40f;
vectorUp.set(initialVectorUp); //Re-init camera up-vector before rotating it to necessary angle
orthoVector.x = cameraVector.z; //Calculate vector orthogonal to camera vector (camera up/down riotation vector)
orthoVector.z = -cameraVector.x;
calcRotMatrix(cameraMatrix,orthoVector,angle); //Calculate rotation matrix
vectorUp.rotate(cameraMatrix); //rotate camera up vector
camera.setOrientation(cameraVector,vectorUp); //Set camera orientation

* Calculates rotation matrix for arbitrary axis
* @param matrix Matrix to fill
* @param rotVector Arbitrary rotation axis vector
* @param angle Rotation angle
         * Calculation algorythm got from the Internet & it works. Can't explain in details how(
protected void calcRotMatrix(Matrix matrix, SimpleVector rotVector, float angle) {
double sin = Math.sin(angle);
double cos = Math.cos(angle);
double subcos = 1 - cos;
float x = rotVector.x;
float y = rotVector.y;
float z = rotVector.z;

matrix.set(0,0,(float) (cos + subcos*x*x));
matrix.set(0,1,(float) (subcos*x*y - sin*z));
matrix.set(0,2,(float) (subcos*x*z + sin*y));

matrix.set(1,0,(float) (subcos*y*x + sin*z));
matrix.set(1,1,(float) (cos + subcos*y*y));
matrix.set(1,2,(float) (subcos*y*z - sin*x));

matrix.set(2,0,(float) (subcos*z*x - sin*y));
matrix.set(2,1,(float) (subcos*z*y + sin*x));
matrix.set(2,2,(float) (cos + subcos*z*z));

--- End code ---


[0] Message Index

[#] Next page

Go to full version