Best way to store player position?

Started by
1 comment, last by Tigra7 20 years, 10 months ago
I am trying to create a small fps in opengl using c++. I was just wondering what some other people''s oppinions were to store the player position and move it accordingly. Particularly, my problem lies within my method for storing it. I use two functions. One of them is MovePlayer(float x, y, z). Another is CheckForKeyPress(), which looks like this: { if(Keypressed = ''VK_UP'') { MovePlayer(speed.x, y, speed.z); } } Now the problem with this is, when I press Up, it changes the position while I press it, but it resets itself when I release the button. How can I fix this? Thanks for any respones.
Advertisement
instead of calling your move function only when a key is pressed, call your move function every frame. As far as when a key is pressed, in the code for that you should set the speed to something higher... maybe Player.xSpeed += 3; or something similar... you might also consider having a direction and magnitude (vector?) member of the player, which describes how far & what direction they''re going to move next frame... and when you press up, generate a vector and add it to the member, which would modify it. then when movePlayer() is called, it will take the vector and move the player in that direction (note, when the vector / speeds are 0, it wont actually move the player...)
there''s 2 ways that you can go about storing your location. First use a structure:

TYPEDEF STRUCT coords {
float x,y,z;
}

Or you can store them array. A problem with this is that you have to remember which value your storing in each element of the array. To do this use:

float coords[3] = {0.0f, 0.0f, 0.0f);

This should work if my coding is correct, just a matter of how you put it to use.

Higher Forces

This topic is closed to new replies.

Advertisement