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

Fixed timestep with box2d and SFML

4 posts in this topic

Hi, I've tried to implement a simple demo using box2d and SFML (a simple moving box). I've managed to implement a fixed timestep to the one described here http://www.unagames.com/blog/daniele/2010/06/fixed-time-step-implementation-box2d . The only problem is that moving my simple square around, it seems to become slightly  blurred and stretched. This maybe because its moving to fast (since it doesn't happen when moving slowly). I've ported this code to SDL as well and it seems to give similar results and I can't put my finger on why this is happening. I'm working on ubuntu but I've made a visual studios  project for windows as well showing the problem:

 

Linux (executable) https://www.dropbox.com/s/dzf4f8w6f6fl1o0/FixedTimeStepTest.rar

WIndows (visual studios project) https://www.dropbox.com/s/ghazj8tqyppk8v9/sfml%20test.zip

 

Should work, but let me know if you have issues (mostly likely need to have box2d and sfml install if your running on linux and running on windows you'll need to point to sfml directory for the include directories).

 

The actual implementation of the fixed timestep can be found below:

void B2DWorld::update(float dt, ActionController<std::string>& actionController){
    m_fixedTimestepAccumulator += dt;
    const int steps = floor(m_fixedTimestepAccumulator / FIXED_TIMESTEP);

    if (steps > 0)
	{
		m_fixedTimestepAccumulator -= steps * FIXED_TIMESTEP;
	}

	assertAccumilation();
    m_fixedTimestepAccumulatorRatio = m_fixedTimestepAccumulator / FIXED_TIMESTEP;

    const int clampedSteps = std::min(steps, MAX_STEPS);
	for (int i = 0; i < clampedSteps; ++ i)
	{
		resetStates();
		actionController.triggerCallbacks(m_fixedTimestepAccumulatorRatio);
		step(FIXED_TIMESTEP);
	}

	interpolateStates();
	m_world.DrawDebugData();

}

void B2DWorld::step(float dt){
    m_world.Step(dt, VELOCITY_ITERATIONS, POSITION_ITERATIONS);
}

void B2DWorld::interpolateStates(){

	const float oneMinusRatio = 1.0f - m_fixedTimestepAccumulatorRatio;

	for (b2Body * b = m_world.GetBodyList (); b != NULL; b = b->GetNext ())
	{
		if (b->GetType () == b2_staticBody)
		{
			continue;
		}

		PhysicsComponent *c   = (PhysicsComponent*) b->GetUserData();
		c->smoothedPosition = m_fixedTimestepAccumulatorRatio * b->GetPosition () + oneMinusRatio * c->previousPosition;
		c->smoothedAngle = floor (m_fixedTimestepAccumulatorRatio * b->GetAngle () + oneMinusRatio * c->previousAngle);
	}

}

void B2DWorld::assertAccumilation(){
	assert (
		"Accumulator must have a value lesser than the fixed time step" &&
		m_fixedTimestepAccumulator < FIXED_TIMESTEP + FLT_EPSILON
	);
}

void B2DWorld::resetStates(){
for (b2Body * b = m_world.GetBodyList (); b != NULL; b = b->GetNext ())
	{
		if (b->GetType () == b2_staticBody)
		{
			continue;
		}
 		PhysicsComponent *c   = (PhysicsComponent*) b->GetUserData();
		c->smoothedPosition = c->previousPosition = b->GetPosition ();
		c->smoothedAngle = c->previousAngle = b->GetAngle ();
	}
}

The whole project is here: https://github.com/SundeepK/Clone/tree/wip/fix_time_step

 

After a bit of playing around, it looks like it maybe the way opengl renders things or the way its being setup. Any idea's on what maybe causing this issue? Thanks.

0

Share this post


Link to post
Share on other sites
Looks like you are interpolating the position/previous position okay based on the error factor.

Don't do OpenGL but D3D single lines tend to look very blurry in motion. Guess that is how debug render is drawing them? I'd try putting some solid filled quads on and see if it is as apparent.
0

Share this post


Link to post
Share on other sites

Looks like you are interpolating the position/previous position okay based on the error factor.

Don't do OpenGL but D3D single lines tend to look very blurry in motion. Guess that is how debug render is drawing them? I'd try putting some solid filled quads on and see if it is as apparent.

 

Yeah, I tried this and it still looks odd.

0

Share this post


Link to post
Share on other sites

You can try temporarily setting m_fixedTimestepAccumulatorRatio to 1 before you call the draw call. That will at least tell you if this is anything to do with the interpolation or not.

 

I did a lot of that sort of thing when I was first trying to get interpolated motion working back in the day. Very handy to be able to rule out errors in the interpolation system.

Edited by Aardvajk
0

Share this post


Link to post
Share on other sites

I'm pretty sure that its not to do with the interpolation, since I've tried using the position from the box2d body directly. I've also written things to work by directly limiting frame rate as a test to 60 fps as well. Still cannot see why I'm getting this odd blurring.  Thanks.

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