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

  • Thread starter kolleamm
  • Start date
358
32
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?

Thanks in advance
 

Attachments

33,470
9,204
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.
 
849
145
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?

Thanks in advance
Do you have a coordinate system in the game ?
 
358
32
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
Science Advisor
2018 Award
20,628
4,359
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.
 
11,032
4,533
Also take care that ##x_2 - x_1## is not zero.
 

Related Threads for: Find line slope based on degrees (for video game programming)

Replies
35
Views
5K
Replies
4
Views
1K
  • Last Post
Replies
2
Views
3K
  • Last Post
Replies
15
Views
2K
  • Last Post
Replies
2
Views
5K
Replies
4
Views
10K
  • Last Post
Replies
2
Views
6K
  • Last Post
Replies
5
Views
7K

Hot Threads

Top