Predict projectile ballistics, including gravity and motion

would this work with a missile flying towards a moving target?

1 Like

Ballistics missiles, probably. Never tested it. It’s definitely NOT for cruise missiles though.

1 Like

How about an active homing missile such as the AIM-120 for an irl example

1 Like

no idea you’re free to try it though

all it does is calculate the direction needed to reach a target given positions and velocities

an active homing missile basically never uses any sort of ballistics because it’s always being propelled and making course corrections so I’m really not sure about the applicability of this

1 Like

after testing it doesn’t predict an intercept at long ranges or low speeds but i suppose thats because its not meant for that haha

1 Like

This is awesome!! One question though, is it possible to calculate acceleration on more axes than just Y (gravity)? For instance, wind pushing against the projectile would likely include X and/or Z acceleration. I don’t understand any of this math so I’m hoping you do…

i also kinda dont lol

The blogpost where I sourced these equations from lists two methods for calculating the launch angle. This module uses the latter one. The other method uses gravity as a vector for the quartic equation, rather than as a scalar, but still gives you the time of flight as the solution. But the way you will use that time of flight for finding the launch angle is what you need to figure out, since the method I used still uses gravity as a scalar value, i.e. it is locked to the Y axis.

It’s either that or I’m really stupid because it uses dot product which kinda undermines the point of them being vectors in the first place

Are there plans to make it account for obstacles? I’m currently using this for heat seeking missile I have, but I often times encounter it hitting buildings when attempting to go towards players.

how would I make this work with hingeconstraints?

I have two hinges (one for the yaw and one for the pitch), and I want to get an angle for each.

It is possible to do it yourself. All this module does is tell you the direction to aim at; you can use that direction to draw a quadratic curve and perform a bunch of raycasts to detect collisions. Essentially, you are recreating the projectile physics.

There are plenty of resources out there to convert a worldspace CFrame rotation to a hinge’s objectspace rotation. It simply involves inversing a CFrame. Here’s one I answered a while back:

2 Likes

Is there an easy way to remove relative gravity from the mix? This module works great, but when attempting to fire a projectile affected by gravity at a moving target also affected by gravity, I need to have no relative gravity to have an accurate hit. Setting gravity to 0 breaks a lot of things(which makes sense). Setting gravity to a very low value works somewhat, but widely increases inaccuracies at range. I’m currently working on a system that works with two non-gravity-affected projectiles, one gravity-affected (for both the target and the interceptor), and no gravity(which I have solved fairly easily).

I advise you to check out the video that @NerfGunsAreLethal sent. It gives an extremely thorough explanation of intercepting various degrees of motion, including what you described. All of this is well out of my reach :melting_face:

1 Like

One of the best and usefull resource share in roblox. Thank you for sharing it with us. +1 like from me

hihi so i was trying to make a turret for my game, used this module today and seems to error, at first run it was perfectly fine but after and after sometimes where i adjusted the gravity, lookAt errors came, doesn’t come from the TurretController since i tried on my predict module and the errors were gone

and to add this, yes i already set it to 0.001 which solves the problem but that makes it stutter and still spams the output

would you be able to use this just to calculate the angle a barrel would need to rotate (or a position the barrel would need to aim at) to hit a target, such as some artillery system (aiming up), and let a projectile manager like FastCast deal with the actual bullet (and still land in the same spot)

example:
your mouse is some distance away and the Target is just some part or the baseplate on the ground
then this would calculate where the barrel would need to rotate or aim at to shoot a projectile that would hit there (projectile would go up and drop down in an arc)

1 Like

I documented the module in the post which clearly states that the return value of the function is Vector3?, which means it is NOT guaranteed to find a solution. A firing solution can be impossible for many reasons, such as the projectile being too far and too slow and simply couldn’t catch up. Your lookAt function errors because you are just directly plugging in the result which could be nil. Please refer to the usage example that’s also provided:

Did you even read the thread? That is exactly what this module does. It gives you a position in the world to aim your gun at. Once again, check the sample code. If you’re talking about the angles themselves, you can still deconstruct that CFrame to retrieve it using :ToOrientation()

2 Likes

this is quackilydoodly friggin insane

I’ve been working on a system that accounts for more than just gravity, it can accept 2 positions, 2 velocities, and 2 accelerations. It also will take in the initial velocity of the projectile. My biggest issue has always been solving the quadratic. If you find any better methods that can somewhat accurately calculate a positive root in certain situations that would be great, as the inaccuracy of the math increases with the velocity.

Either you’re mistaken or I’m mistaken, but doing something like that would necessitate a polynomial that’s beyond quartic (>4th degree), which means there’s no general formula for it. One of the common workarounds is Newton iteration, but another arguably better one is covered in that same video that I recommended which talks about derivatives.

P.S. a lot of people mix up “quadratic” with “quartic”. One is something you see in middle school and the other one is a thorn in the rear.