2
\$\begingroup\$

I am basically from corona background and new to LibGdx , familiar with stage and actor from scene2D. Now wants to use physics body from box2d in the game besides using actor from scene2D.

But I need to use images in the game as body... not shapes..

anyone please explain me the steps to use images as a body.

Thanks..

\$\endgroup\$
  • \$\begingroup\$ Use image as body? I don't understand, do you mean link a texture to a body? Like you would have your player character that is composed of a dynamic body and a sprite displayed at body position? \$\endgroup\$ – nathan Aug 22 '13 at 14:36
7
\$\begingroup\$

But I need to use images in the game as body... not shapes..

In Box2D, you will need to create shapes anyway, you can't really go without them. A body is composed of fixtures and a fixture is a shape, that's it.

Here is the official Box2D manual if you want to read more about those concepts.

anyone please explain me the steps to use images as a body

As i already said, you can't. But a thing you may not know is that you can use convex polygons as shapes (see the related part of the manual).

Assume you have your player entity having this sprite:

enter image description here

You may want to create a body for your player entity which will contain a main fixture having the following polygon as shape:

enter image description here

There is a great tool you may find useful, it's called Physics Body Editor. From the official page:

Physics Body Editor is all about making your life easier with physics engines. Specifically, it targets the creation of collision shapes for your game objects: we call them rigid bodies. It can also let you combine these objects together and link them with joints to create complex objects: we call them dynamic objects.

Note: the above image show the auto-trace feature of this software

Physics Body Editor will create a JSON file that you will need to use in your game to create your fixtures, fortunately, libGDX comes with a native loader and here is a code i took from the demo that shows how to do it:

BodyEditorLoader loader = new BodyEditorLoader(Gdx.files.internal("data/test.json"));

// 1. Create a BodyDef, as usual.
BodyDef bd = new BodyDef();
bd.type = BodyType.DynamicBody;

// 2. Create a FixtureDef, as usual.
FixtureDef fd = new FixtureDef();
fd.density = 1;
fd.friction = 0.5f;
fd.restitution = 0.3f;

// 3. Create a Body, as usual.
bottleModel = world.createBody(bd);

// 4. Create the body fixture automatically by using the loader.
loader.attachFixture(bottleModel, "test01", fd, BOTTLE_WIDTH);
bottleModelOrigin = loader.getOrigin("test01", BOTTLE_WIDTH).cpy();

Your question is not really clear and i assumed that what you wanted to achieve was to have physics simulation based on your sprites. This can't be done directly in Box2D by supplying sprites to body or whatever but here is how to do it using shapes and a pretty useful tool :)

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.