1

I am currently trying to write a shader in unity that draws a triangular pattern around countries in a risk-styled game if both countries are not owned by the same player (visual aid to see your borders).

Right now, I'm having an issue with making the shader set the countries properly.

It always sets country 0 to the left, and country 1 to the right - country 0 and 1 are set programically.

The line, a border, can be between 0 and 359 degrees.

How I find the countries 0 and 1 is I draw 3 points to the left and right of the midpoint of the line, one .01f, one .1f and one 1f away from the midpoints in each direction, then spin them around the midpoint to the appropriate location.

After that I do an even-odd check to see if the points are inside or outside of each country, and compare the weight results (closest gets 3 points, mid gets 2, furthest gets 1, just in case someone builds a really screwed up country that flanks the other country).

In my test map, a close to equally sliced octagon, the borders showed up correctly (after I reversed the positions of country 0 and 1 in the event the angle was over 90 and less then or equal 180). Worked without a flaw, but in other maps it doesn't work very well.

Everything but the country allocation works well, so I'm curious if anyone knows of a better way to figure out which point is to the left or a spun line, or a better conceptual way to handle this.

enter image description here

That above is basically when I'm doing, red being left right being blue, then I'm just checking 3 different spots then weighing in the lefts and rights found with even/odding it into the appropriate countries (one at +/- .01, the other at +/- .1 and the third 1, in case of even/odd rounding issues with closeness).

I then flip them if I find that country A is to the right, as it is on the left according to the angles I had draw. (my shader renders left first and right second, hence why I do this).

Charles
  • 548
  • 1
  • 7
  • 25
  • I fixed my issue - took quite a bit of debugging, but when I added visual aids around my production code I figured out it wasn't properly grabbing the angles and kept all angles at 0 (which is why all of this was confusing the hell out of me). Keeping this open to discuss dot product as I don't know how it'd help me here, but solution found. – Charles Dec 17 '14 at 09:21

1 Answers1

0
  1. which way is left/right on a line?

    From last edit is this not your case. Why not use dot product?

    line x-axis dot

    So if the line goes in -x direction the result is negative and if in the +x direction then the result is positive. if the result is zero that means the line goes up or down only or it is juts a point. If you need specific direction instead of left/right then use appropriate a vector instead of x axis.

    • dot(a,b)=a.x*b.x+a.y*b.y in 2D
    • dot(a,b)=a.x*b.x+a.y*b.y+a.z*b.z in 3D

    Image is relevant for cases where a vector is in unit size in that case the result of dot is perpendicular projection of b into a just like on image

  2. on which side is some point?

    I think this is what you need.

    side of line

    As you can see if you handle line (P0,P1) and point P you want to classify as triangle then its polygon winding determines also the side of the line. So for implicit axis directions:

    • CW(clockwise) polygon winding means right side of the line
    • CCW(counter-clockwise) polygon winding means left side of the line

    How to get winding? ... simply compute normal vector and get its Z coordinate. Its polarity (sign) determines winding (CW/CCW or the other way around depends on the coordinate system).

    • normal vector is computed as cross product of the two vertices of triangle (P1-P0)x(P-P1)

    No need to compute other axises just the z so:

    • normal.z = ((P1.x-P0.x)*(P.y-P1.y)) - ((P1.y-P0.y)*(P.x-P1.x))

    Now just do if (normal.z<0) ... else ... it should never be zero unless you call it for point on the line or the line is a point ... look here at similar question: Determine rotation direction /toward/ variable point on a circle

Community
  • 1
  • 1
Spektre
  • 49,595
  • 11
  • 110
  • 380
  • I updated my question with a visual aid - I am not too sure how dot product would help me here. I answered my own question, it was a error with allocating angles in my production code I didn't catch, but I'm keeping this open for a bit as I'm curious if my own lack of experience with dot product might make it a better choice in the future. – Charles Dec 17 '14 at 09:26
  • @Charles well that is a whole different question then ... you want to determine if point/polygon/half space is on which side (right/left) of some line ... for that is polygon winding ideal will edit in a minute – Spektre Dec 17 '14 at 13:48
  • I don't think this would work for me. It is possible for my polygons to have an angle that is acute on one side and obtuse on the other, which would give a winding order of CW for both. Then my midpoint +/-.1 spun to angle would be my only choice. Or am I missing something on this. – Charles Dec 18 '14 at 21:43
  • @Charles don't get the case you are mentioning please add an image to enlight me. btw Normal polarity (winding) works for any rotation or triangle/convex polyogn... so if something does not work it is a manner of wrong conception (like not use predefined point order or wrong points usage,... or you mean by left/right side something else then I) – Spektre Dec 19 '14 at 14:33