Maths Confusion for Ray Creation

Started by
1 comment, last by Xrystal 17 years, 10 months ago
Can someone confirm I am understanding this correctly ? Cross Product of 2 vectors (A and B) result in a 3rd vector that is perpendicular to the two (ie splitting the angle in two or as I initially thought a line making a right angle)? x = Ay * Bz - By * Az y = Az * Bx - Bz * Ax z = Ax * By - Bx * Ay or Cross(A,B) Dot Product of the 2 vectors result in the length of the aforementioned vector ? Ax * Bx + Ay * By + Az * Bz or Dot(A,B) Normalizing returns the direction vector and not the length ? t = Sqrt(x*x + y*y + z*z); x /= t; y /= t; z /= t; I understand this is for a single vector with each point multiplying itself or are these the 2 initial vectors being multiplied against each other ? I am using the website (http://www.euclideanspace.com/maths/algebra/vectors/) to understand all this stuff so bear with me. It says that sqrt(Cx*Cx + Cy*Cy + Cz*Cz) is the distance from the ray to the sphere center (target to test). I assume the Cx,Cy,Cz values are the co-ords for the center of the sphere as the site doesn't say this explicitly. Looking at the formula above it looks like this is the first stage of normalising but not completely or are they just missing that part assuming we know to include the rest of the normalise process. It then says to multiply the Ray Original End Point (unit length) to that resulting value to retrieve the new end point. However I am not clear on the original end point. Is that the RayDir that is gained from normalize the Start point. | Vx | = Dx * sqrt(Cx*Cx + Cy*Cy + Cz*Cz) | Vy | = Dy * sqrt(Cx*Cx + Cy*Cy + Cz*Cz) | Vz | = Dz * sqrt(Cx*Cx + Cy*Cy + Cz*Cz) where Dx,Dy,Dz is the original Ray End Point. It then says that the following is the distance from the new V vector to the Spheres? C Vector. sqrt( (Cx-Vx)^2 + (Cy-Vy)^2 + (Cy-Vy)^2) And then with the following formula it is stating that if the radius is more than the aforementioned distance then there is an intersection. R > sqrt( (Cx-Vx)^2 + (Cy-Vy)^2 + (Cy-Vy)^2) Now trying to get this into code form is a bit confusing but lets see. Step 1 : Generate the Ray Values

RayStart = Mouse.X,Mouse.Y,Mouse.Z = 0 or should Z be the near value?
RayDir = RayStart.Normalize
RayEnd = Mouse.X, Mouse.Y, Mouse.Z = 1 or should Z be the far value?


From various sites and forums I have read it seems this is the starting code for generating a ray from the mouse point into the screen. Step 2 : Pick a Target and store its distance from Ray

TargetDistance = sqrt(TargetX*TargetX + TargetY*TargetY + TargetZ*TargetZ)


I still don't understand how that formula knows where the Ray is without us telling it. Step 3 : Generate the New Ray End Point from the Targets Distance

RayEnd2X = RayEndX * TargetDistance
RayEnd2Y = RayEndY * TargetDistance
RayEnd2Z = RayEndZ * TargetDistance


Step 4 : Generate Distance from RayEnd2 to the Target

RayToTarget = sqrt( (TargetX-RayEnd2X)^2 + (TargetY-RayEnd2Y)^2 + (TargetY-RayEnd2Y)^2)


Hmm, why isn't that 3rd element for Z part of vector. Step 5 : Test if Intersected Hmm example only shows Sphere Intersection and I cannot think of equivalent for triangles so this is what the sphere test is shown as.

Intersected = SphereRadius > RayToTarget


Step 6 : If not intersected return nearest point, again I cannot see how a non Sphere would work here.

NearestPoint = TargetDistance - SphereRadius


Is this pretty much all there is to generate a ray and pick a point in the screen or is there something else missed off between the steps ? Thanks to anyone taking the time to read this and I look forward to any replies.
Advertisement
Quote:Original post by Xrystal
Can someone confirm I am understanding this correctly ?

You have a lot of question so hopefully I answer them all.

Quote:Cross Product of 2 vectors (A and B) result in a 3rd vector that is perpendicular to the two (ie splitting the angle in two or as I initially thought a line making a right angle)?

In R^3 space it will. Also, remember these two important exceptions to the rule. It will result in a zero vector if one of the vectors has zero length, or both vectors are colinear.

Quote:Dot Product of the 2 vectors result in the length of the aforementioned vector?

Ax * Bx + Ay * By + Az * Bz

Sometimes but not always. It's the projected length of one vector on to another. If vector B is normalized and vector A is facing the same direction then you will get the length of vector A and that is really the only case.

Though, this little trick is used a lot to get the magnitude squared of a vector. If you dot(a,a) then you'll get ||A||^2. (The two surounding || || represent the symbol for magnitude.) This is a useful trick because when your comparing distance you can compare it squared or non-squared and the comparison works the same.


Quote:Normalizing returns the direction vector and not the length ?

t = Sqrt(x*x + y*y + z*z);
x /= t;
y /= t;
z /= t;

Not quite correct. Normalizing a vector returns the same vector, except with a unit length of one. Also, be careful of the case of zero vectors, you'll get an exception if you try to divide x,y,z by t if t = 0.

Quote:I understand this is for a single vector with each point multiplying itself or are these the 2 initial vectors being multiplied against each other ?

I don't understand what you're asking.

Quote:It says that sqrt(Cx*Cx + Cy*Cy + Cz*Cz) is the distance from the ray to the sphere center (target to test). I assume the Cx,Cy,Cz values are the co-ords for the center of the sphere as the site doesn't say this explicitly. Looking at the formula above it looks like this is the first stage of normalising but not completely or are they just missing that part assuming we know to include the rest of the normalise process.
That formula is for finding the distance from the origin to the center of the shere. That is the distance formula. It's derived from the pythagorus theorum of a^2 + b^2 = hypotonus^2. It also works in R^3 space. If you write that formula out using the sphere's center as the x and y and solve for the hypotonus then you'll get the same formula they have above.

Quote:It then says to multiply the Ray Original End Point (unit length) to that resulting value to retrieve the new end point. However I am not clear on the original end point. Is that the RayDir that is gained from normalize the Start point.

| Vx | = Dx * sqrt(Cx*Cx + Cy*Cy + Cz*Cz)
| Vy | = Dy * sqrt(Cx*Cx + Cy*Cy + Cz*Cz)
| Vz | = Dz * sqrt(Cx*Cx + Cy*Cy + Cz*Cz)

where Dx,Dy,Dz is the original Ray End Point.

If you look at the picture and at the red line. Imagine creating a vector from the origin to the center of the sphere. Then sweeping it in a circle, the end point of that vector would draw that red line. What they are doing is just making our ray(which is normally unit length) and stretching it so it has the same magnitude as the distance of the center of the sphere. We want to make sure the ray's end point falls on that red line.

To use numbers. Imagine the ray pointed straight up Ray<0,1>. If the sphere was 5 units away, then we would want the ray's end point to fall on that red line. We would have to multiply the Ray<0,1>*5 = Ray<0,5>.

Quote:Step 6 : If not intersected return nearest point, again I cannot see how a non Sphere would work here.
*** Source Snippet Removed ***

Good observation. This type of intersection test only works for spheres.
Thanks for that.

Since posting this I found a MSDN forum posting from someone with a similar problem as me and they managed to find the problem which worked out to be the same as mine. I can now create a ray visually that looks correct (at least its now in the same quad area). Next step is to figure out the picking functionality but I will surely keep a note of your comments they helped alot.

Quote:
I understand this is for a single vector with each point multiplying itself or are these the 2 initial vectors being multiplied against each other ?


What I meant with the above is that in some of the formulae they used Ax By etc but in that one particular forumla they used a simple x each time. I could only deduce that in that particular instance the x,y,z values were from a single vector point and not 2 as with the Ax, By, Az etc.

Sorry to be so queryable but I prefer to understand how something works than just use it because it does.

This topic is closed to new replies.

Advertisement