So I've just finished coding my 3d vector class. And now am trying to think of how I'm going to do this. I want to introduce gravity into the simulation, but this restricts the range of the turret, which is realistic, does make an infinite loop of checking for targets that are in range a strong probability. Also, I'm trying to think of how exactly to orientate and move the turret.
Heres some solutions I've come up with so far:
I could do all the calculations out like before, OR, since I made my previous 2D turret able to accept console output I could feed the coordinates into that, but that would only orientate the XY plane correctly, and even then, how would I grab the output from the execution? So, while it sparked my brain as a good idea at first, I think I'll ignore it a bit.
I think the hardest thing will be checking for gravity and other things of that nature. I know that in some science fictions, long range projectile weapons have to account for the curvature of the earth, but I don't think that will be my problem. So, to start off, I think the angle the turret's Z will be at is 45, aka the optimal distance angle, this could change a when aiming obviously. And now, before I was just using an arbituary 2 units / s for my projectile, but with the inclusion of gravity I think I've going to have to bump it up a bit to make the projectile move fast enough to stay airborne. I mean, let's face it, waiting 10 seconds for a projectile to hit a target is unrealistic. So I'll probably beef it up to say, 50 units / s or so and then just put the target out 150 or so units.
Since I've figured out the calculations to aim and lead in a 2d plane so I have reasonable confidence in extending it to the next plane up. It's just gravity and maximum distance I'm concerned about right now. Hmm.
I'll keep you updated
Friday, March 25, 2011
Wednesday, March 23, 2011
2D Vector Turret Program Complete (For the Most Part)
Phew!
I've been working since I last posted about the problem I ran into, and I have solved it. Using the good 'ol Tactics of visual representations, notebook, and my brain. It's kinda what I resort to for things like this. Anyway, I started by rewriting all my code to simplify it. The previous version I was working on had quite a phew things that could be simplified and made more efficient.
So heres a quick picture that describes what me, myself, and I (visual,notebook, and brain) came up with:
As you can see, the Fired projectile and the UFO (comeon, if something is unidentified, flying at you, and an object you want to shoot, what else do you call it? ) slowly get closer together as the seconds go by. In both the last and second to last stages of the output you can see it get extremely close and 'pass' the target. But because this 'pass' is so close, it means the UFO got nailed, because I doubt the thing we're trying to shoot is only .5 of a unit in radius.
So now, I think I may contemplate moving this into 3 Dimensions. First I'll have to rework my vector2d class into a 3d vector class. Or maybe I'll just look for source code online. Wish me luck!
I've been working since I last posted about the problem I ran into, and I have solved it. Using the good 'ol Tactics of visual representations, notebook, and my brain. It's kinda what I resort to for things like this. Anyway, I started by rewriting all my code to simplify it. The previous version I was working on had quite a phew things that could be simplified and made more efficient.
So heres a quick picture that describes what me, myself, and I (visual,notebook, and brain) came up with:
![]() |
The thought process for the targeting function |
Once you reduce the vectors down to their magnitude and just work with the simplest of all physics equations (v=d/t) its actually quite simple. And I'm surprised I didn't think of it earlier. If only someone followed this blog, or posted comments, they could suggest things to me. Anyway, Heres the output of my program after running it:
______________________________________________________________________
Turret Theta: 2.26553 Turret Delay: 0.268347
Turret Vectr: 0.768221,-0.640184
Turret Armed: 0 Locked: 1 Fired: 1
Turret Estimation of UFO: 10.6101,-8.61008
UFO: Pos: 10.6101,-8.61008 Vel: 1,-1 (1.41421 units / s)
Fired Projectile: Pos: 9.74371,-8.11976 Vel: 1.53644,-1.28037 (2 units / s)
______________________________________________________________________
Turret Theta: 2.26553 Turret Delay: 0.268347
Turret Vectr: 0.768221,-0.640184
Turret Armed: 0 Locked: 1 Fired: 1
Turret Estimation of UFO: 11.8784,-9.87843
UFO: Pos: 11.8784,-9.87843 Vel: 1,-1 (1.41421 units / s)
Fired Projectile: Pos: 11.6925,-9.74371 Vel: 1.53644,-1.28037 (2 units / s)
______________________________________________________________________
Turret Theta: 2.26553 Turret Delay: 0.268347
Turret Vectr: 0.768221,-0.640184
Turret Armed: 0 Locked: 1 Fired: 1
Turret Estimation of UFO: 13.1468,-11.1468
UFO: Pos: 13.1468,-11.1468 Vel: 1,-1 (1.41421 units / s)
Fired Projectile: Pos: 13.6412,-11.3677 Vel: 1.53644,-1.28037 (2 units / s)
______________________________________________________________________
As you can see, the Fired projectile and the UFO (comeon, if something is unidentified, flying at you, and an object you want to shoot, what else do you call it? ) slowly get closer together as the seconds go by. In both the last and second to last stages of the output you can see it get extremely close and 'pass' the target. But because this 'pass' is so close, it means the UFO got nailed, because I doubt the thing we're trying to shoot is only .5 of a unit in radius.
So now, I think I may contemplate moving this into 3 Dimensions. First I'll have to rework my vector2d class into a 3d vector class. Or maybe I'll just look for source code online. Wish me luck!
Problem with the Y component of Vector
Heres a sample block of output from my program:
______________________________________________________________________
Projectile Position: 0 0
Projectile Velocity: 1.99539 -0.135725
Turret Theta: -0.067915 Heading: 0.997695,-0.0678627
Turret Target Estimate: 8.28801,0.711987 Velocity: 0.707107,-0.707107
Object: Position- 8.99512,0.00488001 Velocity- 0.707107,-0.707107
Pings: 1:6 2:5
______________________________________________________________________
Projectile Position: 1.99539 -0.135725
Projectile Velocity: 1.99539 -0.135725
Turret Theta: -0.067915 Heading: 0.997695,-0.0678627
Turret Target Estimate: 8.28801,0.711987 Velocity: 0.707107,-0.707107
Object: Position- 9.70223,-0.702227 Velocity- 0.707107,-0.707107
Pings: 1:7 2:6
______________________________________________________________________
Projectile Position: 3.99078 -0.271451
Projectile Velocity: 1.99539 -0.135725
Turret Theta: -0.067915 Heading: 0.997695,-0.0678627
Turret Target Estimate: 8.28801,0.711987 Velocity: 0.707107,-0.707107
Object: Position- 10.4093,-1.40933 Velocity- 0.707107,-0.707107
Pings: 1:8 2:7
______________________________________________________________________
Projectile Position: 5.98617 -0.407176
Projectile Velocity: 1.99539 -0.135725
Turret Theta: -0.067915 Heading: 0.997695,-0.0678627
Turret Target Estimate: 8.28801,0.711987 Velocity: 0.707107,-0.707107
Object: Position- 11.1164,-2.11644 Velocity- 0.707107,-0.707107
Pings: 1:9 2:8
______________________________________________________________________
Projectile Position: 7.98156 -0.542901
Projectile Velocity: 1.99539 -0.135725
Turret Theta: -0.067915 Heading: 0.997695,-0.0678627
Turret Target Estimate: 8.28801,0.711987 Velocity: 0.707107,-0.707107
Object: Position- 11.8235,-2.82355 Velocity- 0.707107,-0.707107
Pings: 1:10 2:9
______________________________________________________________________
Projectile Position: 9.97695 -0.678627
Projectile Velocity: 1.99539 -0.135725
Turret Theta: -0.067915 Heading: 0.997695,-0.0678627
Turret Target Estimate: 8.28801,0.711987 Velocity: 0.707107,-0.707107
Object: Position- 12.5307,-3.53065 Velocity- 0.707107,-0.707107
Pings: 1:11 2:10
______________________________________________________________________
Projectile Position: 11.9723 -0.814352
Projectile Velocity: 1.99539 -0.135725
Turret Theta: -0.067915 Heading: 0.997695,-0.0678627
Turret Target Estimate: 8.28801,0.711987 Velocity: 0.707107,-0.707107
Object: Position- 13.2378,-4.23776 Velocity- 0.707107,-0.707107
Pings: 1:12 2:11
______________________________________________________________________
Projectile Position: 13.9677 -0.950077
Projectile Velocity: 1.99539 -0.135725
Turret Theta: -0.067915 Heading: 0.997695,-0.0678627
Turret Target Estimate: 8.28801,0.711987 Velocity: 0.707107,-0.707107
Object: Position- 13.9449,-4.94487 Velocity- 0.707107,-0.707107
Pings: 1:13 2:12
______________________________________________________________________
______________________________________________________________________
Projectile Position: 0 0
Projectile Velocity: 1.99539 -0.135725
Turret Theta: -0.067915 Heading: 0.997695,-0.0678627
Turret Target Estimate: 8.28801,0.711987 Velocity: 0.707107,-0.707107
Object: Position- 8.99512,0.00488001 Velocity- 0.707107,-0.707107
Pings: 1:6 2:5
______________________________________________________________________
Projectile Position: 1.99539 -0.135725
Projectile Velocity: 1.99539 -0.135725
Turret Theta: -0.067915 Heading: 0.997695,-0.0678627
Turret Target Estimate: 8.28801,0.711987 Velocity: 0.707107,-0.707107
Object: Position- 9.70223,-0.702227 Velocity- 0.707107,-0.707107
Pings: 1:7 2:6
______________________________________________________________________
Projectile Position: 3.99078 -0.271451
Projectile Velocity: 1.99539 -0.135725
Turret Theta: -0.067915 Heading: 0.997695,-0.0678627
Turret Target Estimate: 8.28801,0.711987 Velocity: 0.707107,-0.707107
Object: Position- 10.4093,-1.40933 Velocity- 0.707107,-0.707107
Pings: 1:8 2:7
______________________________________________________________________
Projectile Position: 5.98617 -0.407176
Projectile Velocity: 1.99539 -0.135725
Turret Theta: -0.067915 Heading: 0.997695,-0.0678627
Turret Target Estimate: 8.28801,0.711987 Velocity: 0.707107,-0.707107
Object: Position- 11.1164,-2.11644 Velocity- 0.707107,-0.707107
Pings: 1:9 2:8
______________________________________________________________________
Projectile Position: 7.98156 -0.542901
Projectile Velocity: 1.99539 -0.135725
Turret Theta: -0.067915 Heading: 0.997695,-0.0678627
Turret Target Estimate: 8.28801,0.711987 Velocity: 0.707107,-0.707107
Object: Position- 11.8235,-2.82355 Velocity- 0.707107,-0.707107
Pings: 1:10 2:9
______________________________________________________________________
Projectile Position: 9.97695 -0.678627
Projectile Velocity: 1.99539 -0.135725
Turret Theta: -0.067915 Heading: 0.997695,-0.0678627
Turret Target Estimate: 8.28801,0.711987 Velocity: 0.707107,-0.707107
Object: Position- 12.5307,-3.53065 Velocity- 0.707107,-0.707107
Pings: 1:11 2:10
______________________________________________________________________
Projectile Position: 11.9723 -0.814352
Projectile Velocity: 1.99539 -0.135725
Turret Theta: -0.067915 Heading: 0.997695,-0.0678627
Turret Target Estimate: 8.28801,0.711987 Velocity: 0.707107,-0.707107
Object: Position- 13.2378,-4.23776 Velocity- 0.707107,-0.707107
Pings: 1:12 2:11
______________________________________________________________________
Projectile Position: 13.9677 -0.950077
Projectile Velocity: 1.99539 -0.135725
Turret Theta: -0.067915 Heading: 0.997695,-0.0678627
Turret Target Estimate: 8.28801,0.711987 Velocity: 0.707107,-0.707107
Object: Position- 13.9449,-4.94487 Velocity- 0.707107,-0.707107
Pings: 1:13 2:12
______________________________________________________________________
Now that you've scrolled through it here, heres the problem.
Projectile Position: 13.9677 -0.950077
Projectile Velocity: 1.99539 -0.135725
Turret Theta: -0.067915 Heading: 0.997695,-0.0678627
Turret Target Estimate: 8.28801,0.711987 Velocity: 0.707107,-0.707107
Object: Position- 13.9449,-4.94487 Velocity- 0.707107,-0.707107
Pings: 1:13 2:12
______________________________________________________________________
Do you see it? the boldfaced text? For some reason, the y velocity and such of my projectile isn't quite right. The X component is working perfectly, off by a hundreth of a unit? That's great considering rounding errors and such. But the y component is really not acceptable and I'm a bit of a loss as to why this is happening. Hopefully I can figure it out.
Not Sure about my previous solutions for the vector problems
Hm, It missed.
Monday, March 21, 2011
XY Velocity for a projectile? Solved for
Alright, So last I left off I had coded how to deal with projectiles moving in a strictly horizontal or vertical way. And was waiting on making the code for dealing with both velocities. At first I thought I would have to deal with some type of equation of two variables and solve form both of them using some complex business, but it turns out the method I use is at least 3x simpler than that.
So, first off I use the same method I've been using to get the time interval I should be using in my estimates. since I've described that in previous blogs I won't bother redefining it here. From that I get my hBound variable which is the top of said time interval. From here, I can compute how far the projectile will travel in the time given that it travels at a constant speed of 2.0 units. I then figure out the heading of my turret to aim at so it points at the targets estimated position after hBound time. From here all I need is the vector to give to the projectile as its x movement and y movement. So, using the cos of the gun's theta times the distance of the projectile as the x component and the sin of the guns theta times the distance of the projectile as the y component, I make up my vector. From this I have a triangle that may or may not overshoot the position if fired at the wrong time. So how do I figure my delay time out?
Simple, I take the magnitude of that vector I just made and divide by hBound to get how many units of time it will take to traverse this hypotenuse I've created. If that number is equal to the time it takes for the projectile to get to the estimated position then hoorah fire right away, weapons free, danger close. If its less than that then set up delay which is as simple as Time of the target - Time of the projectile. And the other situation, where T(proj) > T(target) never occurs because that would violate the work above with the whole hBound thing, theres a reason it's called the hBound you know. From here things get pretty simple.
since I have my heading, my delay time (if there is one) all that is left to do is to fire. Which is done by created anew object that has the position of the turret, the velocity vector is the heading vector of the turret(which has been normalized) multiplied by the velocitiy I know the projectile will travel (2.0 units), ahem. Tah-dah.
Now of course is where I fork myself, should I proceed to trying to create a 3d version of this? Or should I build up a simple library in DirectX to actually see whats going on instead of just numbers? Hmmm.
So, first off I use the same method I've been using to get the time interval I should be using in my estimates. since I've described that in previous blogs I won't bother redefining it here. From that I get my hBound variable which is the top of said time interval. From here, I can compute how far the projectile will travel in the time given that it travels at a constant speed of 2.0 units. I then figure out the heading of my turret to aim at so it points at the targets estimated position after hBound time. From here all I need is the vector to give to the projectile as its x movement and y movement. So, using the cos of the gun's theta times the distance of the projectile as the x component and the sin of the guns theta times the distance of the projectile as the y component, I make up my vector. From this I have a triangle that may or may not overshoot the position if fired at the wrong time. So how do I figure my delay time out?
Simple, I take the magnitude of that vector I just made and divide by hBound to get how many units of time it will take to traverse this hypotenuse I've created. If that number is equal to the time it takes for the projectile to get to the estimated position then hoorah fire right away, weapons free, danger close. If its less than that then set up delay which is as simple as Time of the target - Time of the projectile. And the other situation, where T(proj) > T(target) never occurs because that would violate the work above with the whole hBound thing, theres a reason it's called the hBound you know. From here things get pretty simple.
since I have my heading, my delay time (if there is one) all that is left to do is to fire. Which is done by created anew object that has the position of the turret, the velocity vector is the heading vector of the turret(which has been normalized) multiplied by the velocitiy I know the projectile will travel (2.0 units), ahem. Tah-dah.
Now of course is where I fork myself, should I proceed to trying to create a 3d version of this? Or should I build up a simple library in DirectX to actually see whats going on instead of just numbers? Hmmm.
Sunday, March 13, 2011
Horizontal Target Tracking
Woo!
Just finished coding the turret of my current project to lead a target thats moving only horizontally. It took me a while to wrap my head around alot of the numbers, and also I was accidentally using degrees to start off my turrets heading and not radians. Wups!
Anyway, so this is how I accomplish leading a target horizontally:
first using vector arithmatic and dot products and arcCos, we get the heading we'll be firing on.
then we account for possible time delays in order to account for differences in distance.
Heres the code: there is some stuff missing that is declared a bit above, such as getting the hBound which is the time where the distance the projectile will travel becomes greater (or equal to) than the distance the target will travel in the same amount of time. This is used so that we can lead the target. But because we lead it, we need to make sure to fire at the correct time in order to score a direct hit. So thats why we do some subtraction between the amount of distance and time and all that. Anyway: Heres the code:
if(target.velocity.y == 0 && target.velocity.x != 0){ //target only moves horizontally
//get the heading to fire on.
estMagPos = estMagPos + estMagVel*hBound;
Vector2D gt = (estMagPos - gun.position ).Normalize();
gun.theta -= acos(Dot(gt,gun.heading));
gun.heading = gt;
//now, account for leading and time delay
//hBound * vel = how much distance the proj will travel.
//Use the gun.theta and this to resolve the vector into it's components
//then the distance between the (hBound * vel) - x of target
double projd = hBound * vel;
projd = (cos(gun.theta ))*projd; // x distance of vector
double deltax = projd - estMagPos.x;
double waitT = deltax/vel;
gun.countdown = -waitT; //negation to make positive. no neg times
gun.locked = true;
std::cout << "Target Locked. Countdown Til Firing: " << gun.countdown << std::endl;
system("hal target lockd");
So, the calls to system and hal are just a speech program to say the string aloud.
Just finished coding the turret of my current project to lead a target thats moving only horizontally. It took me a while to wrap my head around alot of the numbers, and also I was accidentally using degrees to start off my turrets heading and not radians. Wups!
Anyway, so this is how I accomplish leading a target horizontally:
first using vector arithmatic and dot products and arcCos, we get the heading we'll be firing on.
then we account for possible time delays in order to account for differences in distance.
Heres the code: there is some stuff missing that is declared a bit above, such as getting the hBound which is the time where the distance the projectile will travel becomes greater (or equal to) than the distance the target will travel in the same amount of time. This is used so that we can lead the target. But because we lead it, we need to make sure to fire at the correct time in order to score a direct hit. So thats why we do some subtraction between the amount of distance and time and all that. Anyway: Heres the code:
if(target.velocity.y == 0 && target.velocity.x != 0){ //target only moves horizontally
//get the heading to fire on.
estMagPos = estMagPos + estMagVel*hBound;
Vector2D gt = (estMagPos - gun.position ).Normalize();
gun.theta -= acos(Dot(gt,gun.heading));
gun.heading = gt;
//now, account for leading and time delay
//hBound * vel = how much distance the proj will travel.
//Use the gun.theta and this to resolve the vector into it's components
//then the distance between the (hBound * vel) - x of target
double projd = hBound * vel;
projd = (cos(gun.theta ))*projd; // x distance of vector
double deltax = projd - estMagPos.x;
double waitT = deltax/vel;
gun.countdown = -waitT; //negation to make positive. no neg times
gun.locked = true;
std::cout << "Target Locked. Countdown Til Firing: " << gun.countdown << std::endl;
system("hal target lockd");
So, the calls to system and hal are just a speech program to say the string aloud.
Saturday, March 12, 2011
Getting a bit farther with my vectors
So I've gotten a bit father with things, these two images show some of the thought process on what I'm working on right now.
Which is, targeting and leading when the object is moving horizontally only. I've already worked out the case for the stationary target, which is the easiest, so now I'm working on the next easiest (I think) which is just horizontal movement.
normally, I would go into detail on more stuff. But I'm pressed for time right now, but once I get back to the world of internets, then I will gladly post and describe all the difficulties I've been having and how I've been solving them.
Which is, targeting and leading when the object is moving horizontally only. I've already worked out the case for the stationary target, which is the easiest, so now I'm working on the next easiest (I think) which is just horizontal movement.
normally, I would go into detail on more stuff. But I'm pressed for time right now, but once I get back to the world of internets, then I will gladly post and describe all the difficulties I've been having and how I've been solving them.
Subscribe to:
Posts (Atom)