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)
  • 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)

Advanced tuning: BallisticParams

Example (wind + high arc):

import ballistic_solver as bs

p = bs.BallisticParams()
p.g = 9.80665                # gravity
p.wind = (3.0, 0.0, 0.0)     # wind vector
p.dt = 0.01                  # RK4 step
p.tMax = 20.0                # max sim time
p.tolMiss = 1e-2             # hit tolerance
p.maxIter = 20               # LM iterations

result = bs.solve(
    relPos0=(120, 30, 5),
    relVel=(2, -1, 0),
    v0=90,
    kDrag=0.002,
    arcMode="high",
    params=p,
)
print(result["theta"], result["phi"], result["miss"])

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


C ABI (stable interface)

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

See ballistic_solver_c_api.h for BallisticInputs/Outputs definitions and defaults.

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.


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.3.3.tar.gz (21.0 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.3.3-cp312-cp312-win_amd64.whl (105.0 kB view details)

Uploaded CPython 3.12Windows x86-64

ballistic_solver-0.3.3-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (119.8 kB view details)

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

ballistic_solver-0.3.3-cp312-cp312-macosx_10_13_universal2.whl (192.8 kB view details)

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

ballistic_solver-0.3.3-cp311-cp311-win_amd64.whl (103.9 kB view details)

Uploaded CPython 3.11Windows x86-64

ballistic_solver-0.3.3-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (116.9 kB view details)

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

ballistic_solver-0.3.3-cp311-cp311-macosx_10_9_universal2.whl (191.4 kB view details)

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

ballistic_solver-0.3.3-cp310-cp310-win_amd64.whl (103.2 kB view details)

Uploaded CPython 3.10Windows x86-64

ballistic_solver-0.3.3-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (116.3 kB view details)

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

ballistic_solver-0.3.3-cp310-cp310-macosx_10_9_universal2.whl (188.7 kB view details)

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

File details

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

File metadata

  • Download URL: ballistic_solver-0.3.3.tar.gz
  • Upload date:
  • Size: 21.0 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.3.3.tar.gz
Algorithm Hash digest
SHA256 19c58179e32558ec6a185ee827012d2cbcebce80ca51518b65ca244cc2e85722
MD5 fde011afcacde426dbde9e252d992b89
BLAKE2b-256 2a822fd2a9c880dc0b8941e1fc4740f62fbb0c531f4c6277a3fed9d64f724d5f

See more details on using hashes here.

Provenance

The following attestation bundles were made for ballistic_solver-0.3.3.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.3.3-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for ballistic_solver-0.3.3-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 ce91e7bb5b3c40fad4f427b2eca5d469db72e44220888e85a61927b752dd1b76
MD5 17ae05659c07c238bbff0fc5fdc13d81
BLAKE2b-256 2aebe9251c2d532f963ec1bf265594b9f74b72db08811b46a84c2bb71f92b58b

See more details on using hashes here.

Provenance

The following attestation bundles were made for ballistic_solver-0.3.3-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.3.3-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for ballistic_solver-0.3.3-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 a1261b1219ea65fc36f90a0a7e2bcfd53446e9dc1414b0689f23f9d446aa5794
MD5 869b2476487d5fbb40a406fa944d1c50
BLAKE2b-256 a8bd21ac6cdb7e6eb6717b6f1e1cf0e1b845351affa12802c4a7bfe791408275

See more details on using hashes here.

Provenance

The following attestation bundles were made for ballistic_solver-0.3.3-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.3.3-cp312-cp312-macosx_10_13_universal2.whl.

File metadata

File hashes

Hashes for ballistic_solver-0.3.3-cp312-cp312-macosx_10_13_universal2.whl
Algorithm Hash digest
SHA256 1dcfce090e4cfda5c011ea7b3b7e2bed81d28e62b8c2d6d853163806203b7b2c
MD5 c06451e4d9aee8c74e42572438b21e26
BLAKE2b-256 9f10f6d2f8b182df8b2134b9a21932b20875d91b2e63336185c143517c0ee7a4

See more details on using hashes here.

Provenance

The following attestation bundles were made for ballistic_solver-0.3.3-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.3.3-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for ballistic_solver-0.3.3-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 442fff09157f8aa54f8dbe31d338c69e03553ba46840ccb29cf8cfcd726e4f50
MD5 58a4f2011330b399bb04d111590ef201
BLAKE2b-256 f2c1a99e4540c5875efd75a8c6f46ef693d3ce94b5254adf2ba5df02987d6c68

See more details on using hashes here.

Provenance

The following attestation bundles were made for ballistic_solver-0.3.3-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.3.3-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for ballistic_solver-0.3.3-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 e8cd003fa979f84ec8b85c38152c8b6d8959df5139bf238dd1ce2fa42d5f65c0
MD5 1129b9483c25f328b3ad06cd9a089c49
BLAKE2b-256 8da31eba3bb40b1ed0cd8d57240d73e44a870898f0ab4410632eb9e8bd8b2aea

See more details on using hashes here.

Provenance

The following attestation bundles were made for ballistic_solver-0.3.3-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.3.3-cp311-cp311-macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for ballistic_solver-0.3.3-cp311-cp311-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 4d5e28e5c031209d7aedf91a4b43e72e993012fe1c8773abc3fc0f24f1b24872
MD5 509e87398468b80373c7a3a94e0133e3
BLAKE2b-256 87cf2f2aaff978883216373810e978fad167d2259358bc1cf972568b5b9c363e

See more details on using hashes here.

Provenance

The following attestation bundles were made for ballistic_solver-0.3.3-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.3.3-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for ballistic_solver-0.3.3-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 a77400e6e2227f64052ab4b613480b4332c098930b2ae68d377fd735260612f6
MD5 434a12d38e22383667a6e9bf831dd227
BLAKE2b-256 b30f2ac338fcc5af7bfc8a5d084e14fa2b2f5635ade0cad264a9e54951fc4676

See more details on using hashes here.

Provenance

The following attestation bundles were made for ballistic_solver-0.3.3-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.3.3-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for ballistic_solver-0.3.3-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 e3017c4ec6b18ad03ba30228fb1e25067eb74318cb364e255124d558da0215db
MD5 816af27a41b293d60f5b02839046960e
BLAKE2b-256 a5a97b929301a53c084aa0a10dd9063dca7f5ac0183961f1214cca6b4b686f08

See more details on using hashes here.

Provenance

The following attestation bundles were made for ballistic_solver-0.3.3-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.3.3-cp310-cp310-macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for ballistic_solver-0.3.3-cp310-cp310-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 c76bac51a6cd81a7319641ee429b367721f8496d07299e25da146f281bbb4880
MD5 4bf8dd5b781e56b2b11463af20f4ce0e
BLAKE2b-256 608a40677f84cf531c166c4d98b64063b53d04e73c1cbc4d14ae016d1c9fea3a

See more details on using hashes here.

Provenance

The following attestation bundles were made for ballistic_solver-0.3.3-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