Box2D Joining Polygons?

Monkey Forums/Monkey Programming/Box2D Joining Polygons?

CopperCircle(Posted 2012) [#1]
Hi, I want to make an object out of several convex polys, what is the best way to join them into a single entity?

Thanks.


muddy_shoes(Posted 2012) [#2]
You just add multiple fixtures to a body. Take a look in the testbouyancy demo. Here's the bit that creates the cross with the circles on the ends:




Uncle(Posted 2016) [#3]
Sorry to drag up such an old thread, but has anyone found a way to have multiple polygons made from SetAsArray offset by an arbitrary amount? I have a 1 concaved shape that I have split into two convexed shapes but when I attach them they are both centralised around the body i.e. they overlap each other. I would like to offset them from each other. Ive tried changing the verts in the array before using the SetAsArray but this doesn't work.


skid(Posted 2016) [#4]
If you are going to take that approach and the body is not static you will need a joint to hold the two shapes together, probably a prismatic joint.

It is simpler to use edge / chain shapes for non convex surfaces.


muddy_shoes(Posted 2016) [#5]
@Uncle

It's been a while since I looked at this stuff -- and I don't have a buildable project to hand right now to test -- but I did write a component framework that used the box2d collision engine and recall coming across this problem. If I remember correctly I added some methods to set the local position of shapes before they are used to create a fixture. This is the method for the b2PolygonShape

	Method SetLocalOffset:Void( x:Float, y:Float )
		For Local i:Int = 0 Until m_vertexCount
			m_vertices[i].x += x
			m_vertices[i].y += y
		End
	End


So, you create the shape with SetAsArray, use the SetLocalOffset to adjust the position and then use the shape to create the fixture and add it to your body.

Hopefully that should get you there.


Uncle(Posted 2016) [#6]
Awesome! Thanks both for your answers. I will give these a try tonight.