Download presentation
Presentation is loading. Please wait.
Published byBernice Hudson Modified over 6 years ago
1
3D Game Development and Computer Animation Collision Detection
Roger Webster, Ph.D. CS 475 3D Game Development and Computer Animation Copyright © 2016 Roger Webster, Ph.D.
2
3D Game Development Collision Detection Lots of Collisions take place
Collision Detection (CD) can be computationally expensive. Fast Efficient Collision Detection is vital. Some options exist for Collision Detection.
3
Collision Detection Simple Spherical collision detection
Based upon positional calculations and radius simple to calculate Fastest method Not highly accurate (unless objects are pool balls!) Bounding Volumes or Box(es) computationally more expensive than spherical more accurate than spherical Polygonal collision detection most accurate Most computationally expensive (prohibitive large polys) Hierarchical Collision Detection Uses spherical, then bounding boxes, then Polys Coldet library -> most accurate, relatively fast Collision Maps (2D/3D maps based on data structure) Very fast, very efficient, accuracy based on resolution of map
4
Simple Spherical Collision Detection
Bounding Volumes using Spheres Easiest! If anything hits sphere you assume collision. The spheres do not overlap if The separation between the two objects is greater then the sum of the 2 radii In 3D Use vectors P1 – P2 gives distance vector. Find magnitude of P1 – P2 and compare to radii sum. If distance is less then radii some, collision
5
Cylinderical Bounding Volumes
Bounding Volume Collision Detection Cylinderical Bounding Volumes Cylinders Good for games where most objects are oriented the same way (Doom) and reduce collision detection to a 2D problem. Must check top, bottom, and radius. Pseudo Code: IF (distance between center of circles > radii sum) no collision ELSE IF (vertical distances overlap) is a collision no collision // e.g. objects on different levels
6
Bounding Box Collision Detection
Bounding Boxes computationally more expensive than spherical more accurate than spherical
7
Bounding Box Collision Detection
Boxes Good for 2D and 3D. Allows for another shape to choose from for a bounding region. Pseudo Code: //First box FOR (every vertex in box1) IF (box1 vertex is in box2) there is a collision, exit loop //Second box IF (there is no collision from first box) FOR (every vertex in box2) IF (box2 vertex is in box1) Check both because box1 vertex may not be in box2, but still collide.
8
Hierarchical Collision Detection
Use a Library Uses spherical, then hierarchical bounding boxes, then Polygons Highly accurate Relatively efficient and fast (because it is hierarchical) Some models are computationally expensive (ex. F15 plane). Returns the Collision Point (x,y,z) on the polygon and the Surface Normal (x1,y1,z1) normalized (unit vector).
9
Hierarchical Collision Detection
using XNAEngine; using CollisionDetection; CollidableModel gzMissile; CollidableModel target; protected override void LoadContent () { Model tModel = Content.Load<Model>("bomb"); target = new CollidableModel(tModel); Model mModel = Content.Load<Model>(“missile"); gzMissile = new CollidableModel(mModel); } protected void UpdateMissileMovements() if (target.checkRayCollision(gzMissile.Position, dir, LengthofRay*5.0f, out cp, out surfaceNormal)) {//do something}
10
Hierarchical Collision Detection
using XNAEngine; using CollisionDetection; CollidableModel gzMissile; CollidableModel target; protected override void LoadContent () { Model tModel = Content.Load<Model>("bomb"); target = new CollidableModel(tModel); Model mModel = Content.Load<Model>(“missile"); gzMissile = new CollidableModel(mModel); } protected void UpdateMissileMovements() if (target.checkCollision(gzMissile, out cp, out surfaceNormal)) {//do something}
11
2D Collision Maps MyModel.LoadObject(city.3ds)
Very fast, very efficient, accuracy based on resolution of map Essentially you are pre-computing all the collisions MyModel.LoadObject(city.3ds) Calculate total distance across X,Z For every N points across X,Z check collisions Put x,y collision point in data structure Write collision map out to file Load collision map file in Myinit();
12
2D Collision Maps Very fast, very efficient, accuracy based on resolution of map Essentially you are pre-computing all the collisions
13
3D Collision Maps Spatial Partitioning
Divide your universe into cells. Check collisions in regions near object in question This will greatly reduce the number of collision calculation that must be performed. Results are only as good as the resolution.
14
Calculate Your Own We know Maxx, Minx, Maxz, Minz
We know each object’s position and radius
15
Calculate Your Own void myUpdate(void) {
spd = speedMultiplier * normalizeSpeed(); // speed is a function of fps for each tank { speed = tank[i].getSpeed() * spd; tank[i].MoveForward(speed); dirVec.x = curpos[i].x - prevpos[i].x; // set direction vector dirVec.y = curpos[i].y - prevpos[i].y; dirVec.z = curpos[i].z - prevpos[i].z; getWallCollision(tank[i], dirVec, i); prevpos[i] = curpos[i]; tank[i].Draw(camera) tank[i].getPosition(&curpos[i].x, &curpos[i].y, &curpos[i].z); }
16
// determine if tank hit a wall, and if so, bounce off of it
void getWallCollision(MUnode & t, const LVector4 & dirVec, const int & tankNum) { …….//continued if (hitWall) // rotate tank object float theta, costheta, reflect; float lenTankVec = sqrt(dirVec.x * dirVec.x + dirVec.y * dirVec.y + dirVec.z * dirVec.z); costheta = (dirVec.x * wallVec.x + dirVec.z * wallVec.z) / lenTankVec; theta = acos(costheta); theta = muopengl_radians2degrees(theta); reflect = (dir * theta * 2); t.Yaw(reflect); if (tankView && tankNum == camTank) myCamera.Yaw(reflect); //printf("angle %f, reflect %f\n", theta, reflect); }
17
// heading of tank we're going to attach the camera to
float getHeading(const int & i) { LVector3 dirVec; dirVec.x = curpos[i].x - prevpos[i].x; dirVec.y = curpos[i].y - prevpos[i].y; dirVec.z = curpos[i].z - prevpos[i].z; float theta, costheta; float lenTankVec = sqrt(dirVec.x * dirVec.x + dirVec.y * dirVec.y + dirVec.z * dirVec.z); costheta = (dirVec.z) / lenTankVec; theta = acos(costheta); theta = muopengl_radians2degrees(theta); if (dirVec.x < 0) theta = -theta; } return theta;
18
Computer Animation areas of CD research:
Self collision Detection of objects colliding with itself.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.