Skip to content

Interface

Calculator dataclass

Calculator(
    config: Optional[ConfigT] = None,
    engine: EngineProtocolEntry = DEFAULT_ENTRY,
)

Bases: Generic[ConfigT]

Basic interface for the ballistics calculator.

Methods:

Name Description
barrel_elevation_for_target

Calculate barrel elevation to hit target at zero_distance.

set_weapon_zero

Set shot.weapon.zero_elevation so that it hits a target at zero_distance.

fire

Calculate the trajectory for the given shot parameters.

iter_engines

Iterate all available engines in the entry points.

Functions

barrel_elevation_for_target
barrel_elevation_for_target(
    shot: Shot, target_distance: Union[float, Distance]
) -> Angular

Calculate barrel elevation to hit target at zero_distance.

Parameters:

Name Type Description Default
shot Shot

Shot instance we want to zero.

required
target_distance Union[float, Distance]

Look-distance to "zero," which is point we want to hit. This is the distance that a rangefinder would return with no ballistic adjustment.

required
Note

Some rangefinders offer an adjusted distance based on inclinometer measurement. However, without a complete ballistic model these can only approximate the effects on ballistic trajectory of shooting uphill or downhill. Therefore: For maximum accuracy, use the raw sight distance and look_angle as inputs here.

Source code in py_ballisticcalc/interface.py
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
def barrel_elevation_for_target(self, shot: Shot, target_distance: Union[float, Distance]) -> Angular:
    """Calculate barrel elevation to hit target at zero_distance.

    Args:
        shot: Shot instance we want to zero.
        target_distance: Look-distance to "zero," which is point we want to hit.
            This is the distance that a rangefinder would return with no ballistic adjustment.

    Note:
        Some rangefinders offer an adjusted distance based on inclinometer measurement.
        However, without a complete ballistic model these can only approximate the effects
        on ballistic trajectory of shooting uphill or downhill. Therefore:
        For maximum accuracy, use the raw sight distance and look_angle as inputs here.
    """
    target_distance = PreferredUnits.distance(target_distance)
    total_elevation = self._engine_instance.zero_angle(shot, target_distance)
    return Angular.Radian(
        (total_elevation >> Angular.Radian) - (shot.look_angle >> Angular.Radian)
    )
set_weapon_zero
set_weapon_zero(
    shot: Shot, zero_distance: Union[float, Distance]
) -> Angular

Set shot.weapon.zero_elevation so that it hits a target at zero_distance.

Parameters:

Name Type Description Default
shot Shot

Shot instance to zero.

required
zero_distance Union[float, Distance]

Look-distance to "zero," which is point we want to hit.

required
Source code in py_ballisticcalc/interface.py
175
176
177
178
179
180
181
182
183
def set_weapon_zero(self, shot: Shot, zero_distance: Union[float, Distance]) -> Angular:
    """Set shot.weapon.zero_elevation so that it hits a target at zero_distance.

    Args:
        shot: Shot instance to zero.
        zero_distance: Look-distance to "zero," which is point we want to hit.
    """
    shot.weapon.zero_elevation = self.barrel_elevation_for_target(shot, zero_distance)
    return shot.weapon.zero_elevation
fire
fire(
    shot: Shot,
    trajectory_range: Union[float, Distance],
    trajectory_step: Optional[
        Union[float, Distance]
    ] = None,
    *,
    extra_data: bool = False,
    dense_output: bool = False,
    time_step: float = 0.0,
    flags: Union[TrajFlag, int] = NONE,
    raise_range_error: bool = True,
) -> HitResult

Calculate the trajectory for the given shot parameters.

Parameters:

Name Type Description Default
shot Shot

Shot parameters, including position and barrel angle.

required
trajectory_range Union[float, Distance]

Distance at which to stop computing the trajectory.

required
trajectory_step Optional[Union[float, Distance]]

Distance between recorded trajectory points. Defaults to trajectory_range.

None
extra_data bool

[DEPRECATED] Requests flags=TrajFlags.ALL and trajectory_step=PreferredUnits.distance(1).

False
dense_output bool

HitResult stores all calculation steps so it can interpolate any point.

False
time_step float

Maximum time between recorded points. If > 0, points are recorded at least this frequently. Defaults to 0.0.

0.0
flags Union[TrajFlag, int]

Flags for specific points of interest. Defaults to TrajFlag.NONE.

NONE
raise_range_error bool

If True, raises RangeError if returned by integration.

True

Returns:

Name Type Description
HitResult HitResult

Object containing computed trajectory.

Source code in py_ballisticcalc/interface.py
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
def fire(self, shot: Shot,
         trajectory_range: Union[float, Distance],
         trajectory_step: Optional[Union[float, Distance]] = None, *,
         extra_data: bool = False,
         dense_output: bool = False,
         time_step: float = 0.0,
         flags: Union[TrajFlag, int] = TrajFlag.NONE,
         raise_range_error: bool = True) -> HitResult:
    """Calculate the trajectory for the given shot parameters.

    Args:
        shot: Shot parameters, including position and barrel angle.
        trajectory_range: Distance at which to stop computing the trajectory.
        trajectory_step: Distance between recorded trajectory points. Defaults to `trajectory_range`.
        extra_data: [DEPRECATED] Requests flags=TrajFlags.ALL and trajectory_step=PreferredUnits.distance(1).
        dense_output: HitResult stores all calculation steps so it can interpolate any point.
        time_step: Maximum time between recorded points. If > 0, points are recorded at least this frequently.
                   Defaults to 0.0.
        flags: Flags for specific points of interest. Defaults to TrajFlag.NONE.
        raise_range_error: If True, raises RangeError if returned by integration.

    Returns:
        HitResult: Object containing computed trajectory.
    """
    trajectory_range = PreferredUnits.distance(trajectory_range)
    dist_step = trajectory_range
    filter_flags = flags
    if trajectory_step:
        dist_step = PreferredUnits.distance(trajectory_step)
        filter_flags |= TrajFlag.RANGE
        if dist_step.raw_value > trajectory_range.raw_value:
            dist_step = trajectory_range

    if extra_data:
        warnings.warn("extra_data is deprecated and will be removed in future versions. "
            "Explicitly specify desired TrajectoryData frequency and flags.",
            DeprecationWarning
        )
        dist_step = PreferredUnits.distance(1.0)  # << For compatibility with v2.1
        filter_flags = TrajFlag.ALL

    result = self._engine_instance.integrate(shot, trajectory_range, dist_step, time_step,
                                             filter_flags, dense_output=dense_output)
    if result.error and raise_range_error:
        raise result.error
    return result
iter_engines staticmethod
iter_engines() -> Generator[EntryPoint, None, None]

Iterate all available engines in the entry points.

Source code in py_ballisticcalc/interface.py
232
233
234
235
@staticmethod
def iter_engines() -> Generator[EntryPoint, None, None]:
    """Iterate all available engines in the entry points."""
    yield from _EngineLoader.iter_engines()