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;
}