Archived

This topic is now archived and is closed to further replies.

DotProduct

This topic is 4958 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello. I''m looking at this function that would return the intersection between a plane and a ray bool getIntersect(Vector3 *linestart, Vector3 *lineend, Vector3 *vertex, Vector3 *normal, Vector3 *intersection, float *percentage) { Vector3 direction, L1; float linelength,dist_from_plane; direction.x = lineend->x - linestart->x; direction.y = lineend->y - linestart->y; direction.z = lineend->z - linestart->z; linelength = D3DXVec3Dot( &direction,normal ); if ( fabsf(linelength) < 0.0001) { return false; } L1.x = vertex->x - linestart->x; L1.y = vertex->y - linestart->y; L1.z = vertex->z - linestart->z; dist_from_plane = D3DXVec3Dot ( &L1, normal ); *percentage = dist_from_plane / linelength; if ( *percentage < 0.0f ) { return false; } else if ( *percentage > 1.0f ) { return false; } else intersection->x = linestart->x + direction.x * ( *percentage ); intersection->y = linestart->y + direction.y * ( *percentage ); intersection->z = linestart->z + direction.z * ( *percentage ); return true; } I can''t understand how linelength = D3DXVec3Dot( &direction,normal ); can be the lenght of a vector. Also dist_from_plane = D3DXVec3Dot ( &L1, normal ); is not so clear for me... Someone can help me ??

Share this post


Link to post
Share on other sites
Linelength is not the length of the vector, it''s the length of the vector projected onto the plane normal. I''m not sure how to explain this further, but I''ll give you a couple of examples: if the line is parallel to the normal, the projection is the length of the line, and if the line is perpendicular to the normal, the projection is zero.

The idea is to find the projection of the line onto the plane normal, and then to find the projection of the vector from a point on the plane to the line start on the plane normal. This gives you the distance from the start to the end of the line, and the distance from the start of the line to the plane, in terms of the plane normal.

The ratio between these two is your parametric value that you can use to find the intersection point .

Share this post


Link to post
Share on other sites
I know that the projection of a Vector V1 onto another Vector N is: < V1,n > * n where means dotProduct. So to find the length of the vector projected onto the plane normale, i will have to find the lenght of < V1,n > * n (where n is the normal and V1 is the direction vector) that i can find with pitagora ! How could this be equal to

linelength = D3DXVec3Dot( &direction,normal ); ?????

Share this post


Link to post
Share on other sites
The dotproduct gives you the length of the components of a vector that are in the same direction as the normal. The length isn''t really the actual length in 3D space. However, this algorithm works because if you draw a diagram right triangles are actually formed.

Let''s just say you already knew the exact point where the start and end ray hits the plane. This point minus the start point gives you the vector from the start point to where the intersection occurs. You can then find the length of this vector, its length is startlength.

Do the same from the end point to the same point of intersection, this is endlength.

startlength divided by totallength gives you some fraction.
totallength is startlength plus endlength. So, what you''ve got is:

startlength/(startlength + endlength)
This works peachy.

However, using the dotproduct is equivalent because the dotproduct returns the magnitude of one vector, *in the direction of another*. You can think of this as finding a local coordinate system, defined by the unit vector (X, Y, and Z are also just simply unit vectors, and when you expressed a vertex in terms of X, Y, and Z, you are also doing dotproducts if you think about it). The crucial thing to understand here is that the dotproduct gives the length of the perpendicular projection onto the plane normal, which forms a right triangle.

The start dist is DotProduct(StartPoint, PlaneNormal) - PlaneNormalDist (this is plane equation except most people are used to a plus sign, which is just a difference in how the plane normal is computed)

The end dist is DotProduct(EndPoint,PlaneNormal) - PlaneNormalDist

Because of the fact that the dotproduct is the perpendicular projection of the vector onto the normal

start dist / (start dist + end dist)
(or the absolute value of it, because your start and end point will given values of different sign if it crosses the plane)
is going to be exactly equal to what I said above.

This is hard to explain and I cannot access my ftp to draw a stupid picture...that would make it easier.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Hi. There is article in GDNet, witch discuss about your problem:

http://www.gamedev.net/reference/articles/article1026.asp

Share this post


Link to post
Share on other sites