Working on a Box2D slice/split/cut engine
A long time ago (2 years) Guillaume Pommey aka pompom showed us a working version of a Box2D slice/split/cut engine, using the old distribution of the physics library, now deprecated.
I wanted to port it to 2.1a version, but once I start coding I followed another path and this is what I have:
Give me a couple of days to clean the code, and I’ll come with a detailed tutorial about slicing objects with the latest Box2D distribution.
Meanwhile, give me your suggestions/feedback.
They can be easily customized to meet the unique requirements of your project.















(10 votes, average: 4.40 out of 5)










This post has 13 comments
Ari
I really like what you are doing. Have you tried it with the Clipper library though? I was interested to see if it could slice a polygon with a single line (tvo vertices) and it was not possible, however if I turned the line into a one pixel thin rectangle polygon it worked perfectly. I timed it and it measured only 2ms cutting a simple square.
Clipper: http://flassari.is/2011/05/as3-polygon-clipper/
It is an alchemy port, so the first time it is called it takes a little longer while the lib is initializing.
Here’s the code I used to test it:
package
{
import com.flassari.geom.ClipType;
import com.flassari.geom.Clipper;
import flash.display.Sprite;
import flash.geom.Point;
public class SlicePrototype extends Sprite
{
public function SlicePrototype()
{
// The square
var subject:Array = [new Point(10, 10), new Point(100, 10), new Point(100, 100), new Point(10, 100)];
// The clipper line, a rectangle with 1 px width
var clip:Array = [new Point(0, 0), new Point(200, 200), new Point(200, 201), new Point(0, 1)];
var polygons:Array = Clipper.clipPolygon(subject, clip, ClipType.DIFFERENCE);
}
}
}
The code will return two polygons.
This can be used to cut the most complex polygons (concave, hollow) I can think of and still perform well. Also, this can be used to make the cutting line itself as thick as one wants.
-Ari-
Chris Moeller
Awesome, I’m looking forward to it!
I had a problem with converting new shapes to counter clockwise polygons, and breaking up the shape into two new convex shapes. It worked like 80% of the time, but crashed flash when it didn’t work (infinite loop somewhere) – but it was based on someone elses code, and I wasn’t completely clear on how they did both steps (how do you programmatically figure out if points are counter clockwise? How do you figure out if the shape is convex by just knowing the points?)
Kirtimaan
While you are talking about Box2D, there is a question related to framework. Is it possible to have curved shaped objects in Box2D? Say a mountain on which a player object moving. All the examples I have seen have rectangle, circle or squares.
csomak
I’d love to see a feature that can handle worms-like maps and explosions.. so that holes can be created on the ground. if you create a floating island, it would fall down cause of gravity.. how awesome would be that?
Emanuele Feronato
@Ari: nice library, I am taking a look at it
@chris: cutting squares, and any polygon generated by cutting a square, will always give convex shapes. If you store the initial vertices order, then it won’t be difficult to have counter clockwise polys
@Kirtiman: no, it’s not possible, you should approximate the circle with a polygon with an high number of vertices
@csomak: awesome but quite difficult, I’ll try to do something similar
csomak
i will love to help :D
I wanted to build a real time Worms (no waiting for other player, just the reload time) with box2d, but I lack-ed that. I’d love to help with that ;)
Fardeen
Use http://code.google.com/p/quickb2/ . The demos are impressive !
Emanuele Feronato
I don’t like the way it splits polys. Hundreds of lines.
Ari
I tried the quickb2 slicing when I was trying to make my own polygon clipper, the newest version had a lot of bugs in it and diving trough the svn revisions didn’t help.
MC
I suppose you had to create two different polygons from the intersection points, but i always wondered how they split the textures inside the polygons? (in box2d)
Emanuele Feronato
Draw a sprite with the same shape and use it as a mask
santosh
nice development!!!!!!!!!!!
whoop
Please upload your source code