How to do this Here is where my object is Here is where my object is going to be Here is where I want my object to be A box that is Defined by the min and max coordinates of an object Always aligned with the coordinate axes ID: 188619
Download Presentation The PPT/PDF document "Collision Detection and Response" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Collision Detection and ResponseSlide2
How to do this?
Here is where my object is
Here is where my object is going to be
Here is where I want my object to beSlide3
A box that is
Defined by the min and max coordinates of an object
Always 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 2Slide4
Do a bunch of ifsif(
p
x<= max
x &&p
y<= maxy &&pz
<= maxz &&p
x>= minx &&py
>= miny &&p
z>= minz )
then collide = true; else collide = false
AABB
P
min
x
max
x
max
y
min
ySlide5
if mins
/maxes overlap
then collide = trueelse collide = false;
Comparing AABBs
Bmin
x
Bmax
x
Bmax
y
Bmin
y
Amin
x
Amax
xSlide6
AABB Approach
Initialization: Iterate through vertices and find
mins
and maxes
After Transformations: Iterate through AABB vertices and find
mins
and maxesSlide7
Initialization
iterate through all vertices of your model to find the
mins and maxes for x, y, and z
During runtimeTest if any of the AABB mins/maxes of one object overlap with another object’s AABB
mins/maxesMAKE SURE THAT THE AABB VALUES ARE IN THE SAME COORDINATE FRAME (e.g., world coordinates)!If they aren’t, 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,min
z), (maxx,maxy
,maxz), but not required
AABB ApproachSlide8
Keep a position p and a unit vector v.
Each frame add the vector to the position
p+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 projectileSlide9
Equation: Ax+By+Cz+D
= 0
[A,B,C] is the normal of the planeD is how far from the origin it is
p = (xp
,yp,zp)
What is the shortest distance from p to theplane?Ax
p+Byp+Czp+ D = signed distance
(assuming [A,B,C] is length 1)
For AABBs, normals are always
going to be parallel to a principle axise.g., x-axis: [A,B,C] = [1,0,0]
Collision Detection: Planes and Points
[A,B,C]
D
P
+
-Slide10
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 BB
glPopMatrix()
Manually Transforming Objects for Collisions
Get the vertices of this BB and multiply them by the RT matrix: e.g.,
RT
v
i
for each of the 8 vertices,
v
i
. This will put the BB into world coordinates
RT matrix
The
p
here is already a position in world coordinates! YAY!Slide11
Manually transform the projectile into the BB’s 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 BB
glPopMatrix()
Another way to do the transform
RT matrix
Multiply
p
by (RT)
-1
or (-T)(-
R)
p
And do the same its direction vector
v
2) do collision detection and response calculations with the untransformed BB
3) 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
-1T
vSlide12
collide = true; // then what?
Calculate ray intersection with the plane
Calculate reflection vector (sound familiar?)Calculate new positionRays are made of an origin (a point) and a direction (a vector)
Simple Collision Response
Ray
direction
Ray
origin
N
Refl
direction
Current Position
Next PositionSlide13
Make sure N and
Ray
direction are normalized!Adjacent = A*
RayoriginX + B*
RayoriginY + C*RayoriginZ +D
adjacent / cos(θ) = hypotenuseThat is, dot (
Raydirection , N) = cos(θ
)Rayorigin+Raydirection
*hypotenuse = i
Ray-Plane Intersections
Ray
direction
Ray
origin
N
Refl
direction
θ
θ
adjacent
iSlide14
Really we should use physics here but…
Think back to lighting
Refldirection
=-2dot(N, Ray
direction) *N + Raydirection
Calculate a Reflection
Ray
direction
Ray
origin
N
Refl
direction
θ
θ
adjacent
iSlide15
1) test collisions and response on untransformed objects before trying it with applied transforms
Actually, 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 : Don’t worry, I don’t expect terrain collisions
Tips for making Assignment 3 easierSlide16
A box that
Stays oriented to the model regardless of transformations
These are often defined by artists in the 3D modeling program
There 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 1
Airplane Orientation 2Slide17
Take 3 vertices from one side of your box
Compute 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 planeAv1
x + Bv1y + Cv1
z = - DCreating the Plane Equations
v1
v2
v3Slide18
Equation: Ax+By+Cz+D
= 0
[A,B,C] is the normal of the planeD is how far from the origin it is
p = (xp
,yp,zp)
What is the shortest distance from p to theplane?Ax
p+Byp+Czp+ D = signed distance
(assuming [A,B,C] is length 1)
Collision Detection: Planes and Points
[A,B,C]
D
P
+
-Slide19
If( a point evaluates to be <=0 distance from all 6 planes that make up the box
Then collide = true
Else collide = false
OBB – Point CollisionSlide20
Test whether any of the 8 points that make up one box collide with the other
Do this for both boxes.
This won’t always work in 3D…
Collision Detection: OBB and OBBSlide21
In ANY of the following cases, if all of the collision tests evaluate to positive, then assume no intersection
1) Test collisions between all the vertices of BB
A and all the planes of the BB
B2) Test the collisions between all the vertices of BBB
and all the planes of the BBA3) Then test collisions between all the vertices of BBA and BBB
and all cross products of each pair of edge normals of BBA and BBB
This actually works for any convex polyhedron. There are optimizations for OBBs…
Separating AxesSlide22
Again, you will have to make sure that all planes, points, etc are in the same coordinate frame when computing collisions.
Think about
normals
Vertex: Mv as usualNormal: M
-1Tn, n= [A,B,C,0]T // this is a vector!Transform the plane equationp = [A,B,C,D]Matrix M = arbitrary transforms
M-1TpOR, if you don’t have any non-uniform scaling
MpWhat would happen if you had non uniform scaling?Transforming OBB Planes