Perception 1 Collision Detection - PowerPoint PPT Presentation

1 / 39
About This Presentation
Title:

Perception 1 Collision Detection

Description:

True collision detection is calculating the intersection between arbitrarily complex polygons. Polygons deform when the collisions occur ... – PowerPoint PPT presentation

Number of Views:72
Avg rating:3.0/5.0
Slides: 40
Provided by: bdu82
Category:

less

Transcript and Presenter's Notes

Title: Perception 1 Collision Detection


1
Perception 1Collision Detection
  • Bryan Duggan

2
Overview
  • Introduction to perception
  • Collision detection
  • Broad phase
  • Narrow phase
  • Point mesh relationship

3
Perception is important
  • Helps create the illusion of intelligence
  • E.g.
  • You approach a bot silently, from the rear but it
    immediately turns around and frags you with a
    chain gun
  • You run and hide. It is impossible for an enemy
    to know where you are, but it nevertheless
    proceeds directly to your location
  • You notice a guard in a guard tower. It sweeps
    the ground with a search light. You follow a path
    that avoids the search light, but the guard still
    sees you

4
Perception
  • We have 5 senses
  • Sound
  • Touch
  • Sight
  • Smell
  • Taste
  • Games currently model only 3 senses
  • Touch
  • Sound
  • Sight

5
Perception
  • Game bots do not really perceive!
  • Instead use simplified models
  • Distance
  • Intersections of points, planes, spheres, rays
  • States
  • Character modelling
  • Sound effects
  • To give the illusion of perception

6
Touch
  • Detecting when two 3D objects collide
  • Examples
  • The player collides with the walls
  • The player walks up a stairs
  • A projectile hits a game entity
  • A projectile hits a wall
  • The player collides with a game entity
  • The player goes somewhere they are not supposed to

7
Difficulties with Collision Detection
  • If we have n objects
  • Object 1 may collide with n-1 other objects
  • It cant collide with itself
  • Object 2 may collide with n-2 other objects
  • We have already checked 1 and 2 collision
  • Thats equivalent to
  • (n-1) (n-2) (n-3) ... 1
  • Or
  • n!
  • In other words, lots.

8
Difficulties with Collision Detection
  • True collision detection is calculating the
    intersection between arbitrarily complex polygons
  • Polygons deform when the collisions occur
  • A collision can cause a force in the objects that
    collide

9
Difficulties with Collision Detection
  • A Mesh may contain thousands of triangles

10
Collision detection problems
11
Collision detection problems
12
In practice then
  • Broad phase collision detection (AKA Macro
    collision detection
  • Narrow phase collision detection (AKA Micro
    collision detection)

13
Collision detection algorithms
  • Broad Phase
  • Point and bounding box
  • Point and bounding sphere
  • Bounding box bounding box
  • Bounding sphere bounding sphere
  • Box sphere
  • Ray sphere
  • Ray bounding box
  • Narrow phase
  • Point and plane
  • Ray and plane
  • Plane and plane

14
Bounding box and sphere
  • We can use bounding boxes and spheres to
    determine which meshes to check for collisions
    (Broad phase)
  • At some point we need to compare each triangle of
    each mesh whose bounding box/sphere collides to
    check for a true collision (Narrow Phase)
  • We might also need to know where the collision
    occurred

15
Broad Phase Collision Detection
  • Bounding boxes
  • if (A._upperBound.x lt B._lowerBound.x)
  • return false
  • if (A._lowerBound.y gt B._upperBound.y)
  • return false
  • if (A._lowerBound.x gt B._upperBound.x)
  • return false
  • if (A._upperBound.y lt B._upperBound.x)
  • return false
  • return true

16
Bounding boxes
  • Are usually axis aligned (edges aligned with the
    X, Y and Z axis)
  • We have to transform the ray/point into the boxs
    local space to do the check if they are not

17
Bounding Spheres
  • It is very easy to detect if bounding spheres
    overlap, for instance,
  • Object A has centre at ax,ay,az and radius ar
  • Object B has centre at bx,by,bz and radius br
  • Then the bounding spheres intersect if
  • sqrt((ax-bx)2(ay-by)2(az-bz)2) lt arbr
  • OR
  • (ax-bx)2(ay-by)2(az-bz)2 lt (arbr)2
  • The distance from one centre point to the next
    centre point lt the sum of the two radii

18
Bounding spheres
  • You can check if a point is inside a sphere
  • If (distance from point to centre) lt radius then
    the point is inside thesphere

19
Bounding volumes for meshes
  • D3DXComputeBoundingBox
  • D3DXComputeBoundingSphere
  • Do these return coordinates in local space or
    world space?

20
Geometry refresher
  • Point
  • x, y, zD3DXVECTOR3/ D3DXVECTOR2
  • 2D Line
  • Start, end points
  • Slope and intercept (y mx c)
  • Ray
  • pos and look
  • Ray (_pos, _look)
  • p(t) p0 t(u)
  • 0 p0 t(u) p(t)
  • Plane
  • A vector N (The normal) and a point P (on the
    plane)
  • Ax By Cz D
  • The vector A, B, C is the plane Normal
  • D is the shortest signed distance from the origin
    to the plane

21
Planes
  • Can be described using a point on the plane and a
    normal vector, a vector perpendicular to the
    plane

22
Equation of a plane
  • Remember
  • Cos(PI / 2) 0, so
  • If A and B are perpendicular vectorsA.B 0
  • The equation of a plane is given by
  • n.(p p0) 0

23
Equation of a plane
  • n.(p-p0) Can be written as
  • n.p n.p0 (Distributive)
  • We can therefore write the equation asn.p d
    0Where d -n.p0
  • d is also shortest signed distance from the
    origin to the plane
  • When storing a plane, we can get away with
    storing n and d (A 4D vector)

24
Planes in DirectX
  • We can use the classD3DXPLANE to represent a
    plane D3DXPLANE plane D3DXPLANE(float a,
    float b, float c, float d)Where a, b, c are
    the x, y, z of the normal d is the constant d

25
Construction
  • You can construct a planeD3DXPLANE
    D3DXPlaneFromPointNormal( D3DXPLANE pOut, CONST
    D3DXVECTOR3 pPoint, CONST D3DXVECTOR3 pNormal
    )
  • D3DXPlaneFromPoints( plane, planeVertices0,
    planeVertices1, planeVertices2)

26
Useful things to do with planes
  • If p is a pointIf n.p d 0, the point is on
    the planeIf n.p d gt 0, the point is in front
    of the planeIf n.p d lt 0, the point is behind
    the plane
  • Use D3DXPlaneDotCoord(D3DXPLANE plane,
    D3DXVECTOR3 v)
  • To do the above calculation

27
In DalekWorld
  • Everything extends Entity
  • Entity collidesWith(D3DXVECTOR3 point)
  • Supports 3 types
  • inner_box (World)
  • outer_box (Walls)
  • sphere (for Meshes)

28
Inner box
  • // Check if we colide with the roof or ceiling
  • if ((point-gty lt _lowerBound.y) (point-gty gt
    _upperBound.y))
  • return this
  • // Check and see if we collide with the outer
    walls
  • if ((point-gtx gt (_lowerBound.x))
  • (point-gtx lt (_upperBound.x))
  • (point-gtz gt (_lowerBound.z))
  • (point-gtz lt (_upperBound.z)))
  • return NULL
  • else
  • return this
  • break

29
Outer box
  • if ((point-gtx gt _lowerBound.x)
  • (point-gtx lt _upperBound.x)
  • (point-gtz gt _lowerBound.z)
  • (point-gtz lt _upperBound.z)
  • (point-gty gt _lowerBound.y)
  • (point-gty lt _upperBound.y))
  • return this
  • else
  • return NULL

30
Sphere
  • float distance D3DXVec3Length((point -
    _centrePoint))
  • return (distance lt _radius) ? this NULL

31
World
32
  • retVal EntitycollidesWith(point)
  • if (retVal ! NULL)
  • return retVal
  • // Now see if we collide with the inner walls
  • for(int i 0 i lt _walls.size() i )
  • retVal _wallsi-gtcollidesWith(point)
  • if (retVal ! NULL)
  • return retVal
  • // Now see if we colide with any daleks
  • for(int i 0 i lt _daleks.size() i )

33
In Code
  • void Entitywalk(float units)
  • D3DXVECTOR3 newPos
  • D3DXVECTOR3 testPos
  • newPos _pos D3DXVECTOR3(_look.x, 0.0f,
    _look.z) units
  • testPos newPos _look _margin
  • Entity boundableEntity _world-gtcollidesWith(
    testPos)
  • if ((!_isGhost) (boundableEntity NULL)
    (boundableEntity this))
  • _moved true
  • _pos newPos

34
  • void Entitystrafe(float units)
  • D3DXVECTOR3 newPos, testPos
  • newPos _pos D3DXVECTOR3(_right.x, 0.0f,
    _right.z) units
  • testPos newPos _look 2
  • Entity boundableEntity _world-gtcollidesWith(
    testPos)
  • if ( (!_isGhost) (boundableEntity NULL)
    (boundableEntity this))
  • _moved true
  • _pos newPos

35
So far only broad phase
  • But what about rocks etc in the world
  • That have boundaries that are not axis aligned
  • We need to combine broad phase with narrow phase
    for movement
  • First do a broad phase collision detection check
    (bounding sphere/box)
  • Then do a narrow phase check

36
(No Transcript)
37
Point Mesh Collision algorithm
  • First check to see if the point is inside the
    entities bounding sphere
  • If it is,
  • Translate the point by the inverse of the mesh
    world transform, to bring the point into the mesh
    local space
  • Get the number of planes from the Mesh
  • Get the index buffer from the Mesh
  • Get the Vertex buffer from the mesh
  • For each plane
  • Retrieve 3 indices from the index buffer
  • Retrieve the three vertices corresponding to the
    indices
  • Make a plane out of the three vertices
  • Calculate n.p d (call it x)
  • if ( x gt 0)
  • The point is outside one of the planes, so return
    false
  • Return true

38
  • int numPlanes _mesh-gtGetNumFaces()
  • int bytesPerVertex _mesh-gtGetNumBytesPerVertex(
    )
  • int numIndices numPlanes 3
  • WORD indexBuffer NULL
  • char vertexBuffer
  • if (_mesh-gtLockIndexBuffer(D3DLOCK_DISCARD,(LPVOI
    D ) indexBuffer) D3D_OK)
  • _mesh-gtLockVertexBuffer(D3DLOCK_DISCARD,(LPVOID
    ) vertexBuffer)
  • for (int i 0 i lt (numIndices - 3) i 3)
  • memcpy( planeVertices0, vertexBuffer
    (indexBufferi bytesPerVertex),
    sizeof(D3DXVECTOR3))
  • memcpy( planeVertices1, vertexBuffer
    (indexBufferi 1 bytesPerVertex),
    sizeof(D3DXVECTOR3))
  • memcpy( planeVertices2, vertexBuffer
    (indexBufferi 2 bytesPerVertex),
    sizeof(D3DXVECTOR3))
  • printPlane(planeVertices)
  • // Now make a plane out of the 3 vertices
  • D3DXPLANE plane
  • D3DXPlaneFromPoints( plane,
    planeVertices0, planeVertices1,
    planeVertices2)

39
Caveat!!
  • The above algorithm only works for convex meshes
Write a Comment
User Comments (0)
About PowerShow.com