Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact...

78
Contact Generation Erwin Coumans Sony Computer Entertainment US R&D

Transcript of Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact...

Page 1: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

Contact Generation

Erwin Coumans

Sony Computer Entertainment US R&D

Page 2: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

Contact generation

• Pipeline overview

• A single contact point

• Contact clipping

• Multiple contact points using perturbation

• Persistent contact caching

• Internal edges and contact normals

• Dynamic aabb tree acceleration structure

Page 3: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

Physics Pipeline

Forward Dynamics

Computation

Collision Detection

Computation

Detect

pairs

Forward Dynamics

Computation

Setup

constraints

Solve

constraints

Integrate

position

Apply

gravity

Collision Data Dynamics Data

Compute

AABBs

Predict

transforms

Collision

shapes

Object

AABBs

Overlapping

pairs

World

transforms

velocities

Mass

Inertia

Constraints

(contacts,

joints)

Compute

contact

points

Contact

points

timeStart End

AABB = axis aligned bounding box

Page 4: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

Collision Detection Pipeline

Broadphase

Collision Detection

Detect

pairs

Collision Data

Compute

AABBs

Collision

shapes

Object

AABBs

Overlapping

concave

pairs

World

transforms &

velocities

Contact

points

Midphase (concave)

Collision Detection

Narrowphase

Collision Detection

Compute

closest

points

Detect

overlapping

triangles

(trimesh)

Generate

full contact

manifold

Detect

overlapping

child shapes

(compound)

Local

AABB

Tree

Overlapping

convex

pairs

timeStart End

culling using acceleration structures

Page 5: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

Contact generation

• Pipeline overview

• A single contact point

• Contact clipping

• Multiple contact points using perturbation

• Persistent contact caching

• Internal edges and contact normals

• Dynamic aabb tree acceleration structure

Page 6: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

Closest point computation

void computeClosestPoints(…);

• Black box is discussed in Erin Catto’s GJK talk

• GJK needs companion algorithm for penetration

Page 7: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

Touching contact

distance = 0

Page 8: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

Shortest distance

distance > 0

Shape

A

Shape

B

Page 9: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

Closest points (witness)

Page 10: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

Separating vectors

Page 11: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

Minimum translational distance

distance < 0

Page 12: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

Points of deepest penetration

Page 13: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

Minimum separating vector

Page 14: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

• Distance

• A separating normal

• Pair of closest points

What is a contact point?Shape

A

Shape

B

Page 15: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

• a = b + n * d

Removing redundancy

Shape

A

Shape

B

a

n

b

d

Page 16: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

Contact point structure

struct ContactPoint

{

float m_distance;

Vector3 m_normalB;

Vector3 m_pointOnB;

};

Page 17: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

Single contact

Page 18: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

Single contact point

• Works fast and stable for simple cases

Page 19: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

Single contact

Page 20: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

Single contact

Page 21: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

Single contact

Page 22: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

Contact generation

• Pipeline overview

• A single contact point

• Contact clipping

• Multiple contact points using perturbation

• Persistent contact caching

• Internal edges and contact normals

• Dynamic aabb tree acceleration structure

Page 23: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

Box-Box Clipping Setup

• Identify reference face

• Identify incident face

n

incident

reference

Page 24: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

Box-Box Clipping

• Clip incident face against reference face side planes (but not the reference face).

• Consider clip points with positive penetration.

n

clipping planes

Page 25: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

Contact generation

• Pipeline overview

• A single contact point

• Contact clipping

• Multiple contact points using perturbation

• Persistent contact caching

• Internal edges and contact normals

• Dynamic aabb tree acceleration structure

Page 26: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

Multiple points for general convex

• General convex might not have vertices/faces

• Compute multiple closest point samples

• All “single shot” within this simulation step

Page 27: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

Perturbing around normal

• Works well but costs additional GJK queries

Page 28: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

Perturbation pseudo code

contact = computeContact(shapeA,transA,shapeB,transB,…);

calcOrthogonalVectors(contact.normalB,ortho0,ortho1);

Matrix3x3 R_p = MatrixFromAxisAngle (ortho0, perturbationAngle);

float angle = 360 / number of iterations;

for (i = 0 ; i< number of iterations;i++)

{

Matrix3x3 Rn = MatrixFromAxisAngle(contact.normalB,angle*i);

perturbedA = Rn.inverse() * R_p * Rn * transA.R();

computeContact(shapeA,perturbedA,shapeB,transB,...);

updateContactDistance(transA,transB);

}

Page 29: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

• Issue with round shapes: they start rolling

Perturbing around normal

Page 30: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

• Replace contact at actual position

• Keep perturbation small

• Last resort: apply damping or skip round shapes

Perturbing around normal

Page 31: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

Perturbating in 3D

Page 32: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

DEMO!!!

Page 33: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

Contact generation

• Pipeline overview

• A single contact point

• Contact clipping

• Multiple contact points using perturbation

• Persistent contact caching

• Internal edges and contact normals

• Dynamic aabb tree acceleration structure

Page 34: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

Incremental contact caching

• Add a single point at a time to small cache

• Refresh cache, update or remove points

Page 35: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

Adding points to cache

• Check for duplicate points

• Use feature id or distance tolerance

• Reduce points when more than 4 points

• Always keep point with deepest penetration

• Try to maximize area

Page 36: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

Cache local closest points

Shape

A

Shape

B

a

n

b

d a

n

b

d‘

Page 37: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

Update contact distance

worldPosA = transA(localPosA);

worldPosB = transB(localPosB);

distance = (worldPosA-worldPosB).dot(worldNormalB);

d

a

b

n

Page 38: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

Removing points

a

n

b

d‘

Page 39: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

Hybrid method

• Single shot manifold to build a full cache

• Only add a single point to a full cache

• Google for btPersistentManifold

Page 40: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

• GJK doesn’t work in penetrating cases

• and penetration depth calculation is a bit slower

Collision margins

Page 41: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

• Sphere can be a point with radius as margin

Collision margins

Page 42: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

Compensate for margins

Page 43: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

Collision margin approximation

B

core shape

B

Page 44: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

Collision margin approximation

error

error

Page 45: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

DEMO!!!

Page 46: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

Contact generation

• Pipeline overview

• A single contact point

• Contact clipping

• Multiple contact points using perturbation

• Persistent contact caching

• Internal edges and contact normals

• Dynamic aabb tree acceleration structure

Page 47: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

Internal edge collisions

triangle Atriangle B

internal edge

3D view

2D view

Page 48: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

• Object is colliding against triangle B

• Contact normal is pointing against velocity

Internal edge collisions

triangle Atriangle B

internal edge

2D view

Page 49: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

• Works well for flat connected triangles

Solution 1: triangle normal

triangle A triangle normal B

internal edge

2D view

corrected

contact normal

Page 50: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

• Works well for concave edges

Solution 1: triangle normal

triangle A triangle normal B

internal edge

2D view

corrected

contact normal

Page 51: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

• Object can be pushed of a cliff

Cliff problem

triangle A

triangle normal Binternal edge

2D view

corrected

contact normal

Page 52: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

• Only adjust normal outside voronoi region

Solution 2: use voronoi regions

triangle A

triangle normal Binternal edge

2D view

triangle normal Aedge voronoi region

Page 53: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

• No correction needed

Solution 2: use voronoi regions

triangle A

triangle normal B

internal edge

2D view

triangle normal Aedge voronoi region

triangle B

Page 54: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

• Rotate normal towards voronoi region

Solution 2: use voronoi regions

triangle A

triangle normal B

internal edge

2D view

triangle normal Aedge voronoi region

triangle B

Page 55: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

Implementation

• Google for btInternalEdgeUtility

Page 56: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

Contact generation

• Pipeline overview

• A single contact point

• Contact clipping

• Multiple contact points using perturbation

• Persistent contact caching

• Internal edges and contact normals

• Dynamic aabb tree acceleration structure

Page 57: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

Collision Detection Pipeline

Broadphase

Collision Detection

Detect

pairs

Collision Data

Compute

AABBs

Collision

shapes

Object

AABBs

Overlapping

concave

pairs

World

transforms &

velocities

Contact

points

Midphase (concave)

Collision Detection

Narrowphase

Collision Detection

Compute

closest

points

Detect

overlapping

triangles

(trimesh)

Generate

full contact

manifold

Detect

overlapping

child shapes

(compound)

Local

AABB

Tree

Overlapping

convex

pairs

timeStart End

culling using acceleration structures

Page 58: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

Concave shapes

Page 59: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

Convex decomposition

Page 60: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

Concave triangle mesh

Page 61: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

Concave triangle mesh

Page 62: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

Contact for concave shapes

• GJK and EPA can only deal with convex objects

• Convex decomposition

• Triangle meshes: a single triangle is convex

• Concave mesh is just a collection of triangles

• Deal with each triangle individually

Page 63: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

Concave Shapes: AABB trees

splitting axis

Page 64: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

AABB tree structure

convex

leaf nodes

root node

internal nodes

(don’t contain geometry)

Page 65: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

AABB tree queries by traversal

• Find overlapping nodes given a AABB

• Find overlapping nodes given a Ray (from,to)

Page 66: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

AABB tree traversal

• Recursive

• Stackless with skip indices

• History tracking (see Harada’s talk)

Page 67: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

AABB tree

• Static AABB tree

• Can be optimized and quantized

• Allows for basic tree refit operator

• Dynamic AABB tree

• Can deal with change in topology

• Add and remove nodes

• Incrementally rebalance tree

• Is very general purpose

Page 68: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

• Find leaf with smallest Manhattan distance

Adding a node

Page 69: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

Implementation insert leaf nodevoid insertleaf( btDbvt* pdbvt,btDbvtNode* root,btDbvtNode* leaf) {

if(!pdbvt->m_root){pdbvt->m_root=leaf;leaf->parent=0;}

if(!root->isleaf()) {

do {

root=root->childs[Select(leaf->volume,root->childs[0]->volume,

root->childs[1]->volume)];

} while(!root->isleaf());

}

btDbvtNode* prev=root->parent;

btDbvtNode* node=createnode(pdbvt,prev,leaf->volume,root->volume,0);

if(prev) {

prev->childs[indexof(root)]=node;

node->childs[0] = root;root->parent=node;

node->childs[1] = leaf;leaf->parent=node;

do {

if(!prev->volume.Contain(node->volume))

Merge(prev->childs[0]->volume,prev->childs[1]->volume,prev->volume);

else

break;

node=prev;

} while(0!=(prev=node->parent));

}

else {

node->childs[0] = root;root->parent=node;

node->childs[1] = leaf;leaf->parent=node;

pdbvt->m_root = node;

}

}

Page 70: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

• Find and remove node and relink

Removing a leaf node

Page 71: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

Implementation remove leaf1: btDbvtNode* removeleaf(btDbvt* pdbvt, btDbvtNode* leaf){

2: if(leaf==pdbvt->m_root){ pdbvt->m_root=0; return(0);}

3: btDbvtNode* parent=leaf->parent;

4: btDbvtNode* prev=parent->parent;

5: btDbvtNode* sibling=parent->childs[1-indexof(leaf)];

6: if(prev)

7: {

8: prev->childs[indexof(parent)]=sibling;

9: sibling->parent=prev;

10: deletenode(pdbvt,parent);

11: while(prev) {

12: const btDbvtVolume pb=prev->volume;

13: Merge(prev->childs[0]->volume,prev->childs[1]->volume,prev->volume);

14: if(NotEqual(pb,prev->volume)){

15: prev=prev->parent;

16: } else break;

17: }

18: return(prev?prev:pdbvt->m_root);

19: }

20: else

21: {

22: pdbvt->m_root=sibling;

23: sibling->parent=0;

24: deletenode(pdbvt,parent);

25: return(pdbvt->m_root);

26: }

27: }

Page 72: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

Update/move a leaf node

• If new AABB is contained by old do nothing

• Otherwise remove and re-insert leaf

• Re-insert at closest ancestor that was not resized during remove (see line 18 previous page)

• Expand AABB with margin

• Avoid updates due to jitter or small random motion

• Expand AABB with velocity

• Handle the case of linear motion over n frames

Page 73: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

Incremental tree optimization

• Rebalance tree removing and inserting a few leaf nodes at a time

Page 74: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

Dynamic AABB tree broadphase

0 1 2 3

12 13 14 15

5 7

8 10 11

B

C E

D

F

A

B

C E

D

F

ADF

BC E A

Page 75: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

Dynamic BVH tree broadphase

• Keep two dynamic trees, one for moving objects, other for objects (sleeping/static)

• Find neighbor pairs:

• Overlap M versus M and Overlap M versus S

DF

BC E A

QP

UR S T

S: Non-moving DBVT M: Moving DBVT

Page 76: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

DEMO!!!

Page 77: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

Summary

• Use a persistent manifold for multiple points

• keep contacts in local space and update distance

• Adjust normals to avoid internal edge collisions

• only if normal outside edge voronoi region

• Dynamic AABB trees are fast and versatile acceleration structure for

• broadphase pair search and ray test

• midphase for triangle meshes, cloth, deformables

• occlusion and view frustum culling

Page 78: Contact Generation...Contact generation •Pipeline overview •A single contact point •Contact clipping •Multiple contact points using perturbation •Persistent contact caching

Bullet

• An open source 3D physics engine

• http://bulletphysics.org

• Written in C++