// calulate all gravitational forces in game // then will apply the force to the given object public void ApplyGravityToObject(GameObject gameObject) { Vector2 netForce = new Vector2(0f, 0f); Vector2 position = gameObject.position; foreach (GameObject opposer in asteroids) { // find unit vector Vector2 diffVect = opposer.position - position; Vector2 unitVector; if (diffVect.Length() == 0.0f) continue; unitVector.X = diffVect.X / diffVect.Length(); unitVector.Y = diffVect.Y / diffVect.Length(); Vector2 currentForce; currentForce.X = (opposer.mass / diffVect.LengthSquared()) * GRAVITATIONAL_CONSTANT * unitVector.X; currentForce.Y = (opposer.mass / diffVect.LengthSquared()) * GRAVITATIONAL_CONSTANT * unitVector.Y; netForce += currentForce; } netForce.X = netForce.X * gameObject.mass; netForce.Y = netForce.Y * gameObject.mass; gameObject.UpdateVelocityWithForce(netForce); }
private bool IsCollision(GameObject asteroid, GameObject asteroid2) { asteroid.vertices.Add(asteroid.vertices.First()); asteroid2.vertices.Add(asteroid2.vertices.First()); for (int i = 0; i < asteroid.vertices.Count - 1; i++) { for (int j = 0; j < asteroid2.vertices.Count - 1; j++) { if (Intersects(asteroid.vertices.ElementAt(i), asteroid.vertices.ElementAt(i + 1), asteroid2.vertices.ElementAt(j), asteroid2.vertices.ElementAt(j + 1))) { asteroid.vertices.RemoveAt(asteroid.vertices.Count - 1); asteroid2.vertices.RemoveAt(asteroid2.vertices.Count - 1); return true; } } } asteroid.vertices.RemoveAt(asteroid.vertices.Count - 1); asteroid2.vertices.RemoveAt(asteroid2.vertices.Count - 1); return false; }
// calulate all collision forces in game // then will apply the force to the given object public void ApplyCollisionForceToObject(GameObject asteriod) { // woot }
private void HandleObjectCollisions(Stack<Collision> collisions, GameObject asteroid) { foreach (GameObject asteroid2 in asteroids) { if (asteroid.Equals(asteroid2)) { continue; } if (IsWithinRadius(asteroid, asteroid2)) { //possible collision if (IsCollision(asteroid, asteroid2)) { collisions.Push(new Collision(asteroids.IndexOf(asteroid), asteroids.IndexOf(asteroid2))); } } } }
private void HandleEdgeCollisions(Stack<Collision> collisions, GameObject asteroid) { if (CollidesWithEdge(asteroid)) { collisions.Push(new Collision(asteroids.IndexOf(asteroid), -1)); } }
private bool CollidesWithEdge(GameObject asteroid) { //called when checking if an object collides with the border of the screen. asteroid.vertices.Add(asteroid.vertices.First()); for (int i = 0; i < asteroid.vertices.Count - 1; i++) { Vector2 v1 = asteroid.vertices.ElementAt(i); Vector2 v2 = asteroid.vertices.ElementAt(i + 1); Vector2 topLeft = new Vector2(border.Left, border.Top); Vector2 topRight = new Vector2(border.Right, border.Top); Vector2 bottomRight = new Vector2(border.Right, border.Bottom); Vector2 bottomLeft = new Vector2(border.Left, border.Bottom); if (Intersects(v1, v2, topLeft, topRight)) { asteroid.vertices.RemoveAt(asteroid.vertices.Count - 1); return true; } else if (Intersects(v1, v2, topRight, bottomRight)) { asteroid.vertices.RemoveAt(asteroid.vertices.Count - 1); return true; } else if (Intersects(v1, v2, bottomRight, bottomLeft)) { asteroid.vertices.RemoveAt(asteroid.vertices.Count - 1); return true; } else if (Intersects(v1, v2, bottomLeft, topLeft)) { asteroid.vertices.RemoveAt(asteroid.vertices.Count - 1); return true; } } asteroid.vertices.RemoveAt(asteroid.vertices.Count - 1); return false; }
private static bool IsWithinRadius(GameObject asteroid, GameObject asteroid2) { return Vector2.Distance(asteroid.position, asteroid2.position) <= asteroid.radius + asteroid2.radius; }
private void DrawObject(GameObject asteroid) { foreach (Vector2 point1 in asteroid.vertices) { foreach (Vector2 point2 in asteroid.vertices) { DrawLine(point1, point2, asteroid.colour); } } }