Sign in to follow this  

FPS Movement

This topic is 4545 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

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

This topic is 4545 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this