/// <summary> /// Accelerates to another body, based on gravitational effects /// </summary> /// <param name="body">The body to accelerate towards</param> /// <param name="deltaTime">The amount of time since the last calculation</param> public void AccelerateTo(GravitationSource body, TimeSpan deltaTime) { var force = ForceTo(body); var direction = body.Position - Position; Accelerate(force, direction, deltaTime); }
private Vector3 findLagrange(double direction, double start) { // walk the gravity potential between this planet and the star, looking for L1ish Func <double, Vector3> move = percentage => Position.Interpolate(SystemStar.Position, percentage, true); var percent = start; var body = new GravitationSource(100, 10, move(percent), new Vector3(0, 0, 0)); var toStar = body.ForceTo(SystemStar); var toPlanet = body.ForceTo(this) + Moons.Sum((moon => body.ForceTo(moon))); var movedToStar = false; var movedToPlanet = false; while (System.Math.Abs(toStar - toPlanet) > 0.01) { if (toStar > toPlanet) { body.Position = move(percent += direction); movedToStar = true; } else if (toStar < toPlanet) { body.Position = move(percent -= direction); movedToPlanet = true; } toStar = body.ForceTo(SystemStar); toPlanet = body.ForceTo(this); if (!movedToStar || !movedToPlanet) { continue; } direction *= 0.5; movedToPlanet = false; movedToStar = false; } return(body.Position); }
/// <summary> /// Gets the amount of acceleration to a body of mass /// </summary> /// <param name="body">The body to perform calculations on</param> /// <returns>The acceleration in m/s/s</returns> public double AccelerationTo(GravitationSource body) { return(ForceTo(body) / Mass); }
/// <summary> /// Get's the amount of force from this body to another /// </summary> /// <param name="body">The body to perform calculations on</param> /// <returns>The Newtons of force</returns> public double ForceTo(GravitationSource body) { var distance = Position.Distance(body.Position); return((G * Mass * body.Mass) / (System.Math.Pow(distance, 2))); }
/// <summary> /// Determine if there is a collision with another body... /// </summary> /// <param name="body"></param> /// <returns></returns> public bool IsCollidingWith(GravitationSource body) { var distance = Position.Distance(body.Position); return(distance - Radius - body.Radius <= 0); }