public SolidRectangle(float width, float height) { Polygon = Polygon.CreateRectangle(width, height); var graphic = new CCDrawNode(); graphic.DrawRect( new CCRect(-width / 2, -height / 2, width, height), fillColor: CCColor4B.Blue); this.AddChild(graphic); this.AddChild(Polygon); }
public static Polygon CreateRectangle(float width, float height) { var polygon = new Polygon(); var points = new CCPoint[] { new CCPoint(-width/2, -height/2), new CCPoint(-width/2, height/2), new CCPoint(width/2, height/2), new CCPoint(width/2, -height/2), new CCPoint(-width/2, -height/2) }; polygon.Points = points; return polygon; }
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; }
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; }