• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
kingnosis

Is this a better point in triangle test (2D)?

7 posts in this topic

So I was looking around for a way to do the old point in triangle test, and I was finding a lot of ways to do it using trig. Then I found one using areas, and this was simple enough, but the only way I knew to do areas was Heron's formula, and that's a lot of sqrts (16 per triangle test, I believe). Then I found a way to do triangle areas using the determinant. No sqrt, no cos, or sin, etc. I'm working from this. Is this sort of thing going to work, or am I missing something? I haven't written any testing code. But I did write a couple of functions (one to calc area, one to compare areas). They are here:

// A floating point
struct fPoint
{
  float x;
  float y;
};

/* IsPointInTri() - 
 * Used by IsPointInQuad(). Function takes the point and the triangle's 
 * vertices. It finds the area of the passed triangle (v1 v2 v3), and then the
 * areas of the three triangles (pt v2 v3), (pt v1 v3), and (pt v1 v2). If the
 * sum of these three is greater than the first, then the point is outside of
 * the triangle.
 */
bool IsPointInTri(const fPoint *pt, const fPoint *v1, const fPoint *v2, const fPoint *v3)
{
  float TotalArea = CalcTriArea(v1, v2, v3);
  float Area1 = CalcTriArea(pt, v2, v3);
  float Area2 = CalcTriArea(pt, v1, v3);
  float Area3 = CalcTriArea(pt, v1, v2);

  if((Area1 + Area2 + Area3) > TotalArea)
    return false;
  else
    return true;
}

/* CalcTriArea() - 
 * Find the area of a triangle. This function uses the 1/2 determinant
 * method. Given three points (x1, y1), (x2, y2), (x3, y3):
 *             | x1 y1 1 |
 * Area = .5 * | x2 y2 1 |
 *             | x3 y3 1 |
 * From: http://mcraefamily.com/MathHelp/GeometryTriangleAreaDeterminant.htm
 */
float CalcTriArea(fPoint *v1, fPoint *v2, fPoint *v3)
{
  float det = 0.0f;
  det = ((v1->x - v3->x) * (v2->y - v3->y)) - ((v2->x - v3->x) * (v1->y - v3->y));
  return (det / 2.0f);
}


0

Share this post


Link to post
Share on other sites
I didn't look at the code, but yes that should work. I'm guessing you're using the signed area of the triangle formed by an edge and the point as a predicate to see which side of the edge the point is on. This is similar to using tetrahedral volumes in 3D, which is a good method.

The only gotcha I can think of is that as the point gets very close to an edge, the predicate result becomes more or less random due to floating point error. In ray/mesh collision this is an issue because rays can 'fall through the cracks' between triangles. The solution to this problem is to use the same predicate result for any edge shared by two triangles.

If you just want to know whether a single point is inside a single triangle, that shouldn't be an issue though. Points on the edge may register as inside or outside, but whether that's a problem for you or not depends on the application.
0

Share this post


Link to post
Share on other sites
I would probably not advise an algorithm like that, or based on angles, precisely because of the innacuracies. There are lots of ways, one of which is to make sure the point's sign from the edge plane being always positive (or negative) for each planes, or the analytical solution invovling barycentric coordiantes, of using plucker coordinates.


P is in the triangle (A, B, C) if it verifies,

P = A + (B - A) * t + (C - A) * u
and
(t < 1, u < 1, 0 < t + u < 1)

I think that's the inequations you have to solve. But really, the plane check should be enough.

bool b0 = (Vector(P.x - A.x, P.y - A.y) * Vector(A.y - B.y, B.x - A.x) > 0);
bool b1 = (Vector(P.x - B.x, P.y - B.y) * Vector(B.y - C.y, C.x - B.x) > 0);
bool b2 = (Vector(P.x - C.x, P.y - C.y) * Vector(C.y - A.y, A.x - C.x) > 0);
return (b0 == b1 && b1 == b2);

should work for both counter clockwise and clocksie triangles.
0

Share this post


Link to post
Share on other sites
oliii, if I'm reading his code right I think he's pretty much doing the same thing as your plane check example.

Unless I'm screwing up the math somewhere (quite possible), the tri area test is equivalent to the perp-dot between two of the tri edges, which is exactly what your 'plane test' is doing.

However (to the OP), it's not necessary to add the areas, or whatever you're doing. As soon as the sign of one of the predicates is negative (or positive, depending on which way you do the test), the point is known to be outside the triangle and you're done. However, if all three predicates pass, the point is inside the triangle.

Anyway, maybe I missed something somewhere, in which case I assume someone will point out my error...

[Edit: Sorry, tri area isn't quite equivalent to perp-dot, but the sign is the same.]
0

Share this post


Link to post
Share on other sites
To the OP, here is your point/tri test re-written to be a little more efficient and perhaps more robust:


bool IsPointInTri(Point p, Point v1, Point v2, Point v3)
{
if (Sign(p, v1, v2) < 0.0f)
return false;
if (Sign(p, v2, v3) < 0.0f)
return false;
if (Sign(p, v3, v1) < 0.0f)
return false;

// Or > 0.0f in case of opposite winding
}

float Sign(Point v1, Point v2, Point v3)
{
return (v1->x - v3->x) * (v2->y - v3->y) - (v2->x - v3->x) * (v1->y - v3->y);
}



That's untested off-the-top-of-my-head code, so I can't vouch for its correctness. But the idea is sound, and is pretty much equivalent to oliii's second example, the plane check.
0

Share this post


Link to post
Share on other sites
Thanks. The area test wasn't working the way I thought it would when I wrote a test program. But I did get it working using a mix between your Sign() func (which is basically a cross product, if I understand it correctly) and oliii's three bools, since I plan to check both clockswise and ccw triangles.

The final funcs are (for posterity)


float Sign(fPoint p1, fPoint p2, fPoint p3)
{
return (p1.x - p3.x) * (p2.y - p3.y) - (p2.x - p3.x) * (p1.y - p3.y);
}

bool IsPointInTri(fPoint pt, fPoint v1, fPoint v2, fPoint v3)
{
bool b1, b2, b3;

b1 = Sign(pt, v1, v2) < 0.0f;
b2 = Sign(pt, v2, v3) < 0.0f;
b3 = Sign(pt, v3, v1) < 0.0f;

return ((b1 == b2) && (b2 == b3));
}




Thanks for the help.
0

Share this post


Link to post
Share on other sites
Yeah, the perp-dot is the same as the z-term in a 3D cross product. In other words, if you took two 2D vectors and made them 3D by setting z = 0, then took their cross product in 3D, the resulting vector would have x = 0, y = 0, and z = the perp-dot of the two vectors in 2D. In fact, I've seen some Vector2 classes just call the perp-dot function Cross() (Dave Eberly calls it 'Kross').

Anyway, glad you got it sorted :-)
0

Share this post


Link to post
Share on other sites
yes, sorry. the code I gave is just the standard edge plane per product thingy, extended to handle both CCW and CW tests.

The angle/area thing accumulates the error, since you add the results together. As you said, no need to accumulate, just make sure the point if facing every edges the same way, then it's in the triangle. If the point is outside, it cannot mathematically face every edges the same way [grin], no matter is the triangle is ordered CW or CCW.

That also works for convex polys (like quads, ect..).
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0