Compare commits
10 Commits
c102d87b2d
...
ad4496d485
Author | SHA1 | Date |
---|---|---|
ktyl | ad4496d485 | |
ktyl | 5d3efeefce | |
Cat Flynn | c19bf071ea | |
Cat Flynn | a0abce8306 | |
ktyl | e516150465 | |
ktyl | 0ac24d49f6 | |
ktyl | 44cbc7b5ae | |
ktyl | c1e19b21fc | |
ktyl | 89477e7ce8 | |
ktyl | 8d80cdb372 |
|
@ -0,0 +1,5 @@
|
|||
{
|
||||
"editor.formatOnSave": true,
|
||||
"editor.formatOnType": true,
|
||||
"vscode-neovim.neovimExecutablePaths.linux": "/usr/bin/nvim"
|
||||
}
|
|
@ -2,4 +2,7 @@
|
|||
<PropertyGroup>
|
||||
<TargetFramework>net472</TargetFramework>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="vim.math3d" Version="1.6.0" />
|
||||
</ItemGroup>
|
||||
</Project>
|
|
@ -1,18 +1,13 @@
|
|||
[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"]
|
||||
|
||||
[node name="Train" parent="." instance=ExtResource( 1 )]
|
||||
|
||||
[node name="DirectionalLight" type="DirectionalLight" parent="."]
|
||||
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 = 1.0
|
||||
|
||||
[node name="Railway" parent="." instance=ExtResource( 2 )]
|
||||
script = null
|
||||
[node name="Orbit System" parent="." instance=ExtResource( 4 )]
|
||||
|
|
|
@ -6,4 +6,5 @@
|
|||
script = ExtResource( 1 )
|
||||
|
||||
[node name="Camera" type="Camera" parent="."]
|
||||
transform = Transform( 0.636065, -0.249781, 0.73009, 0, 0.946159, 0.323703, -0.771636, -0.205896, 0.601818, 8.49012, 5.30069, 6.71091 )
|
||||
transform = Transform( 1, 0, 0, 0, 0.964146, 0.265371, 0, -0.265371, 0.964146, 0, 2.21115, 9.48434 )
|
||||
fov = 25.0
|
||||
|
|
|
@ -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,22 +0,0 @@
|
|||
[gd_scene load_steps=2 format=2]
|
||||
|
||||
[sub_resource type="CSharpScript" id=1]
|
||||
script/source = "using Godot;
|
||||
using System;
|
||||
|
||||
public class RailNode : Spatial
|
||||
{
|
||||
public override void _Ready()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
// public override void _Process(float delta)
|
||||
// {
|
||||
//
|
||||
// }
|
||||
}
|
||||
"
|
||||
|
||||
[node name="Spatial" type="Spatial"]
|
||||
script = SubResource( 1 )
|
|
@ -1,23 +0,0 @@
|
|||
[gd_scene load_steps=4 format=2]
|
||||
|
||||
[ext_resource path="res://scripts/Railway.cs" type="Script" id=1]
|
||||
|
||||
[sub_resource type="Curve3D" id=1]
|
||||
_data = {
|
||||
"points": PoolVector3Array( 0, 0, 0, 0, 0, 0, 8.10688, -1.81506, 7.57791, 0, 0, 0, 0, 0, 0, -4.46766, -3.67929, 0.175438, 0, 0, 0, 0, 0, 0, -6.79476, 2.00362, -6.79993, 0, 0, 0, 0, 0, 0, 5.56111, 6.33345, -1.84912, 0, 0, 0, 0, 0, 0, 11.2517, 3.50708, 4.91382, 0, 0, 0, 0, 0, 0, 14.9168, -0.251479, 11.0718 ),
|
||||
"tilts": PoolRealArray( 0, 0, 0, 0, 0, 0 )
|
||||
}
|
||||
|
||||
[sub_resource type="SpatialMaterial" id=2]
|
||||
flags_unshaded = true
|
||||
vertex_color_use_as_albedo = true
|
||||
|
||||
[node name="Railway" type="Path"]
|
||||
curve = SubResource( 1 )
|
||||
script = ExtResource( 1 )
|
||||
_color = Color( 0, 1, 0.133333, 1 )
|
||||
|
||||
[node name="ImmediateGeometry" type="ImmediateGeometry" parent="."]
|
||||
material_override = SubResource( 2 )
|
||||
|
||||
[connection signal="curve_changed" from="." to="." method="OnPathCurveChanged"]
|
|
@ -1,12 +0,0 @@
|
|||
[gd_scene load_steps=2 format=2]
|
||||
|
||||
[ext_resource path="res://scripts/Train.cs" type="Script" id=1]
|
||||
|
||||
[node name="Train" type="Node"]
|
||||
script = ExtResource( 1 )
|
||||
|
||||
[node name="CSGBox" type="CSGBox" parent="."]
|
||||
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.5, 0 )
|
||||
width = 1.0
|
||||
height = 1.0
|
||||
depth = 4.0
|
|
@ -0,0 +1,9 @@
|
|||
[gd_scene format=2]
|
||||
|
||||
[node name="Bogey" type="Spatial"]
|
||||
|
||||
[node name="CSGBox" type="CSGBox" parent="."]
|
||||
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.2, 0 )
|
||||
width = 0.9
|
||||
height = 0.4
|
||||
depth = 1.1
|
|
@ -0,0 +1,25 @@
|
|||
[gd_scene load_steps=2 format=2]
|
||||
|
||||
[sub_resource type="Curve3D" id=1]
|
||||
_data = {
|
||||
"points": PoolVector3Array( ),
|
||||
"tilts": PoolRealArray( )
|
||||
}
|
||||
|
||||
[node name="Rail" type="Spatial"]
|
||||
|
||||
[node name="Path" type="Path" parent="."]
|
||||
curve = SubResource( 1 )
|
||||
|
||||
[node name="CSGPolygon" type="CSGPolygon" parent="."]
|
||||
polygon = PoolVector2Array( -0.383256, -0.00220253, -0.383256, 1.0066, 0.383256, 1.00219, 0.383256, -0.00220253 )
|
||||
mode = 2
|
||||
path_node = NodePath("../Path")
|
||||
path_interval_type = 0
|
||||
path_interval = 1.0
|
||||
path_simplify_angle = 0.0
|
||||
path_rotation = 2
|
||||
path_local = false
|
||||
path_continuous_u = true
|
||||
path_u_distance = 1.0
|
||||
path_joined = false
|
|
@ -0,0 +1,47 @@
|
|||
[gd_scene load_steps=3 format=2]
|
||||
|
||||
[ext_resource path="res://scripts/trains/Railway.cs" type="Script" id=1]
|
||||
|
||||
[sub_resource type="Curve3D" id=1]
|
||||
bake_interval = 0.01
|
||||
_data = {
|
||||
"points": PoolVector3Array( -2.68413, 0, -1.21546, 2.68413, 0, 1.21546, -20.005, 0, -8.27961, -0.202576, 0, -2.12705, 0.202576, 0, 2.12705, -15.5484, 0.00448608, -2.35426, -1.06352, 0, -2.88671, 1.06352, 0, 2.88671, -15.8992, 0.000904083, 5.83998, -3.24122, 0, 0.202576, 3.24122, 0, -0.202576, -8.81272, 0, 10.4587, -0.468547, 0, 2.85805, 0.468547, 0, -2.85805, -3.58364, -0.000244141, 4.56199, -1.16602, 4.07454e-10, 3.45615, 1.16602, -4.07454e-10, -3.45615, -5.10296, -0.000259399, -3.5917, -2.78542, 0, -1.56996, 2.78542, 0, 1.56996, 3.2912, -0.000259399, -6.30449, -3.70976, 0, -1.39605, 3.70976, 0, 1.39605, 9.68509, -0.000259399, -0.0972629, 0, 0, 0, 0, 0, 0, 19.9658, -0.000259399, -1.11014 ),
|
||||
"tilts": PoolRealArray( 0, 0, 0, 0, 0, 0, 0, 0, 0 )
|
||||
}
|
||||
|
||||
[node name="Railway" type="Spatial"]
|
||||
script = ExtResource( 1 )
|
||||
RailWidth = 0.05
|
||||
RailHeight = 0.1
|
||||
RailGauge = 0.8
|
||||
_railPaths = [ NodePath("L"), NodePath("R") ]
|
||||
_centreLine = NodePath("Centreline")
|
||||
|
||||
[node name="Centreline" type="Path" parent="."]
|
||||
curve = SubResource( 1 )
|
||||
|
||||
[node name="L" type="CSGPolygon" parent="."]
|
||||
polygon = PoolVector2Array( -0.45, 0, -0.45, 0.1, -0.35, 0.1, -0.35, 0 )
|
||||
mode = 2
|
||||
path_node = NodePath("../Centreline")
|
||||
path_interval_type = 0
|
||||
path_interval = 1.0
|
||||
path_simplify_angle = 0.0
|
||||
path_rotation = 2
|
||||
path_local = false
|
||||
path_continuous_u = true
|
||||
path_u_distance = 1.0
|
||||
path_joined = false
|
||||
|
||||
[node name="R" type="CSGPolygon" parent="."]
|
||||
polygon = PoolVector2Array( 0.35, 0, 0.35, 0.1, 0.45, 0.1, 0.45, 0 )
|
||||
mode = 2
|
||||
path_node = NodePath("../Centreline")
|
||||
path_interval_type = 0
|
||||
path_interval = 1.0
|
||||
path_simplify_angle = 0.0
|
||||
path_rotation = 2
|
||||
path_local = false
|
||||
path_continuous_u = true
|
||||
path_u_distance = 1.0
|
||||
path_joined = false
|
|
@ -0,0 +1,10 @@
|
|||
[gd_scene load_steps=3 format=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 )
|
||||
_carPath = NodePath("Train Car")
|
||||
|
||||
[node name="Train Car" parent="." instance=ExtResource( 2 )]
|
|
@ -0,0 +1,22 @@
|
|||
[gd_scene load_steps=3 format=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 )
|
||||
Wheelbase = 5.0
|
||||
_boxNode = NodePath("CSGBox")
|
||||
_bogiePaths = [ NodePath("Bogie"), NodePath("Bogie2") ]
|
||||
|
||||
[node name="CSGBox" type="CSGBox" parent="."]
|
||||
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0 )
|
||||
width = 1.0
|
||||
height = 1.0
|
||||
depth = 8.0
|
||||
|
||||
[node name="Bogie" parent="." instance=ExtResource( 2 )]
|
||||
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, -2.5 )
|
||||
|
||||
[node name="Bogie2" parent="." instance=ExtResource( 2 )]
|
||||
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 2.5 )
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,53 +0,0 @@
|
|||
using Godot;
|
||||
using System;
|
||||
|
||||
[Tool]
|
||||
public class Railway : Path
|
||||
{
|
||||
[Export]
|
||||
private Color _color;
|
||||
|
||||
[Export]
|
||||
private float _gauge = 1f;
|
||||
|
||||
private ImmediateGeometry _geo = null;
|
||||
private Vector3[] _points = null;
|
||||
|
||||
public void OnPathCurveChanged()
|
||||
{
|
||||
Curve.Tessellate();
|
||||
_points = Curve.GetBakedPoints();
|
||||
}
|
||||
|
||||
// Called when the node enters the scene tree for the first time.
|
||||
public override void _Ready()
|
||||
{
|
||||
base._Ready();
|
||||
|
||||
_geo = GetNode<ImmediateGeometry>("ImmediateGeometry");
|
||||
|
||||
var m = new SpatialMaterial();
|
||||
m.VertexColorUseAsAlbedo = true;
|
||||
m.FlagsUnshaded = true;
|
||||
_geo.MaterialOverride = m;
|
||||
|
||||
OnPathCurveChanged();
|
||||
}
|
||||
|
||||
public override void _Process(float delta)
|
||||
{
|
||||
base._Process(delta);
|
||||
|
||||
if (_geo == null) return;
|
||||
if (_points == null || _points.Length == 0) return;
|
||||
|
||||
_geo.Clear();
|
||||
_geo.Begin(Mesh.PrimitiveType.LineStrip);
|
||||
_geo.SetColor(new Color(1, 0, 0));
|
||||
for(int i = 0; i < _points.Length; i++)
|
||||
{
|
||||
_geo.AddVertex(_points[i]);
|
||||
}
|
||||
_geo.End();
|
||||
}
|
||||
}
|
|
@ -1,22 +0,0 @@
|
|||
using Godot;
|
||||
using System;
|
||||
|
||||
[Tool]
|
||||
public class Tracks : ImmediateGeometry
|
||||
{
|
||||
private SpatialMaterial _m;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
// set up a material for imgui to use
|
||||
_m = new SpatialMaterial();
|
||||
_m.VertexColorUseAsAlbedo = true;
|
||||
_m.FlagsUnshaded = true;
|
||||
this.MaterialOverride = _m;
|
||||
}
|
||||
|
||||
public override void _Process(float delta)
|
||||
{
|
||||
GD.Print("hi!");
|
||||
}
|
||||
}
|
|
@ -1,16 +0,0 @@
|
|||
using Godot;
|
||||
using System;
|
||||
|
||||
public class Train : Node
|
||||
{
|
||||
// Called when the node enters the scene tree for the first time.
|
||||
public override void _Ready()
|
||||
{
|
||||
}
|
||||
|
||||
// // Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
// public override void _Process(float delta)
|
||||
// {
|
||||
//
|
||||
// }
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
using Godot;
|
||||
using System;
|
||||
|
||||
public interface ILocation
|
||||
{
|
||||
Vector3 Position { get; }
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
using Godot;
|
||||
using System;
|
||||
|
||||
public interface IMassive
|
||||
{
|
||||
float Mass { get; }
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
using Godot;
|
||||
using System;
|
||||
|
||||
public class Orbit
|
||||
{
|
||||
public Orbit(PointMass a, PointMass b)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
// ellipse
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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)
|
||||
// {
|
||||
//
|
||||
// }
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,119 @@
|
|||
using Godot;
|
||||
using System;
|
||||
|
||||
[Tool]
|
||||
public class Railway : Node
|
||||
{
|
||||
[Export]
|
||||
private NodePath[] _railPaths;
|
||||
|
||||
|
||||
[Export]
|
||||
private NodePath _centreLine;
|
||||
|
||||
|
||||
private float _railWidth;
|
||||
private float _railHeight;
|
||||
private float _railGauge;
|
||||
|
||||
[Export]
|
||||
private float RailWidth
|
||||
{
|
||||
get => _railWidth;
|
||||
set
|
||||
{
|
||||
_railWidth = value;
|
||||
if (Engine.EditorHint)
|
||||
{
|
||||
SetRailCrossSection();
|
||||
}
|
||||
}
|
||||
}
|
||||
[Export]
|
||||
private float RailHeight
|
||||
{
|
||||
get => _railHeight;
|
||||
set
|
||||
{
|
||||
_railHeight = value;
|
||||
if (Engine.EditorHint)
|
||||
{
|
||||
SetRailCrossSection();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Export]
|
||||
private float RailGauge
|
||||
{
|
||||
get => _railGauge;
|
||||
set
|
||||
{
|
||||
_railGauge = value;
|
||||
if (Engine.EditorHint)
|
||||
{
|
||||
SetRailCrossSection();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Path _path = null;
|
||||
private Path Path
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_path == null)
|
||||
{
|
||||
_path = GetNode<Path>(_centreLine);
|
||||
}
|
||||
return _path;
|
||||
}
|
||||
}
|
||||
|
||||
public Curve3D Curve => Path.Curve;
|
||||
|
||||
// the centre line should not have geometry directly associated with it.
|
||||
// instead, two additional path/csgpolygon combinations should be managed
|
||||
// in reference to the centre line for the left and right rails.
|
||||
|
||||
// Called when the node enters the scene tree for the first time.
|
||||
public override void _Ready()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
private void SetRailCrossSection()
|
||||
{
|
||||
// error checkin
|
||||
if (_railPaths.Length != 2)
|
||||
throw new Exception("need 2 rails");
|
||||
|
||||
// set rail geometry
|
||||
for (int i = 0; i < 2; i++)
|
||||
{
|
||||
var rail = GetNode<CSGPolygon>(_railPaths[i]);
|
||||
|
||||
float w = RailWidth;
|
||||
float h = RailHeight;
|
||||
// horizontal offset of rail from centreline
|
||||
float c = (-.5f + i) * RailGauge;
|
||||
|
||||
var polygon = new Vector2[4];
|
||||
polygon[0] = new Vector2(c - w, 0);
|
||||
polygon[1] = new Vector2(c - w, h);
|
||||
polygon[2] = new Vector2(c + w, h);
|
||||
polygon[3] = new Vector2(c + w, 0);
|
||||
rail.Polygon = polygon;
|
||||
}
|
||||
}
|
||||
|
||||
public override void _Process(float delta)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void AddPathFollower(PathFollow pathFollow)
|
||||
{
|
||||
Path.AddChild(pathFollow);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,121 @@
|
|||
using Godot;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public class Train : Spatial
|
||||
{
|
||||
[Export]
|
||||
private float _speed = 1.0f;
|
||||
|
||||
[Export]
|
||||
private NodePath _railwayPath;
|
||||
|
||||
private Railway _railway;
|
||||
private float _distance = 0;
|
||||
|
||||
[Export]
|
||||
private NodePath _carPath;
|
||||
private TrainCar _car;
|
||||
|
||||
private Spatial[] _bogies;
|
||||
private readonly Dictionary<Spatial, PathFollow> _bogiePathFollows = new Dictionary<Spatial, PathFollow>();
|
||||
|
||||
private ImmediateGeometry _imgui;
|
||||
|
||||
// Called when the node enters the scene tree for the first time.
|
||||
public override void _Ready()
|
||||
{
|
||||
_imgui = new ImmediateGeometry();
|
||||
var m = new SpatialMaterial();
|
||||
m.VertexColorUseAsAlbedo = true;
|
||||
m.FlagsUnshaded = true;
|
||||
_imgui.MaterialOverride = m;
|
||||
AddChild(_imgui);
|
||||
|
||||
_railway = GetNode<Railway>(_railwayPath);
|
||||
_car = GetNode<TrainCar>(_carPath);
|
||||
|
||||
_bogies = new Spatial[_car.Bogies.Length];
|
||||
for (int i = 0; i < _bogies.Length; i++)
|
||||
{
|
||||
var b = _car.Bogies[i];
|
||||
_bogies[i] = b;
|
||||
_bogiePathFollows[b] = new PathFollow
|
||||
{
|
||||
RotationMode = PathFollow.RotationModeEnum.Oriented
|
||||
};
|
||||
_railway.AddPathFollower(_bogiePathFollows[b]);
|
||||
|
||||
// detach the bogies from the car as we need to set the bogie
|
||||
// positions in order to calculate the car position and it will
|
||||
// get confusing if they're in a parent/child relationship,
|
||||
// so make them all siblings
|
||||
_car.RemoveChild(b);
|
||||
this.AddChild(b);
|
||||
}
|
||||
}
|
||||
|
||||
// Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
public override void _Process(float delta)
|
||||
{
|
||||
var length = _railway.Curve.GetBakedLength();
|
||||
|
||||
// first determine which is the front bogie. this is dependent
|
||||
// on the direction of travel.
|
||||
int travelDir = (int)(_distance / length) == 0 ? -1 : 1;
|
||||
|
||||
DrawTravelDirectionIndicator(travelDir);
|
||||
|
||||
// keep track of total distance
|
||||
_distance += delta * _speed;
|
||||
// wrap at 2x total distance
|
||||
_distance %= length * 2;
|
||||
|
||||
var distance = PingPong(_distance, length);
|
||||
|
||||
// get the position of the first bogie on the track
|
||||
var b = _bogies[0];
|
||||
var pathFollow = _bogiePathFollows[b];
|
||||
|
||||
pathFollow.Offset = distance;
|
||||
b.Translation = pathFollow.Translation;
|
||||
b.Rotation = pathFollow.Rotation;
|
||||
|
||||
// now we want to position the car over the first bogie
|
||||
GD.Print(_car.Wheelbase);
|
||||
_car.Translation = b.Translation;
|
||||
_car.Rotation = b.Rotation;
|
||||
|
||||
// TODO: this rotation quat needs to position the other
|
||||
// bogie over the curve
|
||||
_car.Translation += _car.Transform.basis
|
||||
.RotationQuat()
|
||||
.Xform(Vector3.Back) * _car.Wheelbase * 0.5f;
|
||||
}
|
||||
|
||||
private void DrawTravelDirectionIndicator(int travelDir)
|
||||
{
|
||||
_imgui.Clear();
|
||||
_imgui.Begin(Mesh.PrimitiveType.LineStrip);
|
||||
_imgui.SetColor(new Color(1, 0, 0));
|
||||
var c = _car.Translation + Vector3.Up * 2;
|
||||
_imgui.AddVertex(c);
|
||||
var d = travelDir;
|
||||
var l = 5;
|
||||
var e = c + _car.Transform.basis
|
||||
.RotationQuat()
|
||||
.Xform(Vector3.Forward) * d * l;
|
||||
_imgui.AddVertex(e);
|
||||
_imgui.End();
|
||||
}
|
||||
|
||||
// poor mans branching ping pong
|
||||
private float PingPong(float t, float length)
|
||||
{
|
||||
var tml = t % length;
|
||||
|
||||
return t > length
|
||||
? length - tml
|
||||
: tml;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,90 @@
|
|||
using Godot;
|
||||
using System;
|
||||
|
||||
[Tool]
|
||||
public class TrainCar : Spatial
|
||||
{
|
||||
public float Length => Box.Depth;
|
||||
|
||||
[Export]
|
||||
private NodePath _boxNode;
|
||||
|
||||
[Export]
|
||||
private NodePath[] _bogiePaths;
|
||||
|
||||
[Export]
|
||||
public float Wheelbase
|
||||
{
|
||||
get => _wheelbase;
|
||||
set
|
||||
{
|
||||
_wheelbase = value;
|
||||
|
||||
// don't set the wheelbase except from the editor
|
||||
if (!Engine.EditorHint) return;
|
||||
|
||||
SetBogeyPositions();
|
||||
}
|
||||
}
|
||||
private float _wheelbase;
|
||||
|
||||
public Spatial[] Bogies
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_bogies == null)
|
||||
{
|
||||
_bogies = new Spatial[2];
|
||||
for (int i = 0; i < _bogiePaths.Length; i++)
|
||||
{
|
||||
_bogies[i] = GetNode<Spatial>(_bogiePaths[i]);
|
||||
}
|
||||
}
|
||||
return _bogies;
|
||||
}
|
||||
}
|
||||
private Spatial[] _bogies = null;
|
||||
|
||||
private CSGBox _box;
|
||||
private CSGBox Box
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_box == null)
|
||||
{
|
||||
_box = GetNode<CSGBox>(_boxNode);
|
||||
}
|
||||
return _box;
|
||||
}
|
||||
}
|
||||
|
||||
public Railway Railway { get; set; }
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public override void _Process(float delta)
|
||||
{
|
||||
}
|
||||
|
||||
private void SetBogeyPositions()
|
||||
{
|
||||
|
||||
if (Wheelbase > Length)
|
||||
throw new Exception("wheelbase cannot be longer than car length");
|
||||
|
||||
if (_bogiePaths.Length != 2)
|
||||
throw new Exception("_bogeyPaths should be of length 2");
|
||||
|
||||
for (int i = 0; i < 2; i++)
|
||||
{
|
||||
_bogies[i] = GetNode<Spatial>(_bogiePaths[i]);
|
||||
}
|
||||
|
||||
var axis = Vector3.Forward * Wheelbase;
|
||||
_bogies[0].Translation = axis * 0.5f;
|
||||
_bogies[1].Translation = axis * -0.5f;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue