Beispiel #1
0
		public float DistanceTo(ref CCPoint vector, out Segment connectingSegment)
		{
			float segmentLength = (float)this.GetLength();

			CCPoint normalizedLine = new CCPoint(
				(float)(Point2.X - Point1.X) / segmentLength,
				(float)(Point2.Y - Point1.Y) / segmentLength);

			CCPoint pointVector = new CCPoint((float)(vector.X - Point1.X), (float)(vector.Y - Point1.Y));

			float length = CCPoint.Dot(pointVector, normalizedLine);
			connectingSegment = new Segment ();
			if (length < 0)
			{
				connectingSegment.Point1 = vector;
				connectingSegment.Point2 = Point1;

				return (float) connectingSegment.GetLength();
			}
			else if (length > segmentLength)
			{
				connectingSegment.Point1 = vector;
				connectingSegment.Point2 = Point2;

				return (float) connectingSegment.GetLength();
			}
			else
			{
				connectingSegment.Point1 = vector;
				connectingSegment.Point2 = new CCPoint(Point1.X + length * normalizedLine.X,
					Point1.Y + length * normalizedLine.Y);

				return (float)connectingSegment.GetLength();
			}
		}
Beispiel #2
0
		public bool CollideAgainst(Circle circle)
		{
			UpdateAbsolutePoints ();

			// This method will test the following things:
			//  * Is the circle's center inside the polygon?
			//  * Are any of the polygon's points inside the circle?
			//  * Is the circle within Radius distance from any of the polygon's edges?
			// Of course none of this is done if the radius check fails

			if ((circle.Radius + boundingRadius) * (circle.Radius + boundingRadius) >

				(circle.PositionX - PositionX) * (circle.Position.X - Position.X) +
				(circle.PositionY - PositionY) * (circle.Position.Y - Position.Y))
			{
				// First see if the circle is inside the polygon.
				if (IsPointInside(circle.PositionWorldspace.X, circle.PositionWorldspace.Y))
				{
					LastCollisionPoint.X = circle.Position.X;
					LastCollisionPoint.Y = circle.Position.Y;
					return true;
				}

				int i;
				// Next see if any of the Polygon's points are inside the circle
				for (i = 0; i < absolutePoints.Length; i++)
				{
					if (circle.IsPointInside(absolutePoints[i]))
					{
						LastCollisionPoint.X = absolutePoints[i].X;
						LastCollisionPoint.Y = absolutePoints[i].Y;
						return true;
					}
				}
				int k;

				Segment s1 = new Segment();
				Segment s2 = new Segment();

				// Next check if the circle is within Radius units of any segment.
				for (i = 0; i < absolutePoints.Length; i++)
				{
					k = i + 1 < absolutePoints.Length ? i + 1 : 0;
					s1.Point1 = absolutePoints [i];
					s1.Point2 = absolutePoints[k];

					var position = circle.PositionWorldspace;

					var segmentDistance = s1.DistanceTo (ref position, out s2);

					if (segmentDistance < circle.Radius)
					{
						LastCollisionPoint.X = s2.Point2.X;
						LastCollisionPoint.Y = s2.Point2.Y;

						return true;
					}
				}
			}
			return false;
		}
Beispiel #3
0
		public static CCPoint VectorFrom(float x, float y, CCPoint[] vertices, out int pointIndexBefore)
		{
			pointIndexBefore = -1;

			double minDistance = double.PositiveInfinity;
			double tempMinDistance;
			CCPoint vectorToReturn = new CCPoint();

			if (vertices.Length == 1)
			{
				return new CCPoint(
					vertices[0].X - x,
					vertices[0].Y - y);
			}

			Segment connectingSegment = new Segment();
			Segment segment = new Segment();

			for (int i = 0; i < vertices.Length - 1; i++)
			{
				int afterI = i + 1;
				segment.Point1 = new CCPoint(vertices[i].X, vertices[i].Y);
				segment.Point2 = new CCPoint(vertices[afterI].X, vertices[afterI].Y);

				var point = new CCPoint (x, y);
				tempMinDistance = segment.DistanceTo(ref point, out connectingSegment);
				if (tempMinDistance < minDistance)
				{
					pointIndexBefore = i;
					minDistance = tempMinDistance;
					vectorToReturn = new CCPoint(
						connectingSegment.Point2.X - connectingSegment.Point1.X,
						connectingSegment.Point2.Y - connectingSegment.Point1.Y);
				}
			}
			return vectorToReturn;
		}