Page 1 of 1

Box2d

Posted: February 13th, 2017, 6:16 pm
by MrGodin
Just curious, but i have implemented box2d in some code to see what it's about. Using the Chili framework it seems real slow. Anyone else having issues with it as far as speed ?

Re: Box2d

Posted: February 13th, 2017, 6:40 pm
by albinopapa
How slow is slow?

I've used it and I was able to do around 500 collidable bodies using the chili framework. The simulation I was running was on n-bodies so I probably could have done more, but doing that already eats up a lot of CPU cycles.

Re: Box2d

Posted: February 13th, 2017, 6:57 pm
by MrGodin
I haven't benchmarked this, nor do i have a fps timer. May have to include Box2d in c++->additional includes. I have include a relase build and debug build of the box2d libs

Re: Box2d

Posted: February 13th, 2017, 8:31 pm
by albinopapa
I'm getting 60 fps in Release mode.

Since the framework doesn't have fonts, I use the Windows API and set the window title to display the FPS. Here's what I did.

Code: Select all

// MainWindow.h
	void SetText( const std::wstring &Title );

// MainWindow.cpp
void MainWindow::SetText( const std::wstring & Title )
{
	SetWindowText( hWnd, Title.c_str() );
}

// Game.h
#include <chrono>
	std::chrono::time_point<std::chrono::steady_clock> tp;

// Game.cpp
void Game::UpdateModel()
{
// If DEBUG is not defined, try _DEBUG.  If _DEBUG isn't define either, you can add it in the project
// properties under C/C++ -> Preprocessor -> Preprocessor Definitions.
// Just make sure you have the Configuration set to Debug instead of Release or All Configurations.
#ifdef DEBUG
	const float dt = 1.0f / 60.0f;	
#else
	const auto now = steady_clock::now();
	const float dt = duration<float>( now - tp ).count();
	tp = now;
#endif
	wnd.SetText( std::to_wstring( 1.f / dt ) );
	m_world->Step(dt, 6, 2);
}
The #if, #else, #endif will allow for different compilations. This way if you are in debug mode, you get a constant time of 1 / 60 (.01667f), and in release it will be whatever the frame time was.

Re: Box2d

Posted: February 13th, 2017, 8:32 pm
by albinopapa
I got 60 fps in Release and around 30 in Debug

Re: Box2d

Posted: February 13th, 2017, 8:55 pm
by MrGodin
Thanks papa, is it (frame rate) similar to what you had when you tried it yourself ?. I have a quad core q8200 cpu with amd 7700 series video card

Re: Box2d

Posted: February 13th, 2017, 9:14 pm
by albinopapa
Hmm, you know I don't think I properly tested the framerate on my project, I was just looking for something that would run smoothly with the simulation and still have enough CPU time left over for the other stuff.

When I first started the project, before adding Box2D, I wasn't handling collisions, and had about 2000 asteroids ( circles ), 2500 stars ( pixels ), and 1000 ships ( boxes ) all being updated and was very smooth. After adding box2d for collisions with the asteroids, I had to bring the count down to 500 asteroids. I can't remember if I had to change the other values or not, but if I would have included them in the collisions, I'm sure I would have had to. I'd look it up, but I restarted the project because the original was setup to be optimized for SSE and was having troubles dealing with so many arrays and trying to figure out paths and collisions and AI for all that stuff one array at a time was torture. Plus, box2d is setup using Array of Structures, so it makes more sense to also have the rest of the code the same instead of my code laid out in Structures fo Arrays.

Re: Box2d

Posted: February 13th, 2017, 9:35 pm
by MrGodin
Ahh i see, well thanks for the input, I got the same frame rate as you 30 and 60. I upped the box count to 400 and got 60 in release 3.5ish in debug .. anyways am going to fiddle fuck around with this, thanks for the input

Re: Box2d

Posted: February 14th, 2017, 1:30 am
by chili
Performance measuring in Debug is not really relevant. As for b2d, the problem of collisions is inherently n^2. B2d uses a bunch of techniques to bring that down to like, nlogn, but it that still greater than linear time complexity, and it also depends on factors like the distribution of your bodies in space etc. Basically, a lot of dynamic bodies = slow. Also, b2d also has no optimizations for GPU, SIMD, or multithreading, so you have even less resources to work with.

In 2010 ppl were saying 200 bodies=20fps http://www.box2d.org/forum/viewtopic.php?t=4618