> For the complete documentation index, see [llms.txt](https://docs.flatredball.com/flatredball/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.flatredball.com/flatredball/api/flatredball/math/mathfunctions/angletoangle.md).

# AngleToAngle

### Introduction

Returns the shortest distance to rotate from the first argument angle to the second argument angle. All values are in radians. The absolute value of the return value will never be greater than PI (half of a circle). If the value is positive then the shortest distance is achieved by rotating counterclockwise. If the value is negative then the shortest distance is achieved by rotating clockwise.

### Turning towards angle

The AngleToAngle method can be used to turn one object toward a direction. The following code creates a Sprite which will turn toward the point where the cursor is located. Add the following using statements:

```
 using FlatRedBall.Math;
 using FlatRedBall.Gui;
```

Add the following at class scope:

```
 Sprite sprite;
```

Add the following to Initialize after initializing FlatRedBall:

```
 sprite = SpriteManager.AddSprite("redball.bmp");
 sprite.ScaleX = 4;
```

Add the following to Update:

```
 float cursorX = GuiManager.Cursor.WorldXAt(0);
 float cursorY = GuiManager.Cursor.WorldYAt(0);

 float differenceX = cursorX - sprite.X;
 float differenceY = cursorY - sprite.Y;

 if (differenceX != 0 || differenceY != 0)
 {
     double angle = Math.Atan2(differenceY, differenceX);
 
     float angleDifference = (float)MathFunctions.AngleToAngle(
         sprite.RotationZ, angle);
 
     const float minDifferenceForSmoothing = .04f;
 
     if (Math.Abs(angleDifference) < minDifferenceForSmoothing)
     {
         sprite.RotationZ = (float)angle;
     }
     else
     {
         const float rotationSpeed = 2;
         sprite.RotationZVelocity = Math.Sign(angleDifference) * rotationSpeed;
     }
 }
```

![AngleToAngle.png](https://951240982-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M_fye9Ufg3vzJxwX5Hk%2Fuploads%2Fgit-blob-14701e46d80f3e46df250031066a5bfc80e73fde%2Fmigrated_media-AngleToAngle.png?alt=media)

### Range Checks

The AngleToAngle can be used to check if one object is within an angle range. For example, you may be making a stealth game where the player must avoid being seen by enemies. We'll assume that the enemies have a property called ViewAngle which represents the edge-to-edge angle that the enemies can see. We'll also assume that this code has calculated the angle from the enemy to the player. For information on how to calculate this, see [this page](https://github.com/flatredball/FlatRedBallDocs/tree/main/frb/docs/index.php#Angle_Between_Two_Points).

```
// angleToPlayer is the angle from the enemy to the player
// enemy.RotationZ is the angle that the enemy is facing (assuming that the enemy is facing right when RotationZ = 0)

float angleToAngle = FlatRedBall.Math.MathFunctions.AngleToAngle(enemy.RotationZ, angleToPlayer);

// Since enemy.ViewAngle is edge-to-edge, center to edge is enemy.ViewAngle/2.0f
float angleFromCenterToEdge = enemy.ViewAngle/2.0f;

// If the absolute value of the angleToAngle is less than angleFromCenterToEdge, then the player is in view
bool playerIsInView = System.Math.Abs(angleToAngle) < angleFromCenterToEdge;
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.flatredball.com/flatredball/api/flatredball/math/mathfunctions/angletoangle.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
