FruityFalls.Geometry.Polygon.CollideAgainst C# (CSharp) Метод

CollideAgainst() публичный Метод

public CollideAgainst ( Circle circle ) : bool
circle Circle
Результат bool
		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;
		}

Usage Example

Пример #1
0
        private static bool FruitPolygonCollision(Fruit fruit, Polygon polygon, CCPoint polygonVelocity)
        {
            // Test whether the fruit collides
            bool didCollide = polygon.CollideAgainst(fruit.Collision);

            if (didCollide)
            {
                var circle = fruit.Collision;

                // Get the separation vector to reposition the fruit so it doesn't overlap the polygon
                var separation = CollisionResponse.GetSeparationVector(circle, polygon);
                fruit.Position += separation;

                // Adjust the fruit's Velocity to make it bounce:
                var normal = separation;
                normal.Normalize();
                fruit.Velocity = CollisionResponse.ApplyBounce(
                    fruit.Velocity, 
                    polygonVelocity, 
                    normal, 
                    GameCoefficients.FruitCollisionElasticity);

            }
            return didCollide;
        }