Multiple times, I have found myself wanting to draw polygons in code, but after awhile I got really sick of having to rewrite all the drawing code every time.
I wrote some classes that allow you to simply enter vertices to create a polygon sprite.
For example, you can make an array of 5 points, then create a sprite out of them like this:
Vector2[] vertices = new Vector2[] { new Vector2(0, 0), new Vector2(25, 5), new Vector2(35, 45), new Vector2(20, 23), new Vector2(5, 57) }; WTPolygonSprite s = new WTPolygonSprite(new WTPolygonData(vertices)); s.color = Color.green; AddChild(s);
Then you can do whatever you want with it. For example, I can change its scale rotation and position, just like a normal FSprite:
s.scaleX = 2.0f; s.scaleY = 0.5f; s.rotation = 143;
Then, using WTPolygonSprite’s method GetAdjustedVertices(), I can get an array of all the vertices with the sprite’s rotation, scale and position taken into account. This is useful if you want to detect where the polygon’s vertices actually are on the screen:
// This will log the original values of the vertices Vector2[] originalVertices = s.polygonData.polygonVertices; for (int i = 0; i < originalVertices.Length; i++) { Vector2 v = originalVertices[i]; Debug.Log("Original vertex " + i + ": " + v); } Debug.Log("==================================================================================================="); // This will log the values of the vertices after taking rotation, scale, and position into account Vector2[] adjustedVertices = s.GetAdjustedPolygonData().polygonVertices; for (int i = 0; i < adjustedVertices.Length; i++) { Vector2 v = adjustedVertices[i]; Debug.Log("Adjusted vertex " + i + ": " + v); }
Here’s another example of something awesome you can do. I can easily draw a perfect circle in code like this:
int circleResolution = 50; float radius = 50; Vector2[] circleVertices = new Vector2[circleResolution]; for (int i = 0; i < circleResolution; i++) { float x = Mathf.Cos(2 * Mathf.PI / circleResolution * (i + 1)) * radius; float y = Mathf.Sin(2 * Mathf.PI / circleResolution * (i + 1)) * radius; circleVertices[i] = new Vector2(x, y); } WTPolygonSprite sCircle = new WTPolygonSprite(new WTPolygonData(circleVertices)); sCircle.color = Color.cyan; AddChild(sCircle);
Finally, in WTUtils there are some helper methods that help you detect intersections between a circle and a polygon, or between line segments. This is all you have to do:
bool doesIntersect = IntersectCirclePolygon(circlePosition, circleRadius, polygonVertices);
WTPolygonData is a simplified version of FPPolygonalData, and WTPolygonSprite is basically the FPDebugPolygonColliderView class with some alterations and add-ons. See the WTExampleScene class for an example of how to actually use the sprite. If you’re already comfortable with Futile, it will be very simple.
Get the files here. And make sure you are using the development_physics branch of Futile (unless by the time you’re reading this Matt has merged that branch into master.)