[edit] Widelands map geometryThis document describes the geometry of the Widelands map, how it is represented and displayed. First have a look at this image.
It shows that the map is composed of triangles. It is useful to see the map as a graph, especially when making searches on it. Read about graphs at Graph_theory. Graph theory uses the terms vertex (or node) and edge (or arc). A vertex is a corner of a polygon, so in this case a vertex is a corner of a triangle. Each vertex has 6 neighbouring triangles and each triangle has 3 neighbouring vertices. This implies that there are 2 triangles for each vertex. Each vertex has 6 neighbouring edges and each edge has 2 neighbouring vertices. This implies that there are 3 edges for each vertex. The map wraps around in both dimensions, so it forms a torus. Eventhough a torus is a 3-dimensional shape, the fact that the map is a torus does not make it 3-dimensional. The torus itself can never be seen, and all edges are equally long. On a real torus, the edges on the outside would be longer than those on the inside. But the map has some visible 3-dimensional properties; vertices have a property called height. This can be seen in the image above.
[edit] RepresentationThe map is basically an array of vertices. A vertex is a record consisting basically of a number representing the height property and a 2 triangles calledr (right) and d (down). Which those triangles are is seen in this image:
A vertex is also responsible for storing 3 edges, but edges do not yet have any properties to store. If they had, each vertex would be responsible for storing the 3 edges shown in this image:
[edit] DisplayThe map is displayed in parallel perspective. But the view is not from straight above (zenith). A triangle has the measures as shown in this image (source):
To display something on a triangle, it is necessary to get the screen coordinates of it. The screen coordinates of the 3 vertices surrounding the triangle are available; (X1, Y1), (X2, Y2), (X3, Y3). But which coordinates does the middle of the triangle have? To answer that one first has to define what is meant with the middle of the triangle. It is smart to chose a definition that gives an easy answer, like this one; the middle (Xm, Ym) is the one that makes the sum of the squared distances from each surrounding vertex to the midpoint as small as possible. This is known as the least square solution. The sum of the squared distances is sum(i in 1 .. 3, (Xm - Xi)**2 + (Ym - Yi)**2). Solving for the coordinate pair (Xm, Ym) that minimizes the sum gives (Xm, Ym) = ((X1 + X2 + X3) / 3, (Y1 + Y2 + Y3) / 3). An important thing to do when displaying the map is to interpret commands that the user gives with the mouse on the map. To do this one has to determine what the mouse cursor is pointing at. For any point represented by pair of screen coordinates, it has to be determined which triangle it is in or which vertex it is closest to. The 3-dimensional visualization makes this diffucult because the y-coordinates are moved. But it is not nearly as difficult as if both x- and y-coordinates had been affected. (Something that makes it complicated is wrapping.) See this image:
How can one know if a point is above a line? Suppose that we have a line L from the point A = (Xa, Ya) to the point B = (Xb, Yb) and the point P = (Xp, Yp), where Xa <= Xp < Xb. See this image (source):
pdy / pdx > ldy / ldx This may seem strange, but that is only because the y-axis goes in the opposite direction of what one is used to from mathematics. Because pdx and ldx are both always positive, the condition can be rewritten to: pdy * ldx > ldy * pdx; (Here ldx is a constant known at compile time with the value 64, which is a power of 2. Therefore the compiler will make the multiplication as a bit shift, which is much faster than normal multiplication. Not that it matters in this case.) Now one can determine which triangle the point is in. To determine which vertex it is closest to, just calculate the distance to each vertex i, sqrt((Xi - Xp)**2 + (Yi - Yp)**2), and see which is smallest. (Of course it is not neccessary to actually calculate the square roots.)
[edit] RegionsA region can be defined as everything within a certain distance from a certain point.
[edit] Vertex regionsA vertex region is the set of vertii that can be reached from the starting vertex by moving along at most N edges.
[edit] Triangle regionsA triangle region is the set of triangles that can be reached from the starting triangle by crossing at most N edges. This picture illustrates what it might look like for d triangles:
In the two pictures above, the red triangle is the only triangle in the region when N = 0. The grey triangles belong to the region when N = 7 but not when N = 6. |
![[Main Page]](/modules/mediawiki/images/mediawiki.png)
About Geometry
From Widelands.org
Main Page | Recent changes | Edit this page | Page history | Switch to MediaWiki modePrintable version | Disclaimers | Privacy policy












