Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.

Topics - stownshend

Pages: [1]
Support / Flipping Phone causes Weird Translation
« on: October 12, 2011, 10:49:34 am »
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):

Code: [Select]
* 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.

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:

Code: [Select]
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 = 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.

Support / Understanding Translations
« on: October 04, 2011, 10:26:25 am »
Hi all,

I load, rotate, and translate a bunch of objects in my onSurfaceChanged() method (will change this to onSurfaceCreated()) and I'm trying to understand the effect that translations made in one place have on subsequent calls to translate().

For example, in the following code, will object2's initial position be impacted by the translation of object1?

(note the loadOBJModel method is a custom one I wrote as a shortcut to the model/texture loading process)

Code: [Select]
object1 = new Object3D(loadOBJModel(MODEL_1, TEXTURE_NAME_1));


object2 = new Object3D(loadOBJModel(MODEL_2, TEXTURE_NAME_2));


Furthermore, when I am using rotations and translations, is there an order I should be doing these?

This feels like graphics programming 101. I've written Open GL / GLU / GLUT code in C++ before and not been so confused because I could use GL_START and GL_END tags to start and stop each transformation (I think that's what they were called).

IF the answer to the above questions are yes, translate() is impacted by the previously translated position... What is an easy way to translate an Object3D to a particular point in world space?

Support / 3DS Max & JPCT
« on: October 03, 2011, 03:25:48 am »
Hi all,

I am coding in JPCT a game and I have a friend developing the models in 3DS Max, which we're exporting as the .3ds file format.

For the most part it works great, but sometimes when I import the models into JPCT they appear in unusual places.

From what I understand, whatever "world" position the models took up in 3DS Max, they should appear (although rotated to right hand notation) in the same world position in JPCT.

Most of the time this works fine. However (and I seem to remember it always happens when he has multiple objects in a single scene, and exports the objects separately) the objects will appear way off.

For example; One time we had a basic square tile model (5x5 plane) with a PNG texture image. In his project the tile was centred at 0,0,0 but when I imported it into JPCT it was at something bizarre like 278,-48,3. We never worked out why this happened, but he created a new project fresh and re-created it and it worked when he exported it as expected.

I'm 90% sure the problem is with his 3DS Max project/settings, but thought I'd post it here in the hope that someone else has had similar issues and might know if there is anything I can do at my end (or even any settings or hints for 3DS Max he could do) to stop this happening.

In the meantime we're looking at shifting to Blender. He's spent hours and hours on a level which contains 6 models all up, and when we import them they appear all over the place and are no longer aligned. Aligning them manually with translations myself would be painful, and from a theoretically point of view I need to know WHY these models are not importing in the same position as they are in their originating project.

Many thanks,


Support / Keeping track of character direction
« on: September 08, 2011, 02:18:07 am »
I have a character in my game who sits in the middle of a 5x5 grid of tiles. Aesthetically she has a front (where she is looking).

When I click on one of the tiles, I want my character to look at the centre of that tile.

I've currently spent 6 hours or more trying to work this out but I'm failing miserably. I will describe my approach below, but if there is an easier way or a way JPCT-AE can assist me in doing this, that would be great.

My approach:

My character is called the Queen. I've set up a property which is a SimpleVector which represents the direction the Queen is looking. At first she is looking away from the camera so this Vector is set to (0,0,1).

Code: [Select]
private SimpleVector queenDirection;

And the initialization of the initial direction:

Code: [Select]
public void onSurfaceCreated(GL10 gl, EGLConfig config)
// The Queen is originally facing to 0 degrees.
queenDirection = new SimpleVector(0f,0f,1f);

My algorithm is:

- Calculate a vector from the Queen's current position (0,0,0 for now in world space) to the centre of the tile selected. I use the formula vector = destinationX-currentX, destinationY-currentY, destinationZ-currentZ

Code: [Select]
SimpleVector eyeline = new SimpleVector(
destinationX - queen.getTransformedCenter().x,
destinationZ - queen.getTransformedCenter().z).normalize();

- Then I calculate the angle (in radians) between the Queen's current viewing direction, and this new vector using the formula angle = acos((v1 DOT v2) / (v1.length * v2.length)):

Code: [Select]
float dotProduct = queenDirection.calcDot(eyeline);

Double temp = (double)  dotProduct / ( queenDirection.length() * eyeline.length());

double angle = Math.acos(temp)

... but already I run into problems because (perhaps due to the accuracy of float/double or something else) the dotProduct and therefore "temp" variable which is passed into the arc cosine function is sometimes either slightly higher than 1 (1.00000123) or it's negative - either way the acos function returns NaN because it only accepts parameters between 0 and 1.

The code which actually does the rotation is:

Code: [Select]
if ( angle > 0.5 )
queen.rotateY((float) angle);

queenDirection.x = eyeline.x;
queenDirection.z = eyeline.z;

I'm not great at mathematics, but I'll get stuck into if it I have to. My primary question is whether there is an easier way? If not, I'll keep working at this but I wanted to check before I spend another 6 hours at it.

This game is going to have a lot of models who need to turn and face each other often, so I need to have this down before I move on.

Support / Animating
« on: September 07, 2011, 07:03:03 am »
This isn't a question specifically about JPCT-AE and more about GLSurfaceView.

I have a 5x5 grid of tiles. If I touch one of the tiles on screen the camera re-adjusts so that it is centered on that tile. I am trying to animate this process so that it is a smooth transition rather than instant.

To do this I've set up two sets of co-ordinates:

1) The destination x, y, and z co-ords.
2) The current camera co-ordinates

I'm only looking at the x axis currently. In the onDrawFrame() method I do a check. If the current camera X position is not (roughly) equal to the destination X position, then I increment/decrement the current camera X co-ordinate by some small amount (0.1 units for example) to bring it closer to the destination.

My thinking is that it will continue to increment or decrement itself until it is equal to the destination X position, in which case we've reached our destination and no more animation is required.

My problem is:

- If I have the increment amount to something small (cameraX = cameraX + 0.001) then the animation is smooth but extremely SLOW.
- If I have the increment amount to something bigger (cameraX = cameraX + 1) then the animation is fast enough but extremely CHUNKY.

Is my approach valid? How do I implement smooth camera movement animation?

Any help would be much appreciated.

FYI my animation code is below. The modifier is either +1 or -1 depending if the destination is to the left or the right of the current position.

Code: [Select]
public void onDrawFrame(GL10 gl) {

if ( cameraX < destinationX-0.1 || cameraX > destinationX+0.1 )
Float xModifier = 1f;

if ( cameraX > destinationX )
xModifier = -1f;

cameraX = cameraX + (1f * xModifier);



In my AVD I'm getting only 9 FPS on average. It's a Mac laptop (I'm away on holiday). Not sure if this is related to the chunkiness.

Support / Axes in JPCT and 3DS Max
« on: August 25, 2011, 12:14:23 pm »
Hi all,

Apologies if I'm asking a question already covered in previous posts. I've looked around but not found the discussion I'm after.

I have a friend developing models in 3DS Max and I am importing them and using them in JPCT. Naturally the co-ordinate system is quite different:

What's the best way of handling the difference in axes? When he sends me a model which is facing "forward" it is facing "down" to me and I see its feet (as per the difference in axes).

Is there a way to change the axes in JPCT? Or is it better practice to rotate each Object3D in my scene to fit the existing JPCT system?

Ideally I'd like to import models and have them show up as my colleague sees if this is possible.

Support / Matching co-ordinate scale from JPCT and 3DS Max
« on: August 20, 2011, 01:42:06 am »
My friend is creating tiles for a game we're developing for Android.

In JPCT-AE/OpenGL I want each tile to be exactly 10x10 units in size (so I could place one tile at (0,0,0), (0,-10,0),(10,-10,0),(10,0,0) (for example).

Has anyone discovered a way to synchronise the units that he sees in 3DS Max to the measurements I have in JPCT-AE or vice versa? I want him to be able to make the tile to the correct scale for me to use or alternatively a way I can scale his tile to be perfect 10x10 units in size on the zx plane.

Or have any resources explaining how the unit systems work in the Open GL Cartesian Co-ordinate system?

I want these tiles to be perfectly flush with each other.

Many thanks,


Support / Behaviour of World.addObject() method
« on: August 18, 2011, 10:21:50 pm »
Hi all,

I've imported a 3DS model created by a friend which is a flat map-like model to be used as the basis for my level.

Once I've created it and I call world.addObject(level1map); what is the default position in world space at which the object is added? The origin?

And how does it place the object? Which vertex does it choose to position at the origin/start point? i.e. does it start from the lowest x, highest y, lowest z co-ordinate or something? Or is there a "centre" property in a 3DS model?

At the moment its centred at 717,-83,95 and I don't know why.

(In writing this I think I might know why this is happening... I'm scaling the model to 1% of its original size which is my problem - if I am going to scale it I probably need to find the "centre" of the model and place it on the origin first if I want it to sit at 0,0,0 in world space).

Any advice / points to information around how to translate and find the centre of an object in worldspace would be much appreciated.

FYI I'm new to 3D modelling in Android, but I have done two basic Open GL projects using GL/GLU/GLUT in the distant past.

Pages: [1]