D13's Polygon Extension - 90% Complete

  ... the case you feel the need for a new feature or want to submit one.

D13's Polygon Extension - 90% Complete

Postby D13 » Mon Nov 29, 2010 3:45 am

I saw a bit of chatter yesterday about Polygons again, so I thought I would release my Polygon extension if anyone is interested in it, even though it's only about 90% complete. It will probably be awhile before I get around to finishing it up, so here is what I have so far. A lot of it is based on similar concepts that have been shown before, but one major difference is that I took a different route as far as how you request a Polygon. Instead of having to specify vertices for each polygon you want, I treat it more like a Sprite or Rectangle. Just say where, width, height and how many sides and it is built for you (so, if you want to create polygons based on a specific set of known vertices, sorry, this won't help you unless you want to translate those points to a bounding rectangle). For example, the following image shows various Polygons that were created (essentially) with only 2 lines of code:

Image

Polygon(pX,pY,pWidth,pHeight,pSides,pFill)
PolySprite(pX,pY,pWidth,pHeight,pSides,pPolygonTextureRegion)

There is also a helper for Box2D that will create the Box2D body for the Polygon, whatever the number of sides and scaling being used.

PolyBody.createPolyBody(mPhysicsWorld, MyPoly, BodyType.DynamicBody, FIXTURE_DEF)

Packaging

I generally try to avoid modifying library code directly as a force of habit. So all this code is presented as a separate package that extends and adds to the base AndEngine code, so if you use the standard AndEndgine.jar file you can still use this code seamlessly. Just create a package (org.anddev.andengine.D13.polygon is what its currently called in the code) and copy the code files in. It basically extends and duplicates the Rectangle, Sprite, and Texture Region classes of AndEngine (Polygon, PolySprite, and PolygonTextureRegion). Where I could extend existing classes I did, but sometimes, especially for the TextureRegion stuff, I had to create duplicate classes (mainly because the standard classes have a constant for the number of vertices, and that I didn't want to change the original code). I have included a couple of examples, modifications of the CollisionExample and PhysicsExample that show most of the features (still set to launch from the Example launcher). It was these two programs I used for most of my testing. In the CollisionExample, clicking on the PolySprite will cycle the Polygon through different sides and fill/non-fill and also cycle the PolySprite through different number of sides. You can also see here that these guys don't nicely support the standard AndEngine collision as yet (I mainly work with Box2D at the moment so I didn't bother here yet).

Features

* Build filled, outline, and sprite polygons by simply specifying location, width and height of bounding rectangle, and number of sides, from triangles all the way up to essentially circles when you get to a high number of sides.
* Change width, height, and scaling independently to modify the standard symmetrical polygon
* Dynamically change the number of sides of your polygon (whatever number of sides you start with, you can change the number of sides to any lower number down to 3)
* Box2D body builder to automatically build the correct body for your Polygon, supporting scaled Polygons.

Issues

* Although Box2D collision is fully handled by the Box2D createPolyBody method, regular AndEngine collision (collideswith, contains) is still using the standard AndEngine code so it only detects collisions based on the Polygon's bounding rectangle.
* FlippedHorizontal and FlippedVertical is not yet supported.
* PolygonTextureRegions must use symmetrical Textures. In other words, a texture that you use for your PolygonTextureRegions should be sized as a square (32,32), (64,64), (128,128), etc. The reason has to do with a little bit of rotational funkiness that happens in the code that I haven't sorted out. If you don't, an error is written in the log output, and while most PolySprites will show correctly, some (I think Octagons mainly) won't show correctly.
* When changing the number of sides on an outline polygon, just for a frame or two one of the vertices is not drawn correct, just a quick flash, then it's good. No idea what this is, maybe something to do with unused space in the VertexBuffer (because when the number of sides is reduced, I don't bother resizing the VertexBuffer).
* Tiled and Animated PolySprites are not yet supported, but should not be too hard to add as some point.

One last thing to know. When you specify height and width you are specifying the bounding recentangle for whatever polygon is drawn and however it is rotated. One side effect to this is that, for example, a polygon of 4 sides with equal height and width of 32pixels (ie a square) will NOT have sides 32pixels long. What you will get is a square that is 32pixels from 1 vertex to the opposite vertex. The CollisionDetection example kind of shows this. The outer square is a regular AndEngine square. Both the outer square and inner Polygon are the same size. The outer square is scaled to 2, the inner one has a loop shape modifier running the scale from 1 to 2, but you notice the inner polygon never exceeds the bounds of the outer square as it rotates. Oh, and because of this concept, when you use PolySprites you may find the image heavily clipped, especially for triangles and squares. You may need to tune your source images a bit if necessary to work nicely with PolySprites.

Anyways, if this is useful to you, enjoy...
Attachments
D13.Polygon.zip
(15.21 KiB) Downloaded 429 times
D13
 
Posts: 172
Joined: Wed Oct 06, 2010 6:08 am
Location: Toronto, Canada

Re: D13's Polygon Extension - 90% Complete

Postby photek » Tue Nov 30, 2010 12:50 am

Hey, this is cool. Thanks D13!

Bookmarked!
photek
 
Posts: 32
Joined: Fri Sep 03, 2010 8:24 pm

Re: D13's Polygon Extension - 90% Complete

Postby Nicolas Gramlich » Tue Nov 30, 2010 3:01 am

That sounds pretty nice and will probably be very useful for many :)

Best Regards,
Nicolas
Nicolas Gramlich
Site Admin
 
Posts: 1734
Joined: Mon Jun 07, 2010 6:20 pm
Location: Schriesheim, Germany

Re: D13's Polygon Extension - 90% Complete

Postby albrandroid » Sat Dec 04, 2010 9:10 pm

Really cool!!
Thank you..
albrandroid
 
Posts: 26
Joined: Thu Aug 12, 2010 9:17 am
Location: Vienna

Re: D13's Polygon Extension - 90% Complete

Postby morthek » Fri Feb 11, 2011 10:28 pm

VEEERYYY NICE!!!!

Let us know when the colision detector is ok. Thank you a lot!
Victor Araujo
morthek
 
Posts: 29
Joined: Wed Sep 22, 2010 6:31 pm


Return to Features

Who is online

Users browsing this forum: No registered users and 4 guests