StarSim

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

commit b1cf594f1f6ee0b9b8e285958926196627112457
parent f3e0c88a2ee11d5ac959969ed5bc2868516912d3
Author: Mikolaj Lenczewski <33129490+EnderRifter@users.noreply.github.com>
Date:   Mon,  1 Jul 2019 18:14:46 +0100

Finished new rendering pipeline, as well as basic rotation and zoom features. Started to refactor user interface interaction into separate class.

Diffstat:
MStarSim/StarSim/Program.cs | 24+++++++++++++++---------
MStarSim/StarSimLib/Constants.cs | 7++++++-
MStarSim/StarSimLib/Graphics/Drawer.cs | 29+++++++++++++++++++++++++++--
MStarSim/StarSimLib/Physics/Body.cs | 14++++++++++++++
MStarSim/StarSimLib/StarSimLib.xml | 25+++++++++++++++++++++++++
AStarSim/StarSimLib/UI/InputHandler.cs | 16++++++++++++++++
6 files changed, 103 insertions(+), 12 deletions(-)

diff --git a/StarSim/StarSim/Program.cs b/StarSim/StarSim/Program.cs @@ -6,6 +6,7 @@ using SFML.Window; using StarSimLib; using StarSimLib.Graphics; using StarSimLib.Physics; +using StarSimLib.UI; namespace StarSim { @@ -30,6 +31,11 @@ namespace StarSim private static readonly UpdateDelegate bodyPositionUpdater; /// <summary> + /// The input handler to use to provide interactivity to the simulator. + /// </summary> + private static readonly InputHandler inputHandler; + + /// <summary> /// Caches a random number generator to use for all randomised positions and velocities. /// </summary> private static readonly Random Rng; @@ -54,9 +60,6 @@ namespace StarSim window = new RenderWindow(VideoMode.DesktopMode, "N-Body Simulator", Styles.Default, new ContextSettings()); window.SetVisible(false); - View windowView = new View(new Vector2f(window.Size.X / 2f, window.Size.Y / 2f), new Vector2f(window.Size.X, window.Size.Y)); - window.SetView(windowView); - bodies = BodyGenerator.GenerateBodies(Constants.BodyCount, true); bodyShapeMap = BodyGenerator.GenerateShapes(bodies); #if DEBUG @@ -65,6 +68,7 @@ namespace StarSim bodyPositionUpdater = BodyUpdater.UpdateBodiesBruteForce; #endif bodyDrawer = new Drawer(window, ref bodies, ref bodyShapeMap); + inputHandler = new InputHandler(); Rng = new Random(); } @@ -92,42 +96,42 @@ namespace StarSim case Keyboard.Key.W: // rotate the view north bodyDrawer.Rotate(RotationDirection.North, Constants.EulerRotationStep); - msg = $"Rotated by {Constants.EulerRotationStep} anticlockwise in the x axis. " + + msg = $"Rotated by {Constants.EulerRotationStep} degrees anticlockwise in the x axis. " + $"View Rotation: ({bodyDrawer.XAngle},{bodyDrawer.YAngle},{bodyDrawer.ZAngle})"; break; case Keyboard.Key.A: // rotate the view west bodyDrawer.Rotate(RotationDirection.West, Constants.EulerRotationStep); - msg = $"Rotated by {Constants.EulerRotationStep} clockwise in the y axis. " + + msg = $"Rotated by {Constants.EulerRotationStep} degrees clockwise in the y axis. " + $"View Rotation: ({bodyDrawer.XAngle},{bodyDrawer.YAngle},{bodyDrawer.ZAngle})"; break; case Keyboard.Key.S: // rotate the view south bodyDrawer.Rotate(RotationDirection.South, Constants.EulerRotationStep); - msg = $"Rotated by {Constants.EulerRotationStep} clockwise in the x axis. " + + msg = $"Rotated by {Constants.EulerRotationStep} degrees clockwise in the x axis. " + $"View Rotation: ({bodyDrawer.XAngle},{bodyDrawer.YAngle},{bodyDrawer.ZAngle})"; break; case Keyboard.Key.D: // rotate the view east bodyDrawer.Rotate(RotationDirection.East, Constants.EulerRotationStep); - msg = $"Rotated by {Constants.EulerRotationStep} anticlockwise in the y axis. " + + msg = $"Rotated by {Constants.EulerRotationStep} degrees anticlockwise in the y axis. " + $"View Rotation: ({bodyDrawer.XAngle},{bodyDrawer.YAngle},{bodyDrawer.ZAngle})"; break; case Keyboard.Key.Q: // rotate the view anti-clockwise bodyDrawer.Rotate(RotationDirection.Anticlockwise, Constants.EulerRotationStep); - msg = $"Rotated by {Constants.EulerRotationStep} anticlockwise in the z axis. " + + msg = $"Rotated by {Constants.EulerRotationStep} degrees anticlockwise in the z axis. " + $"View Rotation: ({bodyDrawer.XAngle},{bodyDrawer.YAngle},{bodyDrawer.ZAngle})"; break; case Keyboard.Key.E: // rotate the view clockwise bodyDrawer.Rotate(RotationDirection.Clockwise, Constants.EulerRotationStep); - msg = $"Rotated by {Constants.EulerRotationStep} anticlockwise in the z axis. " + + msg = $"Rotated by {Constants.EulerRotationStep} degrees anticlockwise in the z axis. " + $"View Rotation: ({bodyDrawer.XAngle},{bodyDrawer.YAngle},{bodyDrawer.ZAngle})"; break; @@ -254,6 +258,8 @@ namespace StarSim { bodyDrawer.Scale(1 - Constants.ZoomStep); } + + Console.WriteLine($"Current field of view (zoom level): {bodyDrawer.FOV} ({bodyDrawer.ZoomLevel})"); } /// <summary> diff --git a/StarSim/StarSimLib/Constants.cs b/StarSim/StarSimLib/Constants.cs @@ -20,7 +20,7 @@ namespace StarSimLib /// <summary> /// The amount of degrees by which the view will be rotated in any given direction. /// </summary> - public const double EulerRotationStep = 5; + public const double EulerRotationStep = 1; /// <summary> /// The frame rate limit for the program. @@ -58,6 +58,11 @@ namespace StarSimLib public const double SolarMass = 1.98892e30f; /// <summary> + /// The number of previous positions that will be stored by a body + /// </summary> + public const int StoredPreviousPositionCount = 50; + + /// <summary> /// The time step for the simulation. /// </summary> public const double TimeStep = SecondsPerTick * (SimulationRate / (float)FrameRate) * SimulationRate; diff --git a/StarSim/StarSimLib/Graphics/Drawer.cs b/StarSim/StarSimLib/Graphics/Drawer.cs @@ -51,7 +51,7 @@ namespace StarSimLib.Graphics /// <summary> /// Conversion from degrees to radians, as used by the <see cref="Math"/> functions. /// </summary> - private const double DegToRad = 1d / 180 * Math.PI; + private const double DegToRad = Math.PI / 180; /// <summary> /// The furthest distance that can be seen on the <see cref="RenderTarget"/>. Any bodies that are further @@ -201,6 +201,17 @@ namespace StarSimLib.Graphics private double InverseScaleFactor { get { return 1 / Math.Tan(currentFieldOfView * 0.5 * DegToRad); } } /// <summary> + /// Current field-of-view. + /// </summary> + public double FOV + { + get + { + return currentFieldOfView; + } + } + + /// <summary> /// Current angle of rotation in the x axis. /// </summary> public double XAngle @@ -225,6 +236,14 @@ namespace StarSimLib.Graphics } /// <summary> + /// Current zoom level of the drawer. + /// </summary> + public double ZoomLevel + { + get { return FieldOfView / currentFieldOfView; } + } + + /// <summary> /// Updates the rotation matrices for the view. /// </summary> private void UpdateRotationMatrices() @@ -331,9 +350,11 @@ namespace StarSimLib.Graphics break; default: - throw new ArgumentOutOfRangeException(nameof(direction), direction, "The given direction was not within the valid range."); + throw new ArgumentOutOfRangeException(nameof(direction), direction, + "The given rotation direction was not within the valid range."); } + // once we hit 360 (or -360) degrees of rotation, we wrap back around to 0 degrees xAngle %= 360; yAngle %= 360; zAngle %= 360; @@ -349,6 +370,10 @@ namespace StarSimLib.Graphics { currentFieldOfView /= scaleMultiplier; + // limits field of view to a lower bound and an upper bound + currentFieldOfView = currentFieldOfView < 0.0001 ? 0.0001 : currentFieldOfView; + currentFieldOfView = currentFieldOfView > 179d ? 179 : currentFieldOfView; + // reassign projection matrix fields as the field of view (used by InverseScaleFactor) has been // modified to zoom in or out projectionMatrix[0, 0] = aspectRatio * InverseScaleFactor; diff --git a/StarSim/StarSimLib/Physics/Body.cs b/StarSim/StarSimLib/Physics/Body.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using StarSimLib.Data_Structures; namespace StarSimLib.Physics @@ -30,6 +31,11 @@ namespace StarSimLib.Physics private Vector4d position; /// <summary> + /// Backing field for the <see cref="PreviousPositions"/> property. + /// </summary> + private Queue<Vector4d> previousPositions; + + /// <summary> /// Backing field for the <see cref="Velocity"/> property. /// </summary> private Vector4d velocity; @@ -90,6 +96,14 @@ namespace StarSimLib.Physics } /// <summary> + /// A <see cref="Queue{T}"/> containing previous positions of the body. + /// </summary> + public ref readonly Queue<Vector4d> PreviousPositions + { + get { return ref previousPositions; } + } + + /// <summary> /// The current velocity of the <see cref="Body"/> in 3D space. /// </summary> public Vector4d Velocity diff --git a/StarSim/StarSimLib/StarSimLib.xml b/StarSim/StarSimLib/StarSimLib.xml @@ -59,6 +59,11 @@ The mass of the sun (1.98892e30f). </summary> </member> + <member name="F:StarSimLib.Constants.StoredPreviousPositionCount"> + <summary> + The number of previous positions that will be stored by a body + </summary> + </member> <member name="F:StarSimLib.Constants.TimeStep"> <summary> The time step for the simulation. @@ -633,6 +638,11 @@ Inverse scale factor used in the projection matrix. </summary> </member> + <member name="P:StarSimLib.Graphics.Drawer.FOV"> + <summary> + Current field-of-view. + </summary> + </member> <member name="P:StarSimLib.Graphics.Drawer.XAngle"> <summary> Current angle of rotation in the x axis. @@ -648,6 +658,11 @@ Current angle of rotation in the x axis. </summary> </member> + <member name="P:StarSimLib.Graphics.Drawer.ZoomLevel"> + <summary> + Current zoom level of the drawer. + </summary> + </member> <member name="M:StarSimLib.Graphics.Drawer.UpdateRotationMatrices"> <summary> Updates the rotation matrices for the view. @@ -697,6 +712,11 @@ Backing field for the <see cref="P:StarSimLib.Physics.Body.Position"/> property. </summary> </member> + <member name="F:StarSimLib.Physics.Body.previousPositions"> + <summary> + Backing field for the <see cref="P:StarSimLib.Physics.Body.PreviousPositions"/> property. + </summary> + </member> <member name="F:StarSimLib.Physics.Body.velocity"> <summary> Backing field for the <see cref="P:StarSimLib.Physics.Body.Velocity"/> property. @@ -737,6 +757,11 @@ The current position of the <see cref="T:StarSimLib.Physics.Body"/> in 3D space. </summary> </member> + <member name="P:StarSimLib.Physics.Body.PreviousPositions"> + <summary> + A <see cref="T:System.Collections.Generic.Queue`1"/> containing previous positions of the body. + </summary> + </member> <member name="P:StarSimLib.Physics.Body.Velocity"> <summary> The current velocity of the <see cref="T:StarSimLib.Physics.Body"/> in 3D space. diff --git a/StarSim/StarSimLib/UI/InputHandler.cs b/StarSim/StarSimLib/UI/InputHandler.cs @@ -0,0 +1,15 @@ +namespace StarSimLib.UI +{ + /// <summary> + /// Provides user input handling functions. + /// </summary> + public class InputHandler + { + /// <summary> + /// Initialises a new instance of the <see cref="InputHandler"/> class. + /// </summary> + public InputHandler() + { + } + } +} +\ No newline at end of file