Alright, so in case you can't tell, I've figured something very important out. The sphere of range around my turret isn't really a sphere. It's more of a cone.
The XY range plane is a sphere because it stretches out to 254.929 in all directions around the turret. Next, as I shrink that XY component of my vector, the heights I can hit will increase. I mentioned this near the end of my last post. I did some thinking, and I've come to believe that there is a directly... or inverse relationship between the two. if I target an object at the end of my XY plane, then the Z height of that object when I hit it will be 0. But If I move back a bit then I can hit a target a little farther up, a little inductive reasoning later and I see the max height of my projectile show up. When can I hit the object directly overhead of my turret? When it's XY plane coordinate is (0,0). So, by this logic, I get a cone. Because of this I'm unsure if my method of comparing magnitudes of the vectors will work this time. Rather, I think I should compare the components of the vector instead. Such that, if a XY component (I'm lumping the two together, so technically I'll still be using that magnitude) and a Z component make up a ratio that is greater than... can that be expressed as a ratio? The two are inversely proportional...
If the two components added together are more than 254 than I think it's out of range. Because say some object is 100 units out on the XY plane. Obviously in range XY wise. But if the height is 200 on this particular object? Then by my proportion It's out of range because 100+200 > 254. Also, I've worked out this example with the wonderful physics formula arctan(v^2+(or -) the square root of (v^4-g(gx^2+2yv^2))) all over gx where x and y are the distance times the cos and sin of the angle of elevation of the object. So I think I can use my proportion to test check to see if things will be out of range or not.
You might be thinking, why doesn't he just use the dang formula from wikipedia to figure it out? My answer? I need to compute these distances and calculations on estimated positions in t amounts of seconds and all that arctan square root lalala is expensive. And if a simple proportion can tell me if somethings out of range or not before I start doing calculations, I'll be quite happy. And thus, I am.
Oh, it was also my birthday yesterday
Monday, March 28, 2011
Saturday, March 26, 2011
Maximum Height
Well, I started working out how to figure when to stop calculations because the target is out of range. And it seems complicated, to say the least.
First off, imagine a semi-sphere with a radius 254.929 units. This is basically the max distance for my turret, but the thing is, when I 'fire' a projectile straight up, if I increment my update function with a value of 1 second, it tells me that the projectile will reach maximum height around 6 or 7 seconds with a height of a little more than 270. This, is wrong. As I lower my increment function to say, .5, it shrinks to 265 or so, then to .25 it lowers to 260ish. Always around 6 or 7 seconds. but the thing is. The calculations performed should still give the same amount of height given the same amount of time. I don't understand why it's not. I noticed that as I lower the increment for time to update, the height approaches 254.929, but still. I can't use an approximation, or a bound for my max distance check if it changes by the time increment. It throws things off, as I'm sure you an understand.
So, what is wrong with this code?
First off, imagine a semi-sphere with a radius 254.929 units. This is basically the max distance for my turret, but the thing is, when I 'fire' a projectile straight up, if I increment my update function with a value of 1 second, it tells me that the projectile will reach maximum height around 6 or 7 seconds with a height of a little more than 270. This, is wrong. As I lower my increment function to say, .5, it shrinks to 265 or so, then to .25 it lowers to 260ish. Always around 6 or 7 seconds. but the thing is. The calculations performed should still give the same amount of height given the same amount of time. I don't understand why it's not. I noticed that as I lower the increment for time to update, the height approaches 254.929, but still. I can't use an approximation, or a bound for my max distance check if it changes by the time increment. It throws things off, as I'm sure you an understand.
So, what is wrong with this code?
//if you add in terminal velocity to the recalculation here first
pos = pos + vel * t;
pos.z = -.5*g*t*t + vel.z * t + pos.z;
if(pos.z <= 0 ){pos.z = 0; vel = Vector3D(0,0,0);} // no negative z's
vel.z = -g*t + vel.z;
//no exceeding maximum distance traveled
if(pos.x > maxD){pos.x = maxD;}
if(pos.y > maxD){pos.y = maxD;}
I don't know. The position is calculated by it's velocity and position vectors. The z position is recalculated using the height function I've found with physics. If the proj hits the ground it's velocity stops. the velocity of the z is affected by gravity by the equations of gravity. And then if the positions of x and y are greater than the maxD 254.929 then they are reset, because they should not be.
Anyway, the issue is in checking to see if the turret should even bother with trying to target an object. If the object is out of range than no attempt should be made. Basically I need to check to see if the object is within my sphere. But that's hard to do. I feel like I should be using arc trig functions to find a lenght of an arc that is whatever my arc length would be? I'm not sure.
In case you're wondering where some of the numbers come from. The 254.929 is the projectile fired at an angle of 45 degrees. Of course this only gives the max distance I can fire off into the XY plane. Making the base of my semiSphere. The maxHeight, is, I believe something along the lines of 254 as well. It would make sense... But then drawing that arc from that imaginary pole to the maxDistance line is... well, I don't know how to do it I suppose? Although...
The radius of my semiSphere is 254. So... 254 * sin theta will give me all the possible heights I can hit? I believe? Which would range from 0-254 if I'm not mistaken. I suppose, I could resolve the vector of the target into it's component vectors and then check those to make sure they're less than 254? But then what if an object is floating around at 254,254,2? Answer? I can't hit it! So it must have to do with the sum of those numbers I suppose? I've been trying to do it with magnitude but I'm not sure if that's correct because of this:
The magnitude of a vector(254,254,254) is 441.55 and without gravity, the distance a projectile can travel is 360.5 (velocity of the projectile (50 units /s) * the maxDistanceTime (7.21)) And if I compare these two, then there will be many cases where the function says: no can do. And I do agree with it in this example because 254,254,254 would be hovering waaay above. So, I suppose... I can hit something at (254,254,0) can I hit something at (0,254,254)? That would mean it's aligned with me on the x axis, but is lying out at the end of my range in the y... floating at 254z. That would be a no. It's mainly the Z that's the kicker, there must be some relationship between the XY components and the Z components. (254,254,0). I mean, 254-254 is 0, so is a bunch of other random stuff though. Halfway out, 128,128,128 I'm pretty sure I could hit. It's well within the XY range. If I do the math out: if it's about 221.7 out from the turret. which even though within the supposed maxD. I'm just still confused.
I think where I'm getting confused is that I keep thinking of the angle as being 45, and my range as being a sphere. When I think it might be more of a cone. Because I can move my Z angle up and down I can hit anywhere from 0-254 in height, but as the height gets higher, the target has to be closer. I just need to figure out that relationship first...
Grar math!
Friday, March 25, 2011
Gravity: Thou art a _ _ _ _ _
I've made progress in my 3D Turret program. I've (unfortunately for my poor brain) decided to include gravity in my calculations. Mainly because I'd prefer for my calculations to not shoot off into the sky without ever coming down again. Gravity's good for that sort of thing. Unfortunately, it's also good at making you do a bunch of calculations for determining maximum distance, terminal velocity, and a few other things. I did have a model that was close to what it should be, but the calculations it was making wasn't quite right. (Specifically it was 2 seconds later in hitting the ground than it should have been)
So I decided to peruse Wikipedia a little, specifically the free fall, projectile motion, and trajectory pages. They're plenty of nice equations ready to use. And although I was slightly saddened that I had admitted defeat in a way and just looked up the answer. The equation's were very similar to the ones I had devised. I had a big comment written at the top of my code that GRAVITY IS M/S^2, but for some reason I didn't square the time in my calculation for figuring the new z position of the projectile. But besides that, my equation was correct. My velocity updating line of code was correct though, so I was quite happy. From this figuring out the maximum distance was a matter of either running trials of different time increments or solving an equation. Or the third option not allowed by any homework ever done by a person. Look up a calculator on the internet that displays all the information when you enter the right numbers. I opted for the third approach and then tested it against my code by running the simulation to that time and making sure the z went to 0.
The only thing I'm still unsure of, is terminal velocity. Which is, unfortunately, rather annoying to figure out when you have no idea what the dimensions of your hypothetical projectile is. So I had to determine what I was actually firing (a small bullet with a mass of .04 kg is what I believe I decided on, with a cross section of 50mm^2, traveling through standard sea level air density (1.5 kg/m^3) with a drag coefficient of (.1) (a small ball by the examples I found, although it had a bullet (.3)) it gave a ridiculous terminal velocity (small that is not large) so I decided to go with my turret firing perfectly round projectiles.
So, after all that and 1 and a half cramped notebook pages later, I have an update position function I feel like I can rely on for my calculations.
Tommorow, after doing some trivial work (required course work for a required class which pertains to my major in no way), I'll start working on the estimation process and how it leads it's target through 3 dimensions. But for now, more reading Gödel, Escher, Bach by Douglas Hoftstadter (I'm a little more than halfway through the book )
So I decided to peruse Wikipedia a little, specifically the free fall, projectile motion, and trajectory pages. They're plenty of nice equations ready to use. And although I was slightly saddened that I had admitted defeat in a way and just looked up the answer. The equation's were very similar to the ones I had devised. I had a big comment written at the top of my code that GRAVITY IS M/S^2, but for some reason I didn't square the time in my calculation for figuring the new z position of the projectile. But besides that, my equation was correct. My velocity updating line of code was correct though, so I was quite happy. From this figuring out the maximum distance was a matter of either running trials of different time increments or solving an equation. Or the third option not allowed by any homework ever done by a person. Look up a calculator on the internet that displays all the information when you enter the right numbers. I opted for the third approach and then tested it against my code by running the simulation to that time and making sure the z went to 0.
The only thing I'm still unsure of, is terminal velocity. Which is, unfortunately, rather annoying to figure out when you have no idea what the dimensions of your hypothetical projectile is. So I had to determine what I was actually firing (a small bullet with a mass of .04 kg is what I believe I decided on, with a cross section of 50mm^2, traveling through standard sea level air density (1.5 kg/m^3) with a drag coefficient of (.1) (a small ball by the examples I found, although it had a bullet (.3)) it gave a ridiculous terminal velocity (small that is not large) so I decided to go with my turret firing perfectly round projectiles.
So, after all that and 1 and a half cramped notebook pages later, I have an update position function I feel like I can rely on for my calculations.
Tommorow, after doing some trivial work (required course work for a required class which pertains to my major in no way), I'll start working on the estimation process and how it leads it's target through 3 dimensions. But for now, more reading Gödel, Escher, Bach by Douglas Hoftstadter (I'm a little more than halfway through the book )
3Dimensional Turret Beginning
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
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
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.
Subscribe to:
Posts (Atom)
