49
CSCE 552 Fall 2012 Math, Physics and Collision Detection By Jijun Tang

CSCE 552 Fall 2012 Math, Physics and Collision Detection By Jijun Tang

Embed Size (px)

Citation preview

CSCE 552 Fall 2012

Math, Physics and Collision Detection

By Jijun Tang

Homework #2

Major use cases of your system Due on Wednesday Oct 17th, before

class.

Use Cases Use Case Name: Place Order Actors:

Registered Shopper (Has an existing account, possibly with billing and shipping information) Fulfillment System (processes orders for delivery to customers) Billing System (bills customers for orders that have been placed)

Triggers: The user indicates that she wants to purchase items that she has selected.

Preconditions: User has selected the items to be purchased.

Post-conditions: The order will be placed in the system. The user will have a tracking ID for the order. The user will know the estimated delivery date for the order.

Flow: The user will indicate that she wants to order the items that have already been selected. The system will present the billing and shipping information that the user previously stored. The user will confirm that the existing billing and shipping information should be used for this order. The system will present the amount that the order will cost, including applicable taxes and shipping charges. The user will confirm that the order information is accurate. The system will provide the user with a tracking ID for the order. The system will submit the order to the fulfillment system for evaluation. The fulfillment system will provide the system with an estimated delivery date. The system will present the estimated delivery date to the user. The user will indicate that the order should be placed. The system will request that the billing system should charge the user for the order. The billing system will confirm that the charge has been placed for the order. The system will submit the order to the fulfillment system for processing. The fulfillment system will confirm that the order is being processed. The system will indicate to the user that the user has been charged for the order. The system will indicate to the user that the order has been placed. The user will exit the system.

Real-time Physics in Game at Runtime:

Enables the emergent behavior that provides player a richer game experience

Potential to provide full cost savings to developer/publisher

Difficult May require significant upgrade of game engine May require significant update of asset creation pipelines May require special training for modelers, animators, and

level designers Licensing an existing engine may significantly

increase third party middleware costs

Location of Particle in World Space SI Units: meters (m)

Changes over time when object moves

Particle Position

zyx ppp ,,p

Particle Velocity and Acceleration

Velocity (SI units: m/s) First time derivative of position:

Acceleration (SI units: m/s2) First time derivative of velocity Second time derivative of position

)()()(

lim)(0

tdt

d

t

tttt

tp

ppV

)()()(2

2

tdt

dt

dt

dt pVa

Newton’s 2nd Law of Motion

Paraphrased – “An object’s change in velocity is proportional to an applied force”

The Classic Equation:

m = mass (SI units: kilograms, kg) F(t) = force (SI units: Newtons)

tmt aF

Concrete Example: Target Practice

F = w eig ht = m gTarget

Projectile LaunchPosition, pinit

Finite Difference Methods-I

The Explicit Euler Integrator:

Properties of object are stored in a state vector, S Use the above integrator equation to incrementally update S over time

as game progresses Must keep track of prior value of S in order to compute the new For Explicit Euler, one choice of state and state derivative for particle:

)( 2

derivative statestateprior statenew

tOtdt

dtttt

SSS

pVS ,m VFS ,dtd

Finite Difference Methods-II

The Verlet Integrator:

Must store state at two prior time steps, S(t) and S(t-t) Uses second derivative of state instead of the first Valid for constant time step only (as shown above) For Verlet, choice of state and state derivative for a particle:

pS aFS mdtd /22

derivative state

2

22

2 stateprior 1 stateprior state new

)(2

t

dt

dtttttt SSSS

Errors

Exact

Euler

Linear Springs

dllkF restspring )(

Viscous Damping

ddVVcF epepdamping ))(( 12

Aerodynamic Drag

S: projected front area

CD: drag coefficient

Friction

Collision Detection and Resolution

What is Collision Detection

A fundamental problem in computer games, computer animation, physically-based modeling, geometric modeling, and robotics.

Including algorithms: To check for collision, i.e. intersection, of

two given objects To calculate trajectories, impact times and

impact points in a physical simulation.

Collision Detection

Complicated for two reasons Geometry is typically very complex, potentially

requiring expensive testing Naïve solution is O(n2) time complexity, since

every object can potentially collide with every other object

Two basic techniques Overlap testing: Detects whether a collision has

already occurred Intersection testing: Predicts whether a collision

will occur in the future

Overlap Testing (a posteriori)

Overlap testing: Detects whether a collision has already occurred, sometime is referred as a posteriori

Facts Most common technique used in games Exhibits more error than intersection testing

Concept For every (small) simulation step, test every pair

of objects to see if they overlap Easy for simple volumes like spheres, harder for

polygonal models

Overlap Testing Results

Useful results of detected collision Pairs of objects will have collision Time of collision to take place Collision normal vector

Collision time calculated by moving object back in time until right before collision Bisection is an effective technique

Bisect Testing: collision detected

B B

t1

t0.375

t0.25

B

t0

I te r a tio n 1F o r w ar d 1 /2

I te r a tio n 2Bac k w ar d 1 /4

I te r a tio n 3F o r w ar d 1 /8

I te r a tio n 4F o r w ar d 1 /1 6

I te r a tio n 5Bac k w ar d 1 /3 2

I n itia l O v er lapT es t

t0.5t0.4375 t0.40625

BB B

A

A

A

AA A

Bisect Testing: Iteration I

B B

t1

t0.375

t0.25

B

t0

I te r a tio n 1F o r w ar d 1 /2

I te r a tio n 2Bac k w ar d 1 /4

I te r a tio n 3F o r w ar d 1 /8

I te r a tio n 4F o r w ar d 1 /1 6

I te r a tio n 5Bac k w ar d 1 /3 2

I n itia l O v er lapT es t

t0.5t0.4375 t0.40625

BB B

A

A

A

AA A

Bisect Testing: Iteration II

B B

t1

t0.375

t0.25

B

t0

I te r a tio n 1F o r w ar d 1 /2

I te r a tio n 2Bac k w ar d 1 /4

I te r a tio n 3F o r w ar d 1 /8

I te r a tio n 4F o r w ar d 1 /1 6

I te r a tio n 5Bac k w ar d 1 /3 2

I n itia l O v er lapT es t

t0.5t0.4375 t0.40625

BB B

A

A

A

AA A

Bisect Testing: Iteration III

B B

t1

t0.375

t0.25

B

t0

I te r a tio n 1F o r w ar d 1 /2

I te r a tio n 2Bac k w ar d 1 /4

I te r a tio n 3F o r w ar d 1 /8

I te r a tio n 4F o r w ar d 1 /1 6

I te r a tio n 5Bac k w ar d 1 /3 2

I n itia l O v er lapT es t

t0.5t0.4375 t0.40625

BB B

A

A

A

AA A

Bisect Testing: Iteration IV

B B

t1

t0.375

t0.25

B

t0

I te r a tio n 1F o r w ar d 1 /2

I te r a tio n 2Bac k w ar d 1 /4

I te r a tio n 3F o r w ar d 1 /8

I te r a tio n 4F o r w ar d 1 /1 6

I te r a tio n 5Bac k w ar d 1 /3 2

I n itia l O v er lapT es t

t0.5t0.4375 t0.40625

BB B

A

A

A

AA A

Bisect Testing: Iteration V

B B

t1

t0.375

t0.25

B

t0

I te r a tio n 1F o r w ar d 1 /2

I te r a tio n 2Bac k w ar d 1 /4

I te r a tio n 3F o r w ar d 1 /8

I te r a tio n 4F o r w ar d 1 /1 6

I te r a tio n 5Bac k w ar d 1 /3 2

I n itia l O v er lapT es t

t0.5t0.4375 t0.40625

BB B

A

A

A

AA A

Time right before the collision

Overlap Testing: Limitations

Fails with objects that move too fast Thin glass vs. bulltes Unlikely to catch time slice during overlap

t0t -1 t1 t2

b u lle t

w in d o w

Solution for This Limitation

Speed of the fastest object multiplies the time step should be smaller than the smallest objects in the scene

Possible solutions Design constraint on speed of objects:

hard to apply without affecting the play Reduce simulation step size: too

expensive

Intersection Testing (a priori)

Predict future collisions When predicted:

Move simulation to time of collision Resolve collision Simulate remaining time step

Intersection Testing:Swept Geometry

Extrude geometry in direction of movement Swept sphere turns into a “capsule” shape

t0

t1

Intersection Testing:Sphere-Sphere Collision

Q 1

Q 2

P 1

P 2

P

Q

t= 0

t= 0

t= 1

t= 1

t

,

2

2222

B

rrΑBt

QP

BAΒΑ .QQPPB

QPA

1212

11

d

Special Cases

No collision:

B2 = 0: both objects are stationary, or they are traveling at parallel

When will collision occur?

02222 QP rrΑBBA

02222 QP rrΑBBA

Intersection Testing:When to Collide

Smallest distance ever separating two spheres:

If

there is a collision

2

222

BAd

BA

22QP rrd

Intersection Testing:Limitations

Issue with networked games Future predictions rely on exact state of

world at present time Due to packet latency, current state not

always coherent Assumes constant velocity and zero

acceleration over simulation step Has implications for physics model and

choice of integrator

Dealing with Complexity

Two issues1. Complex geometry must be simplified

2. Reduce number of object pair tests

Simplified Geometry

Approximate complex objects with simpler geometry, like this ellipsoid or bounding boxes

Minkowski Sum

By taking the Minkowski Sum of two complex volumes and creating a new volume, overlap can be found by testing if a single point is within the new volume

Minkowski Sum

Y}B and :{ XABAYX

X Y =YX X Y =

Using Minkowski Sum

t0

t1

t0

t1

Bounding Volumes

Bounding volume is a simple geometric shape Completely encapsulates object If no collision with bounding volume, no

more testing is required Common bounding volumes

Sphere Box

Box Bounding Volumes

Ax is - Alig n ed Bo u n d in g Bo x O r ien ted Bo u n d in g Bo x

More Examples

Using Bounding Box in Game

Complex objects can have multiple bounding boxes Human object can have one big bounding box

for the whole body Human object can have one bounding box per

limb, head, etc Bounding box can be hierarchical:

Test the big first if possible collision, test the smaller ones

Reduce Number of Detections

O(n) Time Complexity can be achieved.

One solution is to partition space

Achieving O(n) Time Complexity

Another solution is the plane sweep algorithm

Requires (re-)sorting in x (y) coordinate

C

B

R

A

x

y

A 0 A 1 R 0 B0 R 1 C 0 C 1B1

B0

B1A 1

A 0

R 1

R 0

C 1

C 0

Quadtree

Octree

R-tree

K-d tree