Compare commits
4 Commits
ff451ef954
...
24e565fabf
Author | SHA1 | Date |
---|---|---|
Cat Flynn | 24e565fabf | |
ktyl | aa592537c7 | |
ktyl | 160cbb9f17 | |
Cat Flynn | 2646aeb1fa |
|
@ -2,4 +2,7 @@
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>net472</TargetFramework>
|
<TargetFramework>net472</TargetFramework>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="vim.math3d" Version="1.6.0" />
|
||||||
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
|
@ -10,6 +10,5 @@ transform = Transform( 0.515898, 0.606099, -0.605386, -0.393123, 0.795389, 0.461
|
||||||
visible = false
|
visible = false
|
||||||
|
|
||||||
[node name="Camera" parent="." instance=ExtResource( 3 )]
|
[node name="Camera" parent="." instance=ExtResource( 3 )]
|
||||||
_speed = 0.5
|
|
||||||
|
|
||||||
[node name="Orbit System" parent="." instance=ExtResource( 4 )]
|
[node name="Orbit System" parent="." instance=ExtResource( 4 )]
|
||||||
|
|
|
@ -6,5 +6,5 @@
|
||||||
script = ExtResource( 1 )
|
script = ExtResource( 1 )
|
||||||
|
|
||||||
[node name="Camera" type="Camera" parent="."]
|
[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
|
fov = 25.0
|
||||||
|
|
|
@ -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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -53,15 +53,20 @@ public class OrbitSystem : Node, IMassive, ILocation
|
||||||
_orbitGeometry.Clear();
|
_orbitGeometry.Clear();
|
||||||
_orbitGeometry.Begin(Mesh.PrimitiveType.LineLoop);
|
_orbitGeometry.Begin(Mesh.PrimitiveType.LineLoop);
|
||||||
_orbitGeometry.SetColor(new Color(1, 0, 0));
|
_orbitGeometry.SetColor(new Color(1, 0, 0));
|
||||||
|
|
||||||
|
var ellipse = new Ellipse(1, .5);
|
||||||
|
|
||||||
for (int i = 0; i < steps; i++)
|
for (int i = 0; i < steps; i++)
|
||||||
{
|
{
|
||||||
float t = i / (float)steps;
|
float t = i / (float)steps;
|
||||||
float a = t * Mathf.Tau;
|
float a = t * Mathf.Tau;
|
||||||
|
|
||||||
|
var v2 = ellipse.Focus0 + ellipse.GetPosition(a);
|
||||||
|
|
||||||
_orbitGeometry.AddVertex(new Vector3
|
_orbitGeometry.AddVertex(new Vector3
|
||||||
{
|
{
|
||||||
x = Mathf.Sin(a),
|
x = (float)v2.X,
|
||||||
z = Mathf.Cos(a)
|
z = (float)v2.Y
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
_orbitGeometry.End();
|
_orbitGeometry.End();
|
||||||
|
|
|
@ -1,8 +0,0 @@
|
||||||
using Godot;
|
|
||||||
using System;
|
|
||||||
|
|
||||||
public struct Double2
|
|
||||||
{
|
|
||||||
public double x;
|
|
||||||
public double y;
|
|
||||||
}
|
|
|
@ -1,24 +1,53 @@
|
||||||
using Godot;
|
using Godot;
|
||||||
using System;
|
using System;
|
||||||
|
using Vim.Math3d;
|
||||||
|
|
||||||
public struct Ellipse
|
public struct Ellipse
|
||||||
{
|
{
|
||||||
public double a { get => SemiMajorAxis; set => SemiMajorAxis = value; }
|
|
||||||
public double SemiMajorAxis { get; set; }
|
|
||||||
|
|
||||||
public double e { get => Eccentricity; set => Eccentricity = value; }
|
|
||||||
public double Eccentricity { get; set; }
|
|
||||||
|
|
||||||
public double b => SemiMajorAxis;
|
|
||||||
public double SemiMinorAxis => Math.Sqrt(Apisides.max * Apisides.min);
|
|
||||||
|
|
||||||
private Apisides Apisides => new Apisides(a, e);
|
|
||||||
|
|
||||||
public Ellipse(double a = 1, double e = 0)
|
public Ellipse(double a = 1, double e = 0)
|
||||||
{
|
{
|
||||||
SemiMajorAxis = a;
|
SemiMajorAxis = a;
|
||||||
Eccentricity = e;
|
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 struct Apisides
|
||||||
|
|
Loading…
Reference in New Issue