the real fix for ray-vs-plane intersection

This commit is contained in:
Andrew Meadows 2014-09-12 08:40:03 -07:00
parent ff0872a5f0
commit a9957ac7df

View file

@ -11,6 +11,7 @@
#include "PlaneShape.h"
#include "SharedUtil.h"
#include "GLMHelpers.h"
const glm::vec3 UNROTATED_NORMAL(0.0f, 1.0f, 0.0f);
@ -34,6 +35,18 @@ glm::vec3 PlaneShape::getNormal() const {
return _rotation * UNROTATED_NORMAL;
}
void PlaneShape::setNormal(const glm::vec3& direction) {
glm::vec3 oldTranslation = _translation;
_rotation = rotationBetween(UNROTATED_NORMAL, direction);
glm::vec3 normal = getNormal();
_translation = glm::dot(oldTranslation, normal) * normal;
}
void PlaneShape::setPoint(const glm::vec3& point) {
glm::vec3 normal = getNormal();
_translation = glm::dot(point, normal) * normal;
}
glm::vec4 PlaneShape::getCoefficients() const {
glm::vec3 normal = _rotation * UNROTATED_NORMAL;
return glm::vec4(normal.x, normal.y, normal.z, -glm::dot(normal, _translation));
@ -53,7 +66,7 @@ bool PlaneShape::findRayIntersection(RayIntersectionInfo& intersection) const {
}
} else {
float d = glm::dot(_translation - intersection._rayStart, n) / denominator;
if (d > 0.0f && d < intersection._rayLength && intersection._hitDistance) {
if (d > 0.0f && d < intersection._rayLength && d < intersection._hitDistance) {
// ray points toward plane
intersection._hitDistance = d;
intersection._hitNormal = n;