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

In summary, the conversation discusses the process of coding a script for a character in a game to determine if the player is in front or behind them. It involves knowing the character's coordinates and angle of rotation, and finding the slope of a line perpendicular to their angle. The suggested method is to use the dot product of two vectors or the law of sines, and to take care that the difference between x coordinates is not zero.
  • #1
kolleamm
477
44
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

  • IMG_2961.jpg
    IMG_2961.jpg
    33.6 KB · Views: 534
Mathematics news on Phys.org
  • #2
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
kolleamm said:
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 ?
 
  • #4
Buffu said:
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, let's just say 1 )
 
  • #5
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
Also take care that ##x_2 - x_1## is not zero.
 

What is line slope?

Line slope is a measure of how steep or flat a line is. It represents the change in the y-coordinate (vertical) for every unit change in the x-coordinate (horizontal). It is typically denoted as m in mathematical equations.

How do I find the slope of a line?

To find the slope of a line, you need to know two points on the line. Then, you can use the formula m = (y2 - y1) / (x2 - x1), where x1 and x2 are the x-coordinates of the two points, and y1 and y2 are the y-coordinates of the two points.

How do I find the slope based on degrees?

To find the slope based on degrees, you can use trigonometric functions. The slope is equal to the tangent of the angle. So, if you know the degrees of the angle, you can use the formula m = tan(θ), where θ is the angle in degrees.

Why is finding the slope important in video game programming?

Finding the slope is important in video game programming because it helps determine the movement of objects on the screen. By knowing the slope of a line, developers can create realistic movements for characters and objects in the game.

What are some real-life applications of finding the slope?

Finding the slope has many real-life applications, such as in engineering, architecture, and physics. For example, engineers use slope to design roads and bridges, architects use slope to design roofs and stairs, and physicists use slope to calculate the speed and acceleration of objects.

Similar threads

Replies
9
Views
1K
Replies
20
Views
3K
Replies
9
Views
972
  • STEM Career Guidance
Replies
6
Views
3K
  • Precalculus Mathematics Homework Help
Replies
1
Views
522
Replies
7
Views
1K
Replies
10
Views
954
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
Back
Top