Пример #1
0
        public static CCPoint GetSeparationVector(Circle circle, Polygon polygon)
        {
            bool isCircleCenterInPolygon = polygon.IsPointInside(
                                                   circle.PositionWorldspace.X, circle.PositionWorldspace.Y);

            float distance;
            var normal = polygon.GetNormalClosestTo(circle.PositionWorldspace, out distance);

            if (isCircleCenterInPolygon)
            {
                distance += circle.Radius;
            }
            else
            {
                distance = circle.Radius - distance;
            }

            // increase the distance by a small amount to make sure that the objects do separate:
            distance += .5f;

            var separation = normal * distance;

            return separation;
        }
Пример #2
0
		private void CreateCollision()
		{
			Collision = new Circle ();
			Collision.Radius = GameCoefficients.FruitRadius;
			this.AddChild (Collision);
		}
Пример #3
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;
		}