StarSim

StarSim.git
git clone git://git.lenczewski.org/StarSim.git
Log | Files | Refs | README | LICENSE

commit ed5a51381eb68fd2c24325037442a75f3d1238cf
parent dfa491bea05931c905973f3a5590146c6d324600
Author: Mikolaj Lenczewski <33129490+EnderRifter@users.noreply.github.com>
Date:   Mon,  8 Jul 2019 13:36:53 +0100

0 errors, 0 warning, 0 messages.

Diffstat:
MStarSim/StarSim/Program.cs | 23+++++++----------------
MStarSim/StarSimLib/Constants.cs | 7++++++-
MStarSim/StarSimLib/Data Structures/Octant.cs | 15+++++++++++++++
MStarSim/StarSimLib/Data Structures/OctantTree.cs | 4++++
MStarSim/StarSimLib/Data Structures/OrbitTracer.cs | 8+-------
MStarSim/StarSimLib/Graphics/Drawer.cs | 9++++++++-
MStarSim/StarSimLib/Physics/BodyUpdater.cs | 25++++++++++++-------------
MStarSim/StarSimLib/Physics/OrbitGenerator.cs | 6------
MStarSim/StarSimLib/StarSimLib.xml | 24++++++++++++++++++------
9 files changed, 71 insertions(+), 50 deletions(-)

diff --git a/StarSim/StarSim/Program.cs b/StarSim/StarSim/Program.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Threading; using SFML.Graphics; using SFML.Window; using StarSimLib; @@ -32,14 +31,15 @@ namespace StarSim private static readonly UpdateDelegate bodyPositionUpdater; /// <summary> - /// The input handler to use to provide interactivity to the simulator. + /// Maps a <see cref="Body"/> to the <see cref="CircleShape"/> that represents it, and is drawn to the + /// screen at the <see cref="Body"/>s position. /// </summary> - private static readonly InputHandler inputHandler; + private static readonly Dictionary<Body, CircleShape> bodyShapeMap; /// <summary> - /// Caches a random number generator to use for all randomised positions and velocities. + /// The input handler to use to provide interactivity to the simulator. /// </summary> - private static readonly Random Rng; + private static readonly InputHandler inputHandler; /// <summary> /// The SFML.NET window to which everything is rendered. @@ -47,12 +47,6 @@ namespace StarSim private static readonly RenderWindow window; /// <summary> - /// Maps a <see cref="Body"/> to the <see cref="CircleShape"/> that represents it, and is drawn to the - /// screen at the <see cref="Body"/>s position. - /// </summary> - private static Dictionary<Body, CircleShape> bodyShapeMap; - - /// <summary> /// Initialises a new instance of the <see cref="Program"/> class, /// </summary> static Program() @@ -65,22 +59,19 @@ namespace StarSim bodyShapeMap = BodyGenerator.GenerateShapes(bodies); #if DEBUG - //bodyPositionUpdater = BodyUpdater.UpdateBodiesBruteForce; - bodyPositionUpdater = BodyUpdater.UpdateBodiesBarnesHut; + bodyPositionUpdater = BodyUpdater.UpdateBodiesBruteForce; #else bodyPositionUpdater = BodyUpdater.UpdateBodiesBarnesHut; #endif bodyDrawer = new Drawer(window, ref bodies, ref bodyShapeMap); inputHandler = new InputHandler(ref bodies, ref bodyDrawer); - Rng = new Random(); } /// <summary> /// Entry point for our application. /// </summary> - /// <param name="args">Any command line arguments passed to the program.</param> - private static void Main(string[] args) + private static void Main() { Console.WriteLine("Hello, World!"); Console.WriteLine("Press 'enter' to continue..."); diff --git a/StarSim/StarSimLib/Constants.cs b/StarSim/StarSimLib/Constants.cs @@ -13,7 +13,7 @@ namespace StarSimLib /// <summary> /// The amount of bodies that are rendered by default. /// </summary> - public const int BodyCount = 100; + public const int BodyCount = 1_000; /// <summary> /// The mass of the central body, if it is included. @@ -93,6 +93,11 @@ namespace StarSimLib public const double ZoomStep = 0.05; /// <summary> + /// The <see cref="Octant"/> instance representing the rendered universe. + /// </summary> + public static readonly Octant UniverseOctant = new Octant(new Vector4(), UniverseSize); + + /// <summary> /// Converts an enumerable to its string form. /// </summary> /// <typeparam name="T">The type of item held in the enumerable.</typeparam> diff --git a/StarSim/StarSimLib/Data Structures/Octant.cs b/StarSim/StarSimLib/Data Structures/Octant.cs @@ -94,6 +94,21 @@ namespace StarSimLib.Data_Structures } /// <summary> + /// Initialises a new instance of the <see cref="Octant"/> class. + /// </summary> + /// <param name="octant">The previous instance to make a copy of.</param> + public Octant(Octant octant) + { + midpoint = octant.Midpoint; + sideLength = octant.sideLength; + halfSideLength = octant.halfSideLength; + quarterSideLength = octant.quarterSideLength; + + childOctants = new Octant[8]; + octant.childOctants.CopyTo(childOctants, 0); + } + + /// <summary> /// The length of 1 complete side of this instance. /// </summary> public double Length diff --git a/StarSim/StarSimLib/Data Structures/OctantTree.cs b/StarSim/StarSimLib/Data Structures/OctantTree.cs @@ -64,6 +64,10 @@ namespace StarSimLib.Data_Structures get { return SubTree(specifier); } } + /// <summary> + /// Adds the given body to one of the child trees of this instance. + /// </summary> + /// <param name="newBody">The body to add.</param> private void AddToChildTree(Body newBody) { // don't create subtrees if it violates the minimum width limit, to prevent infinitely deep trees and thus diff --git a/StarSim/StarSimLib/Data Structures/OrbitTracer.cs b/StarSim/StarSimLib/Data Structures/OrbitTracer.cs @@ -20,12 +20,6 @@ namespace StarSimLib.Data_Structures private readonly Queue<Vector4> previousPositions; /// <summary> - /// Due to the <see cref="Queue{T}"/>s inability to fetch the second-to-last element, we must explicitly - /// cache it in order to use it in interpolation calculations later. - /// </summary> - private Vector4 dequeuedPosition; - - /// <summary> /// Counts the number of sampling opportunities that have gone by since the last position sample. Resets once /// it reaches the value of <see cref="PositionSampleRate"/>. /// </summary> @@ -82,7 +76,7 @@ namespace StarSimLib.Data_Structures if (previousPositions.Count >= Constants.StoredPreviousPositionCount) { - dequeuedPosition = previousPositions.Dequeue(); + previousPositions.Dequeue(); } // reset the counter diff --git a/StarSim/StarSimLib/Graphics/Drawer.cs b/StarSim/StarSimLib/Graphics/Drawer.cs @@ -8,6 +8,7 @@ namespace StarSimLib.Graphics { /// <summary> /// Enumerates all possible rotation directions (i.e. north, east, south, west, clockwise, anticlockwise). + /// Here, camera forward is north. /// </summary> public enum RotationDirection { @@ -56,7 +57,7 @@ namespace StarSimLib.Graphics /// The furthest distance that can be seen on the <see cref="RenderTarget"/>. Any bodies that are further /// from the camera than this distance are culled and not rendered. /// </summary> - private const double FarDistance = 1 * Constants.UniverseSize; + private const double FarDistance = Constants.UniverseSize; /// <summary> /// The default field of view for the drawer in degrees. @@ -114,6 +115,8 @@ namespace StarSimLib.Graphics /// </summary> private double currentFieldOfView = FieldOfView; +#pragma warning disable IDE0044 // Set field to readonly + /// <summary> /// Projection matrix used for mapping from 3D to 2D. /// </summary> @@ -139,6 +142,8 @@ namespace StarSimLib.Graphics /// </summary> private Matrix4x4 zRotationMatrix; +#pragma warning restore IDE0044 + /// <summary> /// Initialises a new instance of the <see cref="Drawer"/> class. /// </summary> @@ -341,6 +346,8 @@ namespace StarSimLib.Graphics { Vector4[] orbitTracerPositions = body.OrbitTracer.PreviousPositions.ToArray(); + /* use linear interpolation to make the tail look smooth */ + for (uint i = 0; i < orbitTracerPositions.Length; i++) { Vector4 previousPosition = orbitTracerPositions[i]; diff --git a/StarSim/StarSimLib/Physics/BodyUpdater.cs b/StarSim/StarSimLib/Physics/BodyUpdater.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Threading.Tasks; using StarSimLib.Data_Structures; namespace StarSimLib.Physics @@ -27,7 +28,7 @@ namespace StarSimLib.Physics IEnumerable<Body> bodyEnumerable = bodies as Body[] ?? bodies.ToArray(); // root octant represents the main universe. Anything outside this octant of space is not updated - Octant universeOctant = new Octant(new Vector4(), Constants.UniverseSize * 1.1); + Octant universeOctant = new Octant(Constants.UniverseOctant); OctantTree barnesHutTree = new OctantTree(universeOctant); @@ -41,23 +42,21 @@ namespace StarSimLib.Physics } } - // we update the positions of the bodies in the populated tree - foreach (Body body in bodyEnumerable) + // we update the positions of the bodies in the populated tree in a parallel manner, using the thread pool + Parallel.ForEach(bodyEnumerable, body => { - if (body == null) + if (body != null) { - continue; - } - - body.ResetForce(); + body.ResetForce(); - if (body.IsInOctant(universeOctant)) - { - barnesHutTree.UpdateForces(body); + if (body.IsInOctant(universeOctant)) + { + barnesHutTree.UpdateForces(body); - body.Update(deltaTime); + body.Update(deltaTime); + } } - } + }); } /// <summary> diff --git a/StarSim/StarSimLib/Physics/OrbitGenerator.cs b/StarSim/StarSimLib/Physics/OrbitGenerator.cs @@ -29,7 +29,6 @@ namespace StarSimLib.Physics double absAngle = Math.Atan(Math.Abs(position.X / position.Y)); double velocityTheta = Math.PI / 2 - absAngle; - double velocityPhi = Rng.NextDouble() * Math.PI; double vertical = Math.Min(2e8 / distanceToCentralBody, 2e4); @@ -37,11 +36,6 @@ namespace StarSimLib.Physics double vy = (Rng.NextDouble() - 0.5) * vertical; double vz = Math.Sign(position.X) * Math.Sin(velocityTheta) * velocityMagnitude; - // TODO: Implement 3d velocities and rendering - // mapping of the 3d values to 2d values - vy = vz; - vz = 0; - // Randomly orient the orbit return !(Rng.NextDouble() <= 0.5f) ? new Vector4(vx, vy, vz) : new Vector4(-vx, -vy, -vz); } diff --git a/StarSim/StarSimLib/StarSimLib.xml b/StarSim/StarSimLib/StarSimLib.xml @@ -91,6 +91,11 @@ The amount by which the zoom level will be increased or decreased. </summary> </member> + <member name="F:StarSimLib.Constants.UniverseOctant"> + <summary> + The <see cref="T:StarSimLib.Data_Structures.Octant"/> instance representing the rendered universe. + </summary> + </member> <member name="M:StarSimLib.Constants.ConvertEnumerableToString``1(System.Collections.Generic.IEnumerable{``0},System.Func{``0,System.String})"> <summary> Converts an enumerable to its string form. @@ -497,6 +502,12 @@ <param name="midpoint">The central midpoint of this instance.</param> <param name="length">The length of 1 complete side of this instance.</param> </member> + <member name="M:StarSimLib.Data_Structures.Octant.#ctor(StarSimLib.Data_Structures.Octant)"> + <summary> + Initialises a new instance of the <see cref="T:StarSimLib.Data_Structures.Octant"/> class. + </summary> + <param name="octant">The previous instance to make a copy of.</param> + </member> <member name="P:StarSimLib.Data_Structures.Octant.Length"> <summary> The length of 1 complete side of this instance. @@ -616,6 +627,12 @@ <param name="specifier">Which child octant tree instance to return.</param> <returns>The specified child octant tree instance.</returns> </member> + <member name="M:StarSimLib.Data_Structures.OctantTree.AddToChildTree(StarSimLib.Data_Structures.Body)"> + <summary> + Adds the given body to one of the child trees of this instance. + </summary> + <param name="newBody">The body to add.</param> + </member> <member name="M:StarSimLib.Data_Structures.OctantTree.GetOrSetSubTree(StarSimLib.Data_Structures.OctantTree[]@,System.Int32,StarSimLib.Data_Structures.Octant)"> <summary> Returns the specified child octant tree instance, or instantiates a new octant tree if the specified child @@ -689,12 +706,6 @@ Backing field for the <see cref="P:StarSimLib.Data_Structures.OrbitTracer.PreviousPositions"/> property. </summary> </member> - <member name="F:StarSimLib.Data_Structures.OrbitTracer.dequeuedPosition"> - <summary> - Due to the <see cref="T:System.Collections.Generic.Queue`1"/>s inability to fetch the second-to-last element, we must explicitly - cache it in order to use it in interpolation calculations later. - </summary> - </member> <member name="F:StarSimLib.Data_Structures.OrbitTracer.positionSampleCounter"> <summary> Counts the number of sampling opportunities that have gone by since the last position sample. Resets once @@ -1010,6 +1021,7 @@ <member name="T:StarSimLib.Graphics.RotationDirection"> <summary> Enumerates all possible rotation directions (i.e. north, east, south, west, clockwise, anticlockwise). + Here, camera forward is north. </summary> </member> <member name="F:StarSimLib.Graphics.RotationDirection.North">