Vector Projection through a point and onto a plane - vector

I need to calculate the location of the yellow star on the plane, in flat plane Tangent Space.
So first I need to project a vector from the eye, through the pink square, and find the resulting location on the plane (find the yellow star).
Everything is in tangent space already. So I know the location and direction of the eye, and the location of the pink square, in relation to the center of the plane (tangent space).
The resulting yellow star must be on the plane. So its position with respect to the direction of the blue arrow must be always zero. In the above image, it would have a positive value for its position with respect to the green and red arrows.
I've been trying to figure out how to do this using math such as Vector Projection, but I can't seem to wrap my head around it.

So the camera has 3d location vector r and direction vector e. The plane is defined by z=0 which you use on the equation of the line
z = r_z + t * e_z = 0 } t = -r_z/e_z
the coordinate are then
x = r_x + t * e_x
y = r_y + t * e_y
I assume that everything is already expressed in the desired coordinate system with blue along the z-axis.

Related

How do I calculate the yaw, pitch, and roll of a point in 3D?

Given a point in 3D space, what are the three angles (e.g. Euler angles) needed to transform a line to point to that object?
Imagine I have a line (or a box) in 3D and I want to transform its heading, pitch, and bank to point to the 3D point from the origin, what values would I use for those angles?
I can't figure out the math to calculate the angles to point to a location such as (1,2,3).
Note: Instead of "yaw, pitch, roll", I'm going to use the conventions "heading, pitch, bank" as defined by 3D Math Primer for Graphics and Game Development by Fletcher Dunn.
Firstly, notice that in a 2D coordinate system, you only need a single angle + magnitude to "point" to any point in 2D.
Similarly, in a 3D coordinate system, you only need two angles + magnitude to "point" to any point in 3D. The last angle ("bank" or "roll") does not affect the location of a point in 3D. Instead it "spins" the arrow that would point to it. If the object is 360 degrees symmetrical, you won't see spin affecting the object at all. If the object is not symmetrical (e.g. an airplane) it will affect the object (e.g. tilting one wing towards the ground and the other towards the sky).
So the original question actually becomes, "how do I find the heading angle, pitch angle, and magnitude to "point" to any point in 3D space?"
You can easily figure this out using trigonometry functions. Imagine we have the point (1,2,3) and we're trying to calculate the heading, pitch, magnitude.
For the following example, let's use this diagram, where the left axis is X, up is Y, and right is Z. The point (1,2,3), then is represented by the blue sphere.
1. Find the magnitude
First, let's find the easiest value, the magnitude. Luckily for us, the magnitude (length) between any two points is easy to find no matter how many dimensions we are in, simply by using the Pythagorean theorem. Since we are in 3D and we're calculating the distance from the origin to our point, our distance formula becomes:
magnitude = sqrt(x*x + y*y + z*z)
Plugging in our actual values:
magnitude = sqrt(1*1 + 2*2 + 3*3)
= 3.7416573868
So our magnitude (or length) is ~3.741.
2. Find the heading
Next, to find the heading, notice that we just care about rotation about the XZ plane, and we don't care about the Y-axis at all. If we were to "flatten" the 3D space into 2D, it becomes trivial to find the heading.
We can draw a triangle that forms a 90 degree angle with the X-axis (red triangle) and then calculate that angle. Recall from trigonometry tan(angle) = opposite / adjacent, and solving for angle, we get angle = arctan(opposite / adjacent).
In this case "adjacent" is a known quantity (redAdjacent = x = 1), and "opposite" is known too (redOpposite = z = 3). Instead of using arctan to solve the equation though, we want to use atan2 since it'll handle all the different cases of x and y for us.
So we have:
heading = atan2(redOpposite, redAdjacent)
Plugging in our actual values:
heading = atan2(3, 1)
= 1.249045772398
so our heading is 1.249 rad, or ~72°.
3. Find the pitch
Finally we need to find the pitch. Similarly to what we did with the heading, we can flatten the the 3D space into 2D along the plane that contains these three points: (A) the origin (0,0,0), (B) our point (1,2,3), and (C) our point as it would project onto the XZ plane (1,0,3) (e.g. by setting 0 for the Y-value).
If we draw a triangle between all 3 of these points, you will notice that they form a right-triangle again (green triangle). We can simply calculate the angle using arctan2 again.
We already calculated the green hypotenuse in step 1 (i.e. the magnitude of our vector):
greenHypotenuse = sqrt(x*x + y*y + z*z)
= 3.7416573868
We also know the opposite of the green triangle is the same as the y-value:
greenOpposite = y
= 2
Using the pythagorean theorem, we can find the length of the adjacent angle:
greenOpposite^2 + greenAdjacent^2 = greenHypotenuse^2
y*y + greenAdjacent^2 = x*x + y*y + z*z
greenAdjacent^2 = x*x + z*z
greenAdjacent = sqrt(x*x + z*z)
Notice that another way to calculate the adjacent length of the green triangle is to notice that redHypotenuse == greenAdjacent, and we could find redHypotenuse using:
redHypotenuse^2 = redAdjacent^2 + redOpposite^2
= x*x + z*z
redHypotenuse = sqrt(x*x + z*z)
Plugging in actual values, we get:
greenAdjacent = sqrt(1*1 + 3*3)
= 3.1622776602
So now that we know the adjacent and opposite lengths of the green triangle, we can use arctan2 again:
pitch = atan2(greenOpposite, greenAdjacent)
= atan2(2, 3.1622776602)
= 0.563942641356
So our pitch is 0.5634 radians, or about 32°.
Conclusion
If you were to draw a line from the origin, with length 3.741, heading 1.249 rad, and pitch 0.564 rad, it would extend from (0,0,0) to (1,2,3).

How can I obtain UV coordinates of rectangle in 3D with raytracing method?

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.
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)

Rectangle rotation around clipping rectangle center

I have two rectangles where one is a clipping for the other one.
Now I want to rotate the bigger rectangle around the center of the clipping rectangle and adjust x/y values.
How can I calculate the new x/y values after rotation?
I actually just want to rotate the x/y of the bigger box around the center of the smaller box. So the x/y point of the bigger box is relative to the top/left point of the smaller box. I have the width and height of the smaller box so I can calculate x/y point of the big box relative to the center of small box. The angle to rotate is in degrees. The rotation can be any degree, for example 10.
You can do as follows:
determine the angle by which you want to rotate, make sure it suitable for the trigonometric functions (sin(), cos(), ...), i.e. right angle is usually Pi/2
in case of rotating counterclockwise, it is negative
determine the coordinates of c, as cx,cy
process each of the corners of the rectanlge, one by one, for a total of four
for each corner P, currently at coordinates px,py and to move to px2,py2
determine angle between current P and C, using atan2(py-cy, px-cx)
to get from degrees to radians (for use with trigonometry) calculate radians=(pi*degrees)/180.0
add the desired rotation angle to that current angle, to get newangle
determine the distance of current P to C, sqrt((px-cx)(px-cx) + (py-cy)(py-cy))
multiply the distance (which is not changing by rotation), with the appropriate trigonometric function
px2 = distance * cos(newangle)
py2 = distance * sin(newangle)
If you want to rotate a given point P around a point C, which are defined in the same coordinate system you can use a simple rotation matrix. Calculate the P coordinates with respect to C (subtraction), then apply rotation with the matrix and go back to original coordinates by adding C again.
All that matters is the coordinates of the rotation center and the angle.
The most compact formulation is by means of complex numbers (of which I hope you have some understanding; you actually don't need a complex data type, you can expand the formulas).
Let C be the center and α the angle. Then for any point P, the image Q is given by
Q = (P - C) cis(α) + C
where cis(α) = cos(α) + i sin(α).
The inverse rotation is simply given by
P = (Q - C) cis(-α) + C.

Translation coordinates for a circle under a certain angle

I have 2 circles that collide in a certain collision point and under a certain collision angle which I calculate using this formula :
C1(x1,y1) C2(x2,y2)
and the angle between the line uniting their centre and the x axis is
X = arctg (|y2 - y1| / |x2 - x1|)
and what I want is to translate the circle on top under the same angle that collided with the other circle. I mean with the angle X and I don't know what translation coordinates should I give for a proper and a straight translation!
For what I think you mean, here's how to do it cleanly.
Think in vectors.
Suppose the centre of the bottom circle has coordinates (x1,y1), and the centre of the top circle has coordinates (x2,y2). Then define two vectors
support = (x1,y1)
direction = (x2,y2) - (x1,y1)
now, the line between the two centres is fully described by the parametric representation
line = support + k*direction
with k any value in (-inf,+inf). At the initial time, substituting k=1 in the equation above indeed give the coordinates of the top circle. On some later time t, the value of k will have increased, and substituting that new value of k in the equation will give the new coordinates of the centre of the top circle.
How much k increases at value t is equal to the speed of the circle, and I leave that entirely up to you :)
Doing it this way, you never need to mess around with any angles and/or coordinate transformations etc. It even works in 3D (provided you add in z-coordinates everywhere).

Determine if 3D point is inside 2D Circle

I wish to determine if a Point P(x,y,z) is inside a 2D circle in 3D space defined by its center C (cx, cy, cz), radius R, and normal to the plane the circle lies on N.
I know that a point P lying on a 2D circle in 3D space is defined by:
P = R*cos(t)U + Rsin(t)*( N x U ) + C
where U is a unit vector from the center of the circle to any point on the circle. But given a point Q, how do I know if Q is on or inside the circle? What is the appropriate parameter t to choose? And which coordinates do I compare the point Q to see if they are within the circle?
Thanks.
Project P onto the plane containing the circle, call that P'. P will be in the circle if and only if |P - P'| = 0 and |P' - C| < R.
I'd do this by breaking it into two parts:
Find out if the point is on the same plane as the circle (ie. see if the dot product of the vector going from the center to the point and the normal is zero)
Find out if it's inside the sphere containing the circle (ie. see if the distance from the center to the point is smaller than the radius).

Resources