I've begun designing a box2game, and up until now I've just been creating simple squares and circle sprites and connecting them to box2d objects. Now I want to try adding more complicated sprites. I'd like to have some sort of function that takes the sprite data and can figure out how create a b2body made with the proper shape(s) for that particular sprite. Can anyone suggest how this should best be done?
Create b2body from complicated sprite
FlashI think Mayobutter's World COnstruction Kit is you're only way of doing this.
It looks like that could accomplish exactly what I want, but it looks awfully heavy weight. I've been trying to implement it, and have been struggling a little bit. It appears on top of his own library he added he's using a box2d implementation different than mine (classes I don't have, a different folder layout, and basing it on c++ box2d).
Is this how most people handle processing sprites into b2bodys? I don't need any of the other features in this library, I just want a way to handle using sprites that aren't just circles or squares.
I feel like this has to be a common issue, but I'm having trouble searching the forums for it.
Is this how most people handle processing sprites into b2bodys? I don't need any of the other features in this library, I just want a way to handle using sprites that aren't just circles or squares.
I feel like this has to be a common issue, but I'm having trouble searching the forums for it.
Just using sprites doesn't mean automatically generating shapes and a body for a sprite. Usually, you manually design the body and shapes appropriate for a sprite.
That's sort of what I was thinking, it's the implementation I'm uncertain of.
Currently I'll have a sprite named Box, which is imported as class Box (I do not actually make this class in my code). It will have a base class RectangularShape. Rectangular shape has methods to create a b2body using itself as the user data, and it's height and width to create a b2PolygonDef set as a box.
I was figuring best way to handle more elaborate sprites is to create a separate symbol that consists of multiple polygon/circles that approximately cover same space as the drawn sprite. Each shape inside this symbol would have a base class "myb2ShapeGenerator". Then whenever I create a sprite, I make a b2body out of every myb2ShapeGenerator in the sprites corresponding symbol. (So far is this what I should actually be doing?).
However, then within this myb2ShapeGenerator class I'm not sure how I'd accurately create the right shapes generically. Do you need a separate class for circles and polygons? For just polygons how do you properly determine how many vertices the shape has/ where they are? I'll hard code stuff if I have to, but I want to know what the best way to do this is so I don't have to reprogram stuff every time I change the angle of one polygon in a sprite.
Currently I'll have a sprite named Box, which is imported as class Box (I do not actually make this class in my code). It will have a base class RectangularShape. Rectangular shape has methods to create a b2body using itself as the user data, and it's height and width to create a b2PolygonDef set as a box.
I was figuring best way to handle more elaborate sprites is to create a separate symbol that consists of multiple polygon/circles that approximately cover same space as the drawn sprite. Each shape inside this symbol would have a base class "myb2ShapeGenerator". Then whenever I create a sprite, I make a b2body out of every myb2ShapeGenerator in the sprites corresponding symbol. (So far is this what I should actually be doing?).
However, then within this myb2ShapeGenerator class I'm not sure how I'd accurately create the right shapes generically. Do you need a separate class for circles and polygons? For just polygons how do you properly determine how many vertices the shape has/ where they are? I'll hard code stuff if I have to, but I want to know what the best way to do this is so I don't have to reprogram stuff every time I change the angle of one polygon in a sprite.
That sounds awfully close to what I implemented in WCK, though I understand wanting to write your own implementation.
Your myb2ShapeGenerator base class is analogous to my BodyShape class. Then there are Box & Circle classes that extend BodyShape and define a Box or Circle. So if you've got a wheel sprite in Flash, you can just specify Circle as its base class and be done with it. It'll automatically detect the correct radius of the wheel. This is very close to what you're doing.
For more complex bodies, you can just add other Circle and Box display objects as children within another BodyShape and transform them to match your sprite graphics. The parent BodyShape will act as the body, and the children will add shapes to that object rather than trying to create a new body of their own. This works as long as you can compose your shapes of boxes, skewed boxes, and circles.
For really crazy polygons, there's an JSFL file in the "scripts" folder of WCK that will analyze a shape drawn in Flash, decompose it into convex polygons, and then output the vertices. These vertices can be copy-pasted and passed to the "polygon()" function which will create a Box2d polygon (or multiple polygons) for them. Even if you do not use WCK, this JSFL script can still be used to quickly get vertex data from a shape drawn in flash. Warning though - it hangs sometimes. Save before use.
Like Box & Circle, I've recently written a bunch of similar base-shape classes that can be extended to create things like N-sided polygons, arcs, triangles, etc.
Anyway that's the approach I've taken if it helps...
Your myb2ShapeGenerator base class is analogous to my BodyShape class. Then there are Box & Circle classes that extend BodyShape and define a Box or Circle. So if you've got a wheel sprite in Flash, you can just specify Circle as its base class and be done with it. It'll automatically detect the correct radius of the wheel. This is very close to what you're doing.
For more complex bodies, you can just add other Circle and Box display objects as children within another BodyShape and transform them to match your sprite graphics. The parent BodyShape will act as the body, and the children will add shapes to that object rather than trying to create a new body of their own. This works as long as you can compose your shapes of boxes, skewed boxes, and circles.
For really crazy polygons, there's an JSFL file in the "scripts" folder of WCK that will analyze a shape drawn in Flash, decompose it into convex polygons, and then output the vertices. These vertices can be copy-pasted and passed to the "polygon()" function which will create a Box2d polygon (or multiple polygons) for them. Even if you do not use WCK, this JSFL script can still be used to quickly get vertex data from a shape drawn in flash. Warning though - it hangs sometimes. Save before use.
Like Box & Circle, I've recently written a bunch of similar base-shape classes that can be extended to create things like N-sided polygons, arcs, triangles, etc.
Anyway that's the approach I've taken if it helps...
I went back to trying to add wck into one of my existing projects and I can't get it to work. I have classes that extend b2BoundaryListener and b2ContactListener and flex builder is telling me it's not recognizing them. I don't see files for them in wck's flash folder although I can see them referenced in the C++ code. Has anyone run into this/ know how to handle it?
I'm not sure if this is what you're looking for but I wrote a tutorial on how to trace around the outside of a sprite to generate the outer vertices for a Box2D object. Obviously, if you want the object to be dynamic you would need to split the shape up into triangles using a constrained Delaunay triangulation. The tutorial can be found here http://www.deluge.co/?q=box2d-tracing-shape-outlines. In the future I'm also planning to write a tutorial on how to perform the triangulation. Hope this helps!
Original thread: https://box2d.org/forum/viewtopic.php?t=3852