Skip to main content

Numerical launch-angle solver for moving targets under gravity and quadratic drag.

Project description

banner

CI Release Native + PyPI PyPI License

ballistic-solver is a native C/C++ numerical solver that computes launch angles to intercept moving targets under gravity and quadratic air drag, with optional wind.

Unlike vacuum / closed-form solvers, this project simulates the projectile and solves the intercept numerically, aiming for robust real-time use even when trajectories are strongly curved.


Quick start

Python (PyPI)

pip install ballistic-solver

Requires Python >= 3.10.

import ballistic_solver as bs

result = bs.solve(
    relPos0=(120, 30, 5),
    relVel=(2, -1, 0),
    v0=90,
    kDrag=0.002,
)

print(result["theta"], result["phi"], result["miss"])
print(result["success"], result["status"], result["message"])

Demo (Unity)

Highly curved trajectories under strong air drag, still converging to a hit against moving targets.

https://github.com/user-attachments/assets/c0c69cdd-0dd4-4606-9c7d-f21dd002d7f7


Why this solver

Many launch-angle solvers depend on vacuum assumptions or partially linearized models. This project instead simulates the projectile and solves the intercept numerically, targeting robustness in real-time simulations and integration scenarios.


Key properties

  • Moving targets supported
  • Strong air resistance (quadratic drag) supported
  • Low / High arc selection (since v0.2)
  • Wind vector supported (since v0.3)
  • Extended C ABI utilities (since v0.4)
  • Robust in strongly nonlinear regimes (no analytic assumptions)
  • Best-effort result returned even without perfect convergence
  • Explicit success / failure reporting (+ diagnostic message)
  • Stable C ABI for multi-language use
  • Header-only C++ core
  • Easy install via PyPI: pip install ballistic-solver

Python API

solve(...)

solve(relPos0, relVel, v0, kDrag, arcMode=0, params=None) -> dict
  • relPos0: target relative position at t=0 (x,y,z)
  • relVel: target relative velocity (x,y,z)
  • v0: muzzle speed (scalar)
  • kDrag: quadratic drag coefficient
  • arcMode: 0/1 or "low"/"high" (case-insensitive)
  • params: optional BallisticParams for advanced tuning (gravity, wind, integrator and solver knobs)

Returned dict keys include:

  • success (bool)
  • theta, phi (radians)
  • miss (closest-approach distance)
  • tStar (time of closest approach)
  • relMissAtStar (3-vector miss at tStar)
  • status (SolveStatus integer)
  • message (short diagnostic string)
  • plus convergence diagnostics (iterations, acceptedSteps, lastLambda, lastAlpha)

Solver validity note

The solver internally integrates projectile motion using:

  • 4th-order Runge–Kutta (RK4)
  • Fixed timestep dt
  • Quadratic drag
  • Wind as air velocity

To match in-game ballistics, your runtime simulation must use the same physical model and integrator configuration.

If your game uses a different integrator (e.g., Euler) or a different timestep, the computed launch angles may not hit even if the solver reports success.


C ABI (stable interface)

Primary intercept API:

void ballistic_inputs_init(BallisticInputs* in);
int32_t ballistic_solve(const BallisticInputs* in, BallisticOutputs* out);

Since v0.4.0, additional utility functions are available:

void ballistic_rk4_step(...);

int32_t ballistic_simulate_trajectory(...);
int32_t ballistic_simulate_trajectory_from_angles(...);

int32_t ballistic_find_closest_approach(...);

int32_t ballistic_vacuum_arc_angles_to_point(...);
void ballistic_initial_guess_vacuum_lead(...);

See ballistic_solver_c_api.h for full signatures and parameter definitions.

This enables usage from:

  • C / C++
  • Python (ctypes via the C ABI)
  • C# / .NET / Unity (P/Invoke)
  • Others via FFI

Prebuilt native binaries are provided via GitHub Releases.


Arc mode (since v0.2)

C ABI convention:

  • arcMode = 0 → Low
  • arcMode = 1 → High

High arc example:

https://github.com/user-attachments/assets/4334ed87-597e-4ad4-b21e-c1a1a17e8cd8


Wind (since v0.3)

C ABI convention:

  • wind[3] = air velocity vector (same frame as relPos0/relVel)
  • Drag uses relative airspeed: v_rel = v_projectile - wind

Wind demo:

https://github.com/user-attachments/assets/1cd998cf-34db-4a74-8817-c6393227ef4e


Using prebuilt binaries (C ABI)

Download the archive for your platform from Releases.

Each release contains:

  • Shared library

    • Windows: ballistic_solver.dll
    • Linux: libballistic_solver.so
    • macOS: libballistic_solver.dylib
  • C ABI header: ballistic_solver_c_api.h


C# / Unity usage

A C# P/Invoke example is available in:

examples/dotnet/

On Windows, place ballistic_solver.dll next to the executable (or ensure it is discoverable via PATH), then call ballistic_solve via DllImport.

This works directly inside Unity.


How it works (high level)

  1. Simulate projectile motion using RK4 integration with drag (+ wind)
  2. Track the closest approach between projectile and target
  3. Express the miss at closest approach as an angular residual
  4. Solve the nonlinear system using damped least squares (Levenberg–Marquardt)
  5. Accelerate Jacobian updates with Broyden-style refinement
  6. Return the best solution found

Failure cases are explicitly detected and reported.


Status codes (SolveStatus)

BallisticOutputs.status / Python result["status"] corresponds to:

  • 0 = Ok
  • 1 = InvalidInput
  • 2 = InitialResidualFailed
  • 3 = JacobianFailed
  • 4 = LMStepSingular
  • 5 = ResidualFailedDuringSearch
  • 6 = LineSearchRejected
  • 7 = LambdaTriesExhausted
  • 8 = MaxIterReached

message contains a short diagnostic string.


Build from source

cmake -S . -B build
cmake --build build -j
ctest --test-dir build

The shared library target is ballistic_solver.


ABI notes

  • Plain C layout across the ABI boundary
  • Fixed-size arrays only
  • No dynamic allocation across the boundary

License

MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ballistic_solver-0.4.0.tar.gz (25.5 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

ballistic_solver-0.4.0-cp312-cp312-win_amd64.whl (112.3 kB view details)

Uploaded CPython 3.12Windows x86-64

ballistic_solver-0.4.0-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (130.0 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

ballistic_solver-0.4.0-cp312-cp312-macosx_10_13_universal2.whl (211.7 kB view details)

Uploaded CPython 3.12macOS 10.13+ universal2 (ARM64, x86-64)

ballistic_solver-0.4.0-cp311-cp311-win_amd64.whl (111.2 kB view details)

Uploaded CPython 3.11Windows x86-64

ballistic_solver-0.4.0-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (127.7 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

ballistic_solver-0.4.0-cp311-cp311-macosx_10_9_universal2.whl (210.3 kB view details)

Uploaded CPython 3.11macOS 10.9+ universal2 (ARM64, x86-64)

ballistic_solver-0.4.0-cp310-cp310-win_amd64.whl (110.4 kB view details)

Uploaded CPython 3.10Windows x86-64

ballistic_solver-0.4.0-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (126.6 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

ballistic_solver-0.4.0-cp310-cp310-macosx_10_9_universal2.whl (207.7 kB view details)

Uploaded CPython 3.10macOS 10.9+ universal2 (ARM64, x86-64)

File details

Details for the file ballistic_solver-0.4.0.tar.gz.

File metadata

  • Download URL: ballistic_solver-0.4.0.tar.gz
  • Upload date:
  • Size: 25.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for ballistic_solver-0.4.0.tar.gz
Algorithm Hash digest
SHA256 ba1387b35aab5d3b28b20a6b7f9c85a90b98356b623bee36f152edda812adb92
MD5 733c5c32d791f1d9773a21df7b1f25d2
BLAKE2b-256 30eeaf07a170975047749f7f8ea54256de54d1e8e9717d77033465369698f604

See more details on using hashes here.

Provenance

The following attestation bundles were made for ballistic_solver-0.4.0.tar.gz:

Publisher: release.yml on ujinf74/ballistic-solver

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ballistic_solver-0.4.0-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for ballistic_solver-0.4.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 37794824eb0792ef42fcdc565730b5d6d6867edb9a5f92633abdeb5d8ef4e17e
MD5 b2fec99803e97609a644ed0280ac17cf
BLAKE2b-256 f11723b1f46785e71d8846a0a5d9a3c1dc6c400d447d181a3d417d6cfd56747a

See more details on using hashes here.

Provenance

The following attestation bundles were made for ballistic_solver-0.4.0-cp312-cp312-win_amd64.whl:

Publisher: release.yml on ujinf74/ballistic-solver

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ballistic_solver-0.4.0-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for ballistic_solver-0.4.0-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 949bb584a9014b630d4925c773c32aa7680912d99ec7f027caeddb542d1b3730
MD5 f5a435c82cdfe70f7e672f77714736dc
BLAKE2b-256 800b69eb6574a6bd6b7786ebbbcd552ab92ddb76fa4d99bd496ad692582b32c8

See more details on using hashes here.

Provenance

The following attestation bundles were made for ballistic_solver-0.4.0-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl:

Publisher: release.yml on ujinf74/ballistic-solver

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ballistic_solver-0.4.0-cp312-cp312-macosx_10_13_universal2.whl.

File metadata

File hashes

Hashes for ballistic_solver-0.4.0-cp312-cp312-macosx_10_13_universal2.whl
Algorithm Hash digest
SHA256 2e8b0ed9a92a263da3f526e2f37304b97db44b7669265c27b1c0d7d9c09d0660
MD5 85a6d1867f93d92816af5d9669345867
BLAKE2b-256 a8a1dbd93373ba9091fde4ef7e25c805d97ecd2354b5e020a2788eb8f8b9b92d

See more details on using hashes here.

Provenance

The following attestation bundles were made for ballistic_solver-0.4.0-cp312-cp312-macosx_10_13_universal2.whl:

Publisher: release.yml on ujinf74/ballistic-solver

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ballistic_solver-0.4.0-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for ballistic_solver-0.4.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 4bbd83d2c0179bbe28302aac3621adc43746a5e6b11b7e8ebbdc236123b3adaa
MD5 f89d69492664d32e8b5d1a4cb7d3ecc7
BLAKE2b-256 ba7fd904b03e7092ff93e95eb1e82d7a10fa26284a21be4aa5c791fcedfc5a89

See more details on using hashes here.

Provenance

The following attestation bundles were made for ballistic_solver-0.4.0-cp311-cp311-win_amd64.whl:

Publisher: release.yml on ujinf74/ballistic-solver

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ballistic_solver-0.4.0-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for ballistic_solver-0.4.0-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 b533b4b93208de1667721211b91632d0f27f1ee47334e448dfacac095e7fb1e9
MD5 4fba44a57f638fff3d14ec27192e51c6
BLAKE2b-256 d15deb17a111198d8721316ba2289aa54be402fc5a2a32ce2223f92099ca3d89

See more details on using hashes here.

Provenance

The following attestation bundles were made for ballistic_solver-0.4.0-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl:

Publisher: release.yml on ujinf74/ballistic-solver

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ballistic_solver-0.4.0-cp311-cp311-macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for ballistic_solver-0.4.0-cp311-cp311-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 9d44597d23ca580a085c2cbe8587933bc9c82cb4781897421f9e5e2ba99781dc
MD5 5768eeca8be4b02bd157ca66b78d5210
BLAKE2b-256 e493edcd7bac67441f59824438590a3f7d3c355d18b0bc7b244f67e473d3e89d

See more details on using hashes here.

Provenance

The following attestation bundles were made for ballistic_solver-0.4.0-cp311-cp311-macosx_10_9_universal2.whl:

Publisher: release.yml on ujinf74/ballistic-solver

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ballistic_solver-0.4.0-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for ballistic_solver-0.4.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 04a09dce962eb0c39b924561b37d8b9ec125fd2ca88eb74b53d953537b56c033
MD5 f70b3dde4fc99617e41b335c5d8d6394
BLAKE2b-256 92c08c28b6d0cdec13acae30cdc27dc06f2076e07523fffa0d35e6f1f20924e9

See more details on using hashes here.

Provenance

The following attestation bundles were made for ballistic_solver-0.4.0-cp310-cp310-win_amd64.whl:

Publisher: release.yml on ujinf74/ballistic-solver

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ballistic_solver-0.4.0-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for ballistic_solver-0.4.0-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 88c0f6b416bd9b275f2def1ddd67334fa2a5c724e50a94fefc6573510a4f4682
MD5 f209fba4cadd4f12c789d0052a09ecd6
BLAKE2b-256 8e9667f762a4aaf6989954e801a9f2950821646264e916014a847bc7cfeecbbd

See more details on using hashes here.

Provenance

The following attestation bundles were made for ballistic_solver-0.4.0-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl:

Publisher: release.yml on ujinf74/ballistic-solver

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ballistic_solver-0.4.0-cp310-cp310-macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for ballistic_solver-0.4.0-cp310-cp310-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 5ab436d4faf291a77dc7993ba75e3f1cba468bf897078278d59684126e5ac0ea
MD5 02ca2a2a66134dcdfe4e47080082cf97
BLAKE2b-256 0404cdd9d1c968fa120de9f8cec3e106c74cfe02b48a7056532f5a9afd51d61f

See more details on using hashes here.

Provenance

The following attestation bundles were made for ballistic_solver-0.4.0-cp310-cp310-macosx_10_9_universal2.whl:

Publisher: release.yml on ujinf74/ballistic-solver

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page