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

  • Context: Undergrad 
  • Thread starter Thread starter CGandC
  • Start date Start date
  • Tags Tags
    Intersection Vectors
Click For Summary
SUMMARY

This discussion focuses on determining which face of an upright square a ray intersects when projected from a point A=(a_x, a_y). The method involves using mathematical equations to compare the ray's direction with the normals of the square's faces. Specifically, the ray's equation is expressed as f(x,y)=0, and the intersection is established by checking the product of the function values at the endpoints of the square's sides. The conversation highlights the importance of calculating intersection points and using inequalities to ascertain the intersected face.

PREREQUISITES
  • Understanding of ray casting principles in 2D geometry
  • Familiarity with vector mathematics and normal vectors
  • Knowledge of linear equations and inequalities
  • Basic programming skills, particularly in Java for ray casting implementations
NEXT STEPS
  • Study the mathematical foundations of ray-surface intersection algorithms
  • Learn about normal vector calculations and their applications in 2D geometry
  • Explore advanced ray casting techniques in graphics programming
  • Investigate texture mapping methods for polygonal surfaces in graphics simulations
USEFUL FOR

This discussion is beneficial for game developers, graphics programmers, and anyone interested in implementing ray casting techniques for collision detection and rendering in 2D environments.

CGandC
Messages
326
Reaction score
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
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   Reactions: CGandC and Drakkith
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   Reactions: CGandC
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
 
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   Reactions: CGandC
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   Reactions: CGandC
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   Reactions: CGandC
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 a lot!
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 33 ·
2
Replies
33
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 21 ·
Replies
21
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
1
Views
3K
  • · Replies 35 ·
2
Replies
35
Views
11K
  • · Replies 13 ·
Replies
13
Views
3K