Jump to content

  • Log In with Google      Sign In   
  • Create Account


Transformation and Rendering Speed


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 Medo3337   Members   -  Reputation: 665

Like
0Likes
Like

Posted 29 November 2012 - 03:49 AM


float x;

void move()
{
	x+=0.2;
}

void render()
{
	   device->Clear(...);
	   device->BeginScene();

	   move();
	  
	   // Code to do any transformation to the mesh according to x value...

	  // Other rendering code goes here...
	 device->EndScene();
	 device->Present(...);
}

In some computers, the above code will render 60 times per second, in some other computers the above code will render 40 times per second, this will cause the mesh to move faster in (60 fps) computers while slower in 40 fps computers.

How do I make the mesh move at the same speed no matter how many FPS?

I thought about doing the following:
DWORD currentTime = timeGetTime();
timeElapsed = currentTime - lastTime;
float speed = 0.1f;
x += speed * timeElapsed;

But the problem is that I could call the method move() from any part of the program, I will not be able to know 'timeElapsed' unless I'm calling inside render().

Edited by Medo3337, 29 November 2012 - 03:55 AM.


Sponsor:

#2 Hodgman   Moderators   -  Reputation: 28405

Like
1Likes
Like

Posted 29 November 2012 - 03:58 AM

You'll find some answers under "framerate independent movement".

The simplest way is to measure how long the previous frame took to execute, and then use that amount of time as a scaling factor on all movement.
void move(float dt)
{
	x+=0.2 * dt; // moves 0.2 units per second, instead of units per frame
}

long long previousTicks; // when dealing with *absolute* time values, you want to use 64-bit integers for precision, not floating point.

void render()
{
  long long currentTicks = ReadClockTickCounter();
  long long deltaTicks = currentTicks - previousTicks;
  previousTicks = currentTicks;
  float deltaTimeInSeconds = (float)( (double)deltaTicks / (double)ClockTicksPerSecond() );
  //for *relative* time values, floating point is ok

  move( deltaTimeInSeconds );
On Windows, the made up methods "ReadClockTickCounter" and "ClockTicksPerSecond" can be implemented using QueryPerformanceCounter and QueryPerformanceFrequency. Alternatively you can use timeGetTime (and the number 1000 for the ticks per second), but it's not really accurate enough for most games.
Personally, I use timer_lib, which has implemented a cross-platform high-frequency timer for me.

Once you're comfortable with this basic method, you could also look at using a fixed time step.

Edited by Hodgman, 29 November 2012 - 04:14 AM.


#3 Medo3337   Members   -  Reputation: 665

Like
0Likes
Like

Posted 29 November 2012 - 04:10 AM

Thanks for your quick response, but the problem I'm having is that I could call the method move() from any place in the program other than calling it from render(), so I don't know what is the value of 'deltaTimeInSeconds'.

Take a look at the following:
void render()
{
  long long currentTicks = ReadClockTickCounter();
  long long deltaTicks = currentTicks - previousTicks;
  previousTicks = currentTicks;
  float deltaTimeInSeconds = (float)( (double)deltaTicks / (double)ClockTicksPerSecond() );
  // I don't need to move it while rendering, it will be moving when move() is called from other methods (move() will not be called from render())
  //move( deltaTimeInSeconds );
}

void work() // This method has nothing to do with render() and it's not called from render()
{
       move(???); // Since work() has nothing to do with render(), I don't know what is the value of deltaTimeInSeconds
}

Edited by Medo3337, 29 November 2012 - 04:11 AM.


#4 Hodgman   Moderators   -  Reputation: 28405

Like
0Likes
Like

Posted 29 November 2012 - 04:24 AM

In almost every game, at some point there's a main loop that executes 'frames'. The timing logic that determines dt can be moved up to this level.
If something else needs to know dt, then it can be passed down from the loop.
e.g. say you've got:
void move() { /*need dt*/ }
void foo() { move(); }
void bar() { foo(); }
void game_main()
{
	for(;;)
	{
		bar();
	}
}
Then it becomes:
void move(float dt)  { ... }
void foo(float dt) { move(dt); }
void bar(float dt) { foo(dt); }
void game_main()
{
	Timer timer;
	for(;;)
	{
		float dt = timer.update();
		bar(dt);
	}
}
This is just a part of learning how to architecture your code properly. Eventually answering questions like that will just be second nature.

When people are first starting out, it's sometimes useful to use "simpler" architectures (that work, but wouldn't be good choices in a large-scale project) though, such as just making your timer a global variable. This way any part of your program can access dt. N.B. this is just a crutch to make learning easier though, not a real solution.

#5 Medo3337   Members   -  Reputation: 665

Like
0Likes
Like

Posted 29 November 2012 - 04:34 AM

Since I have too many parts in the game and too many methods that can call move(), I think a good option is to make "deltaTimeInSeconds" global variable, will that work correctly no matter how large the project is?

#6 Tispe   Members   -  Reputation: 982

Like
0Likes
Like

Posted 29 November 2012 - 04:48 AM


float x;

void move()
{
	x+=0.2;
}

void render()
{
	   device->Clear(...);
	   device->BeginScene();

	   move();
	  
	   // Code to do any transformation to the mesh according to x value...

	  // Other rendering code goes here...
	 device->EndScene();
	 device->Present(...);
}



Do move() before BeginScene. You prepare all mesh data (transforms) before you call begin scene. You only draw when everything is ready, this frees up time so you dont "hog" the GPU while doing math stuff you could do another place.

The only time you want to do this stuff like "move() while rendering" is inside a shader.

#7 kunos   Crossbones+   -  Reputation: 2203

Like
0Likes
Like

Posted 29 November 2012 - 05:14 AM

Since I have too many parts in the game and too many methods that can call move(), I think a good option is to make "deltaTimeInSeconds" global variable, will that work correctly no matter how large the project is?


yes it will work. But you will not learn anything from doing it, and if this approach is valid for "deltaT" then it is valid for every variable.
At that point you might end up with everything being global and your intuition should suggest that that isn't really a good thing.

I don't think you are releasing your game tomorrow.. so there is no rush to implement this as an hack. Take your time to use this as an excuse to learn how to structure your code properly.

In my code, all the "update" methods have a "float deltaT" input parameters. "update" is only called from 1 place, so, as long as your deltaT variable is available there, you are done. Movement can only happen in a "update" function.. stick to these very simple rules and you are done with a clean and safe solution.
Stefano Casillo
Lead Programmer
TWITTER: @KunosStefano
AssettoCorsa - netKar PRO - Kunos Simulazioni

#8 Medo3337   Members   -  Reputation: 665

Like
0Likes
Like

Posted 29 November 2012 - 08:28 AM

I tried to make the elapsedTime global and I see the rendering is clearly lagging, so I made a method like move() asking for parameter elapsedTime: move(float elapsedTime).

I'm now doing something like:
float speed = 0.1f;
void move(elapsedTime)
{
     x += elapsedTime * speed;
}

This should work perfectly if I will always call move() from render(), but here is the problem, look at this method:
// The following is a callback method, the method will be called when the collision is detected in order to respond to that collision

void collisionCallback()
{
       move(?) // I want to move the mesh, how can I know "elapsedTime" in this case? When I use global variable the whole game is lagging
}





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS