# B Find line slope based on degrees (for video game programming)

1. May 30, 2017

### kolleamm

I'm trying to code a script for a character in my game. This will tell the character if the player is in front or behind them.

Basically I know the coordinates of the character and their angle of rotation (if rotated 45 deg, that is where they are looking)

Perpendicular to their angle is a line that defines the location that is out of view to them. How can I find the slope of this line with just a single point and an angle?

#### Attached Files:

• ###### IMG_2961.jpg
File size:
41.6 KB
Views:
37
2. May 30, 2017

### Staff: Mentor

The dot product of two vectors (e. g. your direction of view and the direction to the object) is positive if the angle between them is smaller than 90 degree.

If you want different angles, you can also calculate the angle explicitly based on the dot product.

3. May 30, 2017

### Buffu

Do you have a coordinate system in the game ?

4. May 30, 2017

### kolleamm

Yes I do. I'm guessing I will have to use the law of sines since I know all of the angles and only one side (who's value does not matter, lets just say 1 )

5. May 30, 2017

### Drakkith

Staff Emeritus
If you already know the angle of the character, then simply compare that angle with the angle of a line drawn between the character and the player.

To get the slope of the line: $\frac{Y_2-Y_1}{X_2-X_1}$, where 1 is the character and 2 is the player.
Then find the tangent of the slope to get the angle.

6. May 30, 2017

### Staff: Mentor

Also take care that $x_2 - x_1$ is not zero.