I am currently working on raytracing. I have problem with view Ray collisions. I cant figure out how to get intersection point of ray and plane, to be more precise, my problem is not figure out intersection point of ray vs plane, problem is to convert this coordinate into uv coordinate(this rectangle can be rotated anyhow in world) for texture mapping. I know One point on this rectangle, its normal and bounds.
-
1This looks more like a math question than a specific programming question. – πάντα ῥεῖ May 01 '19 at 19:46
-
1Do you have a local coordinate system of the rectangle? How is it represented? – Nico Schertler May 02 '19 at 02:36
-
Hello, i dont understand what tou mean by local coordinate system. I know only its size and middle point of this rectangle and normal – Haluzman May 02 '19 at 06:47
-
@Haluzman that is far from enough to even render it ... either you got the 4 points or you got its size and [coordinate system aligned to it](https://stackoverflow.com/a/28084380/2521214) ... just a point+normal will tell you only its plane ... for 1st case take a look at this [Reflection and refraction impossible without recursive ray tracing?](https://stackoverflow.com/a/45140313/2521214) It supports triangles and spheres ... so rectangle is 2 x triangle .... the UV is obtained from barycentric. For 2nd case you just convert global position to local and rescale to texture size ... – Spektre May 02 '19 at 08:48
1 Answers
We have 4 vertices of a rectangle lying on a sphere:
A - top left
B - top right
C - bottom right
D - bottom left
Center of the sphere:
O
And intersection point on the sphere inside rectangle ABCD:
I
The idea is to identify all sides of the triangle AID
, because it will allow us to know the coordinates of the point I
on the plane. So if we move the rectangle on the plane with A(0, rect.height)
and D(0, 0)
then point I
could be found by solving the following system of equations:
x^2+y^2=DI^2 - circle equation with center in point D and radius DI
x^2+(y-rect.height)^2=AI^2 - circle equation with center in point A and radius AI
from which it follows that:
y = (DI^2-AI^2+rect.height) / (2*rect.height)
and x
could have 2 values (positive and negative), however we are interested only in positive value, because only it will be inside the rect.
x = sqrt(DI^2-(DI^2-AI^2+rect.height)/(2*rect.height))
Then UV could be calculated the following way uv(x/rect.width, y/rect.height)
However length of AI
and DI
still not known, but could be calculated using formula of Great-circle distance
AI = (Radius of the Sphere) * (Angular orthodromy length must be in radians)
Radius of the Sphere = sqrt((O.x - A.x)^2+(O.y - A.y)^2+(O.z - A.z)^2)
Angular orthodromy length = arccos(sin(a1)*sin(a2)+cos(a1)*cos(a2)*cos(b2-b1))
a1 is angle AOA1, where A1(A.x, O.y, A.z)
b1 is angle O1OA1, where O1(O.x, O.y, A.z)
a2 is angle IOI1, where I1(I1.x, O.y, I.z)
b2 is angle O2OI1, where O2(O.x, O.y, I.z)

- 1,639
- 1
- 15
- 26
-
@Spektre I would say that knowing the normal and the boundary points of the rectangle(and I assume that they are known) is more than enough to rotate rect to (0,0,0). However, at least one boundary point (e.g top left) must be distinguished from the others. – eparvan May 03 '19 at 08:05
-
Hello, thanks for commenting, to be more precise I know one point, size of rectangle I want it to be (width and height of it) and normal which is rotated somehow in the scene. And I need this rectangles to be lying on sphere. So I have these points where are they placed, their rotated normal, they are pointing on origin and sizes of the rectangle. – Haluzman May 03 '19 at 13:23
-
@Haluzman so rectangle is imposed on a sphere and you know its boundary points(and can distinguish them), right? – eparvan May 03 '19 at 14:15
-
1@Haluzman then if you know the radius of a sphere on which a rectangle is imposed you may calculate arcDistance(topLeft, intersection) and arcDistance(bottomLeft, intersection). This way on the plane you will have a triangle with all sides known and finding uv coordinate of the third vertex becomes trivial task – eparvan May 03 '19 at 15:07
-
@Haluzman you should edit your question to add this info as its crucial to answering this (right now your question is unanswerable) ... maybe also sample rectangle and sphere... If you can add screenshot or sketch ... – Spektre May 04 '19 at 06:18