How to establish which side of a square a ray will intersect?

In summary, to determine which side of a square a ray will intersect, first establish the ray's origin and direction. Calculate the intersection points between the ray and each side of the square by using the equations of the lines that define the square's boundaries. Check the parameters of these intersection points to ensure they lie within the segment of the square's sides. The first valid intersection point encountered in the direction of the ray indicates the side of the square that the ray will intersect.
  • #1
CGandC
326
34
Consider some ray ## \bar{r} ## that starts at point ## A=(a_x,a_y) ## and faces some direction and consider an upright square ( i.e. it's not rotated ) at some location:

1703275450289.png
Question: if we let the ray continue until hitting the square, how can we detect which face of the square was hit? is there a mathematical equation relating between ## \bar{r} ## and the normals of the square that allows me to deduce the intersected face?

I read ( here https://forum.unity.com/threads/how...-a-ray-hit-i-e-raycast-only-backfaces.769556/ ) that checking the ray's direction against the normals of the square can give me the answer but I'm not sure how. ( I know any point on the ray will be of the form ## ( a_x + \lambda\cdot r_x , a_y + \lambda\cdot r_y ) ## for some ## \lambda \in \mathbb{R} ## but couldn't reach some useful conclusion with this )

I know there is an algorithmical way of establishing this (based on an algorithm presented here https://permadi.com/1996/05/ray-casting-tutorial-7/ ) and I understand it but I want a more "mathematical" way of establishing which face of the square was hit, an equation perhaps.
 
Mathematics news on Phys.org
  • #2
conceptually trivial, algebraically very messy. You'll need two equations, with the first comparing the slope of the ray to that of the slopes of the lines from (ax,ay) to (x3, y3) and that of the line from (ax,ay) to (x1,y1). That equation will be of a form like X1 < A < X2. If that inequality holds true then the left face gets hit. Then you need a second such equation for the bottom face.
 
  • Like
Likes CGandC and Drakkith
  • #3
Make the equation of your ray in the form ##f(x,y)=0##. For any face of the square ##(x_a,y_a)(x_b,y_b)##, the ray hits it iff ##f(x_a,y_a)f(x_b,y_b)<0##.
 
  • Like
Likes CGandC
  • #4
phinds said:
conceptually trivial, algebraically very messy. You'll need two equations, with the first comparing the slope of the ray to that of the slopes of the lines from (ax,ay) to (x3, y3) and that of the line from (ax,ay) to (x1,y1). That equation will be of a form like X1 < A < X2. If that inequality holds true then the left face gets hit. Then you need a second such equation for the bottom face.
I understand, thank you so much!

Hill said:
Make the equation of your ray in the form ##f(x,y)=0##. For any face of the square ##(x_a,y_a)(x_b,y_b)##, the ray hits it iff ##f(x_a,y_a)f(x_b,y_b)<0##.
thank you so much, I just a have question regarding this method, I hope you can help me because I want to understand:
The equation of a normal vector ## (x,y) ## to the vector ## r=(r_x,r_y) ## will be ## f(x,y) := x\cdot r_x + y\cdot r_y = 0##, is this the ray equation ( i.e. the ## f(x,y) ## ) you were talking about? if it is so, then why multiplying two equations (i.e. ## f(x_a,y_a)f(x_b,y_b)<0 ## ) of a plane ( straight line in this case since we're at 2D ) will allow me to deduce which face the ray intersected? I just don't understand the correctness of the theorem
 
  • #5
Another "conceptually trivial, algebraically very messy."
  • Calculate the points of intersection between the sides of the square and the line. Two of these will be on the square (i.e. between the corners)
  • Calculate the distance between each of the two points you just calculated and the line's starting point.
  • Pick the smallest one.
 
  • Like
Likes CGandC
  • #6
CGandC said:
is this the ray equation ( i.e. the f(x,y) ) you were talking about?
No, it is not. What I mean is this. The equation of the line is ##y=ax+b##. Make it ##y-ax-b=0##. This is the ##f(x,y)##.
For any ##(x,y)## on the line, ##y-ax-b=0##. For any ##(x,y)## on one side from the line, ##y-ax-b>0##. For any ##(x,y)## on the other side, ##y-ax-b<0##. The ray intersects a face if the endpoints of the face are on opposite sides from the line, i.e., one has ##y-ax-b>0## and another has ##y-ax-b<0##. The product of these two is ##<0## iff the endpoints are on opposite sides from the line.
 
  • Like
Likes CGandC
  • #7
Given that you know your square's sides are parallel to the axes, just solve for the intercept with the horizontal and vertical lines you get be extending the square's edges seems the simplest approach, no?
 
  • Like
Likes CGandC
  • #8
Vanadium 50 said:
Another "conceptually trivial, algebraically very messy."
  • Calculate the points of intersection between the sides of the square and the line. Two of these will be on the square (i.e. between the corners)
  • Calculate the distance between each of the two points you just calculated and the line's starting point.
  • Pick the smallest one.
Very clever indeed. Thank you.

Hill said:
No, it is not. What I mean is this. The equation of the line is ##y=ax+b##. Make it ##y-ax-b=0##. This is the ##f(x,y)##.
For any ##(x,y)## on the line, ##y-ax-b=0##. For any ##(x,y)## on one side from the line, ##y-ax-b>0##. For any ##(x,y)## on the other side, ##y-ax-b<0##. The ray intersects a face if the endpoints of the face are on opposite sides from the line, i.e., one has ##y-ax-b>0## and another has ##y-ax-b<0##. The product of these two is ##<0## iff the endpoints are on opposite sides from the line.
I understand perfectly now, very insightful, thank you.

Ibix said:
Given that you know your square's sides are parallel to the axes, just solve for the intercept with the horizontal and vertical lines you get be extending the square's edges seems the simplest approach, no?
I agree with you, finding the first point of intersection of the ray with the square and then checking which edge/line it belongs to ( this is done by checking inequalities for different edges similar to those @phinds mentioned ) is the simplest.
In general I wanted to know the different approaches people might take to this problem since I've built my own ray caster in Java and it uses an approach similar to what @Vanadium 50 proposed. I needed to know which face of the square/wall my ray was hitting in the simulation in order to texture them right; after that I thought to myself how would I be able to texturize world objects / arbitrary polygons in a graphics simulation when a ray hits them ( since I'll need to know the face of the polygon the ray had hit ) and the answers here established basis for the answer to this question. Thanks alot!
 

FAQ: How to establish which side of a square a ray will intersect?

1. What is a ray in geometry?

A ray in geometry is a part of a line that starts at a specific point, called the endpoint, and extends infinitely in one direction. It is typically represented with an arrow indicating the direction of extension.

2. How do I define the square's sides mathematically?

A square can be defined by its vertices in a Cartesian coordinate system. For a square with bottom-left corner at (x1, y1) and side length 's', the vertices are (x1, y1), (x1 + s, y1), (x1 + s, y1 + s), and (x1, y1 + s). The sides can then be represented as line segments connecting these vertices.

3. What is the process to determine which side of the square a ray intersects?

To determine which side of the square a ray intersects, you can follow these steps: First, express the ray as a parametric equation. Then, for each side of the square, derive the equation of the line segment representing that side. Next, solve for the intersection points by substituting the ray's equation into the line segment equations. Finally, check if the intersection points lie within the bounds of the segments to confirm a valid intersection.

4. What tools or methods can be used to find the intersection?

You can use analytical geometry methods, such as solving linear equations, or computational geometry techniques, like ray-casting algorithms. Programming languages and libraries that support geometric calculations (like Python with NumPy or geometric libraries) can also be helpful for more complex scenarios.

5. What should I do if the ray does not intersect the square?

If the ray does not intersect the square, it means that the ray is either completely outside the square or is parallel to one of its sides without crossing it. In such cases, you can analyze the ray's direction and position relative to the square's vertices and sides to understand its spatial relationship with the square.

Back
Top