Reputation: 2244
I found a solution for the ray plane intersection code in Wikipedia, which works and where I simply solve a linear equation system.
Later I found some code for a point onto plane projection, which is obviously implemented differently and also yields different solutions under certain conditions.
However, I do not really get what is the difference between a projection of a point along a vector and the intersection of a ray (build by the point and vector). In both cases I would expect just to find the point where the ray intersects the plane?!
Is there anywhere a figure to illustrate the difference?
struct Plane {
glm::vec3 _normal;
glm::vec3 _point;
};
glm::vec3 intersection(const Plane &p, const glm::vec3 &point, const glm::vec3 &direction) {
// See: https://en.wikipedia.org/wiki/Line%E2%80%93plane_intersection
const float t = glm::dot(p._normal, point - p._point) / glm::dot(p._normal, -direction);
return point + t * direction;
}
glm::vec3 orthogonalProjection(const Plane &p, const glm::vec3 &point, const glm::vec3 &direction) {
// from https://stackoverflow.com/questions/9605556/how-to-project-a-point-onto-a-plane-in-3d
const float t = -(glm::dot(point, direction) - glm::dot(p.getOrigin(), p.getNormal()));
return point+ t * direction;
}
Upvotes: 0
Views: 1043
Reputation: 350
For those interested, here's a c# version of the intersection function above.
Thanks @dgrat you made my day.
/// <summary>Calculates the intersection of a ray with a plane</summary>
/// <param name="rayOrigin">Any point of the ray</param>
/// <param name="rayDirection">The direction of the ray</param>
/// <param name="planeOrigin">Any point of the plane</param>
/// <param name="planeNormal">The normal of the plane</param>
/// <returns>The intersection</returns>
private Point3D Intersection(Point3D rayOrigin, Vector3D rayDirection, Point3D planeOrigin, Vector3D planeNormal)
{
var length = Vector3D.DotProduct(planeNormal, rayOrigin - planeOrigin) / Vector3D.DotProduct(planeNormal, -rayDirection);
return rayOrigin + length * rayDirection;
}
Upvotes: 0
Reputation: 867
A ray is an infinite line, so it has a direction. Intersecting a ray with a plane means finding where the line passes through the plane.
A point is a dimensionless dot suspended somewhere in space. Projecting a point onto a plane means shooting a ray that passes through the point and is perpendicular to the plane (called the "normal"), and seeing where it lands.
The ray already has a direction, the point doesn't. The direction chosen to project the point is the one perpendicular the plane simply because that's how the projection is defined.
So you can have a special case where the ray's direction and the plane's normal coincide, in which case intersecting the ray with the plane and projecting a point that happens to lie somewhere on the ray lead to the same result, but that's just a special case.
Upvotes: 1