Class TrajectoryUtil

java.lang.Object
nokunami.floral_warfare.common.entity.projectile.TrajectoryUtil

public final class TrajectoryUtil extends Object
TrajectoryUtil — projectile trajectory helpers for Minecraft Forge 1.20.1 Minecraft projectile physics recap: - Each tick: velocity *= drag, then velocity.y -= gravity - Default arrow: drag = 0.99, gravity = 0.05 (per tick) - Default snowball/egg: drag = 0.99, gravity = 0.03 - Fireball: drag = 0.95, gravity = 0.0 - ThrownPotion: drag = 0.99, gravity = 0.05 All positions are in blocks (doubles). Time is measured in ticks.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final double
     
    static final double
     
    static final double
     
    static final double
     
    static final double
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static net.minecraft.world.phys.Vec3
    aimArrow(net.minecraft.world.phys.Vec3 shooterEye, net.minecraft.world.phys.Vec3 target, double power)
    Aim an arrow at a target given a power multiplier (0.0–1.0 → 0–3 blocks/tick).
    static net.minecraft.world.phys.Vec3
    calculateAimVelocity(net.minecraft.world.phys.Vec3 shooterEye, net.minecraft.world.phys.Vec3 target, double speed, double drag, double gravity, int maxTicks)
    Calculates the launch velocity needed to hit a target position, given a fixed projectile speed (magnitude) and physics constants.
    static net.minecraft.world.phys.Vec3
    getEyePosition(net.minecraft.world.entity.Entity entity)
    Returns the eye-level position of an entity as a Vec3, convenient as the launch origin for projectiles.
    static net.minecraft.world.phys.Vec3
    getImpactPosition(net.minecraft.world.phys.Vec3 origin, net.minecraft.world.phys.Vec3 velocity, double drag, double gravity, double targetY, int maxTicks)
    Returns only the final landing position (Y ≤ targetY) or the position after maxTicks if the projectile never reaches that height.
    static net.minecraft.world.phys.Vec3[]
    simulateArrow(net.minecraft.world.phys.Vec3 origin, net.minecraft.world.phys.Vec3 velocity, int maxTicks)
    Simulate an arrow (drag=0.99, gravity=0.05).
    static net.minecraft.world.phys.Vec3[]
    simulatePath(net.minecraft.world.phys.Vec3 origin, net.minecraft.world.phys.Vec3 velocity, double drag, double gravity, int maxTicks)
    Simulates a projectile step-by-step and returns the position at each tick.
    static net.minecraft.world.phys.Vec3[]
    simulateThrowable(net.minecraft.world.phys.Vec3 origin, net.minecraft.world.phys.Vec3 velocity, int maxTicks)
    Simulate a thrown snowball / egg / potion (drag=0.99, gravity=0.03).
    static net.minecraft.world.phys.Vec3
    velocityFromAngles(double yawRad, double pitchRad, double speed)
    Builds a velocity vector from yaw/pitch angles and a speed scalar.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Method Details

    • simulatePath

      public static net.minecraft.world.phys.Vec3[] simulatePath(net.minecraft.world.phys.Vec3 origin, net.minecraft.world.phys.Vec3 velocity, double drag, double gravity, int maxTicks)
      Simulates a projectile step-by-step and returns the position at each tick.
      Parameters:
      origin - starting position (block coords)
      velocity - initial velocity vector (blocks/tick)
      drag - velocity multiplier applied each tick (e.g. 0.99)
      gravity - downward acceleration applied each tick (e.g. 0.05)
      maxTicks - maximum number of ticks to simulate
      Returns:
      array of positions, index 0 = origin (tick 0)
    • getImpactPosition

      public static net.minecraft.world.phys.Vec3 getImpactPosition(net.minecraft.world.phys.Vec3 origin, net.minecraft.world.phys.Vec3 velocity, double drag, double gravity, double targetY, int maxTicks)
      Returns only the final landing position (Y ≤ targetY) or the position after maxTicks if the projectile never reaches that height.
      Parameters:
      origin - launch position
      velocity - initial velocity
      drag - per-tick drag coefficient
      gravity - per-tick downward acceleration
      targetY - Y level considered "ground" (stop condition)
      maxTicks - safety cap
      Returns:
      estimated impact position
    • calculateAimVelocity

      public static net.minecraft.world.phys.Vec3 calculateAimVelocity(net.minecraft.world.phys.Vec3 shooterEye, net.minecraft.world.phys.Vec3 target, double speed, double drag, double gravity, int maxTicks)
      Calculates the launch velocity needed to hit a target position, given a fixed projectile speed (magnitude) and physics constants. Uses a binary-search approach on the pitch angle to find a solution. Returns null if no valid arc exists within the given constraints.
      Parameters:
      shooterEye - position of the shooter's eye (launch point)
      target - position to hit
      speed - scalar speed of the projectile (blocks/tick)
      drag - per-tick drag coefficient
      gravity - per-tick gravity
      maxTicks - maximum flight time to consider
      Returns:
      velocity vector to launch the projectile, or null
    • simulateArrow

      public static net.minecraft.world.phys.Vec3[] simulateArrow(net.minecraft.world.phys.Vec3 origin, net.minecraft.world.phys.Vec3 velocity, int maxTicks)
      Simulate an arrow (drag=0.99, gravity=0.05).
    • simulateThrowable

      public static net.minecraft.world.phys.Vec3[] simulateThrowable(net.minecraft.world.phys.Vec3 origin, net.minecraft.world.phys.Vec3 velocity, int maxTicks)
      Simulate a thrown snowball / egg / potion (drag=0.99, gravity=0.03).
    • aimArrow

      public static net.minecraft.world.phys.Vec3 aimArrow(net.minecraft.world.phys.Vec3 shooterEye, net.minecraft.world.phys.Vec3 target, double power)
      Aim an arrow at a target given a power multiplier (0.0–1.0 → 0–3 blocks/tick).
    • getEyePosition

      public static net.minecraft.world.phys.Vec3 getEyePosition(net.minecraft.world.entity.Entity entity)
      Returns the eye-level position of an entity as a Vec3, convenient as the launch origin for projectiles.
    • velocityFromAngles

      public static net.minecraft.world.phys.Vec3 velocityFromAngles(double yawRad, double pitchRad, double speed)
      Builds a velocity vector from yaw/pitch angles and a speed scalar. Angles should be in radians.
      Parameters:
      yawRad - horizontal angle (0 = south / +Z)
      pitchRad - vertical angle (negative = upward in standard math, but Minecraft stores pitch inverted; pass raw look angle)
      speed - magnitude in blocks/tick