Flipping Phone causes Weird Translation

Started by stownshend, October 12, 2011, 10:49:34 AM

Previous topic - Next topic

stownshend

Hi there,

I have a level of tiles, and a main character. The camera and main character move to a tile when the user touches it. Here is the code I am using to implement this (which is inside my onDrawFrame() method):


/**
* CAMERA MOVEMENT
*
* This first section of code is part of the algorithm which handles
* camera movement. When a touch event occurs, the origin of the tile which
* was selected is saved as the destinationX and destinationZ
* properties. The following code animates moving the camera from its
* existing position to this destination. */

// To animate smoothly (frame rate independent) we're going to keep
// track of how much time elapses between frames. We need a calendar
// object to do this.
Calendar now = Calendar.getInstance();

// Calculate how much time has passed since the previously rendered frame.
long millisecondsPassed = now.getTimeInMillis() - previousFrameTime;

// If the camera isn't already approximately at the destination...
if ( cameraX < destinationX-1 || cameraX > destinationX+1)
{
// The xModifier is a variable which is +1 if the destination is down
// the positive x axis and -1 if it is down the negative.
Float xModifier = 1f;

// If the destination is down the -x axis, change the xModifier accordingly.
if ( cameraX > destinationX )
{
xModifier = -1f;
}

// Set the camera's new position. The xModifier determines if it
// will be down the +x or -x direction, and the millisecondsPassed
// makes this frame rate independent - so on a slow device the
// animation will be blocky but fast, and on a fast device the
// animation will be smooth and fast.
cameraX = cameraX + (0.01f * xModifier * millisecondsPassed);

// Translate the AntQueen object
antQueen.translate((0.01f * xModifier * millisecondsPassed), 0, 0);
}

// This block of code does exactly the same as the x-direction code
// above but for the z-direction.
if ( cameraZ < destinationZ-1 || cameraZ > destinationZ+1)
{
Float zModifier = 1f;

if ( cameraZ > destinationZ )
{
zModifier = -1f;
}

cameraZ = cameraZ + (0.01f * zModifier * millisecondsPassed);

// Translate the AntQueen object
antQueen.translate(0, 0, (0.01f * zModifier * millisecondsPassed));
}

// Record the current time for the next call to this method (the next
// frame).
previousFrameTime = now.getTimeInMillis();

// Move the camera to its new position. The -10 is a constant offset
// on the z axis.
camera.setPosition(cameraX,cameraY,cameraZ-10);


My problem is that if the camera and queen are moving when I flip the camera over (and it re-adjusts for landscape or portrait) the queen will often teleport out of sync with the camera. Normally she is meant to sit in the centre of the camera, so the camera is essentially locked on to the top of her head.

I'm trying to work out why. All of my creation code is in my onSurfaceCreated() method, and the only thing inside onSurfaceChanged() is:


public void onSurfaceChanged(GL10 gl, int w, int h)
{
// Checks if there is an existing FrameBuffer and disposes it if required.
if (frameBuffer != null)
{
frameBuffer.dispose();
}

frameBuffer = new FrameBuffer(gl, w, h);
}


I know I haven't provided a lot of information and I'm finding it hard to describe my problem, but in-case someone recognizes the issue and has dealt with it before I thought it was worth a shot posting this.

I will try and post some better information as I work through it myself.

EgonOlsen

Maybe some threading issue with the old gl thread still fiddling around with the onDrawFrame-method while the other one is already running...not sure, if Android would allow this to happen though.

K24A3

I can confirm that if translations/rotations are changed outside of the rendering thread, the objects can sometimes render in the wrong location at random frames.

Use variables to store the orientation in OnSensor(), and do all camera/object translations and rotations in onFrame() using those variables.

EgonOlsen

Quote from: K24A3 on October 15, 2011, 06:52:29 AM
I can confirm that if translations/rotations are changed outside of the rendering thread, the objects can sometimes render in the wrong location at random frames.
Yes, because jPCT isn't thread safe.

K24A3