My little sandbox app

Not quite finished, but I already have a bunch of functionality in this thing.

Eventually I'm looking to include an object type made from arbitrarily-shaped polygons that will break dynamically based on collision forces and locations.

======== Controls (also refer to the readme): ========

  • Left click: mouse joint (move)
  • Right click - drag: move camera
  • Mouse scroll: zoom camera (i'm very proud of this effect- so simple and so nice looking)
  • Middle click: delete object
  • 1 (one): Toggle polygon editor:
    • Left click to define polygon
    • Right click to finish and create it
    • Middle click to cancel/clear
    • Z to undo the last vertex
  • F1 thru F8: Debug functions (play around with them, you'll see what they do)
  • E: Spawn a big pile of boxes (slows down when framerate slows down)
  • C: Break apart all multi-shape bodies.
  • R: Reload Simulation
  • Esc: Quit

Image
-- I drew out all of these shapes myself with the polygon editor.

Don't worry -- It comes with a pre-made stack of more than enough squares and circles. And if that's not enough, press E a couple times.

Download!

Note: polygon decomposition code is NOT bug-free, and you will encounter weird, filled-in polygons if you try to make certain types of shapes. I am currently working on this and should have it fixed soon.

Will also add joints shortly after. They'll be very quick to implement.
Fun to play around with! What are your future plans for it?
Eventually it'll be an asteroids-inspired game with dynamic asteroid breakage. If I can get it to look nice.
Sweet; I really love space games. It looks like your code to handle the breaking apart of objects is working well, it's just that sometimes the construction of the objects isn't always perfect.
theluckyleper wrote:
Sweet; I really love space games. It looks like your code to handle the breaking apart of objects is working well, it's just that sometimes the construction of the objects isn't always perfect.


yeah, exactly. When it works well it works really well, but i just have to figure out that one part, i know what to do but i need a bit of time to work on it. The code to break apart objects was literally a 2 minute job and 10 lines provided you already had the polygons partitioned...Box2d is great.

I was thinking about some way to do shape deformations along with breakage, during impact, not quite like with soft bodies, but like in many racing games where a hard enough impact will put a dent in, or flatten parts of a shape, without breaking off a significant chunk. This is much more realistic behavior for metals. I'd have to have some pretty complicated code to analyze my polygon vertices and swap out the shape for the modified one, though.

I also really wanted to make a platformer type game with guns and vehicles (and gravity!), especially since box2d has incredibly good stacking stability (there wont be much stacking or complex interactions in zero gravity), but I think I'd be better at making a space game since I've done one already in the past. Maybe I'll work both aspects into it, but that just might not work out well from a gameplay/design perspective...
Quote:
The code to break apart objects was literally a 2 minute job and 10 lines provided you already had the polygons partitioned...Box2d is great.
Can you tell me how exactly you can break apart objects? Does it work with only 1 body and multiple shapes or does it have to be different bodies with Distance Joints?
Thx
aero wrote:
Quote:
The code to break apart objects was literally a 2 minute job and 10 lines provided you already had the polygons partitioned...Box2d is great.
Can you tell me how exactly you can break apart objects? Does it work with only 1 body and multiple shapes or does it have to be different bodies with Distance Joints?
Thx


Theres currently no way to create joints in this yet, so all of the multiple shape things are single bodies. If you press F7 (i believe.. its one of the F5-F8 keys) you can get a debug display drawing a red dot at the center of gravity of each body to see where everything is.

Heres the code:

Code:
void breakBody(b2Body *& body)
{
   b2BodyDef def;
   def.position = body->GetPosition();
   def.angle = body->GetAngle();
   for(b2Shape * shape = body->GetShapeList();shape;shape = shape->GetNext())
   {
      b2Body * subBody = world->CreateBody(&def);
      subBody->SetUserData(new BodyData());
      b2PolygonDef shDef; shDef.density = 1; shDef.friction = shape->GetFriction(); shDef.restitution = shape->GetRestitution();
      shDef.vertexCount = ((b2PolygonShape*)shape)->GetVertexCount();
      for(int i=0;i<shDef.vertexCount;++i)
         shDef.vertices[i] = ((b2PolygonShape*)shape)->GetVertices()[i];
      subBody->CreateShape(&shDef);
      subBody->SetMassFromShapes();
      subBody->SetLinearVelocity(body->GetLinearVelocityFromWorldPoint(subBody->GetWorldCenter()));
      subBody->SetAngularVelocity(body->GetAngularVelocity());
   }
   if (mouseJoint && mouseJoint->GetBody2() == body)  //nullify joint pointer since joint will be implicitly destroyed
      mouseJoint = NULL;
   world->DestroyBody(body);
   body = NULL;
}


pretty straightforward, you just have to set the right velocities.

edit: I noticed that this may cause crashes because of a cast if there are any bodies with multiple shapes where some shapes are circles. So in that case you may need to do some editing.
Thank you dc443!
I see it now.
Finished Convex Decomposition so it actually works for all shapes now. Theres better ways of doing it, and I'll update it later if someone shows me some constrained delaunay code, but until then, this will work well enough for almost any shape you could ever need in a physics engine.

Have not done the finishing touches yet so i'll update this when I upload it.
Image

success! (to put the circle in the Q, i just spawned it around the circle..)

Image

The spiral shape inside the outside circle is actually suspended within it. it's all one single body.

Image

Look! it's one of those ball puzzle things!! (I got bored of drawing all the outlines to fill up the shape.. but you get the idea)

The download on sourceforge will be up shortly. it's right here: http://sourceforge.net/projects/box2dsandbox/

edit: updated! v0.23 should be completely bug-free!

Original thread: https://box2d.org/forum/viewtopic.php?t=975