Compare commits

...

7 Commits

Author SHA1 Message Date
Cat Flynn a0abce8306 drag to rotate camera 2022-09-04 17:48:10 +01:00
ktyl e516150465 move train stuff into folders 2022-09-04 17:44:55 +01:00
ktyl 0ac24d49f6 train on bogie on track 2022-08-18 00:42:04 +01:00
ktyl 44cbc7b5ae bogies and exported properties 2022-08-16 01:02:44 +01:00
ktyl c1e19b21fc 2 tracks 2022-08-14 23:50:09 +01:00
ktyl 89477e7ce8 make train go back n forth 2022-08-12 00:16:38 +01:00
ktyl 8d80cdb372 vscode settings 2022-08-12 00:16:23 +01:00
18 changed files with 489 additions and 159 deletions

5
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,5 @@
{
"editor.formatOnSave": true,
"editor.formatOnType": true,
"vscode-neovim.neovimExecutablePaths.linux": "/usr/bin/nvim"
}

View File

@ -1,18 +1,18 @@
[gd_scene load_steps=4 format=2] [gd_scene load_steps=4 format=2]
[ext_resource path="res://scenes/Train.tscn" type="PackedScene" id=1] [ext_resource path="res://scenes/trains/Train.tscn" type="PackedScene" id=1]
[ext_resource path="res://scenes/Railway.tscn" type="PackedScene" id=2] [ext_resource path="res://scenes/trains/Railway.tscn" type="PackedScene" id=2]
[ext_resource path="res://scenes/OrbitCamera.tscn" type="PackedScene" id=3] [ext_resource path="res://scenes/OrbitCamera.tscn" type="PackedScene" id=3]
[node name="Main" type="Node2D"] [node name="Main" type="Node2D"]
[node name="Train" parent="." instance=ExtResource( 1 )]
[node name="DirectionalLight" type="DirectionalLight" parent="."] [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 ) 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 )] [node name="OrbitCamera" parent="." instance=ExtResource( 3 )]
_speed = 1.0
[node name="Railway" parent="." instance=ExtResource( 2 )] [node name="Railway" parent="." instance=ExtResource( 2 )]
script = null
[node name="Train" parent="." instance=ExtResource( 1 )]
_speed = 5.0
_railwayPath = NodePath("../Railway")

View File

@ -6,4 +6,5 @@
script = ExtResource( 1 ) script = ExtResource( 1 )
[node name="Camera" type="Camera" parent="."] [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

View File

@ -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 )

View File

@ -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"]

View File

@ -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

9
scenes/trains/Bogie.tscn Normal file
View File

@ -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

25
scenes/trains/Rail.tscn Normal file
View File

@ -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

View File

@ -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

10
scenes/trains/Train.tscn Normal file
View File

@ -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 )]

View File

@ -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 )

View File

@ -4,15 +4,44 @@ using System;
public class OrbitCamera : Spatial public class OrbitCamera : Spatial
{ {
[Export] [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) 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;
} }
} }

View File

@ -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();
}
}

View File

@ -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!");
}
}

View File

@ -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)
// {
//
// }
}

119
scripts/trains/Railway.cs Normal file
View File

@ -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);
}
}

121
scripts/trains/Train.cs Normal file
View File

@ -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;
}
}

View File

@ -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;
}
}