Hitting a target

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

Hitting a target

Postby tod » Tue Sep 13, 2011 5:13 am

Hi. Just a noob question. I have a target some distance away and my character throws a rock. How do I set the initial velocity of the rock for it to hit the target? Must I manually calculate the needed velocity taking into account the gravity and distance, or is there another way?
tod
 
Posts: 29
Joined: Thu Mar 04, 2010 4:39 pm

Re: Hitting a target

Postby Sweenie » Tue Sep 13, 2011 9:55 am

If the rock is expected to always hit the target wouldn't it be easier to just skip the physics part and just calculate a smooth trajectory using a bezier curve and move the rock along it?
Sweenie
 
Posts: 503
Joined: Mon Jan 24, 2005 7:59 am
Location: Sweden

Re: Hitting a target

Postby tod » Tue Sep 13, 2011 12:07 pm

If I already have physics why not use them? Besides, I expect the rock to bounce of stuff, and your proposed solution would mean I use both approaches, in the end.
tod
 
Posts: 29
Joined: Thu Mar 04, 2010 4:39 pm

Re: Hitting a target

Postby JoeJ » Tue Sep 13, 2011 1:30 pm

The formula you want is in the book:
Physics for Game Developers, by David M. Bourg
User avatar
JoeJ
 
Posts: 1489
Joined: Tue Dec 21, 2010 6:18 pm

Re: Hitting a target

Postby Julio Jerez » Tue Sep 13, 2011 1:45 pm

You need to determine the initial velocity and the initial angle of teh shell.
It will be good for you to search for Projectile motion in Google, you will find literally hundreds of entries.
I did it and I found this
http://electron9.phys.utk.edu/phys135d/ ... motion.htm

Read and see if you can figure out, it will be good for you.

If you cannot I can post a function for you to used.

The ideal problem is very easy for solve, however it is not so easy when there are other factors like frictions.
It is hard enough that after war II, the United States wanted to reduce the number of artillery missed with shot mortal to the enemy,
the troops were missing so many time that they started to wonder if they could be more accurate and save more ammo,
so they commission the construction of a special machine to do the calculation, and in 1942 the military made the first electronic computer just for that, the ENIAC.

Basically when you add friction to the problem, it becomes a not linear problem, so the only way to determine the correct angle and velocity is by doing repeated integrations.
I suggest you read the physics behind Projectile motion, and at least come up with the ideal solution, (the friction less case)
Then from there I can post you a small function to do the calculation with friction using the ideal solution as the first Guess, just like the computer on artillery weapons do today.
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Hitting a target

Postby tod » Wed Sep 14, 2011 3:35 am

Thanks Julio,
I'll use the simple case, I don't think friction would be useful. I can manage the physics behind this, my question was if the engine provides some sort of standard functions for this situation, but I guess not.
tod
 
Posts: 29
Joined: Thu Mar 04, 2010 4:39 pm

Re: Hitting a target

Postby Julio Jerez » Wed Sep 14, 2011 8:56 am

the standard function would be the integrator. physics engines do no specialized on special case solution. it si on eh user to write teh driver funtion that will set the initial conditions.

basically whne you crate teh shell, you nee to set it initial velocity vector.
you calculate that usin eh fomulas for teh projectile motion, and teh phisics do teh rest.

on the the "doing teh simple case, you may nee to do the secund part, because physics engines do no use exact integation method to move a particle, they use first order linear aproximation.
so even if ther is no friction in the equation, teh fact that you are using a numerericla integration make the problem non linear as well.

tyr getting the first part and see how it goes, and if is is not good enoght that add the aming refinement. that secudn part is very eassy to add, and make your locic a lot smarter.
in fact you can use it for AI inteligence.
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Hitting a target

Postby tod » Wed Sep 14, 2011 2:45 pm

Hmmm! I've tried the basic approach but I got it all wrong probably :D.
My rock goes straight for the ground.
I used the formula from wikipedia.
But I'm not sure I used it correctly. Mostly the stuff when I convert from the plane formula to the real world coordinates.
I'll try to fix it tomorrow, but in the meantime if someone has a passion for brain teasers, here is my current code (where start is start vector and stop in the end vector).

Code: Select all
Ogre::Vector3 PhysicsManager::computeThrowVelocity(Vector3 &start, Vector3 &stop)
{
   //prepare values for angle computation
   // the distance between the vectors
   Real staightDistance = start.distance(stop);
   // height on y
   Real height = start.y - stop.y;
   // distance on local x
   Real distance = Math::Sqrt(staightDistance*staightDistance - height*height);
   Real speed = getMaxThrowSpeed();
   Real gravity = Math::Abs(SettingsManager::getSingleton().getGravity());
   // compute angles from horizontal
   Radian angle = Math::ATan((Math::Sqr(speed)+Math::Sqrt(Math::Pow(speed,4) - gravity*(gravity*Math::Sqr(distance)+2*height*Math::Sqr(speed))))/(gravity*distance));
   //Radian angle = Math::ATan((Math::Sqr(speed)-Math::Sqrt(Math::Pow(speed,4) - gravity*(gravity*Math::Sqr(distance)+2*height*Math::Sqr(speed))))/(gravity*distance));
   // compute velocity vector
   Vector3 velocity = start - stop;
   velocity.y = velocity.x * Math::Tan(angle);
   velocity.normalise();
   velocity = velocity * speed;

   return velocity;
}


This should compute the velocity I set to the body.
tod
 
Posts: 29
Joined: Thu Mar 04, 2010 4:39 pm

Re: Hitting a target

Postby JoeJ » Thu Sep 15, 2011 3:31 am

The wikipedia formula is much easier to read than that in the book :)
I've implemented it and it seems to work, as far as i can see in my testbed.
I apply the velocity given by GetProjectileToTargetVelocity() using NewtonBodySetVelocity(), and it hits the target nicely.

Code: Select all
inline float GetLaunchAngle (float vel, float grav, float diffH, float diffV)
{
   vel *= vel;
   float r = sqrt (vel*vel - grav * (grav * diffH*diffH + 2.0*diffV*vel));
   float s = vel + r; // two solutions: vel - r would take the lower angle
   float t = s / (grav*diffH);
   return atan(t);
}

inline sVec3 GetProjectileToTargetVelocity (sVec3 target, sVec3 launchPos, float launchVel)
{
   // assuming gravity vec (0, GRAVITY_Y, 0)
   sVec3 diff = target - launchPos;
   float diffV = diff[1];
   float diffH = sqrt (diff[0] * diff[0] + diff[2] * diff[2]);
   // todo: handle out of reach and numerical special cases
   float angle = GetLaunchAngle (launchVel, fabs((float)GRAVITY_Y), diffH, diffV);

   // construct velocity vector to apply to projectile...
   sVec3 vv(diff[0], 0, diff[2]); vv /= diffH; // project to ground plane and normalize
   vv *= cos(angle);
   vv[1] += sin(angle);
   return vv * launchVel;
}


EDIT: velocity construction optimised, this seems also the differing part to your code

Idea: If wind is a constant force, adding it to gravity and rewriting code to handle true 3D gravity vector should work too?
So air friction remains the only unlinear factor, which we don't need to handle.
Here's a nice extension to handle moving target and laucher:
http://www.gamedev.net/topic/599033-projectile-trajectory-on-target/
User avatar
JoeJ
 
Posts: 1489
Joined: Tue Dec 21, 2010 6:18 pm

Re: Hitting a target

Postby tod » Thu Sep 15, 2011 4:45 am

Can you help me understand your code a bit?

I have Y axis is up. You?

Are diff [0],[1],[2] values for X,Y,Z?

What are diffV and diffH?
tod
 
Posts: 29
Joined: Thu Mar 04, 2010 4:39 pm

Re: Hitting a target

Postby JoeJ » Thu Sep 15, 2011 5:00 am

Yes Y=up and [0,1,2]=xyz
diffV = vertical difference (aligned with grav.)
diffH = horizontal diff
gravity is negative and downwards in my case.
User avatar
JoeJ
 
Posts: 1489
Joined: Tue Dec 21, 2010 6:18 pm

Re: Hitting a target

Postby tod » Thu Sep 15, 2011 5:22 am

I understood the first part and it seems the same.
The last part I don't understand.
Code: Select all
sVec3 vv(diff[0], 0, diff[2]); vv /= diffH;

So you take the difference vector and set Y to zero. Then you divide by the X value? To set X value to 1.
Code: Select all
vv *= cos(angle);

Now you multiply with the cosinus?
Code: Select all
vv[1] += sin(angle);


So you get (cos,sin,diff[2]/diff[0]*cos) in the end.
What's the logic behind this?

PS. thanks for your time!
tod
 
Posts: 29
Joined: Thu Mar 04, 2010 4:39 pm

Re: Hitting a target

Postby tod » Thu Sep 15, 2011 5:31 am

I think I found my error.
Code: Select all
velocity.y = velocity.x * Math::Tan(angle);

should be
Code: Select all
velocity.y = distance * Math::Tan(angle);
tod
 
Posts: 29
Joined: Thu Mar 04, 2010 4:39 pm

Re: Hitting a target

Postby JoeJ » Thu Sep 15, 2011 6:08 am

the method i use works by projecting the diff vector to the groundplane (setting upaxis to zero),
then the division normalizes it to unit lenght.
this gives the the cosine direction.
The sine direction is simply upwards (perpendicular to groundplane) so we add (0,1,0) * sine.

EDIT: i thought i've a bug but it's correct.
Last edited by JoeJ on Thu Sep 15, 2011 6:47 am, edited 1 time in total.
User avatar
JoeJ
 
Posts: 1489
Joined: Tue Dec 21, 2010 6:18 pm

Re: Hitting a target

Postby tod » Thu Sep 15, 2011 6:32 am

I didn't test my solution yet. So it's still not working until proved otherwise. :D
tod
 
Posts: 29
Joined: Thu Mar 04, 2010 4:39 pm

Next

Return to General Discussion

Who is online

Users browsing this forum: No registered users and 3 guests