Edit of asset "Ballistic Solutions (GDScript, C#)" Accepted

Old/Current New/Edit
Title Ballistic Solutions (GDScript, C#) BDC - Ballistic Deflection Calculator (GDScript, C#)
Description Library for calculating interception times, impact positions, and firing vectors, taking into account the velocities and accelerations of both projectile and target.

Quickstart
1. Install the addon in Godot or reference the DLL in your C# project.
2. In your scene, compute the vector to the target:
`var to_target = target.global_position - global_position`
3. Call `best_firing_velocity` to get initial projectile velocity.
4. Instantiate your projectile and assign velocity and acceleration.

Warning
Godot Physics Consideration:
Godot applies linear damping to physics bodies by default, which gradually reduces object velocity. This can significantly affect ballistic accuracy if not properly accounted for.

Recommendations:
- Set default_linear_damp = 0 in the project settings, if you want pure projectile motion
- Test thoroughly with your specific physics settings
Ballistic deflection calculator is a tool for calculating the shot vector considering speeds and accelerations for Godot.

Methods:
GDScipt:
Bdc

# Times To Hit
Array[float] times_to_hit(projectile_speed: float, to_target: Variant, target_velocity: Variant = Vector4.ZERO, projectile_acceleration: Variant = Vector4.ZERO, target_acceleration: Variant = Vector4.ZERO) static
Array[float] times_to_hit_vector2(projectile_speed: float, to_target: Vector2, target_velocity: Vector2 = Vector2.ZERO, projectile_acceleration: Vector2 = Vector2.ZERO, target_acceleration: Vector2 = Vector2.ZERO) static
Array[float] times_to_hit_vector3(projectile_speed: float, to_target: Vector3, target_velocity: Vector3 = Vector3.ZERO, projectile_acceleration: Vector3 = Vector3.ZERO, target_acceleration: Vector3 = Vector3.ZERO) static
Array[float] times_to_hit_vector4(projectile_speed: float, to_target: Vector4, target_velocity: Vector4 = Vector4.ZERO, projectile_acceleration: Vector4 = Vector4.ZERO, target_acceleration: Vector4 = Vector4.ZERO) static

# Velocities
Array velocities(projectile_speed: float, to_target: Variant, target_velocity: Variant = Vector4.ZERO, projectile_acceleration: Variant = Vector4.ZERO, target_acceleration: Variant = Vector4.ZERO) static
Array[Vector2] velocities_vector2(projectile_speed: float, to_target: Vector2, target_velocity: Vector2 = Vector2.ZERO, projectile_acceleration: Vector2 = Vector2.ZERO, target_acceleration: Vector2 = Vector2.ZERO) static
Array[Vector3] velocities_vector3(projectile_speed: float, to_target: Vector3, target_velocity: Vector3 = Vector3.ZERO, projectile_acceleration: Vector3 = Vector3.ZERO, target_acceleration: Vector3 = Vector3.ZERO) static
Array[Vector4] velocities_vector4(projectile_speed: float, to_target: Vector4, target_velocity: Vector4 = Vector4.ZERO, projectile_acceleration: Vector4 = Vector4.ZERO, target_acceleration: Vector4 = Vector4.ZERO) static

# Velocity From Time To Hit
Variant velocity_from_time_to_hit(time_to_hit: float, to_target: Variant, target_velocity: Variant = Vector4.ZERO, projectile_acceleration: Variant = Vector4.ZERO, target_acceleration: Variant = Vector4.ZERO) static
Vector2 velocity_from_time_to_hit_vector2(time_to_hit: float, to_target: Vector2, target_velocity: Vector2 = Vector2.ZERO, projectile_acceleration: Vector2 = Vector2.ZERO, target_acceleration: Vector2 = Vector2.ZERO) static
Vector3 velocity_from_time_to_hit_vector3(time_to_hit: float, to_target: Vector3, target_velocity: Vector3 = Vector3.ZERO, projectile_acceleration: Vector3 = Vector3.ZERO, target_acceleration: Vector3 = Vector3.ZERO) static
Vector4 velocity_from_time_to_hit_vector4(time_to_hit: float, to_target: Vector4, target_velocity: Vector4 = Vector4.ZERO, projectile_acceleration: Vector4 = Vector4.ZERO, target_acceleration: Vector4 = Vector4.ZERO) static


C#:
BallisticDeflectionCalculator.Bdc

// Times To Hit
T[] TimesToHit<T>(T projectileSpeed, Vector2 toTarget, Vector2 targetVelocity = default, Vector2 projectileAcceleration = default, Vector2 targetAcceleration = default) where T : IFloatingPoint<T>;
T[] TimesToHit<T>(T projectileSpeed, Vector3 toTarget, Vector3 targetVelocity = default, Vector3 projectileAcceleration = default, Vector3 targetAcceleration = default) where T : IFloatingPoint<T>;
T[] TimesToHit<T>(T projectileSpeed, Vector4 toTarget, Vector4 targetVelocity = default, Vector4 projectileAcceleration = default, Vector4 targetAcceleration = default) where T : IFloatingPoint<T>;

// Velocities
Vector2[] Velocities<T>(T projectileSpeed, Vector2 toTarget, Vector2 targetVelocity = default, Vector2 projectileAcceleration = default, Vector2 targetAcceleration = default) where T : IFloatingPoint<T>;
Vector3[] Velocities<T>(T projectileSpeed, Vector3 toTarget, Vector3 targetVelocity = default, Vector3 projectileAcceleration = default, Vector3 targetAcceleration = default) where T : IFloatingPoint<T>;
Vector4[] Velocities<T>(T projectileSpeed, Vector4 toTarget, Vector4 targetVelocity = default, Vector4 projectileAcceleration = default, Vector4 targetAcceleration = default) where T : IFloatingPoint<T>;

// Velocity From Time To Hit
Vector2 VelocityFromTimeToHit<T>(T timeToHit, Vector2 toTarget, Vector2 targetVelocity = default, Vector2 projectileAcceleration = default, Vector2 targetAcceleration = default) where T : IFloatingPoint<T>;
Vector3 VelocityFromTimeToHit<T>(T timeToHit, Vector3 toTarget, Vector3 targetVelocity = default, Vector3 projectileAcceleration = default, Vector3 targetAcceleration = default) where T : IFloatingPoint<T>;
Vector4 VelocityFromTimeToHit<T>(T timeToHit, Vector4 toTarget, Vector2 targetVelocity = default, Vector4 projectileAcceleration = default, Vector4 targetAcceleration = default) where T : IFloatingPoint<T>;

Example:
GDScipt:
@export var projectile_packed_scene: PackedScene

var projectile_speed: float = 200
var projectile_acceleration: Vector2 = Vector2.ZERO

func shoot(target: Target2D) -> void:
var to_target: Vector2 = target.global_position - global_position
var projectile_velocities: Array[Vector2] = Bdc.velocities_vector2(bullet_speed, to_target, target.velocity, projectile_acceleration, target.acceleration)

if projectile_velocities.size() == 0:
print("Impossible to hit the target")
return

var new_projectile: Projectile2D = projectile_packed_scene.instantiate()
new_projectile.global_position = global_position
new_projectile.velocity = bullet_velocities[0]
new_projectile.acceleration = projectile_acceleration
get_parent().add_child(new_projectile)


C#:
using Godot;
using BallisticDeflectionCalculator;

// ...

[Export]
public PackedScene? ProjectilePackedScene { get; set; }

public float ProjectileSpeed { get; set; } = 200;
public Vector2 ProjectileAcceleration { get; set; } = Vector2.Zero;

public void Shoot(Target2D target) {
Vector2 toTarget = target.GlobalPosition - GlobalPosition;
Vector2[] projectileVelocities = Bdc.Velocities(ProjectileSpeed, toTarget, target.Velocity, ProjectileAcceleration, target.Acceleration);

if (projectileVelocities.Length == 0) {
GD.Print("Impossible to hit the target");
return;
}

Projectile2D newProjectile = ProjectilePackedScene.Instantiate<Projectile2D>();
newProjectile.GlobalPosition = GlobalPosition;
newProjectile.Velocity = projectileVelocities[0];
newProjectile.Acceleration = ProjectileAcceleration;
GetParent().AddChild(newProjectile);
}
Category Scripts
License MIT
Repository Provider GitHub
Repository Url https://github.com/neclor/ballistic-solutions
Issues Url https://github.com/neclor/ballistic-solutions/issues
Godot version Godot 4.0
Version String 4.0.1 3.0.0
Download Commit ede88e056e566e707deb9a57eacb84628021aebb 5b32f69f5020253c93bc7ad0b50d7856db6ffb2a
Download Url (Computed) https://github.com/neclor/ballistic-solutions/archive/ede88e056e566e707deb9a57eacb84628021aebb.zip https://github.com/neclor/ballistic-solutions/archive/5b32f69f5020253c93bc7ad0b50d7856db6ffb2a.zip
Icon Url https://raw.githubusercontent.com/neclor/ballistic-solutions/main/icon.png