From 7846f1a068dc379bc49e7627ecf7b982f723ca16 Mon Sep 17 00:00:00 2001 From: Lilith Schier Date: Sun, 21 Jun 2026 23:27:10 +0200 Subject: [PATCH] added projectile targeting anticipation --- src/physics/Physics.cs | 45 ++++++++++++++++++++++++++++++++++++++++++ src/player/Player.cs | 42 +++++++++++++++++++-------------------- src/player/player.tscn | 5 ++--- 3 files changed, 68 insertions(+), 24 deletions(-) diff --git a/src/physics/Physics.cs b/src/physics/Physics.cs index 2054031..b907d10 100644 --- a/src/physics/Physics.cs +++ b/src/physics/Physics.cs @@ -1,3 +1,5 @@ +using System; + namespace Dancer; public static class Physics @@ -15,6 +17,49 @@ public static class Physics { 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 diff --git a/src/player/Player.cs b/src/player/Player.cs index 5cbdf35..39b4993 100644 --- a/src/player/Player.cs +++ b/src/player/Player.cs @@ -36,7 +36,7 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner, public float WaterAccel { get; set; } = 1200.0f; [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")] 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!; private ulong MaxInputBufferTimeMicroseconds => (ulong)(MaxInputBufferTime * 1_000_000); - private float _jumpAscentRemainingTime; - private bool _isJumping; - private float _jumpAllowedTime; + public float JumpAscentRemainingTime { get; private set; } + public bool IsJumping { get; private set; } + public float JumpAllowedTime { get; private set; } private Vector2 _currentDirection = Vector2.Zero; private bool _isFacingRight = true; private AnimationNodeStateMachinePlayback _playback = null!; @@ -150,12 +150,12 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner, } // Move up while holding jump - if (_jumpAscentRemainingTime > 0) + if (JumpAscentRemainingTime > 0) { velocity.Y = -JumpAscentSpeed; - _jumpAscentRemainingTime -= deltaF; - if (_jumpAscentRemainingTime < 0) - velocity += GetGravity() * -_jumpAscentRemainingTime; + JumpAscentRemainingTime -= deltaF; + if (JumpAscentRemainingTime < 0) + velocity += GetGravity() * -JumpAscentRemainingTime; } else if (!IsOnFloor() && !_isInWater) { @@ -168,7 +168,7 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner, velocity += Physics.DragForce(-velocity, 0.02f) * deltaF; } // Drag while in air - else if (!IsOnFloor() && _jumpAscentRemainingTime <= 0) + else if (!IsOnFloor() && JumpAscentRemainingTime <= 0) { velocity += Physics.DragForce(-velocity, 0.009f) * deltaF; } @@ -191,14 +191,14 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner, } MoveAndSlide(); - _jumpAllowedTime = Mathf.Max(0, _jumpAllowedTime - deltaF); + JumpAllowedTime = Mathf.Max(0, JumpAllowedTime - deltaF); if (IsOnFloor()) - _jumpAllowedTime = LateJumpTimeLimit; + JumpAllowedTime = LateJumpTimeLimit; if (IsOnCeiling()) { - _isJumping = false; - _jumpAscentRemainingTime = 0.0f; + IsJumping = false; + JumpAscentRemainingTime = 0.0f; } } @@ -266,7 +266,7 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner, } else if ( input.Event.IsPressed() - && _jumpAllowedTime > 0 + && JumpAllowedTime > 0 && (!IsOnFloor() || !IsInputVecDown(input.Direction))) { inputFound = true; @@ -277,7 +277,7 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner, inputFound = true; WallJump(); } - else if (input.Event.IsReleased() && _isJumping) + else if (input.Event.IsReleased() && IsJumping) { JumpCut(); } @@ -304,23 +304,23 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner, private void GroundJump() { AdjustFacing(); - _jumpAllowedTime = 0.0f; - _jumpAscentRemainingTime = JumpAscentMaxTime; - _isJumping = true; + JumpAllowedTime = 0.0f; + JumpAscentRemainingTime = JumpAscentMaxTime; + IsJumping = true; } private void WallJump() { AdjustFacing(); - _jumpAllowedTime = 0.0f; + JumpAllowedTime = 0.0f; Velocity += GetWallNormal() * 200.0f; Velocity = Velocity with { Y = Mathf.Min(0, Velocity.Y) }; } private void JumpCut() { - _isJumping = false; - _jumpAscentRemainingTime = 0.0f; + IsJumping = false; + JumpAscentRemainingTime = 0.0f; // Jump cut only once, if we're still moving up if (Velocity.Y < 0) Velocity = Velocity with { Y = Velocity.Y * 0.1f }; diff --git a/src/player/player.tscn b/src/player/player.tscn index 693982e..2952ab1 100644 --- a/src/player/player.tscn +++ b/src/player/player.tscn @@ -6,10 +6,10 @@ [ext_resource type="Script" uid="uid://b4k42r866lkut" path="res://src/combat/Hitbox.cs" id="4_244u8"] [sub_resource type="RectangleShape2D" id="RectangleShape2D_jiejy"] -size = Vector2(10, 28) +size = Vector2(8, 28) [sub_resource type="RectangleShape2D" id="RectangleShape2D_o822w"] -size = Vector2(10, 12) +size = Vector2(8, 12) [sub_resource type="RectangleShape2D" id="RectangleShape2D_244u8"] size = Vector2(27.5, 20) @@ -302,7 +302,6 @@ debug_color = Color(0, 0.5282814, 0.99723786, 0.41960785) position = Vector2(0, -16) process_callback = 0 limit_enabled = false -position_smoothing_enabled = true [node name="PointLight2D" type="PointLight2D" parent="." unique_id=1795955896] position = Vector2(0, -16)