Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » New to Box2D, 2 objects way too slow

This thread is locked; no one can reply to it. rss feed Print
New to Box2D, 2 objects way too slow
User9000
Member #15,315
September 2013

I just found an example of using box2d with allegro 5: https://www.allegro.cc/forums/thread/607063/913936#target

Why does this run so slow? It only has two objects. I am new to box2d, so I'm sure I'm missing something since so many games use it. Which leads me to think that the issue is with a box2d setting. Stepping too small? Low gravity? Too large a world?

It isn't my machine, since I can run most modern 3d games on the highest detail level without issues. This appears to run slow on both Linux and windows. Additionally the box2d example / testing program when compiling the library runs as a reasonable speed.

beoran
Member #12,636
March 2011

Well, I started out with Chipmunk, which is fast enough for many objects.

But both Chipmunk and Box2D are not very efficient if you are using tile maps. So I developed my own physics engine for my game. You can see it here: https://github.com/beoran/eruta/blob/master/src/bump.c if you're interested.

Writing your own physics engine does take some effort, but it helps you understand what is going on much better.

SiegeLord
Member #7,827
October 2006
avatar

It might have something to do with this line:

#SelectExpand
132al_rest(1.0 / 5.0);

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

Elias
Member #358
May 2000

Chipmunk uses spatial hashing so is plenty fast with even a hugest tilemap (there should be a TINS entry where I use it that way). Just make sure to set the tilemap geometry as static so no physics update is run on the tiles.

--
"Either help out or stop whining" - Evert

beoran
Member #12,636
March 2011

Well, Chipmunk is fast enough, but it's not very practical and a waste of space because you have to make a static body for every tile in the tile map. This in turn makes loading and unloading the tile map harder then it should be. In my own physics engine I collide the dynamic objects directly with the tile map which greatly simplifies the algorithm as well.

Elias
Member #358
May 2000

beoran: Colliding with a tilemap is far from trivial though. You still have to create (up to) four line segments per tile (to get the right normal vectors).

Also, with chipmunk, you only need to create static geometry where necessary. For example if I have three tiles XXX then I only need two collision lines |XXX|. In the game I used it in I think chipmunk was about as efficient as possible - unless you came up with an algorithm where you don't create line segments for your tiles at all.

--
"Either help out or stop whining" - Evert

beoran
Member #12,636
March 2011

It's not necessary to use line segments at all, since my tile map is regular in size, so I can query the tile map directly based on the position of the object. Also, my collision engine isn't as "realistic" as that of Chipmunk, but for certain types of games like mine, that can be a good thing. And the fact that I don't have to create any static geometry means that I can swap out tile maps instantly and it "just works".

User9000
Member #15,315
September 2013

Solved. al_rest as indeed most of the issue. I wonder why that was in there. I think there may be a zoom different than what I expect too. I just wanted to be able to read though and understand what was necessary for box2d and allegro to function together. I all check out chipmunk too.

My last project I wrote my own physics sonic-like engine, but I want to add more cool physicy things. It works, but I've learned a lot since I wrote it.
My hope is that my next project will use generic programming, BSP-trees, Tiles, XML scripting and bouncy neat physics. :D

Go to: