0

I have a point in 3D space and two angles, I want to calculate the resulting line from this information. I have found how to do this with 2D lines, but not 3D. How can this be calculated?

If it helps: I'm using C++ & OpenGL and have the location of the user's mouse click and the angle of the camera, I want to trace this line for intersections.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
Shawn Buckley
  • 506
  • 6
  • 16

2 Answers2

5

In trig terms two angles and a point are required to define a line in 3d space. Converting that to (x,y,z) is just polar coordinates to cartesian coordinates the equations are:

x = r sin(q) cos(f)

y = r sin(q) sin(f)

z = r cos(q)

Where r is the distance from the point P to the origin; the angle q (zenith) between the line OP and the positive polar axis (can be thought of as the z-axis); and the angle f (azimuth) between the initial ray and the projection of OP onto the equatorial plane(usually measured from the x-axis).

Edit:

Okay that was the first part of what you ask. The rest of it, the real question after the updates to the question, is much more complicated than just creating a line from 2 angles and a point in 3d space. This involves using a camera-to-world transformation matrix and was covered in other SO questions. For convenience here's one: How does one convert world coordinates to camera coordinates? The answers cover converting from world-to-camera and camera-to-world.

Community
  • 1
  • 1
vdbuilder
  • 12,254
  • 2
  • 25
  • 29
  • Thanks for the edit, but I already have the point using GluUnproject. – Shawn Buckley Dec 31 '11 at 15:22
  • @Shawn B Basically, from there you assume the point you have is the Origin create the line with the math above and apply a tranformation matrix to get the line coords in world coords. – vdbuilder Dec 31 '11 at 21:28
  • y = r sin(q) sin(f)? So if the x angle was 0 degrees it would multiply with the y angle to make the answer zero? – john k Nov 24 '19 at 05:36
  • according to this answer, angles 0,0 and 0,1 end up at the same point. – john k Nov 24 '19 at 05:49
2

The line can be fathomed as a point in "time". The equation must be vectorized, or have a direction to make sense, so time is a natural way to think of it. So an equation of a line in 3 dimensions could really be three two dimensional equations of x,y,z related to time, such as:

x = ax*t + cx
y = ay*t + cy
z = az*t + cz

To find that set of equations, assuming the camera is at origin, (0,0,0), and your point is (x1,y1,z1) then

ax = x1 - 0
ay = y1 - 0
az = z1 - 0

cx = cy = cz = 0

so

x = x1*t 
y = y1*t 
z = z1*t 

Note: this also assumes that the "speed" of the line or vector is such that it is at your point (x1,y1,z1) after 1 second.

So to draw that line just fill in the points as fine as you like for as long as required, such as every 1/1000 of a second for 10 seconds or something, might draw a "line", really a series of points that when seen from a distance appear as a line, over 10 seconds worth of distance, determined by the "speed" you choose.

Motomotes
  • 4,111
  • 1
  • 25
  • 24
  • I have the angles of the camera in degrees, what unit should they be in for the calculation? – Shawn Buckley Dec 31 '11 at 06:23
  • The angle information is embedded in the assumption that the camera will be the origin (where the line originates), the point (0,0,0). Given just the angles of the camera or a point in 3D space, assuming camera as origin, one can deduce a 3D line, you don't need both. – Motomotes Dec 31 '11 at 06:32
  • What if the camera weren't at the origin, and instead had a rotation of 45 degrees on both X and Y? – Shawn Buckley Jan 01 '12 at 00:33