PDA

View Full Version : How do you get the point?



howie
05-07-2006, 03:46 PM
To calculate the angle between the player and an agent(via the dot product), I need to get the point on the map, the player is at.

Since this is a FPS game, you move the player around by holding the forward button and steer with the mouse. So somehow I need to pull this point from the matrix. To make this even more crazy, the point neeeds to be as if the player is in local view. As if the player has been moved around the map like the agents are.

howie
05-07-2006, 04:05 PM
never mind. I got it.

k_szczech
05-07-2006, 04:31 PM
From what I've read from your previous post and from this one I'm starting to get the feeling that you are trying to do all the things exactly the opposite way they should be. It's ok, I used to make the same mistakes in the beginning, but I strongly suggest you get things right before your code become 10 times more complicated and slower than it should be.
First of all you should never try to get player's position from OpenGL - you should pass it to OpenGL from your application (by setting up projection/modelview matrices).

The simplest thing would be to keep EVERYTHING in one coordinate system (world space). This includes player position, agents, map, light sources, sound sources, camera position, particles - the list goes on.
Never compute object's movement relative to another object - calculate all physics, ai and player movement in world space.
When rendering place camera where you want it and use gluLookAt or any other method to setup projection/modelview matrices. Always REINITIALIZE your matrices before rendering a frame (glLoadIdentity).
Use glPushMatrix / glTranslate / glRotate / glPopMatrix to draw movable objects.

Never try to extract player position from matrices, please! ;)

Look at this:

struct Player
{
float x, z;
float angleY;
};
void updatePlayer(void)
{
float forwardX = sin(angleY * 3.14159265f / 180.0f);
float forwardZ = -cos(angleY * 3.14159265f / 180.0f);
x = x + forwardX * playerSpeed * timeSinceLastFrame;
z = z + forwardZ * playerSpeed * timeSinceLastFrame;
}This way you always have your player's position stored in x and y fields. You could have agent's position stored the same way.
When you want to render the world visible from players eye, then you do something like this:

void Render(void)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(.....);
glRotatef(-player.angleX, 0, 1, 0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(-player.x, 0.0, -player.z);

renderMap();

glPushMatrix();
glTranslatef(agent.x, 0.0, agent.z);
glRotatef(agent.angleY, 0, 1, 0);
renderAgent();
glPopMatrix();
}Note, that player's coordinates and angle are reversed when passed to matrices - we do that because player becomes a camera. If you would want to see world from agent's point of view then you use reversed agent's coordinates with the matrices.

Get things right before you move on with the code, really.

Best regards.

howie
05-07-2006, 10:43 PM
k_szczech - way cool. thanks!!

You've given me a whole new way to think about all this. Thanks for taking out the time to share your knowledge! Nice!