Yetispapa
Yetispapa

Reputation: 2296

Box2D - b2Fixture touching

I have a question about b2Fixture. I have a body with multiple fixtures (polygons). In my picture you can see a body with multiple fixtures for example. The line in the middle of the body should represent a point. My question is how can i figure out which fixtures are on which site, including the fixtures which are connected.

Illustration1

My first throught was to loop through all vertices of each fixture and find out where the current vertex are located from the line (left or right?).

But this doesn't work because the fixtures on right side can connected with other fixtures which are overlap over the line like here in the image.

Illustration2

So is there a way to find out which fixture are connected with other fixtures? Or can i order the fixtures from left to right?

Hope anyone understand me. And sorry for the bad images (:

Thank you in advance.

Greetings alex

Upvotes: 0

Views: 215

Answers (1)

NauticalMile
NauticalMile

Reputation: 1795

My advice would be to keep track of the fixtures during construction. You have to create these fixtures at some point, right? Because you didn't tag any specific version of box2d I'll assume you're using the c++ version.

box2d allows you to SetUserData for the fixture. If you are not already using the userData then you can use this to refer to an object that stores the fixture's neighbors. A simple structure might look like this:

struct FixtureNeighbors
{
    b2Fixture* leftNeighbor;
    b2Fixture* rightNeighbor;
};

During construction of the fixtures, you should create a FixtureNeighbors object, cast it as a void*, and call b2Fixture::SetUserData. Then, during the game, whenever you want to find out who a fixture's neighbors are just call b2Fixture::GetUserData, cast the result back to a fixtureNeighbors object, and use that to access the left and right neighbors.

Notes

If you are already using the fixture userData to point to an entity or something else, you should add a GetEntity method to your FixtureNeighbors structure and you can still access the entity if you have the fixture.

If a fixture can be touching more than two neighbors, just use an stl vector to store a list of them.

I hope this helps!

Upvotes: 1

Related Questions