Collisions

22
Collision Detection and Response

description

Collision theory presentation

Transcript of Collisions

Collision Detection and Response

Collision Detection and ResponseHow to do this?Here is where my object isHere is where my object is going to beHere is where I want my object to beA box that is Defined by the min and max coordinates of an objectAlways aligned with the coordinate axesHow can we tell if a point p is inside the box?Axis Aligned Bounding Boxes(AABB)

Initial Airplane Orientation Airplane Orientation 2Do a bunch of ifsif( px= minz )then collide = true;else collide = false

AABBPminxmaxxmaxyminyif mins/maxes overlapthen collide = trueelse collide = false;Comparing AABBsBminxBmaxxBmaxyBminyAminxAmaxxAABB Approach

Initialization: Iterate through vertices and find mins and maxes

After Transformations: Iterate through AABB vertices and find mins and maxesInitializationiterate through all vertices of your model to find the mins and maxes for x, y, and zDuring runtimeTest if any of the AABB mins/maxes of one object overlap with another objects AABB mins/maxesMAKE SURE THAT THE AABB VALUES ARE IN THE SAME COORDINATE FRAME (e.g., world coordinates)!If they arent, then manually transform them so they are.This is equivalent to multiplying the 8 points by a matrix for each object Then make sure to recalculate your mins/maxes from the 8 transformed points!Note: it is possible to do this with only 2 points from the box: (minx,miny,minz), (maxx,maxy,maxz), but not required

AABB ApproachKeep a position p and a unit vector v.Each frame add the vector to the positionp+v*speed, This is essentially how the camera works in my latest code sample on my webpageHow about gravity?Add a gravity vector (e.g., g = [0,-1,0]v+=v+g*gravityp +=v*speed

glTranslatefv(p)where gravity and speed are float

Shoot a projectileEquation: Ax+By+Cz+D = 0 [A,B,C] is the normal of the planeD is how far from the origin it isp = (xp,yp,zp)What is the shortest distance from p to theplane?Axp+Byp+Czp+ D = signed distance(assuming [A,B,C] is length 1)

For AABBs, normals are alwaysgoing to be parallel to a principle axise.g., x-axis: [A,B,C] = [1,0,0]

Collision Detection: Planes and Points[A,B,C]DP+-Manually (i.e., make your own matrix multiplication functions) transform all things collidable into the same coordinate frame (e.g. world coordinates)E.g., if you have :

gluLookat()glPushMatrix()glTranslatefv(p);Draw sphere projectileglPopMatrix()glPushMatrix();glRotate(a,x,y,z)glTranslate(tx,ty,tz)Draw a BBglPopMatrix()Manually Transforming Objects for CollisionsGet the vertices of this BB and multiply them by the RT matrix: e.g., RTvi for each of the 8 vertices, vi. This will put the BB into world coordinatesRT matrixThe p here is already a position in world coordinates! YAY!Manually transform the projectile into the BBs object coordinate frame (less work for cpu)E.g., if you have :

gluLookat()glPushMatrix()glTranslatefv(p);Draw sphere projectileglPopMatrix()glPushMatrix();glRotate(a,x,y,z)glTranslate(tx,ty,tz)Draw a BBglPopMatrix()Another way to do the transformRT matrixMultiply p by (RT)-1 or (-T)(-R)pAnd do the same its direction vector v2) do collision detection and response calculations with the untransformed BB3) Put p back into world coordinates with RTp and its direction vector v

Watchout for non-uniform scaling for this you would need do do multiplications of the form M-1Tvcollide = true; // then what?Calculate ray intersection with the planeCalculate reflection vector (sound familiar?)Calculate new positionRays are made of an origin (a point) and a direction (a vector)

Simple Collision ResponseRaydirectionRayoriginNRefldirectionCurrent PositionNext PositionMake sure N and Raydirection are normalized!Adjacent = A*RayoriginX + B*RayoriginY + C*RayoriginZ +Dadjacent / cos() = hypotenuseThat is, dot (Raydirection , N) = cos()Rayorigin+Raydirection*hypotenuse = i

Ray-Plane IntersectionsRaydirectionRayoriginNRefldirectionadjacentiReally we should use physics here butThink back to lightingRefldirection =-2dot(N, Raydirection) *N + Raydirection

Calculate a ReflectionRaydirectionRayoriginNRefldirectionadjacenti1) test collisions and response on untransformed objects before trying it with applied transformsActually, the only requirement is projectile transformations.2) use spheres for the projectiles ( you can basically treat these as points) and then you do not need to implement separating axes with OBB3) draw your bounding boxes so you can see them (this is actually required is the project)

4) graduates : Dont worry, I dont expect terrain collisionsTips for making Assignment 3 easierA box thatStays oriented to the model regardless of transformationsThese are often defined by artists in the 3D modeling programThere are algorithms to compute the minimum OBB, but this is out of scope for this class How to create the initial box?1) Either:Iterate through vertices (same as AABBMake a nice box with a modeling program2) Convert to plane equations Oriented Bounding Boxes (OBB)

Airplane Orientation 1Airplane Orientation 2

Take 3 vertices from one side of your boxCompute the normal[v3-v1] X [v2-v1] = [a,b,c]Normalize the normal[A,B,C] =[a,b,c] / ||[a,b,c]||Solve the following:Ax +By+ Cz + D = 0Plug in a point we know is on the planeAv1x + Bv1y + Cv1z = - DCreating the Plane Equationsv1v2v3Equation: Ax+By+Cz+D = 0 [A,B,C] is the normal of the planeD is how far from the origin it isp = (xp,yp,zp)What is the shortest distance from p to theplane?Axp+Byp+Czp+ D = signed distance(assuming [A,B,C] is length 1)

Collision Detection: Planes and Points[A,B,C]DP+-If( a point evaluates to be