SupaLidlGame/Tests/ContextBasedSteering.cs

59 lines
1.6 KiB
C#
Raw Normal View History

2022-11-12 16:45:04 -08:00
using Godot;
2022-11-27 19:37:16 -08:00
using SupaLidlGame.Extensions;
2022-11-12 16:45:04 -08:00
using System;
namespace SupaLidlGame.Prototyping
{
public partial class ContextBasedSteering : Node2D
{
2022-11-27 19:37:16 -08:00
public float PreferredDistance { get; set; } = 256.0f;
Vector2 _direction;
2022-11-12 16:45:04 -08:00
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
2022-11-27 19:37:16 -08:00
GD.Print("Started ContextBasedSteering test");
2022-11-12 16:45:04 -08:00
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{
2022-11-27 19:37:16 -08:00
_direction = GetDirection();
QueueRedraw();
}
2022-11-12 16:45:04 -08:00
2022-11-27 19:37:16 -08:00
public Vector2 GetDirection()
{
float directWeight;
float strafeWeight;
2022-11-12 16:45:04 -08:00
2022-11-27 19:37:16 -08:00
Vector2 towards = GetGlobalMousePosition() - GlobalPosition;
float dist = towards.Length();
2022-11-12 16:45:04 -08:00
2022-11-27 19:37:16 -08:00
Vector2 directDir = towards.Normalized();
Vector2 strafeDir = directDir.Clockwise90();
2022-11-12 16:45:04 -08:00
2022-11-27 19:37:16 -08:00
// weights approach 1
// dy/dx = 1 - y
// y = 1 - e^(-x)
2022-11-12 16:45:04 -08:00
2022-11-27 19:37:16 -08:00
directWeight = 1 - Mathf.Pow(Mathf.E, -(dist / PreferredDistance));
strafeWeight = 1 - directWeight;
2022-11-12 16:45:04 -08:00
2022-11-27 19:37:16 -08:00
/*
Vector2 midpoint = (strafeDir * strafeWeight)
.Midpoint(directDir * directWeight);
*/
Vector2 midpoint = (directDir * directWeight)
.Midpoint(strafeDir * strafeWeight);
2022-11-12 16:45:04 -08:00
2022-11-27 19:37:16 -08:00
return midpoint.Normalized();
2022-11-12 16:45:04 -08:00
}
public override void _Draw()
{
2022-11-27 19:37:16 -08:00
DrawLine(Vector2.Zero, _direction * 256, Colors.Green);
2022-11-12 16:45:04 -08:00
}
}
}