Jump to content
  • Advertisement

Archived

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

hammerstein_02

Obtaining the movement..

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

OK, I have come to work on a frame-independant movement system (my first time I have got this far) and I am wondering how to implement it correctly, here is what I have..
  


void cFPS::Setup()
{
	fps = 0;
	lastGameTime = 0;
	dt = 0;
	lastUpdate = 0;

	if(QueryPerformanceCounter((LARGE_INTEGER*)&ticksPerSecond))
		ticksPerSecond = 1000;

	timeStart = 0;
	timeStart = GetGameTime();
}

float cFPS::GetGameTime()
{
	UINT64	ticks;
	float	time;

	if(QueryPerformanceCounter((LARGE_INTEGER*)&ticks))
		ticks = (UINT64) timeGetTime();

	time = (float)(__int64)ticks/(float)(__int64)ticksPerSecond;

	time -= timeStart;

	return(time);
}

void cFPS::UpdateFPS()
{
  numFrames++;

  float currentUpdate = GetGameTime();

  if( currentUpdate - lastUpdate > fpsUpdateInterval )
  {
    fps = numFrames / (currentUpdate - lastUpdate);
    lastUpdate = currentUpdate;
    numFrames = 0;
  }
}

void cFPS::Update()
{
	UpdateFPS();

	dt = GetGameTime() - lastGameTime;

	lastGameTime += dt;
}
  
I got the code from Angelcode from a link a friend sent me. Now when I move my player each frame, I try to get the dt and my app crashes, any ideas? =*= If things seem bad, think that they can get a whole load worse, and they don''t seem so bad anymore =*=

Share this post


Link to post
Share on other sites
Advertisement
where is ticksPerSecond defined? if it's not being set to something meaningful you are dividing by zero in GetGameTime(). also you should be diviting the results of QueryPerformanceCounter by QueryPerformanceFrequency so that your value is the same for all computo devices (i.e. store the result of QueryPerformanceFrequency in ticksPerSecond).

also, the block with QueryPerformanceCounter should be:

if (!QueryPerformanceCounter....

QueryPerformanceCounter returns a non-zero value when it is sucessful so you only want to revert to GetTickCount if it returns false.

-me

[edited by - Palidine on January 22, 2003 6:01:40 PM]

Share this post


Link to post
Share on other sites
Ok, that was stupid of me and rather hasty, I posted without fully checking the obvious. A bit of variable assignment and all was good.! But now I have a new problem, nothing moves.. I have checked my TimeUpdate() function does return a value, but my player isn''t moving when I press keys anymore


  
float dt = timeUpdate->TimeUpdate(); //Only do this function call once


if(keys.right)
{
if(((int)m_vPosition.x + m_iWidth) + m_MovementVal < m_pGraphics->GetScreenWidth())
{
m_vPosition.x += (m_MovementVal * dt);
}
else
m_vPosition.x = (m_pGraphics->GetScreenWidth() - m_iWidth);
}

if(keys.left)
{
if((int)m_vPosition.x > 0 + m_MovementVal)
m_vPosition.x -= (m_MovementVal * dt);
else
m_vPosition.x = 0;
}

if(keys.up)
{
if((int)m_vPosition.y > 0 + m_MovementVal)
m_vPosition.y -= (m_MovementVal * dt);
else
m_vPosition.y = 0;
}

if(keys.down)
{
if(((int)m_vPosition.y + m_iHeight) + m_MovementVal < m_pGraphics->GetScreenHeight())
m_vPosition.y += (m_MovementVal * dt);
else
m_vPosition.y = (m_pGraphics->GetScreenHeight() - m_iHeight);
}


This is still general programming.. and it is related, so not worth another thread. Thanks for your reply paledine. I did change the QueryPerformanceCounter bit to a if(!Query.... however I don''t quite understand what you mean about the Frequency bit, could you explain that a little more?

=*=
If things seem bad, think that they can get a whole load worse, and they don''t seem so bad anymore

=*=

Share this post


Link to post
Share on other sites
QueryPerformanceCounter returns the number of clock cycles that have passed on your CPU since some start time. if you just have that number it's going to end up big on some machines (fast clocks) and slow on others (slow clocks). so you need a way to normalize the number to a standard (say seconds). so you use QueryPerformanceFrequency which gives you the number of clock ticks per second on your processor.

so you want to get the ticksNow with QueryPerformanceCounter and subtract from it the tisksThen which has stored the value you got from QueryPerformanceCounter the last time the fctn was run.

so -> ticksPassedSinceLastFrame = ticksNow - ticksThen;

to normalize you then divide that number by the number you get from QueryPerformanceFrequency. which converts your ticks into seconds

ticks / ticks/second => seconds.....

you should read about QueryPerformanceCounter and QueryPerformanceFrequency on msdn (http://msdn.microsoft.com : use the search feature)

i'd check to make sure that QueryPerformanceFrequency isn't already being called in your copied code otherwise you'll just prrobably make things worse. the problem with copying code is that it's a bitch to debug if you have no idea how it works.

-me

[edited by - Palidine on January 22, 2003 6:46:41 PM]

Share this post


Link to post
Share on other sites
Thanks yet again paledine. I appreciate your answers. And I do know that copying code is not a good idea. I do like to learn, which is why I wrote the code into my own program using a method I decided upon (possibly why the problems). I do understand now what it is doing, and I understand why I was doing what I was doing, and changing the first QueryPerformanceCounter to the frequency one instead fixed my problem.

=*=
If things seem bad, think that they can get a whole load worse, and they don''t seem so bad anymore

=*=

Share this post


Link to post
Share on other sites
heh. sorry for coming off harsly. it wasn't spoken in my head with that tone. i grab code for various things frequently (a lot of times it's old code of my own that i've forgotten the particulars of). always bashing my own head against the same wall

glad it worked out.

-me

[edited by - Palidine on January 22, 2003 7:08:14 PM]

Share this post


Link to post
Share on other sites

  • 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!