Added little pirate and better movement.

WorldChunkRefactoring
Martin Felis 2022-12-06 21:06:25 +01:00
parent 5209657cef
commit 91863845c5
7 changed files with 1147 additions and 17 deletions

Binary file not shown.

File diff suppressed because it is too large Load Diff

View File

@ -6,26 +6,70 @@ public class MovableComponent : Node
public Vector3 targetPosition = Vector3.Zero;
public Vector3 currentPosition = Vector3.Zero;
public Vector3 currentVelocity = Vector3.Zero;
public float targetAngle = 0;
public float currentAngle = 0;
public float currentAngularVelocity = 0;
[Export] public float maxSpeed = 3;
private SpringDamper _springDamper;
private SpringDamper _positionSpringDamper;
private SpringDamper _angleSpringDamper;
[Signal]
delegate void PositionUpdated(Vector3 newPosition);
[Signal]
delegate void OrientationUpdated(float newAngle);
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
_springDamper = new SpringDamper(4, 0.99f, 0.5f);
_positionSpringDamper = new SpringDamper(4, 0.99f, 0.5f);
_angleSpringDamper = new SpringDamper(4, 0.99f, 0.5f);
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(float delta)
{
if ((targetPosition - currentPosition).LengthSquared() > 0.01)
Vector3 targetDir = Vector3.Zero;
Vector3 targetError = targetPosition - currentPosition;
if (targetError.LengthSquared() > 0.01)
{
targetDir = targetError.Normalized();
targetAngle = new Vector3(0, 0, 1).SignedAngleTo(targetDir, Vector3.Up);
if (currentAngle < Mathf.Pi && targetAngle > Mathf.Pi)
{
}
if (targetAngle - currentAngle > Mathf.Pi)
{
currentAngle += 2 * Mathf.Pi;
}
else if (targetAngle - currentAngle < -Mathf.Pi)
{
currentAngle -= 2 * Mathf.Pi;
}
}
if (Mathf.Abs(currentAngularVelocity) > 0.1 || Mathf.Abs(targetAngle - currentAngle) > 0.01)
{
var springDamperResult = _angleSpringDamper.Calc(currentAngle, currentAngularVelocity, targetAngle, delta);
currentAngle = springDamperResult.Item1;
currentAngularVelocity = springDamperResult.Item2;
EmitSignal("OrientationUpdated", this.currentAngle);
}
if (( Mathf.Abs(currentAngularVelocity) < 5 && Mathf.Abs(targetAngle - currentAngle) < 0.3)
&& (targetPosition - currentPosition).LengthSquared() > 0.01)
{
var springDamperResult =
_springDamper.CalcClampedSpeed(currentPosition, currentVelocity, targetPosition, delta, maxSpeed);
_positionSpringDamper.CalcClampedSpeed(currentPosition, currentVelocity, targetPosition, delta, maxSpeed);
currentPosition = springDamperResult.Item1;
currentVelocity = springDamperResult.Item2;

View File

@ -4,6 +4,7 @@ using System;
public class Player : KinematicBody
{
private MovableComponent _movable;
private Spatial _geometry;
// Called when the node enters the scene tree for the first time.
public override void _Ready()
@ -12,7 +13,10 @@ public class Player : KinematicBody
if (_movable != null)
{
_movable.Connect("PositionUpdated", this, nameof(OnPositionUpdated));
_movable.Connect("OrientationUpdated", this, nameof(OnOrientationUpdated));
}
_geometry = (Spatial)FindNode("Geometry", false);
}
private void OnPositionUpdated(Vector3 newPosition)
@ -21,4 +25,9 @@ public class Player : KinematicBody
transform.origin = newPosition;
Transform = transform;
}
private void OnOrientationUpdated(float newOrientation)
{
_geometry.Transform = new Transform (new Quat(Vector3.Up, newOrientation), Vector3.Zero);
}
}

View File

@ -9,17 +9,17 @@
config_version=4
_global_script_classes=[ {
"base": "Node",
"base": "Reference",
"class": "ClickableComponent",
"language": "GDScript",
"path": "res://components/ClickableComponent.gd"
}, {
"base": "KinematicBody2D",
"base": "Reference",
"class": "CollisionLine",
"language": "GDScript",
"path": "res://utils/CollisionLine.gd"
}, {
"base": "Node",
"base": "Reference",
"class": "ColorComponent",
"language": "GDScript",
"path": "res://components/ColorComponent.gd"
@ -54,7 +54,7 @@ _global_script_classes=[ {
"language": "GDScript",
"path": "res://utils/SpringDamper.gd"
}, {
"base": "Sprite",
"base": "Reference",
"class": "TintedSpriteComponent",
"language": "GDScript",
"path": "res://components/TintedSpriteComponent.gd"

View File

@ -1,6 +1,7 @@
[gd_scene load_steps=13 format=2]
[gd_scene load_steps=14 format=2]
[ext_resource path="res://scenes/AdaptiveWorldStream.cs" type="Script" id=1]
[ext_resource path="res://assets/CreatusPiratePack/characters/Pirate1final.glb" type="PackedScene" id=2]
[ext_resource path="res://scenes/World.gd" type="Script" id=3]
[ext_resource path="res://scenes/StreamContainer.cs" type="Script" id=4]
[ext_resource path="res://entities/Player.cs" type="Script" id=5]
@ -211,7 +212,13 @@ script = ExtResource( 6 )
[node name="MeshInstance" type="MeshInstance" parent="Player"]
transform = Transform( 1, 0, 0, 0, -4.37114e-08, -1, 0, 1, -4.37114e-08, 0, 0.5, 0 )
visible = false
mesh = SubResource( 8 )
[node name="Movable" type="Node" parent="Player"]
script = ExtResource( 8 )
[node name="Geometry" type="Spatial" parent="Player"]
[node name="Mesh" parent="Player/Geometry" instance=ExtResource( 2 )]
transform = Transform( 0.3, 0, 0, 0, 0.3, 0, 0, 0, 0.3, 0, 0, 0 )

View File

@ -4,11 +4,8 @@ using Godot;
using System;
using System.Diagnostics;
public class SpringDamper : Node
public class SpringDamper
{
// Declare member variables here. Examples:
// private int a = 2;
// private string b = "text";
public float omega = 1;
public float zeta = 1;
@ -19,12 +16,20 @@ public class SpringDamper : Node
zeta = Mathf.Log(1.0f - osc_red) / (-omega * osc_red_h);
}
// Called when the node enters the scene tree for the first time.
public override void _Ready()
public (float, float) Calc(float x, float v, float xt, float h)
{
}
float f = 1 + 2 * h * zeta * omega;
float oo = omega * omega;
float hoo = oo * h;
float hhoo = hoo * h;
float det_inv = 1.0f / (f + hhoo);
float det_x = f * x + h * v + hhoo * xt;
float det_v = v + hoo * (xt - x);
return (det_x * det_inv, det_v * det_inv);
}
public (Vector3, Vector3) Calc(Vector3 x, Vector3 v, Vector3 xt, float h)
{
float f = 1 + 2 * h * zeta * omega;