I have a texture which is not just a box or circle and my body needs to be the same of this shape so I was thinking to combine multiple bodies to achieve my desired shape, is it even possible? or there are better ways to do it? I'm using java with libgdx framework.
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
The shape of body is being defined by Fixture instance. Since body can have multiple fixtures you can combine many shapes as you wish.
To create many fixtures you jest call createFixture method many times with others FixtureDef objects like
Although please notice that Box2D supports more than circles and rectangles by providing ChainShape that allows you to create any shape you want
To join bodies there is Joint (take a look here) mechanism but I guess it's not what you want here
To create different shapes you can get idea from these examples:
To create circle shape.
To create box(Rectangle or Square) shape.
To create unbox shape(polygon).
Yes you can do so by following steps below
There is an option of PolygonShape or ChainShape which suits your work
Step 1: Define a body.
Step 2: Define the Shape.
Step 3: Configure the Shape. The ChainShape object is a series of connected vertices. To create the chain, we must first specify an array of vertices (each as a Vec2 object). To create the chain with the vertices, the array is then passed into a function called
A Shape is not part of Box2D unless it is attached to a body. Even if it is a fixed boundary and never moves, it must still be attached.
Polygon Shape
Official testbed examples
I strongly recommend that you go over all the Testbed examples on the GUI until you find the effect you are looking for.
By doing so, I was able to find the following examples:
multiple fixtures per body as mentioned at https://stackoverflow.com/a/35667538/895245 :
This is the best approach I've seen so far.
ChainShape
: https://github.com/erincatto/Box2D/blob/f655c603ba9d83f07fc566d38d2654ba35739102/Box2D/Testbed/Tests/CharacterCollision.h#L56 But it won't work if one of the edges of your shape is not a straight line, e.g. a circle.WeldJoint
: https://github.com/erincatto/Box2D/blob/f655c603ba9d83f07fc566d38d2654ba35739102/Box2D/Testbed/Tests/Cantilever.hHowever, this does not make the two bodies completely joined, as can be seen on the example itself, and from the Box2D manual:
So it is more like a revolute joint that attempts to leave the two bodies at a given angle.
Those examples are very simple, and will be supported as Box2D evolves.