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

#### 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?

#### Attachments

• 41.6 KB Views: 238

#### mfb

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.

#### Buffu

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?

Do you have a coordinate system in the game ?

#### kolleamm

Do you have a coordinate system in the game ?
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 )

#### Drakkith

Staff Emeritus
2018 Award
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.

#### jedishrfu

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