Home > Enterprise >  Calculating the correct pitch
Calculating the correct pitch

Time:05-10

        public double GetPitchToFace(double Z2, double Z1, double X2, double X1)
    {            
        double Arc;
        Arc = Math.Atan2(Z2 - Z1, X2 - X1);
        return Arc;
    }

I am attempting to work out the correct pitch in order to "face" a specific point.

Using Atan2 (as seen above) seems to return the correct values however in-game the pitch system seems to work rather strangely.

Instead of increasing anti-clockwise from 0 radian all the way back to 6.2 it starts from 0 up to 3 radian then jumps down to -3 radian and works its way back to 0 once again.

Example of Pitch

Hopefully you can understand the bad drawing example above.

I need a way to convert from that strange pitch / radian system to the standard 0 - 6.2 radians in order to return a correct pitch.

Thanks.

CodePudding user response:

Since the Math.Atan2 method returns the angle θ (in radians) such that -π ≤ θ ≤ π it will yield negative values when the input point is in the third or fourth quadrant.

To return only positive values (i.e. 0 ≤ θ ≤ 2π) one can use simple mathematics. Since is a full rotation you can add that whenever the Math.Atan2 method returns a negative value. This will only give you values in your wanted range.

 public double GetPitchToFace(double Z2, double Z1, double X2, double X1)
    {            
        double Arc;
        double theta = Math.Atan2(Z2 - Z1, X2 - X1);
        Arc = (theta >= 0) ? theta : (2*Math.PI   theta);
        return Arc;
    }
  • Related