private static bool Circle(CircleCollider a, CircleCollider b) { if ((a.Radius + b.Radius) > ((a.position + a.center) - (b.position + b.center)).Length()) { return(true); } return(false); }
public Bullet(float xPosition, float yPosition, float xDirection, float yDirection, float speed) { position = new Vector2(xPosition, yPosition); direction = new Vector2(xDirection, yDirection); this.speed = speed; visual = new Sprite(Renderer.Instance.bullets, 32, 0, 32, 32); collider = new CircleCollider(position.X, position.Y, 26, 15, 11); }
// Finds the maximum and minimum values of a circle's projection onto an axis. private static void GatherCircleProjectionExtents(Vector2 projectionAxis, CircleCollider circle, out float min, out float max) { // Project the circle's center onto the axis with a dot product. float centerProjection = Vector2.Dot(circle.position + circle.center, projectionAxis); // Add and subtract the radius to get the extents. min = centerProjection - circle.Radius; max = centerProjection + circle.Radius; }
public BombPowerup(int xPosition, int yPosition) { position = new Vector2(xPosition, yPosition); direction = new Vector2(-1, 0); speed = 200; visual = new AnimatedSprite(Renderer.Instance.powerup_bomb, 1, 8, 8, 1000, true); collider = new CircleCollider(position.X, position.Y, 32, 32, 32); }
public Bomb(int xPosition, int yPosition) { position = new Vector2(xPosition, yPosition); visual = null; collider = new CircleCollider(position.X, position.Y, 0, 0, 128); AudioSystem.Instance.PlaySound("ui_back"); }
private static bool CircleHalfPlane(CircleCollider c, HalfPlaneCollider hp) { // Distance from the circle's center to the half-plane. float distance = Math.Abs(hp.A * (c.position.X + c.center.X) + hp.B * (c.position.Y + c.center.Y) + hp.C) / (float)Math.Sqrt(hp.A * hp.A + hp.B * hp.B); if (distance < c.Radius) { return(true); } return(false); }
private static bool AABBCircle(AABBCollider a, CircleCollider c) { List <Vector2> AABBVerts = new List <Vector2>(); AABBVerts.Add(new Vector2(0, 0)); AABBVerts.Add(new Vector2(a.Width, 0)); AABBVerts.Add(new Vector2(a.Width, a.Height)); AABBVerts.Add(new Vector2(0, a.Height)); PolygonCollider hax = new PolygonCollider(a.position.X, a.position.Y, AABBVerts); return(PolygonCircle(hax, c)); }
private static bool PolygonCircle(PolygonCollider p, CircleCollider c) { // Circle projection: project center, add and subtract radius. // Circle - only one axis, runs from the center of the circle to the closest vertex on the polygon. for (int i = 0; i < p.vertices.Count; i++) { // Get the axis to project the polygons' points onto. Vector2 edge; if (i != p.vertices.Count - 1) { edge = p.vertices[i + 1] - p.vertices[i]; } else { edge = p.vertices[0] - p.vertices[i]; } Vector2 axis = new Vector2(edge.Y, -edge.X); // Rotate the vector 90 degrees to get the axis vector. Rotation direction doesn't matter. axis.Normalize(); // Normalize. It only needs to describe direction. // Project each polygon onto the axis. Remember the extents (minimum and maximum) of each projection. float aMin, aMax, bMin, bMax; GatherPolygonProjectionExtents(axis, p.vertices, out aMin, out aMax); GatherCircleProjectionExtents(axis, c, out bMin, out bMax); // Test for overlap between the two projections. if (aMin > bMax) { return(false); } if (aMax < bMin) { return(false); } } // When checking against a circle we are interested in only one axis. // It goes from the center of the circle to the closest vertex of the polygon. // We need to find the closest vertex. Vector2 closestVertex = p.vertices[0]; float distance = (c.position - closestVertex).LengthSquared(); // We can use squared length. for (int i = 0; i < p.vertices.Count; i++) { float dist = p.vertices[i].LengthSquared(); if (dist < distance) { distance = dist; closestVertex = p.vertices[i]; } } Vector2 circleEdge = closestVertex - c.position; Vector2 circleAxis = new Vector2(circleEdge.Y, -circleEdge.X); circleAxis.Normalize(); float caMin, caMax, cbMin, cbMax; GatherPolygonProjectionExtents(circleAxis, p.vertices, out caMin, out caMax); GatherCircleProjectionExtents(circleAxis, c, out cbMin, out cbMax); if (caMin > cbMax) { return(false); } if (caMax < cbMin) { return(false); } return(true); }