public void TestPolygonCollision() { Polygon a = new Polygon(); Polygon b = new Polygon(); Polygon c = new Polygon(); a.SetPoints(new Vector2[] { new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1), new Vector2(0, 1) }); b.SetPoints(a.Points); b.Offset(0.9f,0); c.SetPoints(b.Points); c.Offset(0.9f, 0); var resultA = Polygon.Collision(a, b, new Vector2()); var resultB = Polygon.Collision(b, c, new Vector2()); var resultC = Polygon.Collision(a, c, new Vector2()); Assert.AreEqual(true , resultA.Intersect ); Assert.AreEqual(true, resultB.Intersect); Assert.AreEqual(false, resultC.Intersect); }
private static IEnumerable<Vector2> _GetPoints(Polygon polygon, float extend) { var center = polygon.Center; var points = polygon.Points; var newPoints = from p in points let unit = ((center - p).GetNormalized() * extend) select p - unit; return newPoints; }
public void PolygonClone() { Polygon a = new Polygon(); a.SetPoints( new[]{ new Vector2(0, 0), new Vector2(2, 0), new Vector2(2, 1), new Vector2(0, 1), }); var b = a.Clone(); Assert.AreEqual( 0 , b.Points[0].X ); Assert.AreEqual(2, b.Points[1].X); Assert.AreEqual(1, b.Points[3].Y); }
private bool _TryGetCollider(ref Regulus.CustomType.Polygon polygon) { var boxs = gameObject.GetComponentsInChildren <BoxCollider>(); Vector2[] points = _GetVectors(boxs); if (points.Length <= 1) { return(false); } var paths = AForge.Math.Geometry.GrahamConvexHull.FindHull(points.ToList()).ToArray(); _SetPolygon(polygon, paths); return(true); }
public static Regulus.CustomType.Polygon ToRegulusPolygon(this UnityEngine.PolygonCollider2D collider) { var len = collider.points.Length; List<Vector2> points = new List<Vector2>(); for (int i = 0 ; i < len ; ++i) { var point = collider.points[i]; var worldPoint = collider.transform.TransformPoint(point); var connr =CameraHelper.Front.GetScreenPoint(worldPoint); points.Add( new Regulus.CustomType.Vector2(connr.x , connr.y)); } Regulus.CustomType.Polygon polygon = new Regulus.CustomType.Polygon(points.ToArray()); return polygon; }
public void TestExtendingPolygon() { var polygon = new Polygon( new [] { new Vector2(0,1), new Vector2(1,0), new Vector2(-1,0), new Vector2(0,-1), }); var extendPolygon = new ExtendPolygon(polygon , 1.0f); var result = extendPolygon.Result; Assert.AreEqual(new Vector2(0, 2), result.Points[0] ); Assert.AreEqual(new Vector2(2,0), result.Points[1] ); Assert.AreEqual(new Vector2(-2,0),result.Points[2] ); Assert.AreEqual(new Vector2(0,-2),result.Points[3] ); }
private bool _Hit(Regulus.CustomType.Polygon collider) { if (_TryGetCollider(ref _Polygon)) { if (_Polygon.Points.Length > 0 && collider.Points.Length > 0) { var result = Regulus.CustomType.Polygon.Collision(collider, _Polygon, new Regulus.CustomType.Vector2(0, 0)); if (result.Intersect || result.WillIntersect) { _ChangeMaterial(); return(true); } } } return(false); }
// Update is called once per frame void Update() { var angle = -transform.rotation.eulerAngles.y * Mathf.PI / 180.0f; var lefts = (from vl in Left let pnt = new Regulus.CustomType.Vector2(vl.x, vl.y) select Regulus.CustomType.Polygon.RotatePoint(pnt, new Regulus.CustomType.Vector2(), angle)).ToArray(); var right = (from vr in Right let pnt = new Regulus.CustomType.Vector2(vr.x, vr.y) select Regulus.CustomType.Polygon.RotatePoint(pnt, new Regulus.CustomType.Vector2(), angle)).ToArray(); var center = new Regulus.CustomType.Vector2(transform.position.x, transform.position.z); #if UNITY_EDITOR Regulus.Project.ItIsNotAGame1.Game.Play.BattleCasterStatus._DrawAll(lefts, right, transform.position, AllLineColor); #endif //_DrawAll(); var delta = UnityEngine.Time.deltaTime; Regulus.CustomType.Polygon result = _Determination.Find(_LastPart, delta); if (result != null) { result.Rotation(angle, new Regulus.CustomType.Vector2()); result.Offset(center); #if UNITY_EDITOR Regulus.Project.ItIsNotAGame1.Game.Play.BattleCasterStatus._Draw(result, transform.position.y, LineColor); #endif //_Draw(result);3 } _LastPart += delta; if (_LastPart > Total) { _LastPart -= Total; } }
private Regulus.CustomType.Polygon.CollisionResult _Collide(IIndividual individual, Polygon polygon, Vector2 velocity) { var result = Polygon.Collision(polygon, individual.Mesh, velocity); return result; }
public Orbit(Polygon body, Vector2 velocity) { List<Vector2> points = new List<Vector2>(); points.AddRange(body.Points); var polygon = body.Clone(); polygon.Offset(velocity); points.AddRange(polygon.Points); _Rect = points.ToRect(); }
//Unity Debug Code public static void _Draw(Polygon result, float y, UnityEngine.Color color) { var points = (from p in result.Points select new UnityEngine.Vector3(p.X, y, p.Y)).ToArray(); var len = points.Length; if (len < 2) { return; } for (int i = 0; i < len - 1; i++) { var p1 = points[i]; var p2 = points[i + 1]; UnityEngine.Debug.DrawLine(p1, p2, color); } UnityEngine.Debug.DrawLine(points[len - 1], points[0], color); }
public void TestPolygonSerializ() { var polygon1 = new Polygon(); polygon1.SetPoints(new[]{ new Vector2(0,0), new Vector2(1,0), new Vector2(1,1), new Vector2(0,1)}); var buffer = Regulus.TypeHelper.Serializer(polygon1); var polygon2 = Regulus.TypeHelper.Deserialize<Polygon>(buffer); Assert.AreEqual(polygon2.Points[0] , polygon1.Points[0]); }
public ExtendPolygon(Polygon polygon, float extend) { var newPoints = ExtendPolygon._GetPoints(polygon, extend); Result = new Polygon(newPoints.ToArray()); }
private void _SetPolygon(Regulus.CustomType.Polygon polygon, Vector2[] paths) { polygon.SetPoints(from p in paths select new Regulus.CustomType.Vector2(p.x, p.y)); }
public Polygon Clone() { var other = new Polygon(); other.SetPoints(_Points.ToArray()); return other; }
public void TestPolygonXMLSerializ() { var polygon1 = new Polygon(); polygon1.SetPoints(new[]{ new Vector2(0,0), new Vector2(1,0), new Vector2(1,1), new Vector2(0,1)}); var xml = ""; using (var stream = new StringWriter()) { var x = new XmlSerializer(typeof(Polygon)); x.Serialize(stream, polygon1); xml= stream.ToString(); } Polygon polygon2; using (var stream = new StringReader(xml)) { var ser = new XmlSerializer(typeof(Polygon)); polygon2=(Polygon)ser.Deserialize(stream); } Assert.AreEqual(polygon2.Points[0], polygon1.Points[0]); }
public void TestMapFind() { Map map = new Map(); Rect rect = new Rect(0.5f ,5,1,10); Polygon meshs1 = new Polygon(new[]{ new Vector2(1, 1), new Vector2(2, 1), new Vector2(2, 2), new Vector2(1, 2) }); Polygon meshs2 = new Polygon(new[]{ new Vector2(1, 1), new Vector2(2, 1), new Vector2(2, 2), new Vector2(1, 2) }); Polygon meshs3 = new Polygon(new[]{ new Vector2(1, 1), new Vector2(2, 1), new Vector2(2, 2), new Vector2(1, 2) }); Polygon meshs4 = new Polygon(new[]{ new Vector2(1, 1), new Vector2(2, 1), new Vector2(2, 2), new Vector2(1, 2) }); Polygon meshs5 = new Polygon(new[]{ new Vector2(1, 1), new Vector2(2, 1), new Vector2(2, 2), new Vector2(1, 2) }); meshs2.Offset(0,2); meshs3.Offset(0, 4); meshs4.Offset(0, 6); meshs5.Offset(0, 8); IIndividual[] visables = { new Entity(meshs1), new Entity(meshs2), new Entity(meshs3), new Entity(meshs4), new Entity(meshs5) }; foreach (var visable in visables) { map.JoinStaff(visable); } IMapFinder finder = map; var results = finder.Find(rect); foreach (var visable in visables) { map.Left(visable); } Assert.AreEqual(5, results.Length); }
public void _SetBody(Polygon body) { _Mesh = body.Clone(); _Bound = this._BuildBound(this._Mesh); _DetectionRange = 1.0f + _Mesh.Points.ToRect().Width; }
public void SetBody(Polygon body) { _SetBody(body.Clone()); }
public Entity(Polygon mesh) { SetBody(mesh.Clone()); }
public FishCollider() { _Polygon = new Regulus.CustomType.Polygon(); }
private Polygon _GetThroughRange(Vector2 velocity) { var after = this._Individual.Mesh.Clone(); after.Offset(velocity); Vector2[] points = new Vector2[8]; points[0] = _Individual.Mesh.Points[0]; points[1] = _Individual.Mesh.Points[1]; points[2] = _Individual.Mesh.Points[2]; points[3] = _Individual.Mesh.Points[3]; points[4] = after.Points[0]; points[5] = after.Points[1]; points[6] = after.Points[2]; points[7] = after.Points[3]; var polygon = new Polygon(points); return polygon; }
private Rect _BuildBound(Polygon mesh) { return mesh.Points.ToRect(); }
// Structure that stores the results of the PolygonCollision function public static CollisionResult Collision(Polygon polygonA, Polygon polygonB, Vector2 velocity) { if(polygonA._Points.Length== 0 || polygonA._Points.Length== 0) { throw new ArgumentException("param polygonA or polygonB point count are zero."); } var result = new CollisionResult(); result.Intersect = true; result.WillIntersect = true; var edgeCountA = polygonA.Edges.Length; var edgeCountB = polygonB.Edges.Length; var minIntervalDistance = float.PositiveInfinity; var translationAxis = new Vector2(); Vector2 edge; // Loop through all the edges of both polygons for(var edgeIndex = 0; edgeIndex < edgeCountA + edgeCountB; edgeIndex++) { if(edgeIndex < edgeCountA) { edge = polygonA.Edges[edgeIndex]; } else { edge = polygonB.Edges[edgeIndex - edgeCountA]; } // ===== 1. Find if the polygons are currently intersecting ===== // Find the axis perpendicular to the current edge var axis = new Vector2(-edge.Y, edge.X); axis.Normalize(); // Find the projection of the polygon on the current axis float minA = 0; float minB = 0; float maxA = 0; float maxB = 0; Polygon.ProjectPolygon(axis, polygonA, ref minA, ref maxA); Polygon.ProjectPolygon(axis, polygonB, ref minB, ref maxB); // Check if the polygon projections are currentlty intersecting if(Polygon.IntervalDistance(minA, maxA, minB, maxB) > 0) { result.Intersect = false; } // ===== 2. Now find if the polygons *will* intersect ===== // Project the velocity on the current axis var velocityProjection = axis.DotProduct(velocity); // Get the projection of polygon A during the movement if(velocityProjection < 0) { minA += velocityProjection; } else { maxA += velocityProjection; } // Do the same test as above for the new projection var intervalDistance = Polygon.IntervalDistance(minA, maxA, minB, maxB); if(intervalDistance > 0) { result.WillIntersect = false; } // If the polygons are not intersecting and won't intersect, exit the loop if(!result.Intersect && !result.WillIntersect) { break; } // Check if the current interval distance is the minimum one. If so store // the interval distance and the current distance. // This will be used to calculate the minimum translation Vector2 intervalDistance = Math.Abs(intervalDistance); if(intervalDistance < minIntervalDistance) { minIntervalDistance = intervalDistance; translationAxis = axis; var d = polygonA.Center - polygonB.Center; if(d.DotProduct(translationAxis) < 0) { translationAxis = -translationAxis; } } } // The minimum translation Vector2 can be used to push the polygons appart. // First moves the polygons by their velocity // then move polygonA by MinimumTranslationVector2. if(result.WillIntersect) { result.MinimumTranslationVector2 = translationAxis * minIntervalDistance; } return result; }
// Calculate the projection of a polygon on an axis and returns it as a [min, max] interval public static void ProjectPolygon(Vector2 axis, Polygon polygon, ref float min, ref float max) { // To project a point on an axis use the dot product var d = axis.DotProduct(polygon.Points[0]); min = d; max = d; for(var i = 0; i < polygon.Points.Length; i++) { d = polygon.Points[i].DotProduct(axis); if(d < min) { min = d; } else { if(d > max) { max = d; } } } }
public void TestPolygonsXMLSerializ() { var polygon1 = new Polygon(); polygon1.SetPoints(new[]{ new Vector2(0,0), new Vector2(1,0), new Vector2(1,1), new Vector2(0,1)}); var polygon2 = new Polygon(); polygon2.SetPoints(new[]{ new Vector2(2,0), new Vector2(1,0), new Vector2(1,1), new Vector2(0,1)}); var polygons1 = new Polygon[] { polygon1, polygon2 }; var xml = ""; using (var stream = new StringWriter()) { var x = new XmlSerializer(typeof(Polygon[])); x.Serialize(stream, polygons1); xml = stream.ToString(); } Polygon[] polygons2; using (var stream = new StringReader(xml)) { var ser = new XmlSerializer(typeof(Polygon[])); polygons2 = (Polygon[])ser.Deserialize(stream); } Assert.IsTrue( Regulus.Utility.ValueHelper.DeepEqual(polygons2 , polygons1)); }