• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Medo Mex

Transformation and Rendering Speed

7 posts in this topic

[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
0

Share this post


Link to post
Share on other sites
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
1

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


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

Share this post


Link to post
Share on other sites
[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.
0

Share this post


Link to post
Share on other sites
[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.
0

Share this post


Link to post
Share on other sites
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]
0

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  
Followers 0