Title: Animacja Komputerowa
1Animacja Komputerowa
- Barbara Strug
- Elektroniczne Przetwarzanie Informacji
- Semestr IV, Lato 2005
2Collision Detection
- Collision detection, as used in the games
community, usually means intersection detection
of any form - Intersection detection is the general problem
find out if two geometric entities intersect
typically a static problem - Interference detection is the term used in the
solid modeling literature typically a static
problem - Collision detection in the research literature
generally refers to a dynamic problem find out
if and when moving objects collide
3Collision Detection
- Subtle point Collision detection is about the
algorithms for finding collisions in time as much
as space
4Collision Applications
- Determining if the player or characters has a hit
a wall or obstacle - To stop them walking through it
- Determining if a projectile has hit a target
- Detecting points at which behavior should change
- A car in the air returning to the ground
5Collision Applications
- Cleaning up animation
- Making sure a motion-captured characters feet do
not pass through the floor - Simulating motion of some form
- Physics, or cloth, or something else
6Choosing an Algorithm
- The geometry of the colliding objects is the
primary factor in choosing a collision detection
algorithm - Object could be a point, or line segment
- Object could be specific shape a sphere, a
triangle, a cube, - Objects can be concave/convex, solid/hollow,
deformable/rigid, manifold/non-manifold
7Choosing an Algorithm
- The way in which objects move is a second factor
- Different algorithms for fast or slow moving
objects - Different algorithms depending on how frequently
the object must be updated - Of course, speed, simplicity, robustness are all
other factors
8Terminology Illustrated
Convex
Concave
Non-Manifold
Manifold
An object is convex if for every pair of points
inside the object, the line joining them is also
inside the object
An surface is manifold if every point on it is
homeomorphic to a disk. Roughly, every edge has
two faces joining it
9Robustness Explained
- For our purposes, collision detection code is
robust if it - Doesnt crash or infinite loop on any case that
might occur - Better if it doesnt crash on any case at all,
even if the case is supposed to be impossible - Always gives some answer that is meaningful, or
explicitly reports that it cannot give an answer - Can handle many forms of geometry
- Can detect problems with the input geometry,
particularly if that geometry is supposed to meet
some conditions (such as convexity)
10Types of Geometry
- Points
- Lines, Rays and Line Segments
- Spheres, Cylinders and Cones
- Cubes, rectilinear boxes - Axis aligned or
arbitrarily aligned - AABB Axis aligned bounding box
- OBB Oriented bounding box
- k-dops shapes bounded by planes at fixed
orientations - Convex, manifold meshes any mesh can be
triangulated - Concave meshes can be broken into convex chunks,
by hand - Triangle soup
- More general curved surfaces, but not used
(often) in games
AABB
OBB
8-dop
11Fundamental Design Principles
- There are several principles that should be
considered when designing a collision detection
strategy - What might they be?
- Say you have more than one test available, with
different costs. How do you combine them? - How do you avoid unnecessary tests?
- How do you make tests cheaper?
12Fundamental Design Principles
- There are several principles that should be
considered when designing a collision detection
strategy - Fast simple tests first to eliminate many
potential collisions - e.g. Test bounding volumes before testing
individual triangles - Exploit locality to eliminate many potential
collisions - e.g. Use cell structures to avoid considering
distant objects - Use as much information as possible about the
geometry - e.g. Spheres have special properties that speed
collision testing - Exploit coherence between successive tests
- Things dont typically change much between two
frames
13Case Study 1 Player-Wall Collisions
- First person games must prevent the player from
walking through walls and other obstacles - In the most general case, the player is a
polygonal mesh and the walls are polygonal meshes - On each frame, the player moves along a path that
is not known in advance - Assume it is piecewise linear straight steps on
each frame - Assume the players motion could be fast
14Naive Algorithm
- On each step, do a general mesh-to-mesh
intersection test to find out if the player
intersects the wall - If they do, refuse to allow the player to move
- What is poor about this approach? In what ways
can we improve upon it? - In speed?
- In accuracy?
- In response?
15Ways to Improve
- Even the seemingly simple problem of determining
if the player hit the wall reveals a wealth of
techniques - Collision proxies
- Spatial data structures to localize
- Finding precise collision times
- Responding to collisions
16Collision Proxies
- General mesh-mesh intersections are expensive
- Proxy Something that takes the place of the real
object - A collision proxy is a piece of geometry used to
represent a complex object for the purposes of
finding a collision
17Collision Proxies
- If the proxy collides, the object is said to
collide - Collision points are mapped back onto the
original object - What makes a good proxy?
- What types of geometry are regularly used as
proxies?
18Proxy Properties
- A good proxy is cheap to compute collisions for
and a tight fit to the real geometry - Common proxies are spheres, cylinders or boxes of
various forms, sometimes ellipsoids - What would we use for A fat player, a thin
player, a rocket, a car
19Proxy Properties
- Proxies exploit several facts about human
perception - We are extraordinarily bad at determining the
correctness of a collision between two complex
objects - The more stuff is happening, and the faster it
happens, the more problems we have detecting
errors - Players frequently cannot see themselves
- We are bad a predicting what should happen in
response to a collision
20Spatial Data Structures
- You can only hit something that is close to you
- Spatial data structures can tell you what is
close to an object - Fixed grid, Octrees, Kd-trees, BSP trees,
- Recall in particular the algorithm for
intersecting a sphere (or another primitive) with
a BSP tree - Collision works just like view frustum culling,
but now we are intersecting more general geometry
with the data structure - For the player-wall problem, typically you use
the same spatial data structure that is used for
rendering - BSP trees are the most common
21Precise Collision Times
- Generally a player will go from not intersecting
to interpenetrating in the course of a frame - We typically would like the exact collision time
and place - Response is generally better
- Interpenetration may be algorithmically hard to
manage - Interpenetration is difficult to quantify
- A numerical root finding problem
- More than one way to do it
- Hacked (but fast) clean up
- Interval halving (binary search)
22Managing Fast Moving Objects
- Several ways to do it, with increasing costs
- Test a line segment representing the motion of
the center of the object - Pros Works for large obstacles, cheap
- Cons May still miss collisions.
23Managing Fast Moving Objects
- Conservative prediction Only move objects as far
as you can be sure to catch the collision - Pros Will find all collisions
- Cons May be expensive, and need a way to know
what the maximum step is - Space-time bounds Bound the object in space and
time, and check the bound - Pros Will find all collisions
- Cons Expensive, and have to be able to bound
motion
24Collision Response
- For player motions, the best thing to do is
generally move the player tangentially to the
obstacle - Have to do this recursively to make sure that all
collisions are caught - Find time and place of collision
- Adjust velocity of player
- Repeat with new velocity, start time an start
position (reduced time interval) - Ways to handle multiple contacts at the same time
- Find a direction that is tangential to all
contacts - How do you do this for two planes?
25Bodies in Collision Collisions and Contact
So far, no interaction between rigid bodies
Collision detection determining if, when
and where a collision occurs
Collision response calculating the state
(velocity, ) after the collision
26Collisions and Contact
What should we do when there is a collision?
27Rolling Back the Simulation
Restart the simulation at the time of the
collision
Collision time can be found by bisection, etc.
28Ray-Scene Intersection
29Ray-Sphere Intersection
30Ray-Sphere Intersection I
31Ray-Sphere Intersection II
32Ray-Sphere Intersection
33Ray-Triangle Intersection
34Ray-Plane Intersection
35Ray-Triangle I Intersection
36Ray-Triangle II Intersection
37Other Ray-Primitive Intersections
38Intersection Tests How About Object-Object ?
- Methods for
- Spheres
- Oriented Boxes
- Capsules
- Lozenges
- Cylinders
- Ellipsoids
- Triangles
- But first lets check some basic issues on
collision/response
39Collision Detection
Exploit coherency through witnessing
Two convex objects are non-penetrating iff there
exists a separating plane between them
separating plane
First find a separating plane and see if it is
still valid after the next simulation step
Speed up with bounding boxes, grids, hierarchies,
etc.
40Collision Detection
41Collision Detection
Conditions for collision
separating
contact
colliding
42Collision Response
43Collision Response ? Sphere approaching a Plane
What kind of response?
Totally elastic! No kinetic energy is lost ?
response is perfectly bouncy
44Perfectly Bouncy Response
45How to Suggest Sphere Deformation?
k, in 0,1 ? coefficient of restitution
46Using Coefficient of Restitution As k gets
smaller ? more and more energy is lost ? less and
less bouncy
47Using Coefficient of Restitution As k gets
smaller ? more and more energy is lost ? less and
less bouncy
48Collision Detection
Exploit coherency through witnessing
Two convex objects are non-penetrating iff there
exists a separating plane between them
separating plane
First find a separating plane and see if it is
still valid after the next simulation step
Speed up with bounding boxes, grids, hierarchies,
etc.
49Collision Detection and Response
- Collision Detection
- Moving Sphere - Plane
- Moving Sphere - Moving Sphere
- Physically Based Modeling
- Collision Response
- Moving Under Gravity Using Euler Equations
50Ray-Plane Intersection
51Sphere-Sphere Collision
A sphere is represented using its center and its
radius
d
Intersection if d is less than the sum of their
two radius
52How About 2 MOVING spheres?
2 spheres move during a time step from one point
to another
Their paths cross in-between but this is not
enough to prove that an intersection occurred
(they could pass at a different time) nor can the
collision point be determined.
53How About 2 MOVING spheres?
- Sliced time step up into smaller pieces.
- Move the spheres according to that sliced time
step using its velocity, and check for
collisions. - If at any point collision is found (which means
the spheres have already penetrated each other)
then - Take the previous position as the intersection
point - (we could start interpolating between these
points to find the exact intersection position,
but that is mostly not required).
The smaller the time steps, the more slices we
use the more accurate the method is. As an
example lets say the time step is 1 and our
slices are 3. We would check the two balls for
collision at time 0 , 0.33, 0.66, 1.
54Sphere-Plane Collision
- Determine the exact collision point between a
particle and a plane. - The start position of the ray is the position of
the particle and the direction of the ray is its
velocity (speed and direction).
55Sphere-Plane Collision
- Each sphere has a radius, take the center of the
sphere as the particle and offset the surface
along the normal of each plane of interest. - Our actual primitives of interest are the ones
represented by continuous lines, but the
collision testing is done with the offset
primitives (represented with dotted lines). - In essence we perform the intersection test with
a little offset plane.
56Sphere-Plane Collision
- Using this little trick the ball does not
penetrate the surface if an intersection is
determined with its center. - Otherwise we get the situation where the sphere
penetrates the surface. This happens because we
determine the intersection between its center and
the primitives.
57Exact Collision Time
- We move the sphere (according to its velocity),
calculate its new position and find the distance
between the start and end point. - To calculate the exact time we solve the
following simple equation - Tc DscT / Dst
- Dst ? distance between start - end point
- Dsc ? the distance between start - collision
point - T ? the time step as T
58The Collision Point
- The returned time Tc is a fraction of the whole
time step, so if the time step was 1 sec, and we
found an intersection exactly in the middle of
the distance, the calculated collision time would
be 0.5 sec. this is interpreted as "0.5 sec after
the start there is an intersection". - Now the intersection point can be calculated by
just multiplying Tc with the current velocity and
adding it to the start point. Collision point
Start VelocityTc - This is the collision point on the offset
primitive, to find the collision point on the
real primitive we add to that point the reverse
of the normal at that point (which is also
returned by the intersection routines) by the
radius of the sphere.
59Collisions Between Spheres (1)
- U1 and U2 ? velocity vectors of the two spheres
at the time of impact - X_Axis ? vector which joins the 2 centers of the
spheres - U1x, U2x ? projected vectors of the velocity
vectors U1,U2 onto the axis (X_Axis) vector - U1y and U2y are the projected vectors of the
velocity vectors U1,U2 onto the axis which is
perpendicular to the X_Axis
60a) Find X_Axis X_Axis (center2 -
center1)Unify X_Axis, X_Axis.unit() b) Find
Projections U1x X_Axis (X_Axis dot U1)U1y
U1 - U1xU2x -X_Axis (-X_Axis dot U2)U2y U2
- U2x c) Find New Velocities (U1x
M1)(U2xM2)-(U1x-U2x)M2V1x -------------------
---------------------------------
M1M2 (U1x
M1)(U2xM2)-(U2x-U1x)M1V2x -------------------
-----------------------------------
M1M2 d) Find The Final
Velocities V1yU1yV2yU2yV1V1xV1yV2V2xV2y
- M1, M2 is the mass of the two spheres
- V1,V2 are the new velocities after the impact,
- V1x, V1y, V2x, V2y are the projections of the
velocity vectors onto the X_Axis.
61Moving Under Gravity
- All the computations are going to be performed
using time steps. - This means that the whole simulation is advanced
in certain time steps during which all the
movement, collision and response tests are
performed. - As an example we can advanced a simulation 2 sec.
on each frame. Based on Euler equations, the
velocity and position at each new time step is
computed as follows Velocity_New
Velovity_Old AccelerationTimeStepPosition_New
Position_Old Velocity_NewTimeStep
62Moving Under Gravity
- Velocity_New Velovity_Old AccelerationTimeSte
p - Position_New Position_Old Velocity_NewTimeSte
p - Now the objects are moved and tested against
collision using this new velocity. - The Acceleration for each object is determined by
accumulating the forces which are acted upon it
and divide by its mass according to this
equation - Force mass
acceleration - But in our case the only force the objects get is
the gravity, which can be represented right away
as a vector indicating acceleration. - In our case something negative in the Y direction
like (0,-0.5,0).
63- At the beginning of each time step
- Calculate the new velocity of each sphere
- Move them testing for collisions.
- If a collision occurs during a time step
- (say after 0.5 sec with a time step equal to 1
sec.) - Advance the object to this position
- Compute the reflection (new velocity vector)
- Move the object for the remaining time (which is
0.5 in our example) testing again for collisions
during this time. - This procedure gets repeated
- until the time step is completed.
64Collisions Between Spheres (2)
- Spheres of equal size and weight.
- For now lets ignore the rotation and do not
consider friction - The sphere collision will behave exactly like a
particle at the sphere's center of mass.
65Setup Terminology
- Ball A is moving at a speed of 20 feet per second
and collides with ball B at a 40 degree angle. - Impact ? a collision between two rigid bodies,
which occurs in a very short time and during
which the two bodies exert relatively large
forces on each other - Impulsive force ?the force between these two
bodies during the collision (symbolized by j) - Line of collision ? The common normal to the
surfaces of the bodies in contact
66New velocity along the line of collision
Impulsive force between the bodies?
- The impulse acts on both bodies at the same time
- The forces exerted by two particles on each other
are equal in magnitude and opposite in direction. - Since the impulse forces are equal and opposite,
momentum is therefore conserved before and after
the collision. - Momentum of a rigid body is mass times velocity
(mv).
67We need to derive the impulse force directly.
The impulse force creates a change in momentum
of the two bodies with the following relationship
68We need to derive the impulse force directly.
The impulse force creates a change in momentum
of the two bodies with the following relationship
The impulse equation for a general body that does
not rotate.
Chris Hecker "Physics, Part 3 Collision
Response," Behind the Screen, Game Developer,
February/March 1997
69k, in 0,1 ? coefficient of restitution