Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Terrain Following & Collision Detection. 2 Both of topics are very game-type-oriented Both of topics are very game-type-oriented Terrain Terrain For.

Similar presentations


Presentation on theme: "1 Terrain Following & Collision Detection. 2 Both of topics are very game-type-oriented Both of topics are very game-type-oriented Terrain Terrain For."— Presentation transcript:

1 1 Terrain Following & Collision Detection

2 2 Both of topics are very game-type-oriented Both of topics are very game-type-oriented Terrain Terrain For visual purpose For visual purpose Ground / Building / Static models / Dynamic models Ground / Building / Static models / Dynamic models For terrain following For terrain following Polygon mesh Polygon mesh Grids Grids For path finding For path finding Polygon mesh Polygon mesh Grids Grids Terrain following Terrain following Make a 3D entity (character or model) walking on terrain Make a 3D entity (character or model) walking on terrain Path finding Path finding Find a “shortest” path to walk before moving Find a “shortest” path to walk before moving Game AI Game AI A* algorithm A* algorithm Introduction (1/2)

3 3 Collision detection Collision detection The basic solution for collision detection is solving the intersection of a ray with a plane. The basic solution for collision detection is solving the intersection of a ray with a plane. We will introduce : We will introduce : Containment test Containment test Separating axis Separating axis Collision avoidance Collision avoidance Will be introduced at Steer behavior in Game AI section Will be introduced at Steer behavior in Game AI section Introduction (2/2)

4 4 Grid Grid 2D 2D Quadtree Quadtree Height map Height map Procedural height map Procedural height map Using noise function to generate the height Using noise function to generate the height Terrain Formats Perlin Noise ROAM ROAM Real-time Optimally Adapting Meshes Real-time Optimally Adapting Meshes Triangular mesh Triangular mesh Procedurally generated Procedurally generated Created by artists Created by artists

5 5 2D grid map 2D grid map Rectangular or Hexagonal grids Rectangular or Hexagonal grids Attributes Attributes Height Height Walkable or not Walkable or not Texture pattern ID Texture pattern ID Grid Map Step look terrain Step look terrain Application Application 2D games 2D games 3D games with god view 3D games with god view 2D tile-based game terrain 2D tile-based game terrain

6 6 Almost as same as 2D grid map but :" Almost as same as 2D grid map but :" Height on grid vertex Height on grid vertex Only height is saved Only height is saved Regular grid Regular grid Irregular grid but structured Irregular grid but structured Height Map Top view Application Application As the base data structure for ROAM terrain As the base data structure for ROAM terrain Water simulation Water simulation

7 7 Real-time optimally adapting mesh Real-time optimally adapting mesh http://www.llnl.gov/graphics/ROAM/ http://www.llnl.gov/graphics/ROAM/ http://www.llnl.gov/graphics/ROAM/ ROAM Application Application Fly-simulation Fly-simulation

8 8 Use quad tree to construct the level-of-detail of terrain Use quad tree to construct the level-of-detail of terrain A quad tree for LOD A quad tree for LOD Chunked LOD Terrain

9 9 Possibly the most popular way for 3D games Possibly the most popular way for 3D games General General Can be created by artists Can be created by artists Triangular Mesh Multiple-layered terrain issue Multiple-layered terrain issue

10 10 h hahahaha hbhbhbhb hchchchc AaAaAaAa AcAcAcAc AbAbAbAb h = h a + h b + h c where A = A a + A b + A c where A = A a + A b + A c If (A a < 0 || A b < 0 || A c < 0) than the point is outside the triangle “Triangular Coordinate System” “Barycentric Coordinate System” AaAaAaAa AbAbAbAb AcAcAcAc A A A p (x a,y a,z a ) (x b,y b,z b ) (x c,y c,z c ) Barycentric Coordinate System

11 11 Area of a triangle in 2D x a y a A = ½ x b y b x c y c x a y a = ½ (x a *y b + x b *y c + x c *y a – x b *y a – x c *y b – x a *y c ) Triangle Area – 2D (x a,y a,z a ) (x b,y b,z b ) (x c,y c,z c )

12 12 Area of a triangle in 3D Triangle Area – 3D

13 13 Terrain following Terrain following Interpolating the height of arbitrary point within the triangle Interpolating the height of arbitrary point within the triangle Hit test Hit test Intersection of a ray from camera to a screen position with a triangle Intersection of a ray from camera to a screen position with a triangle Ray cast Ray cast Intersection of a ray with a triangle Intersection of a ray with a triangle Collision detection Collision detection Intersection Intersection Barycentric Coordinate System - Application

14 14 Ray Cast – The Ray x = x 0 + (x 1 – x 0 ) t y = y 0 + (y 1 – y 0 ) t, t = 0, z = z 0 + (z 1 – z 0 ) t { Cast a ray to calculate the intersection of the ray with models Cast a ray to calculate the intersection of the ray with models Use parametric equation for a ray Use parametric equation for a ray 8 When t = 0, the ray is on the start point (x 0, y 0, z 0 ) When t = 0, the ray is on the start point (x 0, y 0, z 0 ) Only the t  0 is the answer candidate Only the t  0 is the answer candidate The smallest positive t is the answer The smallest positive t is the answer

15 15 Ray Cast – The Plane Each triangle in the 3D models has its plane equation. Each triangle in the 3D models has its plane equation. Use ax + by + cz + d = 0 as the plane equation. Use ax + by + cz + d = 0 as the plane equation. (a, b, c) is the plane normal vector. (a, b, c) is the plane normal vector. |d| is the distance of the plane to origin. |d| is the distance of the plane to origin. Substitute the ray equation into the plane. Substitute the ray equation into the plane. Solve the t to find the intersect point. Solve the t to find the intersect point.

16 16 Solve the terrain height for the object to stand on. Solve the terrain height for the object to stand on. Use the triangular coordinate system Use the triangular coordinate system Find the next neighboring triangle Find the next neighboring triangle Half-edge data structure Half-edge data structure Terrain Following Using Triangular Mesh

17 17 Create cohesive relationship between triangles using “half edge” Create cohesive relationship between triangles using “half edge” Use half-edge table to search the neighboring triangles Use half-edge table to search the neighboring triangles Edge = two halves Half-edge (1/2)

18 18 struct HE_edge { HE_vert* vert; // vertex at the end of the half-edge HE_vert* vert; // vertex at the end of the half-edge HE_edge* pair; // oppositely oriented adjacent half-edge HE_edge* pair; // oppositely oriented adjacent half-edge HE_face* face; // face the half-edge borders HE_face* face; // face the half-edge borders HE_edge* next; // next half-edge around the face HE_edge* next; // next half-edge around the face }; }; struct HE_vert { float x; float x; float y; float y; float z; float z; HE_edge* edge; // one of the half-edges HE_edge* edge; // one of the half-edges // emantating from the vertex // emantating from the vertex }; }; struct HE_face { HE_edge* edge; // one of the half-edges bordering the face HE_edge* edge; // one of the half-edges bordering the face}; http://www.flipcode.com/tutorials/tut_halfedge.shtml Half-edge (2/2)

19 19 Ray cast Ray cast Containment test Containment test Separating axes Separating axesIntersection

20 20 Intersection = 1, inside Intersection = 2, outside Intersection = 0, outside Trick : Parametric equation for a ray which is parallel to the x-axis x = x 0 + t x = x 0 + t y = y 0, t = 0, y = y 0, t = 0, { 8 (x 0, y 0 ) 2D Containment Test “if the No. of intersection is odd, the point is inside, otherwise, is outside” otherwise, is outside”

21 21 3D Containment Test “if the No. of intersection is odd, the point is inside, otherwise, is outside” otherwise, is outside” Same as the 2D containment test Same as the 2D containment test

22 22 Separating Axes For convex objects only For convex objects only If there is existing an axis (2D) or a plane (3D) to separate two convex objects, these two objects are not intersected. If there is existing an axis (2D) or a plane (3D) to separate two convex objects, these two objects are not intersected. How ? How ? Project the vertices of each object on the axis/plane that is perpendicular to axis/plane we are going to find. Project the vertices of each object on the axis/plane that is perpendicular to axis/plane we are going to find. Get the extreme of the projection area of each object. Get the extreme of the projection area of each object. If the projection are of these two object are not overlapped, the two objects are not intersected. If the projection are of these two object are not overlapped, the two objects are not intersected.

23 23 Separating Axes Algorithm for Convex Polyhedra (1/3) Bool TestIntersect(ConvexPolyhedron C0, ConvexPolyhedron C1) { // test faces of C0 for separation for (i = 0; i < C0.GetFaceCount(); i++) { D = C0.GetNormal(i); ComputeInterval(C0, D, min0, max0); ComputeInterval(C1, D, min1, max1); if (max1 < min0 || max0 < min1) return false; } // test faces of C1 for separation for (i = 0; i < C1.GetFaceCount(); i++) { D = C1.GetNormal(i); ComputeInterval(C0, D, min0, max0); ComputeInterval(C1, D, min1, max1); if (max1 < min0 || max0 < min1) return false; }

24 24 Separating Axes Algorithm for Convex Polyhedra (2/3) // test cross products of pairs of edges for (i = 0; i < C0.GetEdgeCount(); i++) { for (j = 0; j < C1.GetEdgeCount(); j++) { D = Cross(C0.GetEdge(i), C1.GetEdge(j)); ComputeInterval(C0, D, min0, max0); ComputeInterval(C1, D, min1, max1); if (max1 < min0 || max0 < min1) return false; } return true; }

25 25 Separating Axes Algorithm for Convex Polyhedra (3/3) void ComputeInterval(ConvexPolyhedron C, Vector D, double &min, double &max) { min = Dot(D, C.GetVertex(0)); max = min; for (i = 1; i < C.GetVertexCount(); i++) { value = Dot(D, C.GetVertex(i)); if (value < min) min = value; else if (value > max) max = value; }

26 Turn away from possible collision ( 避免可能的碰撞 ) Predict the potential collision ( 預估下一步的碰撞可能性 ) Use bounding spheres Unaligned Collision Avoidance Behavior ( 碰撞迴避 ) If possibly collide, ( 如果可能碰撞, 預估轉向力先行轉向 ) Apply the steering on both characters Steering direction is possible collision result Use “future” possible position The connected line between two sphere centers

27 27 Use bounding volume to improve the performance Use bounding volume to improve the performance Cylinder volume for characters Cylinder volume for characters Object-oriented bounding box (OBB) or bounding sphere for 3D models Object-oriented bounding box (OBB) or bounding sphere for 3D models Collision detection when the object/character is moving Collision detection when the object/character is moving Very game type oriented Very game type oriented Apply “Collision Avoidance” first, and then “Collision Detection” Apply “Collision Avoidance” first, and then “Collision Detection” Summary for Collision Detection


Download ppt "1 Terrain Following & Collision Detection. 2 Both of topics are very game-type-oriented Both of topics are very game-type-oriented Terrain Terrain For."

Similar presentations


Ads by Google