/// <summary> /// Finish adding vertices to this JeloClosedShape, and choose whether to convert into local space. /// JelloClosedShape.winding and JelloClosedShape.Triangles will be set. /// Make sure there are no duplicate points before calling this. use JelloShapeTools.RemoveDuplicatePoints(). /// </summary> /// <param name="recenter">whether to convert the positions of the JelloClosedShape into local space.</param> /// /// <dl class="example"><dt>Example</dt></dl> /// ~~~{.c} /// //Create a closed shape square /// JelloClosedShape shape = new JelloClosedShape(); /// /// shape.begin(); /// /// shape.addPoint(new Vector2(-1,1)); //top left /// shape.addPoint(new Vector2(-1,1)); //top right /// shape.addPoint(new Vector2(-1,1)); //bottom right /// shape.addPoint(new Vector2(-1,1)); //bottom left /// /// shape.finish(); /// ~~~ public void finish(bool recenter = true) { if (mInternalVertices != null && mInternalVertices.Length > 0) { //dont allow duplicate points mInternalVertices = JelloShapeTools.RemoveDuplicatePoints(mInternalVertices); //dont allow points outside of the perimiter for (int i = 0; i < mInternalVertices.Length; i++) { if (!JelloShapeTools.Contains(mEdgeVertices, mInternalVertices[i])) { mInternalVertices[i] = Vector2.one * Mathf.Infinity; } } //dont allow points on the perimiter. (this will also remove any null points) mInternalVertices = JelloShapeTools.RemovePointsOnPerimeter(mEdgeVertices, mInternalVertices); } mCenter = JelloShapeTools.FindCenter(mEdgeVertices); if (recenter) { // now subtract this from each element, to get proper "local" coordinates. for (int i = 0; i < mEdgeVertices.Length; i++) { mEdgeVertices[i] -= mCenter; } if (mInternalVertices != null) { for (int i = 0; i < mInternalVertices.Length; i++) { mInternalVertices[i] -= mCenter; } } } if (JelloShapeTools.HasClockwiseWinding(mEdgeVertices)) { winding = Winding.Clockwise; } else { winding = Winding.CounterClockwise; } Triangulate(); }
/// <summary> /// Flip the JelloClosedShape verticaly. /// </summary> /// /// <dl class="example"><dt>Example</dt></dl> /// ~~~{.c} /// //turn a body around when it hits a ceiling /// /// JelloBody body; /// void handleCollisionEnter(JelloCollisionManifold manifold) /// { /// if(manifold.GetOtherBody(body).gameObject.tag == "ceiling") /// { /// body.flipY(); /// //also reverse movement direction /// } /// } /// ~~~ public void flipY() //TODO check into this --> will i need to retriangulate? because mesh triangles may need to always be wound in a certain direction. { Vector2[] tempVertices = new Vector2[mEdgeVertices.Length]; for (int i = 0; i < mEdgeVertices.Length; i++) { mEdgeVertices[i] = new Vector2(mEdgeVertices[i].x, mEdgeVertices[i].y + 2 * (Center.x - mEdgeVertices[i].x)); tempVertices[mEdgeVertices.Length - i - 1] = mEdgeVertices[i]; } mEdgeVertices = tempVertices; tempVertices = new Vector2[mInternalVertices.Length]; for (int i = 0; i < mInternalVertices.Length; i++) { mInternalVertices[i] = new Vector2(mInternalVertices[i].x, mInternalVertices[i].y + 2 * (Center.x - mInternalVertices[i].x)); tempVertices[mInternalVertices.Length - i - 1] = mInternalVertices[i]; } mInternalVertices = tempVertices; if (winding == Winding.Clockwise) { winding = Winding.CounterClockwise; } else if (winding == Winding.CounterClockwise) { winding = Winding.Clockwise; } else { if (JelloShapeTools.HasClockwiseWinding(mEdgeVertices)) { winding = Winding.Clockwise; } else { winding = Winding.CounterClockwise; } } //finish (); }