-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFish.cs
79 lines (63 loc) · 2.54 KB
/
Fish.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using System;
using Godot;
namespace Fish;
public partial class Fish : CharacterBody3D
{
public Vector3 Forward = Vector3.Right;
public float DirectionDuration = 5.0f;
private readonly float _speed = Random.Shared.NextSingle() + 0.5f;
private const float Angle = 0.1f;
[Export] public Node3D WorldNode;
[Export] public bool IsCameraFish;
private Label3D _labelForward;
private Label3D _labelDistance;
private Label3D _labelCurrentTime;
private float _currentTime;
public override void _Ready()
{
base._Ready();
_currentTime = DirectionDuration;
_labelForward = GetNode<Label3D>("./LabelForward");
_labelDistance = GetNode<Label3D>("./LabelDistance");
_labelCurrentTime = GetNode<Label3D>("./LabelCurrentTime");
if (!IsCameraFish)
{
_labelForward.Hide();
_labelDistance.Hide();
_labelCurrentTime.Hide();
}
var planetCentre = WorldNode.GlobalPosition;
var planetVector = (GlobalPosition - planetCentre);
LookAt((GlobalPosition + Forward), planetVector);
}
public override void _PhysicsProcess(double delta)
{
var planetCentre = WorldNode.GlobalPosition;
var planetVector = (GlobalPosition - planetCentre);
_currentTime -= (float)delta;
if (_currentTime < 0.0f)
{
_currentTime = DirectionDuration;
Forward = Forward.Rotated(planetVector.Normalized(), Random.Shared.NextSingle() * Mathf.Tau);
}
var rotationAxis = planetVector.Cross(Forward).Normalized();
var newVelocity = planetVector.Rotated(rotationAxis, Angle) * _speed;
var offset = newVelocity * (float)delta;
var newPos = (GlobalPosition + offset);
var posOnCircle = ((newPos - planetCentre).Normalized() * planetVector.Length()) + planetCentre;
var correctedOffset = posOnCircle - GlobalPosition;
var newPlanetVector = posOnCircle - planetCentre;
UpDirection = newPlanetVector;
Forward = newPlanetVector.Cross(Forward).Cross(newPlanetVector).Normalized();
LookAt((GlobalPosition + Forward), planetVector);
if (IsCameraFish)
{
_labelCurrentTime.Text = $"{_currentTime:F}";
_labelForward.Text = ForwardLabelText(ref Forward);
_labelDistance.Text = $"{newPlanetVector.Length():F}";
}
MoveAndCollide(correctedOffset);
}
private static string ForwardLabelText(ref Vector3 vec)
=> $"({vec.X:F}, {vec.Y:F}, {vec.Z:F})";
}