Simple Polygon in Box2d

  ... discussions about development with the GLES1 branch of AndEngine.

Simple Polygon in Box2d

Postby droidy » Thu Aug 19, 2010 10:14 am

Hi!
Can anyone tell me how to draw a Polgon filled with a color in Box2d ?
Something like this but with a Polygon:

Line line = new Line(x,y,x2,y2);
PhysicsFactory.createLineBody(this.mPhysicsWorld, line, wallFixtureDef);

There is a Method called craetePolygonBody but there seems to be no Shape as Parameter. Nicolas uses Texture as Parameter ion the examples. Is this the only solution?

Thanks and Best Regards,
Micha
droidy
 
Posts: 5
Joined: Thu Aug 19, 2010 9:59 am

Re: Simple Polygon in Box2d

Postby Radzell » Thu Aug 19, 2010 10:16 am

The best way would be to make a sprite and create the texture in another program like Photoshop or ms paint then make a box2d body from the only primitive shapes for andengine are lines and rectangles.
Radzell
 
Posts: 465
Joined: Sun Jul 04, 2010 8:52 am

Re: Simple Polygon in Box2d

Postby droidy » Thu Aug 19, 2010 10:25 am

Thanks for the fast response!

ok i understand. So do you have solution for this:
I have a path made of lines like a siluette of a mountain region. I would like to fill this mountain with a color. So the only Chance to do this is by building the mountain with peaces of Sprites or rectangles?

Micha
droidy
 
Posts: 5
Joined: Thu Aug 19, 2010 9:59 am

Re: Simple Polygon in Box2d

Postby Mimminito » Thu Aug 19, 2010 12:45 pm

If you take a look at the Physics Examples, you can see that Nicolas creates Triangles and Hexagons via code. He shows you the methods there, and I have posted the Hexagon one below:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. /**
  2.  * Creates a {@link Body} based on a {@link PolygonShape} in the form of a hexagon:
  3.  * <pre>
  4.  *  /\
  5.  * /  \
  6.  * |  |
  7.  * |  |
  8.  * \  /
  9.  *  \/
  10.  * </pre>
  11.  */
  12. public static Body createHexagonBody(final PhysicsWorld pPhysicsWorld, final Shape pShape, final BodyType pBodyType, final FixtureDef pFixtureDef) {
  13.         /* Remember that the vertices are relative to the center-coordinates of the Shape. */
  14.         final float halfWidth = pShape.getWidthScaled() * 0.5f / PIXEL_TO_METER_RATIO_DEFAULT;
  15.         final float halfHeight = pShape.getHeightScaled() * 0.5f / PIXEL_TO_METER_RATIO_DEFAULT;
  16.  
  17.         /* The top and bottom vertex of the hexagon are on the bottom and top of hexagon-sprite. */
  18.         final float top = -halfHeight;
  19.         final float bottom = halfHeight;
  20.  
  21.         final float centerX = 0;
  22.  
  23.         /* The left and right vertices of the heaxgon are not on the edge of the hexagon-sprite, so we need to inset them a little. */
  24.         final float left = -halfWidth + 2.5f / PIXEL_TO_METER_RATIO_DEFAULT;
  25.         final float right = halfWidth - 2.5f / PIXEL_TO_METER_RATIO_DEFAULT;
  26.         final float higher = top + 8.25f / PIXEL_TO_METER_RATIO_DEFAULT;
  27.         final float lower = bottom - 8.25f / PIXEL_TO_METER_RATIO_DEFAULT;
  28.  
  29.         final Vector2[] vertices = {
  30.                         new Vector2(centerX, top),
  31.                         new Vector2(right, higher),
  32.                         new Vector2(right, lower),
  33.                         new Vector2(centerX, bottom),
  34.                         new Vector2(left, lower),
  35.                         new Vector2(left, higher)
  36.         };
  37.  
  38.         return PhysicsFactory.createPolygonBody(pPhysicsWorld, pShape, vertices, pBodyType, pFixtureDef);
  39. }
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


As you can see, you can create your own polygon bodies via code pretty easily. You could then create an editor if need be, and convert the lines you create into vertices for a polygon body.
---------------------------------------
Adam Goodchild
Your AndEngine Forum Moderator!
My Tutorials List
http://www.adam-goodchild.co.uk
---------------------------------------
Mimminito
 
Posts: 360
Joined: Wed Jul 21, 2010 3:08 pm
Location: Chelsmford, UK

Re: Simple Polygon in Box2d

Postby droidy » Thu Aug 19, 2010 1:01 pm

Hi,

That'S right but he allways uses a Sprite as the parameter for the Shape:


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. final AnimatedSprite face;
  2. final Body body;
  3. //...
  4. face = new AnimatedSprite(pX, pY, this.mHexagonFaceTextureRegion);
  5. body = PhysicsExample.createHexagonBody(this.mPhysicsWorld, face, BodyType.DynamicBody, objectFixtureDef);
  6. //...
  7. //in createHexagonBody:
  8. return PhysicsFactory.createPolygonBody(pPhysicsWorld, pShape, vertices, pBodyType, pFixtureDef);
  9.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4

But i don't need a Sprite in the Background of that Polygon.
droidy
 
Posts: 5
Joined: Thu Aug 19, 2010 9:59 am

Re: Simple Polygon in Box2d

Postby Mimminito » Thu Aug 19, 2010 1:05 pm

Gimme five ill look into it
---------------------------------------
Adam Goodchild
Your AndEngine Forum Moderator!
My Tutorials List
http://www.adam-goodchild.co.uk
---------------------------------------
Mimminito
 
Posts: 360
Joined: Wed Jul 21, 2010 3:08 pm
Location: Chelsmford, UK

Re: Simple Polygon in Box2d

Postby Mimminito » Thu Aug 19, 2010 1:17 pm

Well, currently I dont think there is a way no. Because of the way Box2D and AndEngine works, you need to supple a Shape for the Box2D Poly to know certain values.

When you create the Physics Body (correct me if this is wrong) all you are doing is linking the sprite/shape you have in your scene with a physics representation as well, which Box2D can handle. So, what you would need is a form of Shape which allows you to create a Polygon shape, much like Rectangular Shape. You would need to be able to set the vertices though.

Ill see if its doable, otherwise its a job for Nicolas and his nice ToDo list :d
---------------------------------------
Adam Goodchild
Your AndEngine Forum Moderator!
My Tutorials List
http://www.adam-goodchild.co.uk
---------------------------------------
Mimminito
 
Posts: 360
Joined: Wed Jul 21, 2010 3:08 pm
Location: Chelsmford, UK

Re: Simple Polygon in Box2d

Postby droidy » Thu Aug 19, 2010 1:26 pm

Ok Thanks!
I thought there was just something very easy i didn't see...
droidy
 
Posts: 5
Joined: Thu Aug 19, 2010 9:59 am

Re: Simple Polygon in Box2d

Postby Mimminito » Thu Aug 19, 2010 1:27 pm

No, im pretty sure that there is no class which allows you to do this as of yet. I need to get my head around how rectangles are created in AndEngine at their lowest level, so I can see how it would be possible to create a polygon
---------------------------------------
Adam Goodchild
Your AndEngine Forum Moderator!
My Tutorials List
http://www.adam-goodchild.co.uk
---------------------------------------
Mimminito
 
Posts: 360
Joined: Wed Jul 21, 2010 3:08 pm
Location: Chelsmford, UK

Re: Simple Polygon in Box2d

Postby Mimminito » Thu Aug 19, 2010 1:38 pm

I think I have found a way. Ill let you know how it goes. Could be a while till I post back. I cant spend too long on it though, got some of my own stuff to get started/finished. Will post back soon...
---------------------------------------
Adam Goodchild
Your AndEngine Forum Moderator!
My Tutorials List
http://www.adam-goodchild.co.uk
---------------------------------------
Mimminito
 
Posts: 360
Joined: Wed Jul 21, 2010 3:08 pm
Location: Chelsmford, UK

Next

Return to GLES1

Who is online

Users browsing this forum: Yahoo [Bot] and 16 guests