terrain collision detection
TRANSCRIPT
Both of topics are very gameBoth of topics are very game--typetype--oriented dataoriented data
Introduction (1/2)Introduction (1/2)Both of topics are very gameBoth of topics are very game typetype oriented dataoriented dataTerrainTerrain
For visual purposeFor visual purposeGround / Building / Static models / Dynamic modelsGround / Building / Static models / Dynamic models
For terrain followingFor terrain followingP l hP l hPolygon meshPolygon meshGridsGrids
For path findingFor path findingFor path findingFor path findingPolygon meshPolygon meshGridsGrids
Terrain followingTerrain followingMake a 3D entity (character or model) walking Make a 3D entity (character or model) walking onon terrainterrain
P th fi diP th fi diPath findingPath findingFind a “shortest” path to walk before movingFind a “shortest” path to walk before movingWill be taught in Game AI section.Will be taught in Game AI section.
2
Will be taught in Game AI section.Will be taught in Game AI section.A* algorithmA* algorithm
Collision detectionCollision detection
Introduction (2/2)Introduction (2/2)Collision detectionCollision detection
The basic solution for collision detection is solving the intersection of a ray The basic solution for collision detection is solving the intersection of a ray with a plane.with a plane.We will introduce :We will introduce :We will introduce :We will introduce :
Containment testContainment testSeparating axisSeparating axis
Collision avoidanceCollision avoidanceWill be introduced at Steer behavior in Game AI sectionWill be introduced at Steer behavior in Game AI section
3
GridGrid
Terrain FormatsTerrain FormatsGridGrid
2D2DQuadtreeQuadtree
H i h H i h Height mapHeight mapProcedural height mapProcedural height mapUsing noise function to generate the heightUsing noise function to generate the heightg g gg g g
ROAMROAM
Perlin Noise
RRealeal--time time OOptimally ptimally AAdapting dapting MMesheseshesTriangular meshTriangular mesh
Procedurally generatedProcedurally generatedProcedurally generatedProcedurally generatedCreated by artistsCreated by artists
4
2D grid map2D grid map
Grid MapGrid Map2D grid map2D grid map
Rectangular or Hexagonal gridsRectangular or Hexagonal gridsAttributesAttributes
HeightHeightWalkable or notWalkable or notT t tt IDT t tt IDTexture pattern IDTexture pattern ID
Step look terrainStep look terrainpp
ApplicationApplication2D games2D games2D games2D games3D games with god view3D games with god view
2D tile2D tile--based game terrainbased game terrain
5
Almost as same as 2D grid map but :"Almost as same as 2D grid map but :"
Height MapHeight MapAlmost as same as 2D grid map but :Almost as same as 2D grid map but :
Height on grid vertexHeight on grid vertexOnly height is savedOnly height is saved
Top view
Regular gridRegular gridIrregular grid but structuredIrregular grid but structured
ApplicationApplicationAs the base data structure for ROAM terrainAs the base data structure for ROAM terrainWater simulationWater simulation
6
RealReal--time optimally adapting meshtime optimally adapting mesh
ROAMROAMRealReal time optimally adapting meshtime optimally adapting mesh
http://www.llnl.gov/graphics/ROAM/http://www.llnl.gov/graphics/ROAM/
ApplicationApplicationApplicationApplicationFlyFly--simulationsimulation
7
Use quad tree to construct the levelUse quad tree to construct the level--ofof--detail of terraindetail of terrain
Chunked LOD TerrainChunked LOD TerrainUse quad tree to construct the levelUse quad tree to construct the level ofof detail of terraindetail of terrain
A A quad tree for LODquad tree for LOD
8
Possibly the most popular way for 3D gamesPossibly the most popular way for 3D games
Triangular MeshTriangular MeshPossibly the most popular way for 3D gamesPossibly the most popular way for 3D games
GeneralGeneralCan be created by artistsCan be created by artists
MultipleMultiple--layered terrain issuelayered terrain issue
9
hh
Triangular Coordinate SystemTriangular Coordinate System
hhaa
AAcc pp
((xxaa,y,yaa,z,zaa))
hhhhbb
AAaa
ccAAbb
pp
((xxbb,y,ybb,z,zbb))
hhccAA AA AA
((xxcc,y,ycc,z,zcc))
h = hh = haa + + hhbb + + hhcc
where A =where A = AA ++ AAbb + A+ A
AAaa AAbb AAcc
AA AA AA
where A where A AAaa + + AAbb + A+ Acc
If (If (AAaa < 0 || < 0 || AAbb < 0 || A< 0 || Acc < 0) than< 0) thanthe point is outside the trianglethe point is outside the trianglethe point is outside the trianglethe point is outside the triangle
“Triangular Coordinate System”“Triangular Coordinate System”“B t i C di t S t ”
10
“Barycentric Coordinate System”
Triangle Area Triangle Area –– 2D2D
Area of a triangle in 2D
xa yaa yaA = ½ xb yb
xc ycx yxa ya
= ½ (xa*yb + xb*yc + xc*ya – xb*ya – xc*yb – xa*yc)
(xa,ya,za)
(x y z )(xb,yb,zb)
11(xc,yc,zc)
Triangle Area Triangle Area –– 3D3D
Area of a triangle in 3D A = ½ (N. Sum(Pi1 cross Pi2))where (i1, i2) = (a,b), (b,c), (c,a)
float GmArea3(float *x0, float *x1, float *x2, float *n){
float area len sum1 sum2 sum0;float area, len, sum1, sum2, sum0;len = (float) sqrt(n[0] * n[0] + n[1] * n[1] + n[2] * n[2]) * 2.0f;
/* find s m of cross prod cts *//* find sum of cross products */sum0 = x1[1] * (-x0[2] + x2[2]) + x2[1] * (-x1[2] + x0[2]) +
x0[1] * (-x2[2] + x1[2]);sum1 = x1[2] * (-x0[0] + x2[0]) + x2[2] * (-x1[0] + x0[0]) +
x0[2] * (-x2[0] + x1[0]);sum2 = x1[0] * (-x0[1] + x2[1]) + x2[0] * (-x1[1] + x0[1]) +
x0[0] * (-x2[1] + x1[1]);
/* find the area */
12
d t e a ea return = (sum0 * n[0] + sum1 * n[1] + sum2 * n[2]) / len;
}
Terrain followingTerrain following
Triangular Coordinate System Triangular Coordinate System -- ApplicationApplicationTerrain followingTerrain following
Interpolating the height of arbitrary point within the triangleInterpolating the height of arbitrary point within the triangleHit testHit test
Intersection of a ray from camera to a screen position with a Intersection of a ray from camera to a screen position with a triangletriangle
Ray castRay castRay castRay castIntersection of a ray with a triangleIntersection of a ray with a triangle
Collision detectionCollision detectionCollision detectionCollision detectionIntersectionIntersection
13
Ray Cast Ray Cast –– The RayThe RayCast a ray to calculate the intersection of the ray with modelsCast a ray to calculate the intersection of the ray with modelsCast a ray to calculate the intersection of the ray with modelsCast a ray to calculate the intersection of the ray with modelsUse parametric equation for a rayUse parametric equation for a ray
x = xx = x00 + (x+ (x11 –– xx00) t) ty = yy = y00 + (y+ (y11 –– yy00) t, t = 0,) t, t = 0,
(( ) t) t{{ 88
z = zz = z00 + (z+ (z11 –– zz00) t ) t {{
When t = 0, the ray is on the start point (xWhen t = 0, the ray is on the start point (x00,y,y00,z,z00))Only the t Only the t ≥≥ 0 is the answer candidate0 is the answer candidateThe smallest positive t is the answerThe smallest positive t is the answer
14
Ray Cast Ray Cast –– The PlaneThe PlaneEach triangle in the 3D models has its plane equationEach triangle in the 3D models has its plane equationEach triangle in the 3D models has its plane equation.Each triangle in the 3D models has its plane equation.UUse se ax + by + cz + d = 0ax + by + cz + d = 0 as the plane equation.as the plane equation.((a, b, c)a, b, c) is the plane normal vector.is the plane normal vector.(( ))|d||d| is the distance of the plane to origin.is the distance of the plane to origin.Substitute the ray equation into the plane.Substitute the ray equation into the plane.Solve the Solve the tt to find the intersect point.to find the intersect point.
15
Solve the terrain height for the object to stand onSolve the terrain height for the object to stand on
Terrain Following Using Triangular MeshTerrain Following Using Triangular MeshSolve the terrain height for the object to stand on.Solve the terrain height for the object to stand on.
Use the triangular coordinate system (p. 154)Use the triangular coordinate system (p. 154)Find the next neighboring triangleFind the next neighboring triangle
HalfHalf--edge data structureedge data structure
16
Create cohesive relationship between triangles using “half edge”Create cohesive relationship between triangles using “half edge”
HalfHalf--edge (1/2)edge (1/2)
Create cohesive relationship between triangles using “half edge”Create cohesive relationship between triangles using “half edge”UUse halfse half--edge table to search the neighboring trianglesedge table to search the neighboring triangles
Edge = two halves
17
struct HE edge
HalfHalf--edge (2/2)edge (2/2)struct HE_edge{
HE_vert* vert; // vertex at the end of the half-edgeHE_edge* pair; // oppositely oriented adjacent half-edgeHE f * f // f h h lf d b dHE_face* face; // face the half-edge bordersHE_edge* next; // next half-edge around the face
};
struct HE_vert{
fl t float x;float y;float z;HE edge* edge; // one of the half-edgesHE_edge edge; // one of the half edges
// emantating from the vertex};
t t HE fstruct HE_face{
HE_edge* edge; // one of the half-edges bordering the face};
18
};
http://www.flipcode.com/tutorials/tut_halfedge.shtml
Ray castRay cast
IntersectionIntersectionRay castRay castContainment testContainment testSeparating axesSeparating axes
19
2D Containment Test2D Containment Test
Intersection = 1, inside
Intersection = 2, outside
(x y ) Intersection = 0, outside
Trick : Parametric equation for a ray which is parallel to the x axis
(x0, y0)
Trick : Parametric equation for a ray which is parallel to the x-axis
x = x0 + t t 0 { 8y = y0 , t = 0, { 8
“if the No of intersection is odd the point is inside
20
if the No. of intersection is odd, the point is inside,otherwise, is outside”
3D Containment Test3D Containment TestSame as the 2D containment testSame as the 2D containment testSame as the 2D containment testSame as the 2D containment test
“if the No of intersection is odd the point is insideif the No. of intersection is odd, the point is inside,otherwise, is outside”
21
Separating AxesSeparating AxesFFor convex objects onlyor convex objects onlyFFor convex objects onlyor convex objects onlyIIf there is existing an axis (2D) or a plane (3D) to separate two convex f there is existing an axis (2D) or a plane (3D) to separate two convex objects, these two objects are not intersected.objects, these two objects are not intersected.
How ?How ?Project the vertices of each object on the axis/plane that is Project the vertices of each object on the axis/plane that is perpendicular to axis/plane we are going to findperpendicular to axis/plane we are going to findperpendicular to axis/plane we are going to find.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.
IIf the projection are of these two object are not overlapped the f the projection are of these two object are not overlapped the
22
IIf the projection are of these two object are not overlapped, the f the projection are of these two object are not overlapped, the two objects are not intersected.two objects are not intersected.
Separating Axes Algorithm for Convex Polyhedra (1/3)Separating Axes Algorithm for Convex Polyhedra (1/3)
Bool TestIntersect(ConvexPolyhedron C0, ConvexPolyhedron C1){
// test faces of C0 for separationfor (i = 0; i < C0 GetFaceCount(); i++) {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// test faces of C1 for separationfor (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;
}
23
Separating Axes Algorithm for Convex Polyhedra (2/3)Separating Axes Algorithm for Convex Polyhedra (2/3)
// test cross products of pairs of edgesfor (i = 0; i < C0.GetEdgeCount(); i++) {
for (j = 0; j < C1.GetEdgeCount(); j++) {D = Cross(C0 GetEdge(i) C1 GetEdge(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;return true;}
24
Separating Axes Algorithm for Convex Polyhedra (3/3)Separating Axes Algorithm for Convex Polyhedra (3/3)
void ComputeInterval(ConvexPolyhedron C, Vector D,double &min, double &max)
{min = Dot(D C GetVertex(0));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;
}}}
25
Use bounding volume to improve the performanceUse bounding volume to improve the performance
Summary for Collision DetectionSummary for Collision DetectionUse bounding volume to improve the performanceUse bounding volume to improve the performance
Cylinder volume for charactersCylinder volume for charactersObjectObject--oriented bounding box (OBB) or bounding sphere for 3D modelsoriented bounding box (OBB) or bounding sphere for 3D models
Collision detection when the object/character is movingCollision detection when the object/character is movingVery game type orientedVery game type oriented“Collision Avoidance” “Collision Avoidance” vsvs “Collision Detection”“Collision Detection”Tricks! Tricks! Tricks!Tricks! Tricks! Tricks!
26