Compare commits
	
		
			5 Commits
		
	
	
		
			24e565fabf
			...
			ad4496d485
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| ad4496d485 | |||
| 5d3efeefce | |||
| c19bf071ea | |||
| a0abce8306 | |||
| e516150465 | 
| @ -2,4 +2,7 @@ | ||||
|   <PropertyGroup> | ||||
|     <TargetFramework>net472</TargetFramework> | ||||
|   </PropertyGroup> | ||||
|   <ItemGroup> | ||||
|     <PackageReference Include="vim.math3d" Version="1.6.0" /> | ||||
|   </ItemGroup> | ||||
| </Project> | ||||
| @ -1,8 +1,7 @@ | ||||
| [gd_scene load_steps=4 format=2] | ||||
| [gd_scene load_steps=3 format=2] | ||||
| 
 | ||||
| [ext_resource path="res://scenes/Train.tscn" type="PackedScene" id=1] | ||||
| [ext_resource path="res://scenes/Railway.tscn" type="PackedScene" id=2] | ||||
| [ext_resource path="res://scenes/OrbitCamera.tscn" type="PackedScene" id=3] | ||||
| [ext_resource path="res://scenes/OrbitSystem.tscn" type="PackedScene" id=4] | ||||
| 
 | ||||
| [node name="Main" type="Node2D"] | ||||
| 
 | ||||
| @ -10,10 +9,5 @@ | ||||
| transform = Transform( 0.515898, 0.606099, -0.605386, -0.393123, 0.795389, 0.461314, 0.76112, -2.26831e-08, 0.648611, 0, 0, 0 ) | ||||
| 
 | ||||
| [node name="OrbitCamera" parent="." instance=ExtResource( 3 )] | ||||
| _speed = 0.5 | ||||
| 
 | ||||
| [node name="Railway" parent="." instance=ExtResource( 2 )] | ||||
| 
 | ||||
| [node name="Train" parent="." instance=ExtResource( 1 )] | ||||
| _speed = 5.0 | ||||
| _railwayPath = NodePath("../Railway") | ||||
| [node name="Orbit System" parent="." instance=ExtResource( 4 )] | ||||
|  | ||||
| @ -6,5 +6,5 @@ | ||||
| script = ExtResource( 1 ) | ||||
| 
 | ||||
| [node name="Camera" type="Camera" parent="."] | ||||
| transform = Transform( 1, 0, 0, 0, 0.913325, 0.407231, 0, -0.407231, 0.913325, 0, 24.1191, 58.6188 ) | ||||
| transform = Transform( 1, 0, 0, 0, 0.964146, 0.265371, 0, -0.265371, 0.964146, 0, 2.21115, 9.48434 ) | ||||
| fov = 25.0 | ||||
|  | ||||
							
								
								
									
										15
									
								
								scenes/OrbitSystem.tscn
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								scenes/OrbitSystem.tscn
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,15 @@ | ||||
| [gd_scene load_steps=2 format=2] | ||||
| 
 | ||||
| [ext_resource path="res://scripts/orbits/OrbitSystem.cs" type="Script" id=1] | ||||
| 
 | ||||
| [node name="Orbit System" type="Spatial"] | ||||
| script = ExtResource( 1 ) | ||||
| _a = NodePath("A") | ||||
| _b = NodePath("B") | ||||
| _barycenter = NodePath("Barycenter") | ||||
| 
 | ||||
| [node name="A" type="Spatial" parent="."] | ||||
| 
 | ||||
| [node name="B" type="Spatial" parent="."] | ||||
| 
 | ||||
| [node name="Barycenter" type="Spatial" parent="."] | ||||
| @ -1,6 +1,6 @@ | ||||
| [gd_scene load_steps=3 format=2] | ||||
| 
 | ||||
| [ext_resource path="res://scripts/Railway.cs" type="Script" id=1] | ||||
| [ext_resource path="res://scripts/trains/Railway.cs" type="Script" id=1] | ||||
| 
 | ||||
| [sub_resource type="Curve3D" id=1] | ||||
| bake_interval = 0.01 | ||||
| @ -1,7 +1,7 @@ | ||||
| [gd_scene load_steps=3 format=2] | ||||
| 
 | ||||
| [ext_resource path="res://scripts/Train.cs" type="Script" id=1] | ||||
| [ext_resource path="res://scenes/TrainCar.tscn" type="PackedScene" id=2] | ||||
| [ext_resource path="res://scripts/trains/Train.cs" type="Script" id=1] | ||||
| [ext_resource path="res://scenes/trains/TrainCar.tscn" type="PackedScene" id=2] | ||||
| 
 | ||||
| [node name="Train" type="Spatial"] | ||||
| script = ExtResource( 1 ) | ||||
| @ -1,7 +1,7 @@ | ||||
| [gd_scene load_steps=3 format=2] | ||||
| 
 | ||||
| [ext_resource path="res://scripts/TrainCar.cs" type="Script" id=1] | ||||
| [ext_resource path="res://scenes/Bogie.tscn" type="PackedScene" id=2] | ||||
| [ext_resource path="res://scripts/trains/TrainCar.cs" type="Script" id=1] | ||||
| [ext_resource path="res://scenes/trains/Bogie.tscn" type="PackedScene" id=2] | ||||
| 
 | ||||
| [node name="Train Car" type="Spatial"] | ||||
| script = ExtResource( 1 ) | ||||
| @ -4,15 +4,44 @@ using System; | ||||
| public class OrbitCamera : Spatial | ||||
| { | ||||
|     [Export] | ||||
|     private float _speed; | ||||
|     private float _sensitivity = 200f; | ||||
| 
 | ||||
|     public override void _Ready() | ||||
|     private Vector2 _rotation; | ||||
|     private bool _canRotate = false; | ||||
| 
 | ||||
|     public override void _Input(InputEvent e) | ||||
|     { | ||||
| 
 | ||||
|         if (e is InputEventMouseMotion mouseMotion) | ||||
|         { | ||||
|             HandleMouseMovement(mouseMotion); | ||||
|         } | ||||
|         if (e is InputEventMouseButton mouseButton) | ||||
|         { | ||||
|             HandleMouseButton(mouseButton); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     public override void _Process(float delta) | ||||
|     { | ||||
|         Rotate(Vector3.Up, _speed * delta); | ||||
|         Rotation = Vector3.Zero; | ||||
|         var sensitivity = -1f / _sensitivity; | ||||
|         Rotate(Vector3.Right, _rotation.y * sensitivity); | ||||
|         Rotate(Vector3.Up, _rotation.x * sensitivity); | ||||
|     } | ||||
| 
 | ||||
|     // left click to drag | ||||
|     private void HandleMouseButton(InputEventMouseButton mouseButton) | ||||
|     { | ||||
|         if (mouseButton.ButtonIndex != (int)ButtonList.Left) return; | ||||
| 
 | ||||
|         _canRotate = mouseButton.Pressed; | ||||
|     } | ||||
| 
 | ||||
|     private void HandleMouseMovement(InputEventMouseMotion mouseMotion) | ||||
|     { | ||||
|         if (!_canRotate) return; | ||||
| 
 | ||||
|         var delta = mouseMotion.Relative; | ||||
|         _rotation += delta; | ||||
|     } | ||||
| } | ||||
|  | ||||
							
								
								
									
										7
									
								
								scripts/orbits/ILocation.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								scripts/orbits/ILocation.cs
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,7 @@ | ||||
| using Godot; | ||||
| using System; | ||||
| 
 | ||||
| public interface ILocation | ||||
| { | ||||
|     Vector3 Position { get; } | ||||
| } | ||||
							
								
								
									
										7
									
								
								scripts/orbits/IMassive.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								scripts/orbits/IMassive.cs
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,7 @@ | ||||
| using Godot; | ||||
| using System; | ||||
| 
 | ||||
| public interface IMassive | ||||
| { | ||||
|     float Mass { get; } | ||||
| } | ||||
							
								
								
									
										12
									
								
								scripts/orbits/Orbit.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								scripts/orbits/Orbit.cs
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,12 @@ | ||||
| using Godot; | ||||
| using System; | ||||
| 
 | ||||
| public class Orbit | ||||
| { | ||||
|     public Orbit(PointMass a, PointMass b) | ||||
|     { | ||||
| 
 | ||||
|     } | ||||
| 
 | ||||
|     // ellipse | ||||
| } | ||||
							
								
								
									
										109
									
								
								scripts/orbits/OrbitSystem.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										109
									
								
								scripts/orbits/OrbitSystem.cs
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,109 @@ | ||||
| using Godot; | ||||
| using System; | ||||
| 
 | ||||
| public class OrbitSystem : Node, IMassive, ILocation | ||||
| { | ||||
|     [Export] private NodePath _a; | ||||
|     [Export] private NodePath _b; | ||||
|     [Export] private NodePath _barycenter; | ||||
| 
 | ||||
|     private readonly PointMass[] _pointMasses = new PointMass[2]; | ||||
| 
 | ||||
|     public Vector3 Position => Barycenter; | ||||
| 
 | ||||
|     [Export] | ||||
|     public Vector3 Barycenter | ||||
|     { | ||||
|         get | ||||
|         { | ||||
|             var p0 = _pointMasses[0].Position; | ||||
|             var p1 = _pointMasses[1].Position; | ||||
| 
 | ||||
|             return p0.LinearInterpolate(p1, .5f); | ||||
|         } | ||||
|         set => _ = value; | ||||
|     } | ||||
| 
 | ||||
|     public float Mass { get; } = 1; | ||||
| 
 | ||||
|     private Orbit _orbit; | ||||
| 
 | ||||
|     private ImmediateGeometry _orbitGeometry; | ||||
| 
 | ||||
|     public override void _Ready() | ||||
|     { | ||||
|         InitPointMasses(); | ||||
|         InitGeometry(); | ||||
|     } | ||||
| 
 | ||||
|     public override void _Process(float delta) | ||||
|     { | ||||
|         DrawOrbit(); | ||||
|     } | ||||
| 
 | ||||
|     private void InitOrbit() | ||||
|     { | ||||
| 
 | ||||
|     } | ||||
| 
 | ||||
|     private void DrawOrbit() | ||||
|     { | ||||
|         int steps = 100; | ||||
| 
 | ||||
|         _orbitGeometry.Clear(); | ||||
|         _orbitGeometry.Begin(Mesh.PrimitiveType.LineLoop); | ||||
|         _orbitGeometry.SetColor(new Color(1, 0, 0)); | ||||
| 
 | ||||
|         var ellipse = new Ellipse(1, .5); | ||||
| 
 | ||||
|         for (int i = 0; i < steps; i++) | ||||
|         { | ||||
|             float t = i / (float)steps; | ||||
|             float a = t * Mathf.Tau; | ||||
| 
 | ||||
|             var v2 = ellipse.Focus0 + ellipse.GetPosition(a); | ||||
| 
 | ||||
|             _orbitGeometry.AddVertex(new Vector3 | ||||
|             { | ||||
|                 x = (float)v2.X, | ||||
|                 z = (float)v2.Y | ||||
|             }); | ||||
|         } | ||||
|         _orbitGeometry.End(); | ||||
|     } | ||||
| 
 | ||||
|     private void InitGeometry() | ||||
|     { | ||||
|         _orbitGeometry = new ImmediateGeometry(); | ||||
|         var m = new SpatialMaterial(); | ||||
|         m.VertexColorUseAsAlbedo = true; | ||||
|         m.FlagsUnshaded = true; | ||||
|         _orbitGeometry.MaterialOverride = m; | ||||
|         AddChild(_orbitGeometry); | ||||
|     } | ||||
| 
 | ||||
|     private void InitPointMasses() | ||||
|     { | ||||
|         var a = GetPointMass(_a); | ||||
|         var b = GetPointMass(_b); | ||||
| 
 | ||||
|         if (a.Mass > b.Mass) | ||||
|         { | ||||
|             _pointMasses[0] = a; | ||||
|             _pointMasses[1] = b; | ||||
|         } | ||||
|         else | ||||
|         { | ||||
|             _pointMasses[0] = b; | ||||
|             _pointMasses[1] = a; | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     private PointMass GetPointMass(NodePath path) | ||||
|     { | ||||
|         var spatial = GetNode<Spatial>(path); | ||||
|         //var massive = node.GetChild<IMassive>(0); | ||||
| 
 | ||||
|         return new PointMass(spatial, 1f); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										18
									
								
								scripts/orbits/Planet.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								scripts/orbits/Planet.cs
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,18 @@ | ||||
| using Godot; | ||||
| using System; | ||||
| 
 | ||||
| public class Planet : Node, IMassive | ||||
| { | ||||
|     [Export] | ||||
|     public float Mass { get; set; } | ||||
| 
 | ||||
|     public override void _Ready() | ||||
|     { | ||||
| 
 | ||||
|     } | ||||
| 
 | ||||
|     //  public override void _Process(float delta) | ||||
|     //  { | ||||
|     // | ||||
|     //  } | ||||
| } | ||||
							
								
								
									
										25
									
								
								scripts/orbits/PointMass.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								scripts/orbits/PointMass.cs
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,25 @@ | ||||
| using Godot; | ||||
| 
 | ||||
| public struct PointMass : IMassive, ILocation | ||||
| { | ||||
|     public float Mass => _massive == null ? _mass : _massive.Mass; | ||||
|     public Vector3 Position => _spatial.Translation; | ||||
| 
 | ||||
|     private IMassive _massive; | ||||
|     private Spatial _spatial; | ||||
|     private float _mass; | ||||
| 
 | ||||
|     public PointMass(Spatial spatial, IMassive massive) | ||||
|     { | ||||
|         _spatial = spatial; | ||||
|         _massive = massive; | ||||
|         _mass = 0; | ||||
|     } | ||||
| 
 | ||||
|     public PointMass(Spatial spatial, float mass) | ||||
|     { | ||||
|         _spatial = spatial; | ||||
|         _massive = null; | ||||
|         _mass = mass; | ||||
|     } | ||||
| } | ||||
							
								
								
									
										63
									
								
								scripts/orbits/math/Ellipse.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										63
									
								
								scripts/orbits/math/Ellipse.cs
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,63 @@ | ||||
| using Godot; | ||||
| using System; | ||||
| using Vim.Math3d; | ||||
| 
 | ||||
| public struct Ellipse | ||||
| { | ||||
|     public Ellipse(double a = 1, double e = 0) | ||||
|     { | ||||
|         SemiMajorAxis = a; | ||||
|         Eccentricity = e; | ||||
| 
 | ||||
|         // TODO: this is an immutable struct, so initialise everything else  | ||||
|         // in the constructor to avoid recalculating properties whenever | ||||
|         // they are accessed | ||||
|     } | ||||
| 
 | ||||
|     /// <summary> | ||||
|     /// Semi-major axis | ||||
|     /// </summary> | ||||
|     public double a => SemiMajorAxis; | ||||
|     public double SemiMajorAxis { get; } | ||||
| 
 | ||||
|     /// <summary> | ||||
|     /// Eccentricity | ||||
|     /// </summary> | ||||
|     public double e => Eccentricity; | ||||
|     public double Eccentricity { get; } | ||||
| 
 | ||||
|     /// <summary> | ||||
|     /// Semi-minor axis | ||||
|     /// </summary> | ||||
|     public double b => SemiMinorAxis; | ||||
|     public double SemiMinorAxis => Math.Sqrt(Apisides.max * Apisides.min); | ||||
| 
 | ||||
|     /// <summary> | ||||
|     /// Get a position on the auxiliary circle | ||||
|     /// </summary> | ||||
|     /// <param name="t">Angle in radians around the circle</param> | ||||
|     /// <returns>2D position on the circle scaled by the semi-major axis</returns> | ||||
|     public DVector2 GetAuxiliaryPosition2D(double t = 0) => | ||||
|         new DVector2(Math.Cos(t), Math.Sin(t)) * a; | ||||
| 
 | ||||
|     private Apisides Apisides => new Apisides(a, e); | ||||
| 
 | ||||
|     public DVector2 GetPosition(double t) => | ||||
|         new DVector2(Math.Cos(t) * a, Math.Sin(t) * b); | ||||
| 
 | ||||
|     public DVector2 Focus0 => new DVector2(-GetFocusDistance(), 0); | ||||
|     public DVector2 Focus1 => new DVector2(GetFocusDistance(), 0); | ||||
|     private double GetFocusDistance() => Math.Sqrt(a * a - b * b); | ||||
| } | ||||
| 
 | ||||
| public struct Apisides | ||||
| { | ||||
|     public readonly double min; | ||||
|     public readonly double max; | ||||
| 
 | ||||
|     public Apisides(double a, double e) | ||||
|     { | ||||
|         min = a * (1 - e); | ||||
|         max = a * (1 + e); | ||||
|     } | ||||
| } | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user