- Home
- Products
- Integration
- Tutorial
- Barcode FAQ
- Purchase
- Company
GENERATING A TERRAIN in Font
CHAPTER 11 GENERATING A TERRAIN PDF417 Creation In None Using Barcode generation for Font Control to generate, create PDF-417 2d barcode image in Font applications. www.OnBarcode.comBarcode Printer In None Using Barcode encoder for Font Control to generate, create Barcode image in Font 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; } 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 (such as bouncing), you would need to implement a physics system. Making QR Code ISO/IEC18004 In None Using Barcode generator for Font Control to generate, create QR Code image in Font applications. www.OnBarcode.comDraw ECC200 In None Using Barcode maker for Font Control to generate, create Data Matrix image in Font applications. www.OnBarcode.comRay and Terrain Collision
PDF417 Creation In None Using Barcode creator for Font Control to generate, create PDF 417 image in Font applications. www.OnBarcode.comGenerating ANSI/AIM Code 128 In None Using Barcode printer for Font Control to generate, create Code 128 Code Set C image in Font applications. www.OnBarcode.comTo detect when an object in the scene intersects 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. 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 EAN / UCC - 14 Generation In None Using Barcode creation for Font Control to generate, create EAN 128 image in Font applications. www.OnBarcode.comUSPS OneCode Solution Barcode Drawer In None Using Barcode generator for Font Control to generate, create USPS OneCode Solution Barcode image in Font applications. www.OnBarcode.comCHAPTER 11 GENERATING A TERRAIN
PDF 417 Generator In None Using Barcode printer for Font Control to generate, create PDF-417 2d barcode image in Font applications. www.OnBarcode.comPDF 417 Scanner In VB.NET Using Barcode decoder for Visual Studio .NET Control to read, scan read, scan image in Visual Studio .NET applications. www.OnBarcode.comcollision 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 11-13 illustrates the linear search processes, where the nearest points outside and inside the terrain are found. Print Code39 In VB.NET Using Barcode creator for .NET framework Control to generate, create Code 39 Full ASCII image in VS .NET applications. www.OnBarcode.comScanning Data Matrix 2d Barcode In Java Using Barcode recognizer for Java Control to read, scan read, scan image in Java applications. www.OnBarcode.comFigure 11-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 you 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 perform a binary search between these two points to find the closest point to the terrain. You make this search with a fixed number of steps; 32 steps are usually enough for a good level of precision. The code for the binary search follows: Making Quick Response Code In None Using Barcode generator for Microsoft Word Control to generate, create Denso QR Bar Code image in Office Word applications. www.OnBarcode.comPaint Linear 1D Barcode In .NET Using Barcode encoder for ASP.NET Control to generate, create 1D Barcode image in ASP.NET applications. www.OnBarcode.comCHAPTER 11 GENERATING A TERRAIN
Barcode Generation In .NET Framework Using Barcode generator for .NET framework Control to generate, create Barcode image in .NET framework applications. www.OnBarcode.comBarcode Generation In Java Using Barcode encoder for Java Control to generate, create Barcode image in Java applications. www.OnBarcode.comVector3 startPosition = lastRayPosition; Vector3 endPosition = ray.Position; // Binary search with 32 steps. Try to find the exact collision point for (int i = 0; i < 32; i++) { // Binary search pass Vector3 middlePoint = (startPosition + endPosition) * 0.5f; if (middlePoint.Y < height) endPosition = middlePoint; else startPosition = middlePoint; } Vector3 collisionPoint = (startPosition + endPosition) * 0.5f; You then create the Intersects method to check the intersection of a ray and the terrain. The Intersects method returns the distance between the ray s start point and the terrain s collision point, and if there is no collision with the terrain, the method will return null. Following is the code for the Intersects method of the Terrain class: public float Intersects(Ray ray) { float collisionDistance = null; Vector3 rayStep = ray.Direction * blockScale * 0.5f; Vector3 rayStartPosition = ray.Position; // Linear search - Loop until you 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); } // If the ray collides with the terrain if (height >= 0) { Vector3 startPosition = lastRayPosition; Vector3 endPosition = ray.Position; // Binary search. Find the exact collision point for (int i = 0; i < 32; i++) { // Binary search pass Vector3 middlePoint = (startPosition + endPosition) * 0.5f; if (middlePoint.Y < height) endPosition = middlePoint; Barcode Printer In VS .NET Using Barcode encoder for .NET framework Control to generate, create Barcode image in .NET framework applications. www.OnBarcode.comCreating UPC-A Supplement 5 In Objective-C Using Barcode creator for iPhone Control to generate, create GS1 - 12 image in iPhone applications. www.OnBarcode.comMaking UCC - 12 In VS .NET Using Barcode printer for Reporting Service Control to generate, create EAN / UCC - 14 image in Reporting Service applications. www.OnBarcode.comBarcode Generator In .NET Using Barcode encoder for Reporting Service Control to generate, create Barcode image in Reporting Service applications. www.OnBarcode.com |
|