Compare commits

..

6 Commits
main ... trains

Author SHA1 Message Date
ktyl 52541f6d83 move train stuff into folders 2022-09-05 22:15:10 +01:00
ktyl b93511d12f train on bogie on track 2022-09-05 22:15:10 +01:00
ktyl fb9c9d631d bogies and exported properties 2022-09-05 22:15:10 +01:00
ktyl c0fe68a8ac 2 tracks 2022-09-05 22:15:10 +01:00
ktyl 113ae40a9a make train go back n forth 2022-09-05 22:15:10 +01:00
Cat Flynn 8ccd003b00 limit fov range 2022-09-05 20:10:00 +01:00
17 changed files with 462 additions and 155 deletions

View File

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

View File

@ -1,12 +1,14 @@
[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="Train" parent="." instance=ExtResource( 1 )]
_speed = 5.0
_railwayPath = NodePath("../Railway")
[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 )
@ -14,4 +16,3 @@ transform = Transform( 0.515898, 0.606099, -0.605386, -0.393123, 0.795389, 0.461
[node name="OrbitCamera" parent="." instance=ExtResource( 3 )] [node name="OrbitCamera" parent="." instance=ExtResource( 3 )]
[node name="Railway" parent="." instance=ExtResource( 2 )] [node name="Railway" parent="." instance=ExtResource( 2 )]
script = null

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, -16.1055, 0.000946045, 5.85007, -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.17756, 0, 3.41513, 1.17756, 0, -3.41513, -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.08
RailHeight = 0.12
RailGauge = 1.0
_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.58, 0, -0.58, 0.12, -0.42, 0.12, -0.42, 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.42, 0, 0.42, 0.12, 0.58, 0.12, 0.58, 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

@ -5,9 +5,10 @@ public class OrbitCamera : Spatial
{ {
[Export] [Export]
private float _lookSensitivity = 200f; private float _lookSensitivity = 200f;
[Export] [Export]
private float _zoomSensitivity = 10f; private float _zoomSensitivity = 10f;
[Export]
private Vector2 _fovRange = new Vector2(10, 60);
[Export] [Export]
private NodePath _cameraPath; private NodePath _cameraPath;
@ -65,9 +66,18 @@ public class OrbitCamera : Spatial
} }
} }
private void Zoom(float amount) private void Zoom(int dir)
{ {
Camera.Fov += amount * _zoomSensitivity / Camera.Fov; if (dir != 1 && dir != -1)
throw new ArgumentException();
var fov = Camera.Fov;
var zoom = _zoomSensitivity / Camera.Fov;
fov += (float)dir * zoom;
fov = Mathf.Clamp(fov, _fovRange.x, _fovRange.y);
Camera.Fov = fov;
} }
private void HandleMouseMovement(InputEventMouseMotion mouseMotion) private void HandleMouseMovement(InputEventMouseMotion mouseMotion)

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