Lecture07 – Terrain

advertisement
Terrain Following
&
Collision Detection
1
Introduction (1/2)




Both of topics are very game-type-oriented
Terrain
 For visual purpose
 Ground / Building / Static models / Dynamic models
 For terrain following
 Polygon mesh
 Grids
 For path finding
 Polygon mesh
 Grids
Terrain following
 Make a 3D entity (character or model) walking on terrain
Path finding
 Find a “shortest” path to walk before moving
 Game AI
 A* algorithm
2
Introduction (2/2)

Collision detection
 The basic solution for collision detection is solving the intersection of a ray
with a plane.
 We will introduce :
 Containment test
 Separating axis
 Collision avoidance
 Will be introduced at Steer behavior in Game AI section
3
Terrain Formats




Grid
 2D
 Quadtree
Height map
 Procedural height map
 Using noise function to generate the height
ROAM
 Real-time Optimally Adapting Meshes
Triangular mesh
 Procedurally generated
 Created by artists
Perlin Noise
4
Grid Map

2D grid map
 Rectangular or Hexagonal grids
 Attributes
 Height
 Walkable or not
 Texture pattern ID

Step look terrain

Application
 2D games
 3D games with god view
 2D tile-based game terrain
5
Height Map


Almost as same as 2D grid map but :"
 Height on grid vertex
 Only height is saved
 Regular grid
 Irregular grid but structured
Top view
Application
 As the base data structure for ROAM terrain
 Water simulation
6
ROAM

Real-time optimally adapting mesh
 http://www.llnl.gov/graphics/ROAM/

Application
 Fly-simulation
7
Chunked LOD Terrain

Use quad tree to construct the level-of-detail of terrain
 A quad tree for LOD
8
Triangular Mesh

Possibly the most popular way for 3D games
 General
 Can be created by artists

Multiple-layered terrain issue
9
Barycentric Coordinate System
ha
(xa,ya,za)
Ac p
hb
h
(xb,yb,zb)
Ab
Aa
hc
h=
Aa
A
ha +
Ab
A
hb +
(xc,yc,zc)
Ac
A
hc
where A = Aa + Ab + Ac
If (Aa < 0 || Ab < 0 || Ac < 0) than
the point is outside the triangle
“Triangular Coordinate System”
“Barycentric Coordinate System”
10
Triangle Area – 2D
Area of a triangle in 2D
xa ya
A = ½ xb yb
xc yc
xa ya
= ½ (xa*yb + xb*yc + xc*ya – xb*ya – xc*yb – xa*yc)
(xa,ya,za)
(xb,yb,zb)
(xc,yc,zc)
11
Triangle Area – 3D
Area of a triangle in 3D
12
Barycentric Coordinate System - Application




Terrain following
 Interpolating the height of arbitrary point within the triangle
Hit test
 Intersection of a ray from camera to a screen position with a triangle
Ray cast
 Intersection of a ray with a triangle
Collision detection
 Intersection
13
Ray Cast – The Ray


Cast a ray to calculate the intersection of the ray with models
Use parametric equation for a ray
{



x = x0 + (x1 – x0) t
y = y0 + (y1 – y0) t, t = 0,
z = z0 + (z1 – z0) t
When t = 0, the ray is on the start point (x0, y0, z0)
Only the t  0 is the answer candidate
The smallest positive t is the answer
14
Ray Cast – The Plane






Each triangle in the 3D models has its plane equation.
Use ax + by + cz + d = 0 as the plane equation.
(a, b, c) is the plane normal vector.
|d| is the distance of the plane to origin.
Substitute the ray equation into the plane.
Solve the t to find the intersect point.
15
Terrain Following Using Triangular Mesh


Solve the terrain height for the object to stand on.
 Use the triangular coordinate system
Find the next neighboring triangle
 Half-edge data structure
16
Half-edge (1/2)


Create cohesive relationship between triangles using “half edge”
Use half-edge table to search the neighboring triangles
Edge = two halves
17
Half-edge (2/2)
struct HE_edge
{
HE_vert* vert; // vertex at the end of the half-edge
HE_edge* pair; // oppositely oriented adjacent half-edge
HE_face* face; // face the half-edge borders
HE_edge* next; // next half-edge around the face
};
struct HE_vert
{
float x;
float y;
float z;
HE_edge* edge; // one of the half-edges
// emantating from the vertex
};
struct HE_face
{
HE_edge* edge; // one of the half-edges bordering the face
};
http://www.flipcode.com/tutorials/tut_halfedge.shtml
18
Intersection



Ray cast
Containment test
Separating axes
19
2D Containment Test
Intersection = 1, inside
Intersection = 2, outside
(x0, y0)
Intersection = 0, outside
Trick : Parametric equation for a ray which is parallel to the x-axis
{
x = x0 + t
y = y0
, t = 0,
“if the No. of intersection is odd, the point is inside,
otherwise, is outside”
20
3D Containment Test

Same as the 2D containment test
“if the No. of intersection is odd, the point is inside,
otherwise, is outside”
21
Separating Axes



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.
How ?
 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.

If the projection are of these two object are not overlapped, the two
objects are not intersected.
22
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;
}
23
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;
}
24
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;
}
}
25
Unaligned Collision Avoidance Behavior (碰撞迴避)



Turn away from possible collision (避免可能的碰撞)
Predict the potential collision (預估下一步的碰撞可能性)
 Use bounding spheres
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
Summary for Collision Detection




Use bounding volume to improve the performance
 Cylinder volume for characters
 Object-oriented bounding box (OBB) or bounding sphere for 3D models
Collision detection when the object/character is moving
Very game type oriented
Apply “Collision Avoidance” first, and then “Collision Detection”
27
Download