iMSTK
Interactive Medical Simulation Toolkit
Docs/Collision.md
1 # Collision
2 
3 Physics-based objects won't do much without an external force (such as gravity provided in many models). One such interaction is contact. At a high level iMSTK provides various objects for one to specify a collision between two objects.
4 
5 ## PbdObjectCollision
6 
7 Collision between two PBD simulated objects can be specified like so:
8 
9 ```cpp
10 auto collision = std::make_shared<PbdObjectCollision>(myPbdObject, myOtherPbdObject);
11 scene->addSceneObject(collision);
12 ```
13 
14 Both objects must contain a collision geometry
15 ```cpp
16 myPbdObject->setCollidingGeometry(mySphereGeometry);
17 ```
18 
19 These collisions are two-way interactions. Whether it's a rigid or deformable (cloth, tissues). Both objects will experience reactions.
20 
21 Note: Not all geometries collide with all other geometries. See the collision page for a chart on supported collisions.
22 
23 ### PbdObjectCollision Parameters
24 
25 `PbdObjectCollision` supports friction, restitution, and contains a compliance/stiffness. It has appropriate defaults.
26 
27 When rigid-rigid or rigid-deformable collisions are used one can supply the rigid body compliance to alter how "stiff" the collision is. 0 represents infinitely rigid collisions. In reality these are hard to keep stable. So a small epsilon of a compliance is often used. This parameter can also be used to loosen the collision greatly like a spring.
28 
29 ```cpp
30 auto collision = std::make_shared<PbdObjectCollision>(myPbdObject, myOtherPbdObject);
31 collision->setFriction(0.1); // 0-1 value. 0 is no friction. 1 is max friction.
32 collision->setRestitution(0.0); // 0-1 value. 0 is full bounce. 1 is no bounce.
33 collision->setRigidBodyCompliance(0.000001); // 0 is completely rigid
34 ```
35 
36 When deformable-deformable collisions are used one instead can supply a stiffness for each side of the collision. If one side of the collision objects is static/non-simulated then its deformable stiffness is zeroed out. Stiffness here can be seen as step size. With one step of the solver 1.0 is a valid. With multiple steps a smaller value is desirable to avoid overshooting.
37 
38 ```cpp
39 auto collision = std::make_shared<PbdObjectCollision>(myPbdObject, myOtherPbdObject);
40 collision->setFriction(0.1); // 0-1 value. 0 is no friction. 1 is max friction.
41 collision->setRestitution(0.0); // 0-1 value. 0 is full bounce. 1 is no bounce.
42 collision->setDeformableStiffnessA(0.3); // 0 is completely loose
43 collision->setDeformableStiffnessB(0.3); // 0 is completely loose
44 ```
45 
46 <p align="center">
47  <img src="media/rigidCapsuleInThinTissue.png" alt="Capsule dropped into a cloth"/>
48 </p>
49 
50 ### Collision with Statics
51 
52 Collisions with non-simulated objects is also supported, just supply a `CollidingObject` to the `PbdObjectCollision` on the right hand side:
53 
54 ```cpp
55 auto collidingObject = std::make_shared<CollidingObject>();
56 auto pbdObject = std::make_shared<PbdObject>();
57 ...
58 auto collision = std::make_shared<PbdObjectCollision>(pbdObject, collidingObject);
59 scene->addSceneObject(collision);
60 ```
61 
62 ## RigidObjectCollision
63 
64 Gives collision for `RigidObject2` types. These are older deprecated types. It is setup in a similar way though.
65 
66 ```cpp
67 auto collision = std::make_shared<RigidObjectCollision>(myRbdObject, myRbdObject);
68 // OR with a static
69 auto collision = std::make_shared<RigidObjectCollision>(myRbdObject, myCollidingObject);
70 ```
71 
72 It does not allow any compliance, the solver always converges to complete rigidity. It has a friction term, and a baumgarte stabilization term which can vary the restitution and effects the `stiffness` of the collision. When high it will allow the object to penetrate more over a step but bounce more.
73 
74 <p align="center">
75  <img src="media/rbd.png" alt="Grasping thin triangle tissue"/>
76 </p>
77 
78 ## SphObjectCollision
79 
80 Gives collision for `SphObject`. Can only be used for collision between a `SphObject` fluid and static `CollidingObject`.
81 
82 ```cpp
83 scene->addInteraction(std::make_shared<SphObjectCollision>(mySphObject, myCollidingObject));
84 ```
85 
86 ## [Collision Detection](Collision/Collision_Detection.md)
87 
88 For details on `CollisionDetectionAlgorithm`, this page describes the geometric operations under the hood used to compute a collision. Collision can be used standalone if the user does not required resolution.
89 
90 ```cpp
91 SphereToSphereCD detector;
92 detector.setInput(0, mySphereA);
93 detector.setInput(1, mySphereB);
94 detector.update();
95 ```
96 
97 Which then produces `CollisionData` for later usage.
98 
99 ## [Collision Handling](Collision/Collision_Handling.md)
100 
101 For details on PbdObjectCollision and implementations of resolution. Not neccesary for average users but can be helpful. A `CollisionHandler` implements how to consume `CollisionData`. iMSTK provides handling methods, typically these call upon the functions of a DynamicalModel to respond (explicit solve) or add something (such as a constraint) to later implicitly solve.