Formula Needed For Artillery Program

In summary, the formula needed for an artillery program is the ballistic equation, which calculates the trajectory of a projectile in flight. It is used to determine the necessary firing data for an artillery piece and can be adjusted for varying conditions. The formula may vary slightly for different types of artillery, but the basic principles remain the same. It can be used for both short and long-range targets, although its accuracy may decrease for extremely long-range targets. Other factors that can affect the accuracy of an artillery program include the quality of the artillery piece, the skill of the operator, and external conditions such as terrain and weather.
  • #1
MBMorrow
3
0
Hi,
I recently wrote a text version of the old DOS Artillery Duel (or Atari's Cannon Duel) game. In this version, you play against the computer and can control the (projectile) angle and velocity. I'd now like to add one more dimension whereby the height of the cannons randomly changes for each game instantiation. The changes to the math formula is where I need help. The main calculation is in the shot.awk script below. I've also included the other two routines in this game for anyone interested.

cannon.ksh - main program written in Korn Shell. Computer and human take turns shooting at one another

gettarget.awk - a simple awk script to get the target location (in this case the target is your computer enemy)

shot.awk - this is the one where the each angle/velocity are calculated to produce the cannon shot. I have read a lot on physics and gaming forums but just don't quite understand the math to add in the variable factor for the elevation of the cannon. I don't really need help with the programming. Just the math.

Thanks if you can help! -Matt

cannon.ksh
-----------
#! /bin/ksh

currentshot=0
keepshooting=1

# Computer variables
comp_angle=45
comp_hi=100.00
comp_low=1
currentguess=50.00


target=`awk -f gettarget.awk`

# Set level of difficulty
echo " "
echo "Select a level of difficulty: "
echo " "
echo "(e)asy: Displays both computer and human parameters"
echo "(m)edium: Displays only human parameters"
echo "(h)ard: Displays only + or - for human"
echo "(v)ery hard: Same as hard but does not even show you the distance to enemy"
echo " "
read difficulty
if [ "$difficulty" != "v" ]
then
echo " "
echo "Target distance: $target"
fi
echo " "
echo "Input angle (0-90) and velocity (1-100)"
echo " "

while [ $keepshooting -eq 1 ]
do
currentshot=`expr $currentshot + 1`

#
# Human shot
#

echo " "
read parms

angle=`echo $parms | awk ' { print $1 } '`
velocity=`echo $parms | awk ' { print $2 } '`

distance=`awk -f shot.awk $angle $velocity`

if [ $distance -eq $target ]
then
echo " "
echo " "
echo " "
echo " "
echo " "
banner "BOOM!"
echo " "
echo "You hit the target at $distance feet in $currentshot shots! "
echo "Computer was using $comp_angle $currentguess"
echo " "
keepshooting=0
else

echo " "

if [ $distance -gt target ]
then
delta=`expr $distance - $target`
if [ "$difficulty" = "e" ] || [ "$difficulty" = "m" ]
then
echo "+${delta}"
else
if [ "$difficulty" = "h" ] || [ "$difficulty" = "v" ]
then
echo "+ you overshot"
fi
fi
else
delta=`expr $target - $distance`
if [ "$difficulty" = "e" ] || [ "$difficulty" = "m" ]
then
echo "-${delta}"
else
if [ "$difficulty" = "h" ] || [ "$difficulty" = "v" ]
then
echo "- you undershot"
fi
fi
fi

fi

#
# Computer shot
#

# Only take a shot if the human didn't just hit us
if [ $keepshooting -eq 1 ]
then
distance=`awk -f shot.awk $comp_angle $currentguess`

if [ $distance -eq $target ]
then
echo " "
echo " "
echo " "
echo " "
echo " "
banner "BAM!"
echo " "
echo "The computer got you at $comp_angle $currentguess in $currentshot shots! Distance: $target"
echo " "
keepshooting=0
else

echo " "

if [ $distance -gt target ]
then
delta=`expr $distance - $target`
echo " "

if [ "$difficulty" = "e" ]
then
echo "Computer: $comp_angle $currentguess +${delta}"
else
echo "Computer missed!"
fi

# low routine from guess
comp_hi=$currentguess
# need awk for fractions
currentguess=`echo $currentguess $comp_low | awk ' { printf("%2.2f",($1+$2)/2 ) } ' `

else
delta=`expr $target - $distance`
echo " "
if [ "$difficulty" = "e" ]
then
echo "Computer: $comp_angle $currentguess -${delta}"
else
echo "Computer missed!"
fi

# hi routine from guess
comp_low=$currentguess
# need awk for fractions
currentguess=`echo $currentguess $comp_hi | awk ' { printf("%2.2f",($1+$2)/2 ) } ' `

# Check for 99.99 (in order to guess 100) - must convert to int
tcurrentguess=`echo $currentguess | awk ' { printf("%d",$1*100) } ' `
if [ $tcurrentguess -eq 9999 ]
then
currentguess=100
fi
fi

fi
fi

done

exit

gettarget.awk
-------------
BEGIN {
trand=srand()
target=int(1000*rand())+1
printf("%d\n",target)
}

shot.awk
---------
BEGIN {

angle=ARGV[1]
ARGV[1]=""
velocity=ARGV[2]
ARGV[2]=""

gravity=(9.8*-1)

# convert angle to degrees
angle=(angle*3.14159265)/180

# original vector for x and y
vx=velocity*cos(angle)
vy=velocity*sin(angle)

# Midflight (velocity=0, no rise, no fall)
mf=(vy*-1)/gravity

# apex - used for graphics
#apex=(vy*mf)+(0.5*gravity*(mf*mf))

# Fullflight is total time of flight
ff=mf*2

distance=vx*ff

printf("%3.0f\n",distance)
}
 
Physics news on Phys.org
  • #2
</code>The math formula you need to use is the equation of motion in two dimensions. It is given by:x = x0 + v0*t + (1/2)*a*t^2where x is the position of the projectile at time t, x0 is the initial position of the projectile, v0 is the initial velocity, and a is the acceleration due to gravity.You can use this formula to calculate the distance traveled by the projectile given the angle, velocity, and elevation of the cannon. When calculating the elevation, you will need to take into account the fact that the elevation can cause the projectile to have an upward or downward trajectory. You can do this by adding or subtracting the acceleration due to gravity from the initial velocity (v0).For example, if the elevation of the cannon is 10 degrees above the horizon, the initial velocity for the projectile should be v0 + (1/2)*a*sin(10), where a is the acceleration due to gravity. Similarly, if the elevation of the cannon is 10 degrees below the horizon, the initial velocity should be v0 - (1/2)*a*sin(10).Once you have the initial velocity, you can plug it into the equation of motion and solve for the distance traveled by the projectile. Good luck!
 
  • #3


Hi Matt,

Thank you for sharing your project with us. It sounds like you have already put a lot of work into creating your artillery program and are now looking to add a new dimension to it. Adding in the variable factor for the elevation of the cannon can definitely enhance the gameplay experience.

The main calculation in the shot.awk script is where you will need to make the changes to incorporate the elevation of the cannon. In order to do this, you will need to use the basic principles of projectile motion.

Projectile motion is the motion of an object through the air due to the force of gravity. When an object is launched at an angle, it follows a curved path known as a parabola. The key factors that determine the trajectory of the projectile are the initial velocity, the angle of launch, and the force of gravity.

To incorporate the elevation of the cannon into your program, you will need to add the height of the cannon to the initial position of the projectile. This will change the starting point of the projectile and therefore affect its trajectory. You can use trigonometry to calculate the horizontal and vertical components of the initial velocity, taking into account the angle of launch and the height of the cannon.

Additionally, you will need to adjust the gravity in your calculation to account for the elevation of the cannon. The force of gravity will be slightly less at higher elevations, so you will need to adjust the value accordingly.

I suggest doing some research on projectile motion and using some basic trigonometry to incorporate the elevation of the cannon into your calculation. There are also many online resources and tutorials available that can help you with the math and programming aspects.

Good luck with your project!
 

1. What is the formula needed for an artillery program?

The formula needed for an artillery program is the ballistic equation, which calculates the trajectory of a projectile in flight. It takes into account factors such as muzzle velocity, projectile mass, air resistance, and gravity to determine the projectile's path.

2. How is the formula used in an artillery program?

The formula is used to determine the necessary firing data for an artillery piece, such as the elevation angle and charge, in order to hit a specific target at a given distance. It is also used to make adjustments for varying conditions such as wind speed and direction.

3. Is the formula the same for all types of artillery?

No, the formula may vary slightly depending on the type of artillery being used. For example, the formula for a howitzer may differ from that of a cannon or a rocket launcher. However, the basic principles of the ballistic equation remain the same.

4. Can the formula be used for both short and long-range targets?

Yes, the formula can be used for both short and long-range targets. However, the accuracy of the formula may decrease for extremely long-range targets due to factors such as wind and air resistance that are difficult to account for over longer distances.

5. Are there any other factors besides the formula that affect the accuracy of an artillery program?

Yes, there are several other factors that can affect the accuracy of an artillery program, such as the quality and condition of the artillery piece, the skill and experience of the operator, and external conditions like terrain and weather. These all play a role in the successful execution of an artillery program.

Similar threads

Replies
2
Views
2K
Back
Top