Broad Phase Collision Detection - Department of Computer and ...

80 downloads 42 Views 714KB Size Report
... in the 3D grid space. 3D uniform grid space (Image courtesy Chapter 32, GPU gems 3 ). Object in the grid space (Image courtesy Chapter 32, GPU gems 3 ) ...
Broad Phase Collision Detection Collision detection is a technique to detect collisions between objects in a n-dimensional space. Of the different algorithms used the broad phase collision detection followed by a narrow phase collision detection is the most efficient. Broad phase selects potentially colliding pair of objects which are used as input for the narrow phase which does an exact match of colliding sets of objects. The broad phase has phases and the implementation here has been done in parallel.

Spatial Subdivision Spatial Subdivision will partition the space of into a 3D cubical uniform grid space. This can be visualized as the particle space being divided into cubical cells of the same dimension. The grid space has an origin and the positions of objects are represented as 3D coordinates relative to the origin of the 3D grid. The 3D coordinates of an object are the centroid (cx,cy,cz). If at a certain instant of time an object’s centroid is located within a cell, it is identified and stored as the home (H) cell of the object. Empirically, the objects being considered for collision tests have varied dimensions and have different radii of the bounding spheres around them and are not uniformly distributed in the 3D space. Hence every object might not be located completely within a cell. An object can have its centroid within a cell, the home (H) cell for it and also has the possibility of overlapping with 2n-1of its 3n-1 neighboring cells. This is based on the computation of cell sizes depending on the dimension of the largest bounding sphere of an object. These 2n cells which the object can overlap are known as the phantom cells (P) cells. Furthermore, every cube in the grid cell will be labeled as having a type 1 to 2n where n is the dimension of the grid space which is 3 in this case. This label will be used while processing for collisions between objects in the 3D grid space.

3D uniform grid space (Image courtesy Chapter 32, GPU gems 3 )

Object in the grid space (Image courtesy Chapter 32, GPU gems 3 )

Implementation Considerations of Spatial Subdivision: The 3D grid space does not have one specific data structure but has been generated as a collection of data structures. There are 3 arrays used: - Cell ID Array : o

Size – NUM_OF_OBJECTS * 8

o

Stores - the hash of the home (H) cell & 2n-1 phantom (P) cells of the objects

- Object List Array: o Size – NUM_OF_OBJECTS * 8 o Stores – Array of structures to store object ID and values of the H and P cell types of the object

- General Object Array: o Size – NUM_OF_OBJECTS o Stores – Array of structures to store object ID, centroid coordinates x,y,z , bounding sphere of an object Parallel implementation of Spatial Subdivision on CUDA: The parallel implementation consists of threads being used to perform the creation of the Cell ID and Object List Array. Each of these arrays have a dimension of (NUM_OF_OBJECTS * 8) locations. Each thread used will be responsible for working on a single object from the General Object array or multiple objects if the number of objects is more than the number of threads being used. Parallel computation will promote the possibility of an object involved in multiple collision tests to be updated multiple times by different threads. This has to be avoided to avoid unnecessary duplication in processing time. To prevent this we use the cell types which specify the iteration in which an object was processed. In 3D this means 8 computational passes to cover all possible threads. All objects in a cell type will be traversed at iteration (1 to 8). We do a spatial separation of cells by fixing the cell dimension to be as large as the largest bounding sphere of an object. Spatial separation of cells ensures that every cell is separated by all other cells with the same cell type by one cell of different cell type. This will prevent from multiple threads updating an object. The other problem associated with parallel execution will be to prevent same collision tests between two objects multiple times. This arises if there are 2 objects with centroids in different cells and they overlap each other’s cells. To eliminate this possibility we have: 1. Stored the values to track the H cell & the P cells intersected by the bounding volume of an object 2. We will also further increase the size of the bounding sphere by sqrt(2) times and scale the cell size to 1.5 times of the bounding sphere. Before performing a collision test during traversal T we check if the H cell type T’of the participating objects is less than T and common to the cell types of the 2 objects. We can forego this test, since this implies that the test has already been performed. O1 & O2 have same overlapping cells and different H cell type. In pass T3 we will not consider collision detection, since in a lower pass T1 it has already been performed. In the case of O3 & O4 both overlap cells of type 3 but have their centroids in different cells. The cell size ensures that the 2 do not collide

Eliminating duplicate collision tests using cell type & scaling bounding spheres & cells (Image courtesy Chapter 32, GPU gems 3 )

Our parallel implementation of Spatial Subdivision on CUDA: The creation of H & P cell IDs for each object will be done by one thread. If there are more objects than threads one thread will work on more than 1 object. Preprocessing: 1. Determine the Bounding Sphere of each object 2. Find the largest Bounding Sphere i.e. the largest object 3. Scale it by sqrt(2) times & ensuring grid cell is at least 1.5 times as large as the scaled Bounding Sphere of the largest object 4. Create Object Structure: - Object ID - Position (x, y, z) in 3D space – Generate randomly - Bounding Sphere (B.S.) dimension – Generate randomly 5. Create Object List Structure: - Object ID - Cell Type H or P (indicated with 0 for H and 1 for P) - H cell type ( 1 to 2n) - P cell type ( 1 to 2n) 6. Create Cell ID & Object List array of NUM_OF_OBJECTS x 8 elements. - The Cell ID array stores the cell id of the H cell of the first object followed by cell id of its P cells. The cell id is calculated as the hash value of its centroid coordinates. Whenever the Cell ID array is sorted the Object List array is also similarly reorganized - The Object List array stores the Object ID of every object and the type of cell H or P and the value of the cell type ( 1 – 8). 7. Create the Object array Generation and assignment of Cell ID & Object List Arrays: 1. The object IDs are generated and assigned sequentially to both the Object Array and the Object List Array. 2. We launch CUDA kernel to populate the Cell ID & Object List array. 3. Each thread works on an object or multiple objects if number of objects is more than the number of threads. In this each

thread j in a block B wil work on objects iB + j, iB + j + nT, iB + j + 2nT. The value B is the number of threads per block, j is the thread number and T is the total number of threads, n is the iteration. 4. For each object the home cell is found by a simple hash function:

hash = ((int)(obj_d[object_num].cx / cell_size)