This project has moved and is read-only. For the latest updates, please go here.

Determine if a body with multiple fixtures is split

Topics: Developer Forum
Jan 7, 2013 at 5:19 PM


I have a body with multiple fixtures added as rectangles in a grid. These fixtures get destroyed when hit by a ball. Is there anyway to determine if the body is split in half and then split that into two separate bodies? See linked images.



Jan 7, 2013 at 10:31 PM

This doesn't look like a physics problem but an algorithm problem. You might have more luck posting on stackoverflow. 

Off the top of my head I'd tackle it like this:

The ball is like a ray that goes from start to finish. So all you have to do is find out which fixture is to the left and which to the right of the ray. This is a simple maths problem (looking at a sign of a determinant) considering the centers of the fixtures in global coordinates if the line is in global coordinates too:

Then everything to the left goest to Body1 and everything to the right goes to Body2.

This requires that the destroyer ball has already passed through. Won't work for "live" updates.

The general case is like this: treat the fixtures as vertices on an undirected graph. Each node is connected by an edge to its four neighbours. Now you need to find the connected components of the graph which will represent your pieces. I have no idea how to explain it better (and the maths is too complicated for me). I usually just use QuickGraph to do the work :)

P.S: it's a calculation you would not want to do every frame unless it's absolutely critical to your game since it can be slow

Jan 8, 2013 at 6:32 AM

Thanks for your reply! I'll definitely try stack overflow. The problem is not as easy as determining a straight line since you can split the object without having a line of sight through it. I think I have to traverse nodes regulary, say once a second. Perhaps on a low prio thread. It's not crucial that it happens "live". Semi-live is good enough. :)

Quickgraph looks interesting as well.

Again thanks for your reply!

// AnkMannen