- Home /
Calculate parabolic shot...
Hi, Im having some troubles, not actually coding (maybe) but perhaps math problems, Here's the thing. I have some kind of AI who makes shoots at you in a parabolic trayectory, now, I dont know how to succesfully calculate the force neded to apply into te proyectile in order to reach the target, I only know the distance betwen the shoter and the target with Vector3 target; Vector3 distanceFromTarget = target - transform.position; im giving some "randomish" angle to shot, based on the obstacles the AI have on his way but giving some angles more than 0 and below than 90, i cannot calculate the initial velocity. Im using Unity Physix for the bullets, when I shoot them it works well, I can manually calculate my trayectory and do some "Test and error" shots but, the AI must be capable of calculate the same parameters with a formula. Here's the code i have right now:
void GetElevationDirection()
{
targetPosition = currenTarget.transform.position;
vectorResultante = targetPosition - cannonPlace;
estimatedElevation = Random.Range(30, 50);
float finalAngle = estimatedElevation * 2;
finalAngle = Mathf.Sin(finalAngle * Mathf.Deg2Rad);
float gx = 9.81F * vectorResultante.magnitude;
estimatedSpeed = Mathf.Sqrt(gx / finalAngle);
//estimatedSpeed *= 1.25F;
estimatedElevation *= -1;
}
I need to multiply at the end the "estimatedElevation" variable because when I rotate the 3D model of the cannon, it have inverted X axis and in that way the cannon points up and not down, but the estimated speed, is to low, even with the 25% increment, and somethimes I get an error on the Matfh.Sqrt because the "gx / finalAngle" division gets negative. I hope that someone can help me...
Answer by KrisSchnee · Apr 01, 2014 at 07:10 AM
I dont know how to succesfully calculate the force neded to apply into te proyectile in order to reach the target, I only know the distance betwen the shoter and the target... i cannot calculate the initial velocity.
It sounds like you should look up the math of projectile motion. The relevant equations should be available in physics textbooks on paper or the Web. You'll want to look at the situation like this, I think:
If you have a cannon firing with initial velocity described as Vx and Vy (horizontal and vertical axes), then the distance traveled horizontally at time T is X = (Vx) * T. The current height Y = (Vy)*T - (.5)gT^2, where "g" is the acceleration of gravity (usually estimated as 9.81 m/s^2 near Earth's surface, but you'll have to translate to Unity units) and "T^2" is T squared.
To find the initial velocity required to hit some point XY, not considering any obstacles in the way, you need to solve this "system of equations" for Vx and Vy. I would define a cannon's power not as Vx and Vy directly, but Vo (original velocity) and an angle Theta up from the ground. (0-180 degrees or 0 to pi radians.) Then, you can find Vx and Vy as "Vo cos Theta" and "Vo sin Theta". Now for any given velocity, you can just solve for Theta, or vice versa. One option then is to check several possible velocities and see if any Theta value will hit the target at that velocity.
If you're looking to calculate force rather than initial velocity, the math gets a bit more complex, but figure that F = ma, and you're applying this force over some time period (realistically, the time that the cannonball is flying out of the cannon) to get enough acceleration over time to get the velocity you want.
As for angles going negative, can you take the absolute value of the result?
Hi, thanks for the fast answer, i do have a formula that I get trough internet, that calculates the initial velocity given by the cannon at the shot momentum, knowing that the Vy will be Zero because reaching the target will mean that the proyectile did all the trayectory, the only problems i have are: Calculate the initial velocity of the proyectile neded to reach the X$$anonymous$$ax knowing the Theta angle and the Xmax, and second, Solve and uknown error on the function wich gives me negative values somethimes on the division, givin me an error on the $$anonymous$$atfh.Sqrt function. Thanks
If you need to, i can post the full script here, which calculates the Y rotation of the cannon in order to point the target, and then throw a random angle and then calculate the initial velocity then shot, i can post it as soon as i get home later, just one thing, Im from $$anonymous$$exico and the scrip would have some english and some spanish terms if you don't $$anonymous$$d so, i can upload the C# script :)
Your answer
Follow this Question
Related Questions
how to make the cannon fire yourself 1 Answer
Shot is activated only when I walk. 2 Answers
How Do I make it harder to move rigid body? 1 Answer
How to reverse a sin animation? 2 Answers
mecanim gun shot frame 1 Answer