added projectile targeting anticipation

This commit is contained in:
Lilith Schier 2026-06-21 23:27:10 +02:00
parent eea6830534
commit 7846f1a068
3 changed files with 68 additions and 24 deletions

View file

@ -1,3 +1,5 @@
using System;
namespace Dancer; namespace Dancer;
public static class Physics public static class Physics
@ -15,6 +17,49 @@ public static class Physics
{ {
return 0.5f * fluidMassDensity * area * dragCoefficient; return 0.5f * fluidMassDensity * area * dragCoefficient;
} }
public static AnticipationResult? AnticipateProjectile(
Vector2 projectilePosition,
float projectileSpeed,
Vector2 targetPosition,
Vector2 targetVelocity
)
{
var difference = targetPosition - projectilePosition;
var distanceSquared = difference.LengthSquared();
if (distanceSquared < 0.001) return new(0, targetPosition);
var radialVelocitySquared = difference.Dot(targetVelocity);
var projectileSpeedSquared = projectileSpeed * projectileSpeed;
var targetSpeedSquared = targetVelocity.LengthSquared();
var a = targetSpeedSquared - projectileSpeedSquared;
float t;
if (Mathf.Abs(a) < 0.001)
{
t = -distanceSquared / (2 * radialVelocitySquared);
}
else
{
var discriminant = radialVelocitySquared * radialVelocitySquared - a * distanceSquared;
if (discriminant < 0) return null;
var t0 = (-radialVelocitySquared + Mathf.Sqrt(discriminant)) / a;
var t1 = (-radialVelocitySquared - Mathf.Sqrt(discriminant)) / a;
t = (t0, t1) switch
{
(< 0f, _) => t1,
(_, < 0f) => t0,
_ => Mathf.Min(t0, t1),
};
}
if (t < 0f) return null;
return new(t, TargetPositionAt(t));
Vector2 TargetPositionAt(float time) => targetPosition + targetVelocity * time;
}
public sealed record AnticipationResult(float Time, Vector2 Position);
} }
public static class Extensions public static class Extensions

View file

@ -36,7 +36,7 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
public float WaterAccel { get; set; } = 1200.0f; public float WaterAccel { get; set; } = 1200.0f;
[Export(PropertyHint.None, "suffix:s")] [Export(PropertyHint.None, "suffix:s")]
public float JumpAscentMaxTime { get; set; } = 0.2f; public float JumpAscentMaxTime { get; set; } = 0.25f;
[Export(PropertyHint.None, "suffix:px/s")] [Export(PropertyHint.None, "suffix:px/s")]
public float JumpAscentSpeed { get; set; } = 200.0f; public float JumpAscentSpeed { get; set; } = 200.0f;
@ -62,9 +62,9 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
[Export] public TileMapLayer WaterTileMap { get; set; } = null!; [Export] public TileMapLayer WaterTileMap { get; set; } = null!;
private ulong MaxInputBufferTimeMicroseconds => (ulong)(MaxInputBufferTime * 1_000_000); private ulong MaxInputBufferTimeMicroseconds => (ulong)(MaxInputBufferTime * 1_000_000);
private float _jumpAscentRemainingTime; public float JumpAscentRemainingTime { get; private set; }
private bool _isJumping; public bool IsJumping { get; private set; }
private float _jumpAllowedTime; public float JumpAllowedTime { get; private set; }
private Vector2 _currentDirection = Vector2.Zero; private Vector2 _currentDirection = Vector2.Zero;
private bool _isFacingRight = true; private bool _isFacingRight = true;
private AnimationNodeStateMachinePlayback _playback = null!; private AnimationNodeStateMachinePlayback _playback = null!;
@ -150,12 +150,12 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
} }
// Move up while holding jump // Move up while holding jump
if (_jumpAscentRemainingTime > 0) if (JumpAscentRemainingTime > 0)
{ {
velocity.Y = -JumpAscentSpeed; velocity.Y = -JumpAscentSpeed;
_jumpAscentRemainingTime -= deltaF; JumpAscentRemainingTime -= deltaF;
if (_jumpAscentRemainingTime < 0) if (JumpAscentRemainingTime < 0)
velocity += GetGravity() * -_jumpAscentRemainingTime; velocity += GetGravity() * -JumpAscentRemainingTime;
} }
else if (!IsOnFloor() && !_isInWater) else if (!IsOnFloor() && !_isInWater)
{ {
@ -168,7 +168,7 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
velocity += Physics.DragForce(-velocity, 0.02f) * deltaF; velocity += Physics.DragForce(-velocity, 0.02f) * deltaF;
} }
// Drag while in air // Drag while in air
else if (!IsOnFloor() && _jumpAscentRemainingTime <= 0) else if (!IsOnFloor() && JumpAscentRemainingTime <= 0)
{ {
velocity += Physics.DragForce(-velocity, 0.009f) * deltaF; velocity += Physics.DragForce(-velocity, 0.009f) * deltaF;
} }
@ -191,14 +191,14 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
} }
MoveAndSlide(); MoveAndSlide();
_jumpAllowedTime = Mathf.Max(0, _jumpAllowedTime - deltaF); JumpAllowedTime = Mathf.Max(0, JumpAllowedTime - deltaF);
if (IsOnFloor()) if (IsOnFloor())
_jumpAllowedTime = LateJumpTimeLimit; JumpAllowedTime = LateJumpTimeLimit;
if (IsOnCeiling()) if (IsOnCeiling())
{ {
_isJumping = false; IsJumping = false;
_jumpAscentRemainingTime = 0.0f; JumpAscentRemainingTime = 0.0f;
} }
} }
@ -266,7 +266,7 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
} }
else if ( else if (
input.Event.IsPressed() input.Event.IsPressed()
&& _jumpAllowedTime > 0 && JumpAllowedTime > 0
&& (!IsOnFloor() || !IsInputVecDown(input.Direction))) && (!IsOnFloor() || !IsInputVecDown(input.Direction)))
{ {
inputFound = true; inputFound = true;
@ -277,7 +277,7 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
inputFound = true; inputFound = true;
WallJump(); WallJump();
} }
else if (input.Event.IsReleased() && _isJumping) else if (input.Event.IsReleased() && IsJumping)
{ {
JumpCut(); JumpCut();
} }
@ -304,23 +304,23 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
private void GroundJump() private void GroundJump()
{ {
AdjustFacing(); AdjustFacing();
_jumpAllowedTime = 0.0f; JumpAllowedTime = 0.0f;
_jumpAscentRemainingTime = JumpAscentMaxTime; JumpAscentRemainingTime = JumpAscentMaxTime;
_isJumping = true; IsJumping = true;
} }
private void WallJump() private void WallJump()
{ {
AdjustFacing(); AdjustFacing();
_jumpAllowedTime = 0.0f; JumpAllowedTime = 0.0f;
Velocity += GetWallNormal() * 200.0f; Velocity += GetWallNormal() * 200.0f;
Velocity = Velocity with { Y = Mathf.Min(0, Velocity.Y) }; Velocity = Velocity with { Y = Mathf.Min(0, Velocity.Y) };
} }
private void JumpCut() private void JumpCut()
{ {
_isJumping = false; IsJumping = false;
_jumpAscentRemainingTime = 0.0f; JumpAscentRemainingTime = 0.0f;
// Jump cut only once, if we're still moving up // Jump cut only once, if we're still moving up
if (Velocity.Y < 0) if (Velocity.Y < 0)
Velocity = Velocity with { Y = Velocity.Y * 0.1f }; Velocity = Velocity with { Y = Velocity.Y * 0.1f };

View file

@ -6,10 +6,10 @@
[ext_resource type="Script" uid="uid://b4k42r866lkut" path="res://src/combat/Hitbox.cs" id="4_244u8"] [ext_resource type="Script" uid="uid://b4k42r866lkut" path="res://src/combat/Hitbox.cs" id="4_244u8"]
[sub_resource type="RectangleShape2D" id="RectangleShape2D_jiejy"] [sub_resource type="RectangleShape2D" id="RectangleShape2D_jiejy"]
size = Vector2(10, 28) size = Vector2(8, 28)
[sub_resource type="RectangleShape2D" id="RectangleShape2D_o822w"] [sub_resource type="RectangleShape2D" id="RectangleShape2D_o822w"]
size = Vector2(10, 12) size = Vector2(8, 12)
[sub_resource type="RectangleShape2D" id="RectangleShape2D_244u8"] [sub_resource type="RectangleShape2D" id="RectangleShape2D_244u8"]
size = Vector2(27.5, 20) size = Vector2(27.5, 20)
@ -302,7 +302,6 @@ debug_color = Color(0, 0.5282814, 0.99723786, 0.41960785)
position = Vector2(0, -16) position = Vector2(0, -16)
process_callback = 0 process_callback = 0
limit_enabled = false limit_enabled = false
position_smoothing_enabled = true
[node name="PointLight2D" type="PointLight2D" parent="." unique_id=1795955896] [node name="PointLight2D" type="PointLight2D" parent="." unique_id=1795955896]
position = Vector2(0, -16) position = Vector2(0, -16)