# Aiming from a gun

The article describes a simple gun guidance algorithm when the axis of rotation does not intersect with the axis of the barrel. Quickly find the finished did not work - I had to remember school times. Well, since I remembered - it’s a sin not to share with the others. And someone somewhere, having saved time, uses it to create something else useful ...

The result of the above calculations is used in an arcade game. There were no misses in a month. If you are developing a simulator, you will likely need something more sophisticated.

So the gun is given. When hovering, the turret can rotate in horizontal (red axis of rotation in the figure) and vertical (green axis) planes.

Properties of the gun model

1. The axis of the barrel (indicated in blue) does not intersect with the axes of rotation.
2. The axis of the barrel is parallel to the axis X
3. The direction of the shot coincides with the positive direction X
4. The horizontal rotation axis (red) is parallel to the Z axis, or the axes coincide
5. The vertical rotation axis (green) is parallel to the Y axis, or the axis coincide
6. The top of the model corresponds to the positive direction of the Z axis

For successful aiming at the target, it is necessary to calculate two rotation angles - horizontal and vertical.

Due to the given model limitations (the rotation axes are parallel or coincide with the corresponding coordinate axes), the task of finding the pointing angles in three space can be divided into two in two-dimensional:

1. The angle of horizontal rotation can be found using the projection of the model onto the XY plane (Z = 0)
2. The angle of vertical rotation is the XZ axis (Y = 0)

Both subtasks will be solved equally.

First, consider the starting position

A - the center of rotation. The result of the projection of the axis of rotation on the perpendicular plane.

B - projectile launch point.

Fire - the projection of the direction vector of the shot.

C - position of the target

Define the angle between the shot vector and AB.

Hereinafter, the angular brackets will denote the scalar product of the vectors.

Now suppose the guidance is complete. When turning on the target, point B turned into D, and the Fire vector became Fire1.

From here you can find the length of the DC segment using the cosine theorem.

Where we

rewrite the formula so that it matches the general form of the quadratic equation

for the unknown DC

then

of the two resulting DC values, the positive is suitable for us.

If DC turns out to be negative, then the target is inaccessible (using this algorithm at least) for position guidance.

Now we will consider the figure, which depicts the position of the system before and after pointing

Red indicates the desired angle of pointing.

Define the coordinate of point E. This is the coordinate of target C, rotated by the angle of aim relative to center A in the opposite direction. That is, where the gun would go if it fired at the correct distance, forgetting to turn. To do this, move point B in the direction of the Fire vector by the distance DC.

Then the aiming angle is the angle between the vectors AE and AC, which is found through the scalar product

The angle is found, but you need to remember that the aiming point can be anywhere, including “above” the direction of the shot. Those. you need to find another direction of rotation.

Define the orientation of the vectors AE and AC. Two vectors are not enough for this. We need a third vector perpendicular to the first two. We will call it V. It is precisely its direction that determines the position of the observer who sees a turn clockwise or counterclockwise, depending on which side of the plane with the vectors AE and AC is. Since we work with projections onto coordinate planes, the desired vector will be parallel to the third coordinate axis.

For definiteness, consider an example of projection onto the XY axis. In this case, the perpendicular vector will have the coordinates (0, 0, z), where z is not equal to zero. With the z sign you need to decide. When constructing projections, I proceeded from the fact that I am on the positive side along the Z axis, so z> 0 The

orientation of the vectors is determined by the sign of the determinant, made up of coordinates. The first line is the coordinates of the rotated vector, the second is the vector to which the first and the third are rotated V. Vector. For our case (AE rotates to match AC) this is

Since we determined that z will be strictly positive, its sign will not affect the sign of the result

That is, the final angle of rotation for the case of projection onto the XY axis, this

Similarly, the direction of rotation is calculated for the projection onto the XZ axis.

Well, and the last remark. Turning angles cannot be calculated completely independently of one another. The tower can make a full rotation (from -180 to 180 degrees) around the horizontal axis, but around the vertical it is usually impossible (it will look as if the cannon has come off the base and turned over. It is logical to limit the vertical rotation to the range from -90 to 90 degrees.

Implement this the simplest way:

1. Find the angle of horizontal rotation
2. Rotate the target by this angle around the axis of horizontal rotation in the opposite direction
3. Calculate the vertical angle of guidance for the new coordinates of the target.

Or (which is essentially the same thing) you can first rotate the tower horizontally, and then calculate the angle of vertical rotation.

Now the gun will hit.