1

I tried using this Libgdx and Box2D Draw a custom shape But it didn't helped me.

Let's say that I have a image - http://cdn.motocross.transworld.net/files/2010/09/geicohondausa2.png

I want to create convex polygons for the image and then display individual images for the fixtures/body.

Is that possible? sprite.getVertices won't work also..

Community
  • 1
  • 1
Boldijar Paul
  • 5,405
  • 9
  • 46
  • 94

2 Answers2

1

A Sprite is always rectangular, that's why Sprite.getVertices() makes no sense here.

Using the editor in the linked question, you can create the body as a polygon. Then you have to export/import it in your game as a Body with the correct fixture(s). Probably you should also add some circle wheel fixtures and connect them with joints to the chassis.

You actually cannot add an image to a Body. Box2D knows nothing about rendering or visuals of any kind. You need to do that yourself. Using a SpriteBatch and a Sprite. All you need to do is keeping the sprite and the body synchronized in every frame before rendering the sprite. That means you do something like sprite.setPosition(body.getPosition()) and sprite.setRotation(body.getRotation()). (Just dummy code, you probably need some more conversions).

noone
  • 19,520
  • 5
  • 61
  • 76
  • Thanks for your answer, I found a class that helps you rendering a sprite over a body, so you don't need to use spritebatches. I will try to use that tool. – Boldijar Paul Dec 31 '13 at 10:17
  • @Paul Which class is that? – noone Dec 31 '13 at 10:22
  • https://bitbucket.org/dermetfan/somelibgdxtests/src/945cb3fe1411808b978d741302f425ffe92e52e8/lib/net/dermetfan/libgdx/graphics/FixtureSprite.java?at=default this is. How can I scale my body, is really low and i don't want to zoom the camera – Boldijar Paul Dec 31 '13 at 10:59
  • @Paul Scale the body? or scale the Sprite? Bodies cannot be scaled, and Box2D doesn't know anything about a camera as well. – noone Dec 31 '13 at 12:39
  • Scaling the sprite so it lies on top of the body is a function of (1) deciding what you want your pixel-to-meter ratio (PTM_RATIO) to be, (2) creating graphics that are about the size you want (usually) somewhat larger than than the display size you want, and (3) adjusting the scale of the sprite at runtime based on a notion of the physical size it represents. See: http://stackoverflow.com/questions/20299960/rotating-multiple-sprites-as-one-around-same-origin/20300196#20300196 with image showing sized sprites. There is a link to the code as well. – FuzzyBunnySlippers Jan 01 '14 at 13:27
0

Sprite.getVertices() returns 20 floats, which contain X, Y, U, V and Color, so 5 * 4 = 20, you might get the index by using for example SpriteBatch.X1 for vertex at index 0 and SpriteBatch.Y1 Y2 or so, this is good because library may change.

Note that PolygonShape number of vertices in Box2d should be in range of 3 >= VERTICES <= 8, so you can't use more than 8, however you can use multiple Fixtures and use FixtureDef for every PolygonShape and attach to the body.

Also indices are important on PolygonShape, (however the new version of box2d does not care), and getting from Sprite vertices is good and is arranged.

daniel
  • 697
  • 5
  • 15