I am testings if a ray intersects with a triangle so for the time being im using the following code to test if there is an intersection between a specified ray which at this point has a direction the midpoint of a random triangle:
Ray<float> *ray = new Ray<float>(Vec3<float>(0), chosenTriangle->GetTriangleMidpoint());
Along side is the Vec3 object which im using to store the vector operations:
template<typename T>
class Vec3
{
public:
T x, y, z;
Vec3() : x(T(0)), y(T(0)), z(T(0)) { }
Vec3(T xx) : x(xx), y(xx), z(xx) { }
Vec3(T xx, T yy, T zz) : x(xx), y(yy), z(zz) {}
Vec3& normalize()
{
T nor2 = length2();
if (nor2 > 0) {
T invNor = 1 / sqrt(nor2);
x *= invNor, y *= invNor, z *= invNor;
}
return *this;
}
Vec3<T> operator * (const T &f) const { return Vec3<T>(x * f, y * f, z * f); }
Vec3<T> operator * (const Vec3<T> &v) const { return Vec3<T>(x * v.x, y * v.y, z * v.z); }
T dot(const Vec3<T> &v) const { return x * v.x + y * v.y + z * v.z; }
Vec3<T> operator - (const Vec3<T> &v) const { return Vec3<T>(x - v.x, y - v.y, z - v.z); }
Vec3<T> operator + (const Vec3<T> &v) const { return Vec3<T>(x + v.x, y + v.y, z + v.z); }
bool operator == (const Vec3<T> &v) { return x == v.x && y == v.y && z == v.z; }
Vec3<T> operator - () const { return Vec3<T>(-x, -y, -z); }
T length2() const { return x * x + y * y + z * z; }
T length() const { return sqrt(length2()); }
Vec3<T> CrossProduct(Vec3<T> other)
{
return Vec3<T>(y*other.z - other.y*z, x*other.z - z*other.x, x*other.y - y*other.x);
}
friend std::ostream & operator << (std::ostream &os, const Vec3<T> &v)
{
os << "[" << v.x << " " << v.y << " " << v.z << "]";
return os;
}
the chosen triangle and the ray have the following values where vertA, vertB and vertC are the vertices of the triangle and are found in an object which represents a triangle.
and the code which computes if there is an intersection between a specified ray and an intersection is the followin, this code is found inside the triangle object method where vertA, vertB and vertC are global variables.
bool CheckRayIntersection(Vec3<T> &o, Vec3<T> &d)
{
Vec3<T> e1 = vertB - vertA;
Vec3<T> e2 = vertC - vertA;
Vec3<T> p = d.CrossProduct(e2);
T a = e1.dot(p);
if(a == 0)
return false;
float f = 1.0f/a;
Vec3<T> s = o - vertA;
T u = f * s.dot(p);
if(u < 0.0f || u > 1.0f)
return false;
Vec3<T> q = s.CrossProduct(e1);
T v = f * d.dot(q);
if(v < 0.0f || u+v > 1.0f)
return false;
T t = f * e2.dot(q);
return (t >= 0);
}
I still get a false returned from the function, but im presuming it should return a true since a vector passing through the midpoint of the triangle should intersect the triangle at some point. Can anybody enlighten me whats wrong in my code? or if the test is good to return a false
Vec3
's methods wrong, since your implementation has the shape of the one I referred to – Rerito