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?


//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!

No comments:

Post a Comment