# Finding necesary Muzzle Velocity?

1. Sep 3, 2009

### sciwizeh

First off, this isn't Homework so there is no question per se. I'm having deriving something that probably was in my Physics I/II course, but I forgot.

Here's my problem... I have two cannons (points) in 2D space, I'm not using vectors, just separate scalars for both the x and y coordinates I have a constant angle (generated randomly) and what i want to try to find are the x and y velocities. I need no time dependency in the formulas.

so far i think i have the y velocity but I'm not sure, and I'm at a loss to get the x velocity:

from the formula: v^2=V0^2+2g(r-r0), I was able to get v0=sqrt(2g(r-r0)-v).

but I don't want any dependence on v or time, so I'm slightly stuck.

(I'm trying to write AI for a 2D cannon computer game)

2. Sep 3, 2009

### kuruman

To find two numbers, namely the x and y velocities, you will need some input parameters. You should know that, without input numbers, you cannot expect an algorithm to produce output numbers. So you are looking for the x and y velocities knowing what quantities?

In a simulation, such as the one you are suggesting, one assumes that the initial velocity components are known and then one lets the trajectory evolve from these using the kinematic equations. Or if you want to put a cannon shell over a castle wall, then your input parameters are the distance of the cannon from the wall and the height of the wall.

What are yours? Also, how can you have an animation without time dependence? I am curious...

3. Sep 3, 2009

### Jebus_Chris

I assume that you want 1 cannon to shoot at the other cannon. There are actually an infinite number of (x,y) velocity pairs. If you have a very small x velocity, then you would need a very large y velocity to reach the cannon and vice versa.

4. Sep 3, 2009

### sciwizeh

Well, as for inputs there will be the difference in x coordinates and the difference in y, I will have it generate a random angle for the cannon to fire at (between 0-90 of course)
and all I want to find is the required velocities for hitting the target. there will be terrain, but I already have it destroyable, so getting around it isn't the problem.

the animation is dependent on time, I'm using very simplistic integration that only knows the elapsed time from the last step:
ballVY += GRAV*dt;
ballX += ballVX*dt;
ballY += ballVY*dt;

but i want to know the required initial velocity before the animation begins, so I can start it as such.

5. Sep 3, 2009

### kuruman

If the projectile lands at the same height from which it was launched, the range (horizontal distance) is given by

$$R=\frac{v_{0}^{2}sin(2\theta)}{g}$$

Note that you can get the same range for 45o-θ (howitzer trajectory) as for 45o+θ (mortar trajectory).

6. Sep 3, 2009

### sciwizeh

good to know, solving for v0 would be exactly what i need, but they are likely to be at different heights, so what if they have different heights? that is what is really stumping me. i do have the height of each.

7. Sep 3, 2009

### kuruman

If you have the maximum height (h) and the range (R), then a useful formula is

$$tan\theta=\frac{4h}{R}$$

This is independent of the initial speed. Is that what you were after?

8. Sep 3, 2009

### sciwizeh

not quite,
this sums it up (hopefully my poor drawing abilities get the point across):

9. Sep 3, 2009

### kuruman

I understand. It's getting late where I am. I will come up with an answer in a few hours.

10. Sep 4, 2009

### kuruman

When the firing cannon is higher than the other one, the angle is given by

$$tan\theta=\frac{v^{2}\pm \sqrt{v^{4}-g(gR^{2}-2v^{2}h)}}{gR}$$

Here

v = muzzle speed of cannonball
R = horizontal distance between cannons
h = vertical distance between cannons

The speed and both distances are positive numbers.

When the firing cannon is lower than the other one, use the same expression but change the term between parentheses under the radical from (gR2-2v2h) to (gR2+2v2h).

There are two things your code should test for before applying this

1. The term under the square root must be positive. If negative there is no angle for the given parameters.
2. If test 1 is passed, check for negative angles. Assuming that the cannons cannot fire below the horizon, negative angles should be discarded.

11. Sep 22, 2009

### sciwizeh

I'm having some trouble implementing this, i keep getting NaN (aka, i). I understand that for some angles there is no velocity that will suffice, but when i don't get NaN the velocity is not enough to reach the other cannon.
this is in java, but it should be easy enough to understand for anyone:
Code (Text):

angle = Math.random()*(Math.PI/2-Math.PI/90);
/**
* Used for AI formula
*          _______________________
*         / gR^2(tan(theta)^2+1)
*  V =   /  ---------------------
*      \/    3(Rtan(theta)(+-)h)
*/

double R = Math.abs(CANNON_1_X-CANNON_2_X);
double g = GRAV;
double tan = Math.tan(angle);
double h = Math.abs(cannon2PosY-cannon1PosY);
double RSqu = R*R;
double tanSqu = tan*tan;
double top = (g*RSqu*(tanSqu+1));
double bottom;
if(cannon1PosY<=cannon2PosY){
bottom = (3*(R*tan+h));
} else {
bottom = (3*(R*tan-h));
}
double frac = top/bottom;
double neededPow = Math.sqrt(frac);

NOTE: the coordinate system is 0 at the top increasing downward

neededPow should be the velocity, later i do a check for NaN, but this isn't giving correct results, do you guys see anything wrong with it?