/// <summary> /// Add an object to the list /// </summary> /// <param name="co"></param> public static void AddObject(IQuadtreeObject co) { if (co != null) { AllObjects.Add(co); } }
/// <summary> /// Draws an object on screen /// </summary> /// <param name="obj"></param> /// <param name="size"></param> void Draw(IQuadtreeObject obj, int size) { Ellipse circle = new Ellipse(); circle.Fill = obj.visuals.colour; circle.Height = obj.visuals.size; circle.Width = obj.visuals.size; if (obj.screenPosition.x + obj.visuals.size < centre.x && obj.screenPosition.y + obj.visuals.size < centre.y) { Canvas.SetTop(circle, obj.screenPosition.y + (size / 2) + 200); Canvas.SetLeft(circle, obj.screenPosition.x + (size / 2) + 150); Simulation.Children.Add(circle); } }
///// Methods ///// public void AddBody(IQuadtreeObject body) { if (_childA != null) { var child = GetQuadrant(body.Position); child.AddBody(body); } else { _bodies.Add(body); if (_bodies.Count > _maxBodiesPerNode && _curLevel < _maxLevel) { Split(); } } }
/// <summary> /// Calculate the force between this object and another object then return the values /// </summary> /// <param name="co"></param> /// <returns></returns> public double[] Attraction(IQuadtreeObject co) { if (co == null) { return(null); } double[] forces = new double[2]; double distance = Vector2.DistanceSqr(position, co.position); if (DebugTools.PrintForces) { Console.WriteLine("Celestial Object Attraction() - Distance: " + Math.Sqrt(distance)); Console.WriteLine(Name + " Position: " + position.ToString()); Console.WriteLine(co.Name + " Position: " + co.position.ToString()); } // If the objects are on top of each other, a DivideByZero error would occur so return if (distance == 0) { Console.WriteLine("Objects {0} {1} are on top of each other!", Name, co.Name); return(null); } // Using the formula F = GMm/d^2 double force = (Constants.Gravitational * Mass * co.Mass) / distance; double differenceX = Vector2.DifferenceX(position, co.position); double differenceY = Vector2.DifferenceY(position, co.position); // Calculate the angle between the objects double theta = Math.Atan2(differenceY, differenceX); // Split the force in the x and y components double forceX = force * Math.Cos(theta); double forceY = force * Math.Sin(theta); forces[0] = forceX; forces[1] = forceY; return(forces); }
// Update the velocites of the objects static Vector2[] OnCollision(IQuadtreeObject obj1, IQuadtreeObject obj2) { Vector2[] velocities = new Vector2[2]; double combinedMasses = obj1.Mass + obj2.Mass; double differenceObj1Obj2Mass = obj1.Mass - obj2.Mass; double differenceObj2Obj1Mass = obj2.Mass - obj1.Mass; double obj1Horizontal = obj1.velocity.x * ((differenceObj1Obj2Mass) / (combinedMasses)) + ((2 * obj2.Mass * obj2.velocity.x) / (combinedMasses)); double obj1Vertical = obj1.velocity.y * ((differenceObj1Obj2Mass) / (combinedMasses)) + ((2 * obj2.Mass * obj2.velocity.y) / (combinedMasses)); double obj2Horizontal = obj2.velocity.x * ((differenceObj2Obj1Mass) / (combinedMasses)) + ((2 * obj1.Mass * obj1.velocity.x) / (combinedMasses)); double obj2Vertical = obj2.velocity.y * ((differenceObj2Obj1Mass) / (combinedMasses)) + ((2 * obj1.Mass * obj1.velocity.y) / (combinedMasses)); velocities[0] = new Vector2(obj1Horizontal, obj1Vertical); velocities[1] = new Vector2(obj2Horizontal, obj2Vertical); return(velocities); }
/// <summary> /// Find the first object with the name "name" and then return it /// </summary> /// <param name="name"></param> /// <returns>The object with the name</returns> public static IQuadtreeObject FindObjectWithName(string name) { IQuadtreeObject co = AllObjects.First(s => s.Name == name); return(co); }