I am working with a model where I have to calculate a perpendicular to a vector from p1 and p2 (3d) at point p3 on the line joining these points.
The arrangement would be some thing like this p1--------p3---------p2 . Some times this p3 may coincide with p1 or p2.
I know how to calculate a perpendicular vector to vector p1-p2 in general using the dot products and deciding ratios. But how to make it passing through this point p3 ?
I will be calculating in Geometry shader .. !
Any Ideas .. ?
There are a few misunderstanding:
A vector has no location : a perpendicular vector is nowhere in space, it is just a direction
To determine a normal vector, the usual way is to use a cross product, not a dot product (although you can still get away with dot products with some algebra, like generating a random vector and removing its tangential component)
You may want to create a line segment that originates from p3 and which is perpendicular to p2-p1 : in that case, since you mention that you are already able to generate a normal vector (let's call it V) then drawing such a line would consist in putting a vertex at p3 and another vertex at p3+a*V where "a" is any positive value that will determine the length of your segment
WhitAngl is correct. You are also unconstrained in your vector. There is a plane P passing through point p3 (perpendicular to p1->p2), and any vector contained in P will satisfy your conditions. If the particular vector doesn't matter, I've used the following technique:
start with the x axis vector (1,0,0)
project onto plane P
if projection is zero-length, pick y axis vector (0,1,0) and try again, or z axis and try again
The Plane equation for P can be derived directly from p1 and p2 (||p2-p1|| is the Normal for the plane, then use p3 as the defining point for P).
Related
i am programming a C++ collision detection in my game and am trying to come up with an algorithm:
I have a capsule defined with two center points (C1, C2), length and a radius. Then i have a ray defined with two points (R1, R2). I already know that they are intersecting. I just need to find inner part of ray that is contained in capsule (H1-H2). Thanks in advance for all the help.
First let's take a look at a diagram for reference:
The procedure for calculating H1 and H2 is as follows:
Compute the intersection, if any, between the ray R and the line
segment P1P2. We're only interested in intersections that lie
within the interior of P1P2. Similarly for P3P4. The points P1 to P4 can be calculated easily from the circle centers, C1 and C2, and some vector math. E.g. P1 = C1 + r*nC, where nC is the normal (CCW) of the unit vector from C1 to C2. This
answer on SO provides the necessary math to determine if an
intersection exists between two line segments and, if so, calculate the parameter h, such
that H=R1+h(R2-R1), where H is an intersection point. This step can produce 0, 1, or 2 valid h values, depending on whether the ray intersects neither, one, or both of P1P2, P3P4.
Compute the intersection points, if any, between the ray and each of the 2
circles. Again, an SO answer provides the necessary math for
ray to circle intersection. Each circle can produce 0, 1, or 2
intersections, again represented parametrically.
If no valid h values were generated from steps 1 & 2 then the ray does not intersect the capsule. Otherwise, calculate hMin and hMax, the min and max parameter values of all valid
intersections identified in steps 1 & 2. Note that it's possible that hMin==hMax, in the case where the ray is tangent to one of the circles and does not intersect P1P2 or P3P4. The required intersection point(s) can now be calculated as H1=R1+hMin(R2-R1) and H2=R1+hMax(R2-R1).
I'm afraid my language of choice is Java rather than C++, but hopefully you'll find the code (IDEOne) I put together helpful as a reference. Please be aware that no effort was put it to handle robustness issues resulting from the rounding of double values during calculations.
Assuming a 3D model made by three equidistant points and origin(0,0,0) set at their center. Distance between these points is known. These 3D points are seem by a camera which projection matrix is known. I can access each point (x,y) position on the camera plane. I am trying to retrieve these points' 3D coordinates and partially build a model matrix.
So far, when I multiply a 2D point by the projection matrix I can find their correspondent 3D positions, but I am not sure how to proceed or what exactly to look for in order to help me to establish a relationship between these 3 points to recover the 3D model (scale, rotation) of this object.
Already tried to calculate the normal in order to get orientation using similar approach to an answer on this thread but not there yet.
I am aware three points might be too few information for a single solution, but since the object is equilateral, rotation on the axis point the camera is not relevant.
You can first unproject each of the three 2D points into 3D space with different depths (using the inverse projection matrix). Then, you have for each 2D point a ray, on which the actual point may lie. Let's represent the ray with
x(t) = p + t * d,
where t is a scalar parameter, p1 is one of the unprojected points, and d is the difference between the two unprojected points. Then, given the known distance of the 3D points r, you want to find their locations of the rays. I.e., you want to find the parameters of the three rays t1, t2, t3 such that
(p1 + t1 * d1 - p2 - t2 * d2)^2 = r^2
(p2 + t2 * d2 - p3 - t3 * d3)^2 = r^2
(p3 + t3 * d3 - p1 - t1 * d1)^2 = r^2
Unfortunately, this is pretty hard to solve analytically. If I am not mistaken, you would need to solve a polynomial of degree 8, which does not have an analytic solution.
So, you are left with numerical solvers. I would start with something simple and try Newton's method. Start with a reasonable positive t vector.
I am looking for some assistance on creating an algorithm that is able to calculate a bounding box for an arc. Typically, this would be a simple problem. However, I am required to have one of the edges of the box to be the line that connects the starting point and the ending point of the arc.
I am having difficulty in developing an algorithm that is able to compute the additional two points of the rectangle.
Here is a picture of what I am attempting to calculate:
The two purple dots, I need to develop an algorithm that will determine these two locations. The green dots are the known points that can be inputs. Additionally, I do know the radius and the center of the arc.
I would need the algorithm to handle the different cases for when line AB is vertical, has a + slope, and has a - slope.
I have been considering a few directions. For example, I know that the line through point E is parallel to line AB. Since it is parallel, that means they will have the same slopes and the line from point A to the purple point is perpendicular to this line. I can then consider the intersection point of the line through E and this perpendicular line.
This method seems messy because then I would need to consider the cases for when the slope of line AB is infinite and 0. I am wondering if there is some algorithm that could account for that automatically (or not even consider the slope at all for line AB)
You claim to know points A, B, C, D, E, and that the amplitude of the sustaining angle of the circular arc does not exceed 180° (semi circle).
Let P1, and P2, the two points that complement the bounding box - (in the drawing, P1 is the purple point above A, and P2 the one above B).
Using vectors:
E-C = vector perpendicular to segment AB', i/e subtract C from E; its magnitude is equal to the distance EC.
P1 = A + (E-C)
P2 = B + (E-C)
Bounding Box = Rectangle A, P1, P2, B
If you need a closer fit, you can replace vector (E-C) with vector (D-C) to place the bounding segment P1P2 tangent to D
In three.js, the constructor for the Math Plane takes 2 inputs:
normal -- (Vector3) normal vector defining the plane pointing towards the origin
constant -- (Float) the negative distance from the origin to the plane along the normal vector
Can someone provide an illustration or explain how this works? I can understand given a point and normal how to construct a plane, or 3 co-planar points, but can't figure out how a normal vector and constant can be used.
TL:DR?
Mathy-ness and linear algebra
So planes in 3 dimensional space can be defined as a 2-dimensional infinite rectangle that falls on 3 points (what you know)
It can also be defined by a perpendicular (normal) vector and a constant of how far from the origin the plane is.
Three.js take the normal vector (a vector that is perpendicular to the plane you want) and basically applies linear algebra to find the plane, then moves it the constant distance away from the origin.
Math Calculation Explanation:
If we have a vector A and B that are orthogonal (perpendicular) then their dot product is 0. SO if we use this principle we can actually take a known Vector X and find 2 orthogonal Vectors Y and Z that will be co-planar (due to orthogonality properties) by backwards solving X (dot) Y = 0 and X (dot) Z = 0
Now we have 2 co-planar vectors to make our plane that we set the distance of the constant away from the origin
(think how vectors have an origin and an endpoint. If the co-planar vectors share an origin, then we have 3 points: 2 ends, and 1 origin, aka 3 points to make a plane.)
Math Theory Explanation on why this works ahead:
I can't draw very well (without pen and paper to show), but basically, think about a vector in 3D space. Now think about all the vectors that can be perpendicular to it. Basically, that creates an infinite amount of perpendicular vectors rotated in a circle perpendicularly to the original, and if we span them infinitely, we have created a plane.
If you ever have an opportunity to take a linear algebra class, I would highly recommend it. It is extremely interesting, very related to computer graphics, and explains a lot of 3D space math that THREEjs uses
What is the unit to measure distence between any 2 points in an image of a spherical object.
For example the distance between the 2 red points on the tennis ball.
NOTE: As a matter of fact, the "Euclidean distance" cannot be used, since ball is non-Euclidean. Ball is almost spherical, the imaging system projects the ball surface on a plane, where the image elements are not equispaced, neither represent equal areas. The true Euclidean distance depends on the actual position of the points with respect to the camera.
General idea
Supposing the picture takes an isometric projection of space, we can measure coordinates on the picture as if they were coordinates on a plane.
From there we can transform them into an arbitrary ball-centric spherical coordinate system, and then we'll easily get the distance between them.
Definitions
Let us suppose you know the radius r of the sphere. We'll use a coordinate system centered at the center of the sphere, with x the direction orthonormal to the plane projection induced by the picture (thus the vector comes right out of the photo). Then the directions y and z are in the picture, let's take y horizontal and z vertical. See drawing for reference.
Then the spherical coordinate system induced by this is such that we have a distance to the centre which is always r on the sphere, and 2 angles theta and phi :
(source: motionscript.com)
.
Now we can convert each point into spherical coordinates and compute the distance between them.
Convert to spherical coordinates
For each point, the z coordinate is the vertical distance on the picture between the point and the horizontal line that cuts the ball in two equal halves. Express it in terms of r, the radius of the ball, thus z = c * r, with c in [-1,1], negative if the point is below the line, positive if above.
We know that z = r * cos(theta), so theta = arccos(c). Since theta is in [0,Π], no special cases here.
Now measure y in the same way, which is the horizontal distance (to the right is positive) between the point and the vertical line cutting the ball in 2. With y = r * b, and b in [-1,1].
We need theta's sine, which is sin(theta) = sqrt(1 - c*c), then it comes that phi = arcsin( b / sqrt(1 - c*c) ). Because we can see the point on the picture, we know that it has x > 0 by definition of our coordinate system. That means that phi is in [-Π/2,Π/2], so again, no tricks or surprises in the trigonometry here.
Distance between points on a sphere
Well everything is explained in this math exchange question, because most great-arc distances are expressed in terms of latitude and longitude, which use different conventions.
Now we replace elements of the formula in term of the c1, c2, b1 and b2 we previously computed :
The formula you eventually get is , where cos-1 is also known as the arccos function.
I won't delve into the detail (especially because it's such a pain to include latex from a mobile app), but the steps are :
expand the difference inside the cosine
in the only non-trivial term transform the phi's cosines with sqrt(1-sin2)
push both sin(theta) of that term inside the square root, some multiplying with the sin(phi) will give you the b squared terms
express remaining squared sines under the square root as 1-cos2
The final unit of measure will be in whatever unit you express r.
As you can see, you only need the radius at the end, after the arccos (for bs and cs, you only need the size of r and respectively ys and zs on the picture, not in the physical world).
Then, if you are only going to compare distances of points on that same sphere, you may simplify by r, and compare the angles between points at the center of the sphere (i.e. use only the arccos's result without multiplying by r), since these angles are proportional to the arc's distances on the sphere. Your unit of measurement would then be in radians.