#### Azylaminaz

##### Vox Populi

- Reaction score
- 91

OK, so, basically I want to create a "smarter" terrain check. The classic means (simply detecting the terrain with GetTerrainType) gives funny results because "terrain" is seen as simple squares, ignoring how the art actually looks. What I want is a system that accurately reflects the art....

Terrain actually works in triangles (eight per a traditional square).

Blue & light blue: correctly valid

Purple: incorrectly valid

Red: correctly invalid

Green: incorrectly invalid

*valid means marble, invalid means square tiles.

This is also just one of two ways terrain can act. A tile can "bridge" over another type or "break" at it (in terms of going diagonally). Mrable is an example of bridging. Further more, a tile that bridges can over-extend past itself. Marble is an example of one that doesn't; brick, round/square tiles do over-extend.

Preloading the validity of each triangle would be near impossible as even a tiny map (52x52 playable) has 173,056 triangles. The other way would be simply detect on command. IE, pass over a point to check if it is valid or not - this is the approach I've been trying and failing on for days.

I've attacked this problem from a number of sides. -.- The following is my latest attempt at doing this.

I can probably pretty easily do step 2, the problem is step 1 (finding the center of diamonds/triangles). I want to tell myself the solution is obvious and I'm just running into a mental block, but eh. :/Pre-note: View the terrain as a diamond-checkerboard. The deep-blue diamonds are an example of these (as well as the diamonds diagonally between them).

Rules for simple tiles such as marble:

1) Any diamond that contains three or more valid classically checked triangles is 100% valid (contains obvious blue diamonds as well as "3 light blue 1 green" diamonds)

2) Any diamond that contains two opposite triangles that share obtuse sides with blue triangles is 100% valid (contains diamonds that are bridges, "2 light blue 2 green")

3) Any diamond that contains two adjacent triangles that share obtuse sides with blue triangles is 50% valid, those sharing triangles being the valid ones (contains "2 light blue 2 red")

4) Every other triangle is invalid.

Step 1: For any given point, get a rigid point at the center of the corresponding diamond. IE, a point in any triangle will fall to exactly where the 90 degree angle is. Get angles radiating from the center of the diamond to the midpoints of the obtuse feeding triangles (45/135/225/315).

Step 2: Use above rules to see if a the triangle is valid or not.

Edit: OH. And my attempts at trying to find the center of dimaonds were:

I failed.First: To find if the x or y of the diamonds were closest. The x of the diamond would be Int(x / 64 + .5) * 64, the y similar. Whichever was closer would get it. The other would be +32 or -32. This simply wasn't working and was getting ugly with so many if's.

Second: To work in a rotated Cartesian system. I tried this by converting x/y to polar coordinates, rotating it by 45 degrees, converting back into Cartesian coordinates (on a rotated Cartesian plane, now). Then I'd simply do "Int(x / 64 + .5) * 64" and "Int(y / 64 + .5) * 64". I'd then turn these points back to polar, rotate by -45, and convert back to traditional Cartesian.

I was failing at converting back and fourth (was testing with a simple x = 1, y = 2; x' and y' being the values on the rotated field):

x' = (x^2 + y^2)^.5 * Cos(Atan2(y/x) + Pi/4)

y' = (x^2 + y^2)^.5 * Sin(Atan2(y/x) + Pi/4)

x = (x'^2 + y'^2)^.5 * Cos(Atan2(y'/x') - Pi/4)

y = (x'^2 + y'^2)^.5 * Sin(Atan2(y'/x') - Pi/4)

Somebody help before I have an accident resulting in my unfortunate death. :X