commit 9f2f03178e6d7688ea7cb1bbedb8e910579360a7
parent 6c002eb536b08f394208b53710bc9c1853f5c76d
Author: Mikolaj Lenczewski <33129490+EnderRifter@users.noreply.github.com>
Date: Mon, 24 Jun 2019 15:34:31 +0100
Added extra infrastructure.
Diffstat:
7 files changed, 216 insertions(+), 52 deletions(-)
diff --git a/StarSim/StarSim/Program.cs b/StarSim/StarSim/Program.cs
@@ -12,9 +12,14 @@ namespace StarSim
internal class Program
{
/// <summary>
+ /// The body position update algorithm to use.
+ /// </summary>
+ private static readonly UpdateDelegate bodyPositionUpdater = UpdateBodiesBruteForce;
+
+ /// <summary>
/// Caches a random number generator to use for all randomised positions and velocities.
/// </summary>
- private static readonly Random RNG = new Random();
+ private static readonly Random Rng = new Random();
/// <summary>
/// Holds all the <see cref="Body"/> instances that should be simulated.
@@ -70,10 +75,10 @@ namespace StarSim
Console.WriteLine($"=========== Generation {_generation} ===========");
for (int i = 0; i < _bodies.Length; i++)
{
- float mass = (float)(RNG.NextDouble() * Constants.SolarMass);
+ float mass = (float)(Rng.NextDouble() * Constants.SolarMass);
Vector3d position = OrbitGenerator.RandomPosition();
- Vector3d velocity = OrbitGenerator.RandomOrbit(position); //new Vector3d();
+ Vector3d velocity = OrbitGenerator.RandomOrbit(position);
_bodies[i] = new Body(position, velocity, mass, _generation, id);
BodyShapeMap.Add(_bodies[i], new CircleShape(1) { FillColor = Color.White });
@@ -139,7 +144,7 @@ namespace StarSim
window.Clear();
window.DispatchEvents();
- UpdateBodiesBruteForce(_bodies, Constants.TimeStep);
+ bodyPositionUpdater(_bodies, Constants.TimeStep);
DrawBodies(_bodies, window, originOffset);
window.Display();
diff --git a/StarSim/StarSimLib/Graphics/Drawer.cs b/StarSim/StarSimLib/Graphics/Drawer.cs
@@ -0,0 +1,9 @@
+namespace StarSimLib.Graphics
+{
+ /// <summary>
+ /// Represents a drawer capable of rendering bodies to a screen.
+ /// </summary>
+ public class Drawer
+ {
+ }
+}
+\ No newline at end of file
diff --git a/StarSim/StarSimLib/Physics/Body.cs b/StarSim/StarSimLib/Physics/Body.cs
@@ -11,7 +11,7 @@ namespace StarSimLib.Physics
/// Describes how the <see cref="Body"/> instance should be formatted as a <see cref="string"/>.
/// </summary>
private const string BodyFormatString =
- "Body {0,2}.{1,-4}: Pos-({2:D,-3}, {3:D,-3}, {4:D,-3}), Vel-({5:D,-3}, {6:D,-3}, {7:D,-3}), Mass-{8:D,3}";
+ "Body {0,2}.{1,-4}: Pos-{2}, Vel-{3} Mass-{4,3}";
/// <summary>
/// The generation that this body belongs to.
@@ -24,6 +24,26 @@ namespace StarSimLib.Physics
public readonly uint Id;
/// <summary>
+ /// The backing field for the <see cref="Force"/> property.
+ /// </summary>
+ public Vector3d force;
+
+ /// <summary>
+ /// Backing field for the <see cref="Mass"/> property.
+ /// </summary>
+ public double mass;
+
+ /// <summary>
+ /// Backing field for the <see cref="Position"/> property.
+ /// </summary>
+ public Vector3d position;
+
+ /// <summary>
+ /// Backing field for the <see cref="Velocity"/> property.
+ /// </summary>
+ public Vector3d velocity;
+
+ /// <summary>
/// Initialises a new instance of the <see cref="Body"/> class.
/// </summary>
/// <param name="position">The starting position of the <see cref="Body"/>.</param>
@@ -36,25 +56,44 @@ namespace StarSimLib.Physics
Generation = generation;
Id = id;
- Position = position;
- Velocity = velocity;
- Mass = mass;
+ this.position = position;
+ this.velocity = velocity;
+ this.mass = mass;
+
+ force = new Vector3d();
+ }
+
+ /// <summary>
+ /// The current force on the <see cref="Body"/> in 3D space.
+ /// </summary>
+ public Vector3d Force
+ {
+ get { return force; }
}
/// <summary>
/// The mass of the <see cref="Body"/>.
/// </summary>
- public double Mass { get; private set; }
+ public double Mass
+ {
+ get { return mass; }
+ }
/// <summary>
/// The current position of the <see cref="Body"/> in 3D space.
/// </summary>
- public Vector3d Position { get; private set; }
+ public Vector3d Position
+ {
+ get { return position; }
+ }
/// <summary>
/// The current velocity of the <see cref="Body"/> in 3D space.
/// </summary>
- public Vector3d Velocity { get; private set; }
+ public Vector3d Velocity
+ {
+ get { return velocity; }
+ }
/// <summary>
/// Gets the force vector for the attraction between <see cref="Body"/> A and <see cref="Body"/> B.
@@ -85,6 +124,16 @@ namespace StarSimLib.Physics
}
/// <summary>
+ /// Calculates the force between this instance and the given other body, and adds the resultant vector to the
+ /// internal total force vector.
+ /// </summary>
+ /// <param name="otherBody">The other body to calculate the force between.</param>
+ public void AddForce(Body otherBody)
+ {
+ force += GetForceBetween(this, otherBody);
+ }
+
+ /// <summary>
/// Finds and returns the distance between the current <see cref="Body"/> instance and the given <see cref="Body"/>.
/// In other words, it finds the magnitude of the translation vector between the two <see cref="Body"/> instances.
/// </summary>
@@ -103,14 +152,34 @@ namespace StarSimLib.Physics
}
/// <summary>
+ /// Resets the internal force vector.
+ /// </summary>
+ public void ResetForce()
+ {
+ force.X = 0;
+ force.Y = 0;
+ force.Z = 0;
+ }
+
+ /// <summary>
+ /// Updates the <see cref="Position"/> of the current body using the internal force vector and the given time step.
+ /// </summary>
+ /// <param name="deltaTime">The time step.</param>
+ public void Update(double deltaTime)
+ {
+ velocity += deltaTime * force / mass;
+ position += deltaTime * velocity;
+ }
+
+ /// <summary>
/// Updates the <see cref="Position"/> of the current body using the given force vector and time step.
/// </summary>
/// <param name="forceVector">The sum vector of all forces due to other <see cref="Body"/>s.</param>
/// <param name="deltaTime">The time step.</param>
public void Update(Vector3d forceVector, double deltaTime)
{
- Velocity += deltaTime * forceVector / Mass;
- Position += deltaTime * Velocity;
+ velocity += deltaTime * forceVector / mass;
+ position += deltaTime * velocity;
}
#region Overrides of Object
@@ -118,7 +187,7 @@ namespace StarSimLib.Physics
/// <inheritdoc />
public override string ToString()
{
- return string.Format(BodyFormatString, Generation, Id, Position.X, Position.Y, Position.Z, Velocity.X, Velocity.Y, Velocity.Z, Mass);
+ return string.Format(BodyFormatString, Generation, Id, Position, Velocity, Mass);
}
#endregion Overrides of Object
diff --git a/StarSim/StarSimLib/StarSimLib.csproj b/StarSim/StarSimLib/StarSimLib.csproj
@@ -27,6 +27,5 @@
<ItemGroup>
<Folder Include="Models\" />
- <Folder Include="Graphics\" />
</ItemGroup>
</Project>
\ No newline at end of file
diff --git a/StarSim/StarSimLib/StarSimLib.xml b/StarSim/StarSimLib/StarSimLib.xml
@@ -36,7 +36,7 @@
</member>
<member name="F:StarSimLib.Constants.SofteningFactor">
<summary>
- Softens the force between <see cref="T:StarSimLib.Body"/>s to avoid infinities.
+ Softens the force between <see cref="T:StarSimLib.Physics.Body"/>s to avoid infinities.
</summary>
</member>
<member name="F:StarSimLib.Constants.SofteningFactor2">
@@ -56,12 +56,12 @@
</member>
<member name="F:StarSimLib.Constants.UniverseScalingFactor">
<summary>
- Factor by which to multiply the position of <see cref="T:StarSimLib.Body"/>s to scale them to the screen for displaying.
+ Factor by which to multiply the position of <see cref="T:StarSimLib.Physics.Body"/>s to scale them to the screen for displaying.
</summary>
</member>
<member name="F:StarSimLib.Constants.UniverseSize">
<summary>
- The maximum radius within which <see cref="T:StarSimLib.Body"/>s will be placed (1e18f).
+ The maximum radius within which <see cref="T:StarSimLib.Physics.Body"/>s will be placed (1e18f).
</summary>
</member>
<member name="T:StarSimLib.Contexts.SimulatorContext">
@@ -103,101 +103,156 @@
<param name="vector">The vector whose magnitude to return.</param>
<returns>The magnitude of the given <see cref="T:SFML.System.Vector3f"/>.</returns>
</member>
- <member name="T:StarSimLib.Body">
+ <member name="T:StarSimLib.Graphics.Drawer">
+ <summary>
+ Represents a drawer capable of rendering bodies to a screen.
+ </summary>
+ </member>
+ <member name="T:StarSimLib.Physics.Body">
<summary>
Represents a stellar body.
</summary>
</member>
- <member name="F:StarSimLib.Body.BodyFormatString">
+ <member name="F:StarSimLib.Physics.Body.BodyFormatString">
<summary>
- Describes how the <see cref="T:StarSimLib.Body"/> instance should be formatted as a <see cref="T:System.String"/>.
+ Describes how the <see cref="T:StarSimLib.Physics.Body"/> instance should be formatted as a <see cref="T:System.String"/>.
</summary>
</member>
- <member name="F:StarSimLib.Body.Generation">
+ <member name="F:StarSimLib.Physics.Body.Generation">
<summary>
The generation that this body belongs to.
</summary>
</member>
- <member name="F:StarSimLib.Body.Id">
+ <member name="F:StarSimLib.Physics.Body.Id">
<summary>
The unique id for this body.
</summary>
</member>
- <member name="M:StarSimLib.Body.#ctor(StarSimLib.Vector3d,StarSimLib.Vector3d,System.Double,System.UInt32,System.UInt32)">
+ <member name="F:StarSimLib.Physics.Body.force">
+ <summary>
+ The backing field for the <see cref="P:StarSimLib.Physics.Body.Force"/> property.
+ </summary>
+ </member>
+ <member name="F:StarSimLib.Physics.Body.mass">
+ <summary>
+ Backing field for the <see cref="P:StarSimLib.Physics.Body.Mass"/> property.
+ </summary>
+ </member>
+ <member name="F:StarSimLib.Physics.Body.position">
<summary>
- Initialises a new instance of the <see cref="T:StarSimLib.Body"/> class.
+ Backing field for the <see cref="P:StarSimLib.Physics.Body.Position"/> property.
</summary>
- <param name="position">The starting position of the <see cref="T:StarSimLib.Body"/>.</param>
- <param name="velocity">The starting velocity of the <see cref="T:StarSimLib.Body"/>.</param>
- <param name="mass">The starting mass of the <see cref="T:StarSimLib.Body"/>.</param>
- <param name="generation">The generation that this <see cref="T:StarSimLib.Body"/> belongs to.</param>
+ </member>
+ <member name="F:StarSimLib.Physics.Body.velocity">
+ <summary>
+ Backing field for the <see cref="P:StarSimLib.Physics.Body.Velocity"/> property.
+ </summary>
+ </member>
+ <member name="M:StarSimLib.Physics.Body.#ctor(StarSimLib.Vector3d,StarSimLib.Vector3d,System.Double,System.UInt32,System.UInt32)">
+ <summary>
+ Initialises a new instance of the <see cref="T:StarSimLib.Physics.Body"/> class.
+ </summary>
+ <param name="position">The starting position of the <see cref="T:StarSimLib.Physics.Body"/>.</param>
+ <param name="velocity">The starting velocity of the <see cref="T:StarSimLib.Physics.Body"/>.</param>
+ <param name="mass">The starting mass of the <see cref="T:StarSimLib.Physics.Body"/>.</param>
+ <param name="generation">The generation that this <see cref="T:StarSimLib.Physics.Body"/> belongs to.</param>
<param name="id">The unique id for this body.</param>
</member>
- <member name="P:StarSimLib.Body.Mass">
+ <member name="P:StarSimLib.Physics.Body.Force">
+ <summary>
+ The current force on the <see cref="T:StarSimLib.Physics.Body"/> in 3D space.
+ </summary>
+ </member>
+ <member name="P:StarSimLib.Physics.Body.Mass">
<summary>
- The mass of the <see cref="T:StarSimLib.Body"/>.
+ The mass of the <see cref="T:StarSimLib.Physics.Body"/>.
</summary>
</member>
- <member name="P:StarSimLib.Body.Position">
+ <member name="P:StarSimLib.Physics.Body.Position">
<summary>
- The current position of the <see cref="T:StarSimLib.Body"/> in 3D space.
+ The current position of the <see cref="T:StarSimLib.Physics.Body"/> in 3D space.
</summary>
</member>
- <member name="P:StarSimLib.Body.Velocity">
+ <member name="P:StarSimLib.Physics.Body.Velocity">
<summary>
- The current velocity of the <see cref="T:StarSimLib.Body"/> in 3D space.
+ The current velocity of the <see cref="T:StarSimLib.Physics.Body"/> in 3D space.
</summary>
</member>
- <member name="M:StarSimLib.Body.GetForceBetween(StarSimLib.Body,StarSimLib.Body)">
+ <member name="M:StarSimLib.Physics.Body.GetForceBetween(StarSimLib.Physics.Body,StarSimLib.Physics.Body)">
<summary>
- Gets the force vector for the attraction between <see cref="T:StarSimLib.Body"/> A and <see cref="T:StarSimLib.Body"/> B.
+ Gets the force vector for the attraction between <see cref="T:StarSimLib.Physics.Body"/> A and <see cref="T:StarSimLib.Physics.Body"/> B.
</summary>
- <param name="a">The first <see cref="T:StarSimLib.Body"/> instance.</param>
- <param name="b">The second <see cref="T:StarSimLib.Body"/> instance.</param>
+ <param name="a">The first <see cref="T:StarSimLib.Physics.Body"/> instance.</param>
+ <param name="b">The second <see cref="T:StarSimLib.Physics.Body"/> instance.</param>
<returns></returns>
</member>
- <member name="M:StarSimLib.Body.DistanceTo(StarSimLib.Body,StarSimLib.Vector3d@)">
+ <member name="M:StarSimLib.Physics.Body.AddForce(StarSimLib.Physics.Body)">
<summary>
- Finds and returns the distance between the current <see cref="T:StarSimLib.Body"/> instance and the given <see cref="T:StarSimLib.Body"/>.
- In other words, it finds the magnitude of the translation vector between the two <see cref="T:StarSimLib.Body"/> instances.
+ Calculates the force between this instance and the given other body, and adds the resultant vector to the
+ internal total force vector.
</summary>
- <param name="body">The other <see cref="T:StarSimLib.Body"/> instance to which to calculate the distance.</param>
+ <param name="otherBody">The other body to calculate the force between.</param>
+ </member>
+ <member name="M:StarSimLib.Physics.Body.DistanceTo(StarSimLib.Physics.Body,StarSimLib.Vector3d@)">
+ <summary>
+ Finds and returns the distance between the current <see cref="T:StarSimLib.Physics.Body"/> instance and the given <see cref="T:StarSimLib.Physics.Body"/>.
+ In other words, it finds the magnitude of the translation vector between the two <see cref="T:StarSimLib.Physics.Body"/> instances.
+ </summary>
+ <param name="body">The other <see cref="T:StarSimLib.Physics.Body"/> instance to which to calculate the distance.</param>
<param name="displacement">The vector showing the displacement of the current body from the given one.</param>
- <returns>The distance to the other <see cref="T:StarSimLib.Body"/> as a <see cref="T:System.Single"/>.</returns>
+ <returns>The distance to the other <see cref="T:StarSimLib.Physics.Body"/> as a <see cref="T:System.Single"/>.</returns>
</member>
- <member name="M:StarSimLib.Body.Update(StarSimLib.Vector3d,System.Double)">
+ <member name="M:StarSimLib.Physics.Body.ResetForce">
<summary>
- Updates the <see cref="P:StarSimLib.Body.Position"/> of the current body using the given force vector and time step.
+ Resets the internal force vector.
+ </summary>
+ </member>
+ <member name="M:StarSimLib.Physics.Body.Update(System.Double)">
+ <summary>
+ Updates the <see cref="P:StarSimLib.Physics.Body.Position"/> of the current body using the internal force vector and the given time step.
</summary>
- <param name="forceVector">The sum vector of all forces due to other <see cref="T:StarSimLib.Body"/>s.</param>
<param name="deltaTime">The time step.</param>
</member>
- <member name="M:StarSimLib.Body.ToString">
+ <member name="M:StarSimLib.Physics.Body.Update(StarSimLib.Vector3d,System.Double)">
+ <summary>
+ Updates the <see cref="P:StarSimLib.Physics.Body.Position"/> of the current body using the given force vector and time step.
+ </summary>
+ <param name="forceVector">The sum vector of all forces due to other <see cref="T:StarSimLib.Physics.Body"/>s.</param>
+ <param name="deltaTime">The time step.</param>
+ </member>
+ <member name="M:StarSimLib.Physics.Body.ToString">
<inheritdoc />
</member>
- <member name="T:StarSimLib.OrbitGenerator">
+ <member name="T:StarSimLib.Physics.OrbitGenerator">
<summary>
Initialises positions, velocities, and orbits for bodies.
</summary>
</member>
- <member name="F:StarSimLib.OrbitGenerator.Rng">
+ <member name="F:StarSimLib.Physics.OrbitGenerator.Rng">
<summary>
Random number generator.
</summary>
</member>
- <member name="M:StarSimLib.OrbitGenerator.RandomOrbit(StarSimLib.Vector3d)">
+ <member name="M:StarSimLib.Physics.OrbitGenerator.RandomOrbit(StarSimLib.Vector3d)">
<summary>
Returns a randomised orbit around a central, heavy mass.
</summary>
<param name="positionVector">The position vector for the body for which to generate the orbit.</param>
<returns>The magnitude of the velocity of the orbit.</returns>
</member>
- <member name="M:StarSimLib.OrbitGenerator.RandomPosition">
+ <member name="M:StarSimLib.Physics.OrbitGenerator.RandomPosition">
<summary>
Returns a random position within the universe sphere (see <see cref="F:StarSimLib.Constants.UniverseSize"/>).
</summary>
<returns>A 3D position vector.</returns>
</member>
+ <member name="T:StarSimLib.UpdateDelegate">
+ <summary>
+ Updates the given <see cref="T:StarSimLib.Physics.Body"/> collections individual bodies, using the given delta time.
+ </summary>
+ <param name="bodies">The body collection.</param>
+ <param name="deltaTime">The time step.</param>
+ </member>
<member name="T:StarSimLib.Vector3d">
<summary>
A 3D vector with <see cref="T:System.Double"/> components.
@@ -440,5 +495,8 @@
The positive unit vector for the Y axis.
</summary>
</member>
+ <member name="M:StarSimLib.Vector3d.ToString">
+ <inheritdoc />
+ </member>
</members>
</doc>
diff --git a/StarSim/StarSimLib/UpdateDelegate.cs b/StarSim/StarSimLib/UpdateDelegate.cs
@@ -0,0 +1,12 @@
+using System.Collections.Generic;
+using StarSimLib.Physics;
+
+namespace StarSimLib
+{
+ /// <summary>
+ /// Updates the given <see cref="Body"/> collections individual bodies, using the given delta time.
+ /// </summary>
+ /// <param name="bodies">The body collection.</param>
+ /// <param name="deltaTime">The time step.</param>
+ public delegate void UpdateDelegate(IEnumerable<Body> bodies, double deltaTime);
+}
+\ No newline at end of file
diff --git a/StarSim/StarSimLib/Vector3d.cs b/StarSim/StarSimLib/Vector3d.cs
@@ -361,5 +361,15 @@ namespace StarSimLib
/// </summary>
public static readonly Vector3d Up = new Vector3d(0, 1, 0);
}
+
+ #region Overrides of ValueType
+
+ /// <inheritdoc />
+ public override string ToString()
+ {
+ return $"({X}, {Y}, {Z}";
+ }
+
+ #endregion Overrides of ValueType
}
}
\ No newline at end of file