Sign in to follow this  
Medo Mex

Transformation and Rendering Speed

Recommended Posts

Medo Mex    891
[CODE]

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(...);
}
[/CODE]

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.

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

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

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

Share this post


Link to post
Share on other sites
Hodgman    51226
You'll find some answers under "[url="http://www.google.com.au/search?q=framerate+independent+movement"]framerate independent movement[/url]".

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.
[code]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 );[/code]On Windows, the made up methods "ReadClockTickCounter" and "ClockTicksPerSecond" can be implemented using [url="http://msdn.microsoft.com/en-us/library/windows/desktop/ms644904(v=vs.85).aspx"]QueryPerformanceCounter[/url] and [url="http://msdn.microsoft.com/en-us/library/windows/desktop/ms644905(v=vs.85).aspx"]QueryPerformanceFrequency[/url]. Alternatively you can use [url="https://github.com/rampantpixels/timer_lib"]timeGetTime[/url] ([i]and the number 1000 for the ticks per second[/i]), but it's not really accurate enough for most games.
Personally, I use [url="https://github.com/rampantpixels/timer_lib"]timer_lib[/url], 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 [url="http://gafferongames.com/game-physics/fix-your-timestep/"]fixed time step[/url]. Edited by Hodgman

Share this post


Link to post
Share on other sites
Medo Mex    891
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:
[CODE]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
}
[/CODE] Edited by Medo3337

Share this post


Link to post
Share on other sites
Hodgman    51226
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:[code]void move() { /*need dt*/ }
void foo() { move(); }
void bar() { foo(); }
void game_main()
{
for(;;)
{
bar();
}
}[/code]Then it becomes:[code]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);
}
}[/code]
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 ([i]that work, but wouldn't be good choices in a large-scale project[/i]) 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.

Share this post


Link to post
Share on other sites
Medo Mex    891
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?

Share this post


Link to post
Share on other sites
Tispe    1468
[quote name='Medo3337' timestamp='1354182541' post='5005243']
[CODE]

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(...);
}
[/CODE]


[/quote]

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.

Share this post


Link to post
Share on other sites
kunos    2254
[quote name='Medo3337' timestamp='1354185256' post='5005253']
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?
[/quote]

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.

Share this post


Link to post
Share on other sites
Medo Mex    891
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:[CODE]
float speed = 0.1f;
void move(elapsedTime)
{
x += elapsedTime * speed;
}
[/CODE]

This should work perfectly if I will always call move() from render(), but here is the problem, look at this method:
[CODE]
// 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
}
[/CODE]

Share this post


Link to post
Share on other sites

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