Question for Newton Experts
Miscellaneous Forums/General Discussion/Question for Newton Experts
I've posed this question on the Newton SDK forums too, but I suspect most Blitzers won't frequent that forum as a rule ( I know I don't ) so I'll pose it here too. It is specifically for Newton though, so Newton units throughout.
Let's say I'm throwing a grenade. When the character's hand releases the grenade, it is at X0,Y0,Z0 and he is trying to throw the grenade towards point X1,Y1,Z1. What I need to calculate is the impulse vector which will land the grenade as close as possible to that point.
Because the bounce(s) will bring materials into play, that's probably too complex. Let's assume that I just want the first bounce to land on this point. If someone could point me to an answer without mathematical notation ( pseudo code or actual code in any language is fine) then I'd really appreciate it.
I believe John Pickford needed to solve this problem back along, for Naked War.
I seem to remember it requires some calculus, or something?! The thread should still be in the B3D programming forum if you search for it. I think Fredborg and possibly others posted some demo code.
[edit] Odd, I can't seem to find the thread. I definately remember it, though. :/
I didn't actually realise that JP used a physics engine for Naked War. You learn something new, etc etc.
Anyway, I've gone though JP's posting history right up to page 44, which is far as I can go, but I haven't spotted anything. It only goes back four years though, so it may not be far enough. I've tried various other search combinations ( calculus, projectile, ballistic, trajectory, etc ) but no go so far.
I solved this problem long ago for "fun", but doing a little research shows the completed formula is easily available from wikipedia (under "trajectory of a projectile"):
To hit a target at range x and altitude y when fired from (0,0) and with initial velocity v the required angle(s) of launch are:
Just make sure the square root input value isn't invalid - if it is, it means the destination is out of range.
Here's some pseudocode of how you might implement this:
Function CalculateTrajectory:Vec3(start:Vec3, dest:Vec3)
Local destVec:Vec3 = dest - start
Local d:Float = Sqrt(destVec.x*destVec.x + destVec.z*destVec.z)
Local y:Float = destVec.y
Local v:Float = TheVelocityOfYourGrenadeThrow
Local g:Float = TheGravityAccelerationRateNewtonUses
Local sqr:Float = v*v*v*v - g*(g*d*d + 2*y*v*v)
If sqr < 0 Then Return ErrorCode 'Impossible to reach target
Local sqrt:Float = Sqrt(sqr)
Local ratio1:Float = (v*v + sqrt) / g*d
Local ratio2:Float = (v*v - sqrt) / g*d
'Choose the lowest of the two possible trajectories
Local ratio:Float
If ratio1<ratio2 Then ratio = ratio1 Else ratio = ratio2
'Calculate the impulse vector
Local throwVec:Vec3
throwVec.x = destVec.x
throwVec.z = destVec.z
throwVec.y = d * ratio
throwVec.Normalize()
throwVec = throwVec * TheVelocityOfYourGrenadeThrow
return throwVec
EndFunction
(The atan can be eliminated when calculating a impulse vector)
Yeh, that's simple enough.
What about a shrapnel grenade? What's the calculation for it splitting into 200 pieces that fly out at 360 degrees (spherically - "puki" term")?
I didn't actually realise that JP used a physics engine for Naked War. You learn something new, etc etc.
I didn't. I did get some help on here with the algorithm.
Yeah, sorry Gabriel - I didn't mean to imply JP was using a newtonian physics wrapper, or anything. It was just I remember the basic algorithm/formula being discussed, which I thought may have been of use.
Thanks John, but that's not really what I need. Your equation assumes force is a constant, but it's not. It's the force I'm trying to calculate. I'm happy to pass time as a constant, that seems reasonable since we're talking about throwing a grenade. Passing the force as a constant, however, just isn't right. I mean if I gave you a cannonball and an apple and asked you to throw them both the same distance, you would surely try to use a lot more force on the cannonball than the apple.
I assume that I could adapt your equation to account for mass by multiplying the gravity by the mass in every part of the equation. For testing purposes, I just used an object with a mass of 1 for a baseline reading, and it seemed to be correct, but it took a lot of trial and error to get a suitable force vector, and that just wouldn't do in the game. I need every throw to be possible unless I say otherwise. ( IE: If the distance vector is too big, I won't even try. )
I think the problem is that my equation gives you a required velocity, but you want a required force (acceleration). This shouldn't be a problem if Newton lets you set the velocity of an object directly (I know Ageia PhysX does, but I'll assume Newton doesn't).
Or are you saying the velocity at which your projectile will be launched isn't always the same? Normally grenades, cannons, etc. fire at a constant velocity.
Newton will let me set the velocity, it just won't let me achieve what I'm trying to achieve. You're quite correct about cannons, they would in all likelihood have a constant force. What I'm trying to do though is calculate the ability of a human to throw an arbitrary object. A grenade is just an example. If you want to throw a grenade half as far, in a real world situation you wouldn't adjust the angle, you'd adjust the amount of force with which you threw it. The mass is also arbitrary, which pushes me even more down the road of having to have a variable force.
Looking back at my original question, I did only use the word impulse and I should have made it clear that I wanted a force because people who are using other physics engines might well be able to help if I'd phrased it correctly. Sorry about that.
If you want to throw a grenade half as far, in a real world situation you wouldn't adjust the angle, you'd adjust the amount of force with which you threw it.
The problem with that is there is basically an infinite number of ways to launch a trajectory at a specific destination. From a purely mathematical perspective, there's no way you can arrive at a single solution (velocity/angle value) unless you be more specific about just what kind of trajectory you want.
For example, if you wanted a trajectory that passes through a certain point (a window, for example, in the case of throwing a grenade), you could arrive at a single solution. Or if you specify that you want the grenade to be traveling at a minimum velocity on impact, you could probably arrive at a single solution.
As I said, my constant is time ( I'm measuring time in ticks, to make the calculation easier. ) Surely there can only be one possible trajectory which will take exactly that amount of time.
I mean I can say :
XVelocity=(ToX-FromX)/Ticks
ZVelocity=(ToZ-FromZ)/Ticks
I just don't know how to get from velocity to impulse. Do I just multiply by the mass here? If so, then all I need is the YVelocity.
Somewhere in my head, I'm thinking of a formula which involved 1/2 and T Squared ( I can't find the power of 2 symbol in charmap :P ) It should ring a bell too because I must have done this in Tennis Babes. EDIT: NM, found the wikipedia article and it's part of the reasoning behind the formula you gave.
Don't forget to divide by 60 for the time step.
As I said, my constant is time ( I'm measuring time in ticks, to make the calculation easier. ) Surely there can only be one possible trajectory which will take exactly that amount of time.
Ok, somehow I missed that. There should definitely be only one trajectory for a constant "flight" time (although I don't know how you'd calculated it).
EDIT: Somewhere in my head, I'm thinking of a formula which involved 1/2 and T Squared ( I can't find the power of 2 symbol in charmap :P ) It should ring a bell too because I must have done this in Tennis Babes.
Maybe this:
y = (g/2)(t^2) + vt + p
Calculates the Y coordinate of the projectile along a trajectory when given the time after launch, t. Constants are g for gravity, v for the initial Y velocity (at launch), and p for the initial Y position (at launch).