homing projectiles

This commit is contained in:
2026-05-10 12:26:43 +02:00
parent 150e007b22
commit 0cd942d90e
5 changed files with 54 additions and 11 deletions

View File

@@ -68,18 +68,44 @@ public partial class Projectile : RigidBody3D, IForgeEntity, ITargetable, IKilla
[Node("ForgeEntityNode")] public required ForgeEntityNode ForgeEntity { get; set;}
[Export] public float Damage = 10f;
[Export] public float Speed = 10f;
[Export] public float HomingFactor = 0.2f;
public Node3D Target { get; set; }
public Node3D? Target { get; set; }
public void Init()
{
ApplyCentralImpulse(Vector3.Up * 10);
ApplyCentralImpulse(ComputeImpulseToTarget());
}
public Vector3 ComputeImpulseToTarget()
{
if (Target == null) return Vector3.Zero;
return Vector3.Up * Speed;
var targetDir = GlobalPosition.DirectionTo(Target.GlobalPosition);
var hDir = new Vector3(targetDir.X, 0, targetDir.Z).Normalized();
var throwDir = (hDir + 3*Vector3.Up).Normalized();
var distanceTo = GlobalPosition.DistanceTo(Target.GlobalPosition);
return throwDir * distanceTo*0.8f;
}
public override void _IntegrateForces(PhysicsDirectBodyState3D state)
{
var direction = GlobalPosition.DirectionTo(Target.GlobalPosition);
state.LinearVelocity += direction / 10.0f;
if (Target == null) return;
var targetPos = Target is ITargetable targetable ? targetable.GetTargetGlobalPosition() : Target.GlobalPosition;
var targetVel = state.LinearVelocity + GlobalPosition.DirectionTo(targetPos) * HomingFactor;
if (targetVel.Length() > Speed) targetVel = targetVel.Normalized() * Speed;
state.LinearVelocity = targetVel;
// var targetDir = GlobalPosition.DirectionTo(targetPos);
// var hDir = new Vector3(targetDir.X, 0, targetDir.Z).Normalized();
// var currentVelocityHDir = new Vector3(state.LinearVelocity.X, 0, state.LinearVelocity.Z);
// var finalHDir = hDir*HomingFactor + currentVelocityHDir;
// var combinedVelocity = new Vector3(finalHDir.X, state.LinearVelocity.Y, finalHDir.Z);
// if (combinedVelocity.Length() > Speed) combinedVelocity = combinedVelocity.Normalized() * Speed;
// state.LinearVelocity = combinedVelocity;
// state.LinearVelocity += targetDir/5.0f;
}
public void OnResolved()