# Box2d

Hey OF,
Box2d is unreal. I’ve been able to get it to compile outside of OF, but not within. I was wondering if anyone has played around with this or have suggestion to connect to OF.

Box2d is setup to have its own GL world and drawing techniques, I would love to make it work within OF.

http://www.box2d.org/

Thanks,
T

i use box2d in OF.
what’s the error msg?
what platform are you on?

i just followed the instructions in the user manual and it worked.
just make sure that you use the right scale:
http://www.box2d.org/manual.html#units
that means: objects should be between 0.1 and 10 units large. otherwise you get all kinds of strange effects. it took me quite a while to figure that out.

it’s real fun to play with…

best
joerg

Hi

I was yesterday looking for a physical engine. I was indeed trying ode, when i saw this post and decided to give Box2d a try as all I need is 2d.

I’ ve been trying with small measures between 0.1 and 10 an then multiplying positions and sizes by 100 when drawing, it more or less works at the beginning, but gets pretty innacurate in not much time.

Any clue on how to convert meters to pixels?, I’ve read in the manual that they use openGL viewport transform, but I don’t know very much about openGL, also the reverse coordinate system in OF is an issue when I try to adapt code from their examples.

Thanks

Arturo

you can invent your own way of converting meters to pixels. i decided that my screen is 10m wide. so one pixel is 10/maxx.
you don’t have to care about reverse coordinate systems. the only difference might be that gravity pulls your shapes upwards. if you don’t like that just invert the direction of gravity.

you have to tweak the other parameters like mass. be sure to calculate mass, inertia and inv-inertia with the supplied functions.

best
joerg

Hi jroge

That helped, indeed I was also trying to draw shapes appliying the body’s angle as rotation and then getting it’s coordinates, so I ended with really strange effects like objects floating or not colliding as they should.

Thanks

Thanks this is a big help.

I got it to run in OF, pretty awesome stuff. Next I want to build a addon ill post what I have, wish we had a svn setup. Then we could all work on this together.

One thing that I found strange is that when you build a body then create shapes and add those shapes to the world, you can’t have more than 100. Anyone know why?

update: I guess its if you add more than 100 at once like in a for loop.

Newton is the man!

T

hmmm

so i tried a new scaling thing. Instead of scaling the bodies (positon and size) I am scaling the rending.

I think that it may be better cause:

``````

float scale = 30;
body.position.Set(10*scale, 10*scale);
is a bit weird when thinking in meters.

``````

I would rather just scale the rendering i think?

About the scaling, yes you have to scale the rendering, in the code you post, your bodies will measure 300x300 m, apart that it’s strange working with that dimensions, Box2d only works well with objects between 0.1 and 10 meters. So you’ll end with strange behaviours.

Try what jroge suggests, give your screen a measure in meters and then find the proportions between that and the pixels width and height then when rendering multiply every position by that proportions, it works really well for me.

awesome got some cool stuff working.

http://www.vimeo.com/974567

i found some severe limitations in box2d. if i add more than 150 bodies (all rectangles) and speed them up quite a bit they seem to collide very often so there are some assertions in box2d-code that fail.
under 150 bodies i don’t have this problem. of course you can always change the limits in the box2d header and recompile but i fear the performance will decrease.
chipmunk used to be much more tolerant in this regard but it was much less reliable (although much faster). for my inkscape plugin i was able to use more than 1000 shapes, but i won’t recommend it for realtime.

still waiting for an einsteinian physics engine…

all the best
joerg

wow nice video ! that looks great. I can’t wait to try it.

in the past, I’ve hooked up polycolly:
http://www.gamedev.net/community/forums-…–id=341162
but I think box2d is much more evolved. have heard good things about chipmunk too.

take care!
zach

ps: about SVN I’m pretty sure that we do have it for addons… theo can hook it up and get whoever wants to be on it up.

Anyone know what the differences are between Flatland and Box2D?
What are people’s preferences regarding physics engines. Also what are peoples’ favourite 3D physics engines?

ohh man chipmunk looks awesome. what to use box2d vs chipmunk?

might be helpful for seeing the differences:
http://nightschool.near-time.net/news/2-…-g-to-box2d

• zach

i started with chipmunk and made a first prototype with it but decided to switch to box2d. i had so many problems with chipmunk (bodies falling through floors etc.). but maybe it was some wrong code i wrote. for the non-realtime physics plugin i wrote for inkscape it worked alright.

best
joerg

Hi

I’ve been testing chipmunk also, here’s what i’ve seen…

Box2d:

• much more accurate
• uses preconditions assertion, so if you create an object that it’s not going to work (concave for example), it advice you with an error, that hangs the app, although I suppose is just an exception that can be chaught.
• more elements, like bullets
• properties names are more intuitive than chipmunk where everything is called p, u, v…

Chipmunk

• perhaps a little bit faster
• allows many more vertex in a shape by default
• allows linear objects, for example to draw terrain curves
• doesn’t have measure limitations, so you can work directly with pixels

Can someone post testApp.h and testApp.cpp for Box2D?

I am including the header Box2D.h and try to implement the trivial “hello world” example found at the beginning of Box2D doc. But the compiler produces a weird error when I declare the world variable in testApp.h ie:

``````
b2World world;

``````

…\src\main.cpp | | In function `int main()':| ...\src\main.cpp |14| error: no matching function for call to`testApp::testApp()’|
…\src\testApp.h |6|note: candidates are: testApp::testApp(const testApp&)|
||=== Build finished: 1 errors, 0 warnings ===|

Any help is greatly appreciated…

are you on VS compiler, by chance ? VS 2008 ?

compiler error seems very similar to this one:
http://forum.openframeworks.cc/t/one-class-compiles-one-doesn’t/668/9

are you including the right box2d header files?

I think there is a bug with VS that leads it to give weird errors when it see unknown types in testApp.

take care!!

• zach

Oh no… I am using codeblocks …

I believe Box2D.h is the correct header.

Most probably I am doing something wrong, although I can’t figure out what…

and the error is very strange…

The box2d “hello world” example is not supposed to render anything, but I am thinking that the error might have to do with the internal renderer of Box2D getting mixed up with the OF one…I’ll have to dig-on…

thanks Zach!!

if you want to zip up anything that I can recreate in 0.05 codeblocks I’ll give it a try… I’m curious about this error occuring on two different compilers, it’s a bit weird and would like to see it in action. if you do upload, be sure to include any libs (box2d, etc) that are needed to compile.

thanks!
zach