public Universe Load(string filename) { TextReader reader = new StreamReader(filename); GravityObject var; List<GravityObject> planets = new List<GravityObject>(); int index = 0; string line = reader.ReadLine(); while (line != null) { if (line.Contains("{")) { line = reader.ReadLine(); float x = float.Parse(line.Substring(1, line.Length - 1)); line = reader.ReadLine(); float y = float.Parse(line.Substring(1, line.Length - 1)); Vector2 coordinates = new Vector2(x, y); line = reader.ReadLine(); float xv = float.Parse(line.Substring(1, line.Length - 1)); line = reader.ReadLine(); float yv = float.Parse(line.Substring(1, line.Length - 1)); Vector2 velocity = new Vector2(xv, yv); line = reader.ReadLine(); DebugFileManager.GetDebugFileManager().WriteLineF(line); float radius = float.Parse(line.Substring(1, line.Length - 1)); var = new GravityObject(index, coordinates, velocity, radius); planets.Add(var); index++; if (!reader.ReadLine().Contains("}")) { DebugFileManager.GetDebugFileManager().WriteLineF("There was an error in loading the planet data. Please make sure the savegame is not corrupted."); Environment.Exit(0); } } line = reader.ReadLine(); } Universe universe = new Universe(); foreach (GravityObject go in planets) { universe.AddGravityObject(go); } DebugFileManager dfm = DebugFileManager.GetDebugFileManager(); dfm.WriteLine("[SAVEGAME LOADING INFORMATION]"); dfm.WriteLine(" We managed to load the savegame. Here are the results generated from the savefile:"); foreach (GravityObject go in universe.planets) { dfm.WriteLine("\t[GRAVITYOBJECT START]"); dfm.WriteLine("\t "+ go.Diameter); dfm.WriteLine("\t[GRAVITYOBJECT END]"); } dfm.WriteLine("[SAVEGAME LOADING INFORMATION END]"); dfm.Flush(); return universe; }
public void CreatePath(Vector2 coordinates, Vector2 velocity, float mass, int points) { ghosts.Clear(); GravityObject go = new GravityObject(planets.Count, coordinates, velocity, mass, true); for (int i = 0; i < points; i++) { foreach (GravityObject go2 in planets) { go.CalculateForce(go2); } go.Update(-1); go.ClearForce(); ghosts.Add(new PathPoint( (int) go.Coordinates.X, (int) go.Coordinates.Y, 600, Color.White)); } }
public void AddGravityObject(GravityObject go) { planets.Add(go); }
public bool Handled(GravityObject go) { return false; }
/// <summary> /// Checks whether this object collides with the passed object, /// and if so, destroys the lighter object and increases the mass /// of the bigger object by the mass of the lighter object. /// If masses are equal, this object takes precendence. /// </summary> /// <param name="target">The object against which collisions will be checked.</param> public void CheckCollision(GravityObject target) { if (IsMarkedDelete) return; float dY = target.coordinates.Y - coordinates.Y; float dX = target.coordinates.X - coordinates.X; float c = Pythagoras(dY, dX); // Compares the distance between the planets to the radius of both planets added together. // Evaluates to true if objects have a collision. if (c <= (radius + target.Radius)) { int r = (this.color.R + target.color.R / 2); int g = (this.color.G + target.color.G / 2); int b = (this.color.B + target.color.B / 2); float targetSignificance = target.Mass / this.mass; if (radius > target.Radius) { this.mass += target.Mass; Vector2 vTargetTotal = target.velocity * new Vector2(targetSignificance, targetSignificance); Vector2 vTotal = this.velocity + vTargetTotal; this.velocity = vTotal / (1f + targetSignificance); this.color = new Color(r, g, b); target.IsMarkedDelete = true; } else if (radius < target.Radius) { target.Mass += this.mass; Vector2 vTargetTotal = target.velocity * new Vector2(targetSignificance, targetSignificance); Vector2 vTotal = this.velocity + vTargetTotal; target.velocity = vTotal / (1f + targetSignificance); target.color = new Color(r, g, b); this.IsMarkedDelete = true; } else { this.mass += target.Mass; this.coordinates.X += dX/2; this.coordinates.Y += dY/2; this.velocity = ((this.velocity + target.velocity) / new Vector2(2, 2)); this.color = new Color(r, g, b); target.IsMarkedDelete = true; } RecalculateSize(); target.RecalculateSize(); } }
/// <summary> /// Calculate the force between the 'this' object and the passed object. /// Force will only be applied on this object, so for a correct calculation of the force /// between object a and b, you need to run both a.CalculateForce(b) and b.CalculateForce(a). /// </summary> /// <param name="go">The GravityObject against which the force of this object will be calculated.</param> public void CalculateForce(GravityObject go) { float dX = go.coordinates.X - coordinates.X; float dY = go.coordinates.Y - coordinates.Y; float r = Pythagoras(dY, dX); float force = Universe.GRAVITATIONAL_CONSTANT * ((mass * go.Mass) /(float)Math.Pow(r,3)); lastForce = force; this.force.Y += dY * force; this.force.X += dX * force; }