Archived

This topic is now archived and is closed to further replies.

framerate independant movement not working

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

my program compiles fine and works exept for timing. I'm using the high resolution timer but movement speed still vary's with framerate. i have global variables LARGE_INTEGER tickspersecond; LARGE_INTEGER currentticks; LARGE_INTEGER oldticks; double fps; at the beggining of main i set them like this: QueryPerformanceFrequency(&tickspersecond); QueryPerformanceCounter(&oldticks); QueryPerformanceCounter(¤tticks); at the beggining of my main loop I calculate the fps like so:
oldticks = currentticks;
QueryPerformanceCounter(¤tticks);
fps = double(((currentticks.QuadPart - oldticks.QuadPart)/tickspersecond.QuadPart));//this gives seconds per frame


if(fps == 0)//prevents divide by zero

{
	fps = 1.0;
}
fps = 1/fps; //turns seconds per frame into fps



and finally I calculate object movement in my object movement function:
   
int runobjects(object * objpointer, unsigned int startx, unsigned int starty)
{
bool done = 0;
	while(!done)
	{
		unsigned long int x = (startx*32) + objpointer->x;//this translates world coodinates to screen coordinates

		unsigned long int y = (starty*32) + objpointer->y;

		float ppf = 0;
		ppf = objpointer->speed/fps;//pixels per frame


		if( (x<1600) && (y<1200))//only calculate if near screen

		{
			if(objpointer->type == 1)//moving platform

			{
					//state will be either 1 or -1


					//int slopex = objpointer->endx-objpointer->x;

					//int slopey = objpointer->endy-objpointer->y;


                    double slope;

                    if( (objpointer->endy-objpointer->y) == 0)//prevents a divide by 0 error

                    {

						slope = (float(objpointer->endx-objpointer->startx))/(float(objpointer->endy-objpointer->starty));
                    }
                    else //heres our slope calculation!!

                    {
					    slope = (float(objpointer->endx-objpointer->x))/(float(objpointer->endy-objpointer->y));
                    }
					
					objpointer->x = (objpointer->x + objpointer->state*ppf);//move the platform on the x coordinate


					if(objpointer->endy < objpointer->starty)//move platform on the y-coordinate

					{
						objpointer->y = (objpointer->y-(slope*objpointer->state*ppf));

						//check if reached end

						if(objpointer->y <= objpointer->endy)
						{
								objpointer->y = objpointer->endy;

						}
						if(objpointer->y >= objpointer->starty)//we reached starty

						{
								objpointer->y = objpointer->starty;

						}

					}
					else//move platform on the y-coordinate

					{
						objpointer->y = (objpointer->y+(slope*objpointer->state*ppf));

						//check if reached end

						if(objpointer->y <= objpointer->starty)
						{
								objpointer->y = objpointer->starty;
								//objpointer->state = -objpointer->state;

						}
						if(objpointer->y >= objpointer->endy)//we reached starty

						{
								objpointer->y = objpointer->endy;
								//objpointer->state = -objpointer->state;

						}
					}

					if(objpointer->x >= objpointer->endx)
					{
						objpointer->x = objpointer->endx;
						objpointer->state = -1;
					}
					else if(objpointer->x <= objpointer->startx)
					{
						objpointer->x = objpointer->startx;
						objpointer->state = 1;
					}
					
			}
		}

		if(objpointer->nextobj == 0)
		{
			done = 1;
		}
        else
        {
            objpointer = objpointer->nextobj;
        }
	}
	return 1;
}
the moving platforms speed is in pixels per second and basically I convert that to pixels per frame and move by that amount. thanks bye. EDIT: full source code here [edited by - googlyeyes on December 13, 2003 8:41:50 PM]

Share this post


Link to post
Share on other sites