Just wanted to share this. If anyone recalls a few months ago last year I was fooling around with my project and Git and lost a bunch of work. Well dare I say I’ve managed to figure this thing out haha just took some patience and not being friggin’ hasty with it.
Presently I am working on a side project which has the aim of finding the intercept point for two bullets to collide along a linear flight path with constant velocity. I managed to solve the math problem (I have almost no physics knowledge). Most the work has just been towards getting the turret properly aiming and now firing with a few hiccups here and there.
Solved the math just to learn how to solve problems like that, and decided to turn it in to a unity project just to test it (also because its pretty dang cool even though turrets have been done to death in games). There’s something immensely satisfying about solving something on your own and watching it work. Also this project will definitely help with the Mouse Flight project, provided I decide to keep working on it. The next minor step is to tell my turret when the best time to fire is when the missile comes in range and the next major step is to do it from a moving platform.
I would but right now I’m having a bit of a problem. I got the bullet firing methods working (had some trouble with the Timer class, turns out I was calling the new keyword with it instead of gameobject.addComponent() ) but every time a shot is fired my turret recoils [the bullet uses 3d rigidbodies], which is strange.
I’ll post something in the scripting boards about it. When its fully working will post an update.
Interesting problem. Seems to me you could define a plane for one of the bullets, and raycast from the other gun to find the intersection point. Just make the center-line of the plane run along the first gun’s vector and then make the plane face the other gun that’s shooting.
Of course, you could define the two bullet’s vectors as legs of a triangle, and use triganometry + witchcraft to figure out where they meet.
Neat.
I like weird bugs, I’d be willing to spend some time looking at it for fun and let you know if I figure anything out.
@MasterFrog its actually really simple. At first I thought there would be some serious calculus involved, and some trig. As it turns out since its not about finding the slope of any graphs (ie. an accelerating object) one doesn’t exactly need calculus. Trigonometry is overkill. The question is really “how far ahead do I need to aim to make these bullets collide?” and work backward from there (hint: there’s one more variable you need to find that) using what you know.
Actually just noticed that I copied my solution in to my help post on the scripting boards, haha, go take a look if you want! Its literally one line.
The entire problem boils down to just a distance/time = rate equation and some logic.
Isn’t the question also expressed as “how much time in advance do I need to shoot to make this bullet hit that other bullet?”
Because two bullets fired at the same time, can only intersect if they are fired from one specific angle, they must travel an even distance to intersect.
Sort of. I don’t think it was clear. I didn’t mention it but it was kind of implied, the constant velocities can be different (which was part of the purpose). In fact depending on the distance between the intercept point and the origin of the bullet being fired at the missile, even if the velocities were exactly the same they would miss. This is due to the fact that diagonal paths are longer than lengths along an axis. Unless you decide to fire when the intercept point is directly above, you will miss.
Yet you are correct about the time. That was the other variable you need.
Basically the solution works for any angle and any velocity with no trig or calculus. It doesn’t factor in drop due to gravity, wind resistance or acceleration (which isn’t the point because my game takes place in space).
One can do some figuring to find the best time to fire too. My plan is to use the intercept’s current point once within my radar trigger, and find the best time to fire to neutralize the missile as it flies over it (the missile isn’t aimed at the turret just yet). Ie. the distance between the intercept point and the turret is at its lowest and the number of shots required to “take it out” is part of knowing when you want to fire and for how long.
I was even thinking about if some missile were on a curved path. One could account for the mass of the missile (think inertia) to determine how much it could deviate given possible thrust quantities and get a good estimation for how much its direction might change. That’s important (kind of) for my project because at some point I want to implement a target lead on the other ships in space (and incoming missiles) so that the player can hit them with his guns. The only reason that’s important is because of the chaotic nature of the game (so much stuff is moving) and the relatively slow speed of bullets that a human cannot accurately predict where to aim. This prediction algorithm I’m thinking of would be important for a missile to intercept a ship too.
I always thought designing missile systems and stuff would be a fun challenge. The only problem was that bombs/missiles/bullets are designed to kill people. At the career fair the US Navy Weapons research program shows up, and I considered applying for an internship position but this thought stopped me. I think game dev is a good substitute.
It is a cool problem to try and solve. Although IRL because there are so many variables, the missiles themselves (missiles meaning any moving projectile, including bullets) are able to tweak their vectors as they are flying, to move towards the hottest point in front of them. So while technically sophisticated, the real world solution is a missile that can steer itself towards it’s target.
Even using your best efforts, making a gun that can shoot a high-speed missile with another high-speed missile requires a prediction-based method, which in principle is AI cheating because it allows you to have more info about the problem than you possibly could.
An interesting challenge would be to create a component that uses raycasts to simulate infrared motion detection, and using data obtained from detections over time, predict the trajectory of the missile and attempt to shoot it down.
You are right there are lots of variable’s in reality, and this was a simple exercise though I still think things like the United States STAR WARS program and the Israeli “Iron Dome” system, or even the point defense systems on Israeli and some international armored vehicles must find an intercept point in any case. Otherwise your defense missile ends up chasing the attacker’s missile or overshooting it, which is dangerously inefficient with lives on the line.
I marveled the other day about it. To get to this point, just finding the intercept point one has to have a lot of supporting systems. IR signature detectors, image recognition, or just plain ole radar either on the nose of the missile or on the ground all contribute to finding the actual velocity/position/other variable’s to find where and when to shoot. Then you have to ignite the rocket (or jet) and steer toward the target using the weapons fins, and these all have constraints on power, fuel, space, weight, etc.
I think a game simulation of the IR detection would be grossly inefficient (raycasts), and a little too close to reality to be much fun.
If you’re going to design some kind of real time IR seeking thing, why not simulate everything else too?
Edit: Star Citizens reportedly utilizes PID’s and simulates everything from the clock cycles on the processors for components on the ships to actual physics when damaged (engines/thrusters failing, hull breaches, overheating, etc.). When I read that I got super excited and really appreciated Star Citizen for it technical ingenuity more than I previously had.
While this was an interesting problem to attempt I just wanted to update everyone with the following.
My solution did not factor in acceleration. I didn’t realize that would be a problem until I tried to get my turret to aim in Unity. I did not realize one can’t ignore acceleration in any physics engine. So I’m back to square one. We just got in to parametric equations in calc 2 which is super interesting because that’s whats needed to solve this, according to my friend who has passed calc 3.
On a related note I realized recently that I knew more about procedural programming and problem solving than OOP techniques. So I’m rewriting my Mouse Flight project to use OOP design while taking the basic concepts from the original project, b/c the current codebase has never utilized OOP.
Also in the new version (with OOP) I managed to get the flight model working with physics!! That is something which eluded me originally. It really wasn’t all that difficult.
How I make so many mistakes with this project I don’t know.
I would take the distance traveled in the previous frame and raycast forward by that distance to find the intersect. This also allows wind drift and drag to be considered per frame and still keeps the ability to hit targets with high velocity projectiles without passing through them. A raycast from the gun barrel is not accurate due to gravitational forces just to begin with as it will not take into account a parabolic trajectory.