# create barcode c#.net s GENERATING A TERRAIN in Font Maker Data Matrix 2d barcode in Font s GENERATING A TERRAIN

CHAPTER 10 s GENERATING A TERRAIN
ECC200 Printer In None
Using Barcode creation for Font Control to generate, create Data Matrix image in Font applications.
www.OnBarcode.com
USS Code 128 Generation In None
Using Barcode maker for Font Control to generate, create Code-128 image in Font applications.
www.OnBarcode.com
Figure 10-12. A block in the terrain grid. If the X position inside the block is bigger than the Z position, the object is in the top triangle. Otherwise, the object is in the bottom triangle.
Drawing Barcode In None
Using Barcode creator for Font Control to generate, create Barcode image in Font applications.
www.OnBarcode.com
Drawing Code 39 In None
Using Barcode drawer for Font Control to generate, create Code 3 of 9 image in Font applications.
www.OnBarcode.com
After finding in which triangle the object is positioned, you can obtain the height of a position inside this triangle through a linear interpolation of the height of the triangle s vertices. Use the following code for the GetHeight method to calculate the height of a terrain s position:
Drawing Barcode In None
Using Barcode creation for Font Control to generate, create Barcode image in Font applications.
www.OnBarcode.com
GS1 128 Printer In None
Using Barcode generation for Font Control to generate, create UCC - 12 image in Font applications.
www.OnBarcode.com
private float GetHeight(float positionX, float positionZ) { float height = -999999.0f; if (heightmap == null) return height; // Get the position relative to the terrain grid Vector2 positionInGrid = new Vector2( positionX - (StartPosition.X + Transformation.Translate.X), positionZ - (StartPosition.Y + Transformation.Translate.Z)); // Calculate the grid position Vector2 blockPosition = new Vector2( (int)(positionInGrid.X / blockScale), (int)(positionInGrid.Y / blockScale)); // Check if the object is inside the grid if (blockPosition.X >= 0 && blockPosition.X < (vertexCountX - 1) && blockPosition.Y >= 0 && blockPosition.Y < (vertexCountZ - 1)) { Vector2 blockOffset = new Vector2( blockPosition.X - (int)blockPosition.X, blockPosition.Y - (int)blockPosition.Y);
PDF417 Encoder In None
Using Barcode printer for Font Control to generate, create PDF 417 image in Font applications.
www.OnBarcode.com
Code 93 Printer In None
Using Barcode printer for Font Control to generate, create Code 9/3 image in Font applications.
www.OnBarcode.com
CHAPTER 10 s GENERATING A TERRAIN
Data Matrix 2d Barcode Printer In None
Using Barcode printer for Excel Control to generate, create Data Matrix ECC200 image in Office Excel applications.
www.OnBarcode.com
Encoding Data Matrix 2d Barcode In Java
Using Barcode encoder for Java Control to generate, create Data Matrix 2d barcode image in Java applications.
www.OnBarcode.com
// Get the height of the four vertices of the grid block int vertexIndex = (int)blockPosition.X + (int)blockPosition.Y * vertexCountX; float height1 = heightmap[vertexIndex + 1]; float height2 = heightmap[vertexIndex]; float height3 = heightmap[vertexIndex + vertexCountX + 1]; float height4 = heightmap[vertexIndex + vertexCountX]; // Top triangle float heightIncX, heightIncY; if (blockOffset.X > blockOffset.Y) { heightIncX = height1 - height2; heightIncY = height3 - height1; } // Bottom triangle else { heightIncX = height3 - height4; heightIncY = height4 - height2; } // Linear interpolation to find the height inside the triangle float lerpHeight = height2 + heightIncX * blockOffset.X + heightIncY * blockOffset.Y; height = lerpHeight * heightScale; } return height; }
Barcode Creator In Visual Basic .NET
Using Barcode drawer for .NET Control to generate, create Barcode image in .NET applications.
www.OnBarcode.com
EAN-13 Drawer In None
Using Barcode printer for Online Control to generate, create EAN-13 image in Online applications.
www.OnBarcode.com
Notice that you use this method only to ensure that all scene objects are positioned over the terrain. To produce a realistic interaction between the objects and the terrain you would need to implement a physics system.
PDF 417 Reader In Visual C#.NET
www.OnBarcode.com
Using Barcode scanner for VS .NET Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
Ray and Terrain Collision
Decoding Barcode In Visual Basic .NET
Using Barcode Control SDK for VS .NET Control to generate, create, read, scan barcode image in .NET framework applications.
www.OnBarcode.com
UPC Code Encoder In None
Using Barcode generation for Software Control to generate, create UPC Code image in Software applications.
www.OnBarcode.com
To detect when an object in the scene intercepts a part of the terrain, you need to create some collision test methods. One useful collision test is between a ray and the terrain. For example, if an object is moving in the scene, you can trace a ray in the direction in which this object is moving and get the distance between it and the terrain.
Barcode Generation In None
Using Barcode creation for Microsoft Excel Control to generate, create Barcode image in Excel applications.
www.OnBarcode.com
Printing Code 128A In Java
Using Barcode printer for Android Control to generate, create Code128 image in Android applications.
www.OnBarcode.com
CHAPTER 10 s GENERATING A TERRAIN
Creating Barcode In Java
Using Barcode printer for BIRT reports Control to generate, create Barcode image in BIRT reports applications.
www.OnBarcode.com
Code 3/9 Encoder In None
Using Barcode creation for Microsoft Word Control to generate, create Code 39 Extended image in Microsoft Word applications.
www.OnBarcode.com
To check the ray and terrain collision, you ll do a collision test between the ray and the terrain s height map, instead of testing the ray against the terrain s mesh (many triangles). The collision test will be divided in two parts. In the first part, you ll do a linear search on the ray until you find a point outside (above) and another inside (below) the terrain. Then, you ll perform a binary search between these two points to find the exact collision point with the terrain. Figure 10-13 illustrates the linear search processes, where the nearest points outside and inside the terrain are found.
Figure 10-13. Linear search used to find one point inside and another outside the terrain
You can use the following code to perform the linear search on the terrain:
// A good ray step is half of the blockScale Vector3 rayStep = ray.Direction * blockScale * 0.5f; Vector3 rayStartPosition = ray.Position; // Linear search - Loop until find a point inside and outside the terrain Vector3 lastRayPosition = ray.Position; ray.Position += rayStep; float height = GetHeight(ray.Position); while (ray.Position.Y > height && height >= 0) { lastRayPosition = ray.Position; ray.Position += rayStep; height = GetHeight(ray.Position); }
After the linear search, the lastRayPosition variable stores the position outside the terrain and the ray variable stores the position inside the terrain. You then need to make a