Jump to content
  • Advertisement
Sign in to follow this  
kwake40

FPS Movement

This topic is 4886 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Can Somebody tell me the code for FPS movement for C++. I would really apreatiate it, thank you

Share this post


Link to post
Share on other sites
Advertisement
You need to be more specific, theres a hell of a lot of different parts to "FPS movement". Theres the camera/player movement, collision detection and response. And if you want that all to work at reasonable speed you're going to need some kind of space partioning, such as bsp. Like DrEvil said, if you have specific questions then ask them.

Share this post


Link to post
Share on other sites
Here's the movement code for my FPS at http://www.coolgroups.com/amaze/amazesetup5.exe:

static double xoff=8.5;
static double yoff=8.5;
static float angmike = 0;

void physics_processing(void)
{
const double rotspeed=1.5; // rotation speed in degrees
const double speed=0.06; // linear movement speed
const float closeness = 0.4; // closeness to a wall before bouncing back

if (GetAsyncKeyState(VK_RIGHT)) angmike+=rotspeed;
if (GetAsyncKeyState(VK_LEFT)) angmike-=rotspeed;

float anginrad = angmike/360.0*6.28;

if (!GetAsyncKeyState(VK_UP) && !GetAsyncKeyState(VK_DOWN)) return;

if (GetAsyncKeyState(VK_UP))
{yoff+=sin(anginrad-1.57)*speed; xoff+=cos(anginrad-1.57)*speed;
}

if (GetAsyncKeyState(VK_DOWN))
{yoff-=sin(anginrad-1.57)*speed; xoff-=cos(anginrad-1.57)*speed;
}

for (float testangle = 0; testangle < 6.28; testangle+=.1) {

set<int, less<int> >::iterator i = mikeset[int(yoff)*MAPWIDTH+int(xoff)].begin();
int thesize = mikeset[int(yoff)*MAPWIDTH+int(xoff)].size();


float orig[3] = {xoff, 0.5, yoff};
float dir[3] = {cos(testangle),0,sin(testangle)};
float goodt;

double MAXT = 5000000;
double mint = MAXT;
int minq;
for (int ctr = 0; ctr < thesize; ctr++)
{
int isect = v[(*i)].intersect(orig, dir, &goodt);
if (isect && goodt < mint && goodt > 0) {mint = goodt; minq = *i;}
i++;
}


// if the user is too close to a wall, bounce him back

if (mint != MAXT && mint > 0 && mint < closeness) {
xoff+=v[minq].norm[0]*(closeness-mint);
yoff+=v[minq].norm[2]*(closeness-mint);
}

}

}


Mike C.
http://www.coolgroups.com/zoomer/

Share this post


Link to post
Share on other sites
I really should be using a better approximation of PI. Here's a new version:

static double xoff=8.5;
static double yoff=8.5;
static float angmike = 0;

void physics_processing(void)
{
const double rotspeed=1.5; // rotation speed in degrees
const double speed=0.06; // linear movement speed
const float closeness = 0.4; // closeness to a wall before bouncing back

if (GetAsyncKeyState(VK_RIGHT)) angmike+=rotspeed;
if (GetAsyncKeyState(VK_LEFT)) angmike-=rotspeed;

float anginrad = angmike/360.0*M_PI*2;

if (!GetAsyncKeyState(VK_UP) && !GetAsyncKeyState(VK_DOWN)) return;

if (GetAsyncKeyState(VK_UP))
{yoff+=sin(anginrad-M_PI_2)*speed; xoff+=cos(anginrad-M_PI_2)*speed;
}

if (GetAsyncKeyState(VK_DOWN))
{yoff-=sin(anginrad-M_PI_2)*speed; xoff-=cos(anginrad-M_PI_2)*speed;
}

for (float testangle = 0; testangle < 2*M_PI; testangle+=.1) {

set<int, less<int> >::iterator i = mikeset[int(yoff)*MAPWIDTH+int(xoff)].begin();
int thesize = mikeset[int(yoff)*MAPWIDTH+int(xoff)].size();


float orig[3] = {xoff, 0.5, yoff};
float dir[3] = {cos(testangle),0,sin(testangle)};
float goodt;

double MAXT = 5000000;
double mint = MAXT;
int minq;
for (int ctr = 0; ctr < thesize; ctr++)
{
int isect = v[(*i)].intersect(orig, dir, &goodt);
if (isect && goodt < mint && goodt > 0) {mint = goodt; minq = *i;}
i++;
}


// if the user is too close to a wall, bounce him back

if (mint != MAXT && mint > 0 && mint < closeness) {
xoff+=v[minq].norm[0]*(closeness-mint);
yoff+=v[minq].norm[2]*(closeness-mint);
}

}

}

Mike C.
http://www.coolgroups.com/zoomer/

Share this post


Link to post
Share on other sites
Actually, this brings me to an interesting question. Does anyone know why I need to subtract 90 degrees before processing the VK_UP and VK_DOWN?

This is all I'm doing for the drawing translations:

glRotatef(angmike, 0, 1, 0);
glTranslatef(-xoff, -0.5, -yoff);

Mike C.
http://www.coolgroups.com/zoomer/

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!