PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Camera movement

Could anyone help me to make a 1st person like camera movement based on the worldtranslate and worldrotate. Thanks in advanced.
Hi Raq,

Here is an example which uses x, y and z-rotation. This may be suitable for games with free movement in 3 dimensions. First add variables for translation and rotation your framework class:

Code:

//in your class definition
EVECTOR3D vrot;
EVECTOR3D vtrans;


And initialize them in the constructor. The default position is above the model and moved away from the camera.

Code:

vrot.x = 0;
vrot.y = 0;
vrot.z = 0;
vtrans.x = 0;
vtrans.y = -1024 * 16;
vtrans.z = -2048 * 256;


Matrix math is used to determine the speed in which of the x, y and z-axis the camera moves. This depends on the rotation of the camera. Add code to OnNextFrame:

Code:

EVECTOR3D vtransadd;
long matrix[16];
ClassEMath::MatrixLoadIdentity(matrix);
ecd.math->MatrixSetRotate((unsigned short)vrot.x, (unsigned short)vrot.y, (unsigned short)vrot.z, matrix);
vtransadd.x = ((4096 * matrix[8]) >> 12);
vtransadd.y = -((4096 * matrix[9]) >> 12);
vtransadd.z = ((4096 * matrix[10]) >> 12);


These can be used to add to the translation vector to change position:

Code:

if (ecd.input->KeyHold(DIAM_MENUCONFIRM))
{
vtrans.x += vtransadd.x * (long)timedelta / 64;
vtrans.y += vtransadd.y * (long)timedelta / 64;
vtrans.z += vtransadd.z * (long)timedelta / 64;
}


The camera rotation can be changed with the arrow keys:

Code:

if (ecd.input->KeyHold(DIAM_MENUUP))
vrot.x -= timedelta;
else if (ecd.input->KeyHold(DIAM_MENUDOWN))
vrot.x += timedelta;
if (ecd.input->KeyHold(DIAM_MENULEFT))
vrot.y -= timedelta;
else if (ecd.input->KeyHold(DIAM_MENURIGHT))
vrot.y += timedelta;


Finally use these for the WorldTranslate and WorldRotate functions:

Code:

display->WorldReset();
display->WorldRotate(vrot.x, vrot.y, vrot.z);
display->WorldTranslate(-vtrans.x / 16, vtrans.y / 16, vtrans.z / 16);


Let us know if it helps :)
Hi Edge,
The code is great It helped me so much, this is exactly what I was asking for, thank you so much. See this went into a little great tutorial :wink:
Ok I have another one question that I can't handle with. Well when we translating the world, we translating all the objects in this world, so for example if we have a character in that world how can we make so that character translates with the world translation speed so that we could always see it in the camera window (need this for 3rd person view). Hope you understand :roll:
Hi,

If you want to have a third person view we recommend making the following changes to the sample above:

Use the position and rotation vectors to tranlate and rotate the character model, instead of the world.

Change the world translation and rotation to take the same position as the character model, and zoom out:

Code:

display->WorldReset();
display->WorldTranslate(0, 0, -1024); //zoom out
display->WorldRotate(vrot.x, vrot.y, vrot.z);
display->WorldTranslate(-vtrans.x / 16, vtrans.y / 16, vtrans.z / 16);
Hi again,
Well I'am able to make the character rotation the same as the camera rotation, but I still can't make the character to move with the same speed as the camera. I mean if I use
Character.Translate(-vtrans.x / 16, vtrans.y / 16, vtrans.z / 16);
and than making the same for the world:
display->WorldTranslate(-vtrans.x / 16, vtrans.y / 16, vtrans.z / 16);
the character translates slower than the world and I don't know how much slower because I was trying to increase the character translation speed twice but it didn't work out.
Hi Raq,

Would you like to try changing the order of the function calls? Call them in the following order:

- World reset
- World rotate
- World translate (zoom out)
- World translate

Let me know if it helps.
Hi Edge,
I've fixed this problem, the problem was because I called the model.Scale before the translation and rotation :lol: when I called it after translation and rotation everything become ok.
Now everything is great, thank a lot for the help.