1

I try to write an method bool intersect(const Ray& ray, Intersection& intersection) that returns true, when the Intersection is inside the Triangle.

What i've done so far , is check if there are Points on the Plane, that is created by 2 Vectors of the Triangle.

The Problem is now to check, if the Point is inside the Triangle.I use barycentric Coordinates

Vec3 AB = b_-a_;
Vec3 AC = c_-a_;
double areaABC = vec_normal_triangle.dot(AB.cross(AC));

Vec3 PB = b_-intersection.pos;
Vec3 PC = c_-intersection.pos;
double alpha = vec_normal_triangle.dot(PB.cross(PC));

Vec3 PA = a_-position.pos;
double beta = vec_normal_triangle.dot(PC.cross(PA));
double gamma = 1-alpha-beta;

if((beta+gamma) < 1 && beta > 0 && gamma > 0) {
    return true;
}

Actually its not even a triangle, just about random Points. Can someone explain me or knows how i compute the barycentric Coordinates for 3 given Vectors?

greedsin
  • 1,252
  • 1
  • 24
  • 49
  • 1
    What do you want ? The barycenter ? What barycentric coordinates are ? Or how to test whether a ray intersects a convex polygon ? In the latter case, see [this other question](http://stackoverflow.com/questions/4497841/optimal-algorithm-if-line-intersects-convex-polygon). Remember that a convex polygon with 3 vertices is always a triangle ! So you can just read the answers of that question with `n=3` – Cimbali Jan 20 '15 at 19:50

1 Answers1

2

Assuming vec_normal_triangle is the vector computed as AB.cross(AC) normalized (in other words, the triangle's normal), you should divide alpha and beta by areaABC to get the barycentric coordinates of the intersecton point.

double alpha = vec_normal_triangle.dot(PB.cross(PC)) / areaABC;

and

double beta = vec_normal_triangle.dot(PC.cross(PA)) / areaABC;

This normalizes alpha and beta so that your computation of gamma and comparison against 1 make sense.

I'd also like to make a suggestion. To avoid recomputation and make the code a bit cleaner you could replace your test with the following.

if(alpha > 0 && beta > 0 && gamma > 0) {
    return true;
}

Aside from that, I see that you first use intersection.pos and then position.pos. Is this intentional? My guess is that you need to use intersection.pos both times.

pixcel
  • 66
  • 5