From 15fd6234fbfc96f1c99d569c197702cb5a0bf95d Mon Sep 17 00:00:00 2001
From: DominicKarma <51094361+DominicKarma@users.noreply.github.com>
Date: Sun, 6 Mar 2022 14:23:58 -0600
Subject: [PATCH 1/2] Hyper-precise predictiveness utility
---
Utilities/ProjectileUtils.cs | 37 ++++++++++++++++++++++++++++++++++++
1 file changed, 37 insertions(+)
diff --git a/Utilities/ProjectileUtils.cs b/Utilities/ProjectileUtils.cs
index c7aed48b2b..53dabb6d09 100644
--- a/Utilities/ProjectileUtils.cs
+++ b/Utilities/ProjectileUtils.cs
@@ -42,6 +42,43 @@ namespace CalamityMod
return null;
}
+ // NOTE - Do not under any circumstance use these predictive methods for enemies or bosses. It is intended for minions and player-created projectiles.
+ // Due to its extremely precise nature, it will have little openings that allow players to react without dashing through the enemy.
+ // The results will be neither fun nor fair.
+
+ ///
+ /// Calculates a velocity that approximately predicts where some target will be in the future based on Euler's Method.
+ ///
+ /// The starting position from where the movement is calculated.
+ /// The position of the target to hit.
+ /// The velocity of the target to hit.
+ /// The speed of the predictive velocity.
+ /// The number of iterations to perform. The more iterations, the more precise the results are.
+ public static Vector2 CalculatePredictiveAimToTarget(Vector2 startingPosition, Vector2 targetPosition, Vector2 targetVelocity, float shootSpeed, int iterations = 4)
+ {
+ float previousTimeToReachDestination = 0f;
+ Vector2 currentTargetPosition = targetPosition;
+ for (int i = 0; i < iterations; i++)
+ {
+ float timeToReachDestination = Vector2.Distance(startingPosition, currentTargetPosition) / shootSpeed;
+ currentTargetPosition += targetVelocity * (timeToReachDestination - previousTimeToReachDestination);
+ previousTimeToReachDestination = timeToReachDestination;
+ }
+ return (currentTargetPosition - startingPosition).SafeNormalize(Vector2.UnitY) * shootSpeed;
+ }
+
+ ///
+ /// Calculates a velocity that approximately predicts where some target will be in the future based on Euler's Method.
+ ///
+ /// The starting position from where the movement is calculated.
+ /// The target to hit.
+ /// The speed of the predictive velocity.
+ /// The number of iterations to perform. The more iterations, the more precise the results are.
+ public static Vector2 CalculatePredictiveAimToTarget(Vector2 startingPosition, Entity target, float shootSpeed, int iterations = 4)
+ {
+ return CalculatePredictiveAimToTarget(startingPosition, target.Center, target.velocity, shootSpeed, iterations);
+ }
+
public static void KillAllHostileProjectiles()
{
for (int x = 0; x < Main.maxProjectiles; x++)
From 24b102c54442a4bfecfb889d526cacfef7870510 Mon Sep 17 00:00:00 2001
From: DominicKarma <51094361+DominicKarma@users.noreply.github.com>
Date: Sun, 6 Mar 2022 14:37:20 -0600
Subject: [PATCH 2/2] Superhoming utility
---
Utilities/ProjectileUtils.cs | 13 +++++++++++++
1 file changed, 13 insertions(+)
diff --git a/Utilities/ProjectileUtils.cs b/Utilities/ProjectileUtils.cs
index 53dabb6d09..b84b6418ee 100644
--- a/Utilities/ProjectileUtils.cs
+++ b/Utilities/ProjectileUtils.cs
@@ -79,6 +79,19 @@ namespace CalamityMod
return CalculatePredictiveAimToTarget(startingPosition, target.Center, target.velocity, shootSpeed, iterations);
}
+ ///
+ /// Makes a projectile home in such a way that it attempts to fractionally move towards a target's expected future position.
+ /// This is based on the results of the method.
+ ///
+ /// The projectile that should home.
+ /// The target.
+ /// The inertia of the movement change.
+ public static Vector2 SuperhomeTowardsTarget(this Projectile projectile, NPC target, float homingSpeed, float inertia)
+ {
+ Vector2 idealVelocity = CalculatePredictiveAimToTarget(projectile.Center, target, homingSpeed);
+ return (projectile.velocity * (inertia - 1f) + idealVelocity) / inertia;
+ }
+
public static void KillAllHostileProjectiles()
{
for (int x = 0; x < Main.maxProjectiles; x++)