Page MenuHome

Cloth: New collision algorithm
Changes PlannedPublic

Authored by Germano Cavalcante (mano-wii) on Aug 15 2020, 12:14 AM.
"Like" token, awarded by IPv6."Love" token, awarded by Schamph."Love" token, awarded by ish_bosamiya."Love" token, awarded by ogierm."Love" token, awarded by reanimate."Love" token, awarded by mindinsomnia."Love" token, awarded by astrand130."100" token, awarded by Kickflipkid687."Love" token, awarded by brilliant_ape."Love" token, awarded by franMarz."Mountain of Wealth" token, awarded by philstopford."Love" token, awarded by vitorbalbio."Like" token, awarded by higgsas."Like" token, awarded by rpserge."Love" token, awarded by andruxa696."Love" token, awarded by amonpaike."Party Time" token, awarded by lopoIsaac."100" token, awarded by Frozen_Death_Knight."Like" token, awarded by YAFU."Like" token, awarded by MetinSeven."Love" token, awarded by jorsh.



Cloth: New collision algorithm

The current cloth collision algorithm is quite confusing.

It has some mysterious magic numbers, it also has some incomprehensible
differences between self-collision and collision in addition to other

I spent a lot of time trying to understand it, but in the end it pays
more to do another collision algorithm from scratch.

In the new collision algorithm we have:

  • Improved collision response;
  • The friction factor is more noticeable (before the difference was minimal);
  • The damping factor now works on cloth too (it only worked for soft bodies).

In addition to the new collision algorithm, this patch also:

  • Corrects an error in the BVHTree Kdop epsilon;

I haven't made a benckmark yet but this patch should be slightly faster.

Friction Test (100% and 0%):

Damping Test (0.0 and 1.0):

Diff Detail

rB Blender
cloth-collision (branched from master)
Build Status
Buildable 9683
Build 9683: arc lint + arc unit

Event Timeline

Germano Cavalcante (mano-wii) requested review of this revision.Aug 15 2020, 12:14 AM
Germano Cavalcante (mano-wii) created this revision.
Germano Cavalcante (mano-wii) retitled this revision from Fix epsilon of BVHTree nodes not representing the actual distance to Cloth: New collision algorithm.Aug 15 2020, 12:15 AM
Germano Cavalcante (mano-wii) edited the summary of this revision. (Show Details)

Awesome! Nice job on simplifying the code. I really like what you have done here.

The only thing I have except that you should add versioning code (as you stated), is that we should perhaps convert dampening and friction to percentage variables.

So then in the UI dampening and friction will go from 0% to 100%.

It was very weird before that it was capped at 80 without any mention in unit.

If it is percentages I think it would be more clear.

Or hmm... Maybe just convert the friction variable to be PROP_FACTOR and go from 0 to 1.
Then you don't have to do the 0.01 multiplication later (and it is consistent with the other collision properties like this.

  • Improve comments and separate impulse from repulsion
  • Change percentage to factor and add versioning
  • Add comment for specific colth parameters "damping" and "friction" that are not being used in either python or C.

Collision's physics values are the only ones used to simule these effects.

  • fix small mistake in versioning
  • Put versioning in the correct version (2.91)
  • Clamp impulse so that high-quality simulation doesn't push too hard.
  • Update new initial friction values

Do you have any performance numbers?

To me it doesn't really matter that much, I'm interested to see how it compares to the old code.

  • Improve impulse detection in intersecting geometry

Do you have any performance numbers?

To me it doesn't really matter that much, I'm interested to see how it compares to the old code.

Due to the correction of the BVHTree kdop epsilon, the number of overlapping geometry is greater.
So at first this patch may appear to be a little slower.

In my test file:
1min 24secs in master vs 1min 25secs with the patch.

Without the BVHTree Kdop correction it would be 1min 8secs

  • Change UI so that cloths use their own damping and friction properties (instead of depending on the object's property)
  • UI: damping and friction to hair collision
This revision is now accepted and ready to land.Aug 18 2020, 4:14 PM

This patch should be tested on more complicated files, has that been done?

A regular grid on a plane is not representative of difficult collision cases.


Why modify a value that is unused?


If this is a runtime field, why is it kept at all?


If it's only for versioning, why not mark it with DNA_DEPRECATED?

But is that actually true, I don't see any properties removed from RNA?

This patch should be tested on more complicated files, has that been done?

A regular grid on a plane is not representative of difficult collision cases.

These are the names of the files I used for testing:
colision_hair.blend: (plane with hair and 2 colliders)
collision_damp_test.blend: (collision between 4 unique triangles)
collision_damp_test_2.blend: (shown in the GIF)
collision_friction.blend: (single triangle sliding into another triangle)
collision_friction_test_2.blend: (shown in the GIF)
collision_intersect.blend: (single triangle intersecting another triangle)
collision_self.blend: (triangle with array modifier and self colision)
collision_tests.blend: (Different tests between 2 triangles)
Flags_test2.blend: (Complex file that tests collision and self collision in high poly geometry)
Flags_test.blend: (A simulation of a flag in the wind. Used for benckmark)


I was in doubt and decided to change too since I didn't remove it from RNA


I'm not sure how warnings work in other compilers, so I wasn't sure to mark it as deprecated since it's used in versioning.

I found other cloth properties not used but in RNA (like friction and damping).
At first I removed it, but it seemed to follow a convention here.

But I will remove it.

It would be good to put any cloth test files in lib/tests then when this gets committed.

These kinds of tests are useful, but the existing cloth collision algorithm was tested with more complex scenes as well:

@Luca Rood (LucaRood), do you still have .blend files used for these videos?

Germano Cavalcante (mano-wii) marked 3 inline comments as done.Aug 19 2020, 8:44 PM

I tried to redo one of the test files shown in the video:


It is very difficult to find a friction value that holds the knot and does not destroy the simulation.
But it seems that this is a limitation in both systems.

I realized that when I tighten the knot, the whole cloth vibrates a lot in the new system.
I know what is causing the vibration, but I'm not sure how to resolve it.
I need to investigate further.

  • Use the "damping" and "friction" properties of the collider object even though it affects the behavior of old file simulations.
  • Improve impulse calculation thereby reducing cloth vibration

I did the tests and I believe that the new system is working well.
The only problem I see now is versioning.
Changing the range from [0 - 100] to [0 - 1] only works if the property is not animated.

I don't know if there is any utility that updates the F-curves.
Other parts of versioning do not update F-curves.
I wonder if it's really important.


Perhaps rename this to dt for extra clarity?

impulse_fac doesn't make this think that this is time based.


I'm guessing this is to make it less prone to vibrating?
So it is less prone to "stick" to itself?

Perhaps add a comment explaining why 0.5f is a good value.
This is so people like me can figure out why this value was picked.

Sebastian Parborg (zeddb) requested changes to this revision.Aug 20 2020, 11:36 AM
This revision now requires changes to proceed.Aug 20 2020, 11:36 AM

@Brecht Van Lommel (brecht) I talked to Sergey and it seems like updating animated fcurvers will be very cumbersome and involved.
So perhaps we can just skip that case?

I don't think that there will be many files that had these animated as the friction variable didn't really do much even when maxed out before this rework.

  • Rebase master
  • Rename 'impulse_fac' to 'dt'
  • Comment 'damping_fac' valor
Germano Cavalcante (mano-wii) marked 2 inline comments as done.Aug 20 2020, 1:46 PM
Germano Cavalcante (mano-wii) added inline comments.

A damping value makes the simulation a little more stable.
But it can change the behavior seen in old files.
So I'm going to change it back to 1.0f.

(I also had an idea of how to make the collision more reliable).

It's fine to not update animated values in versioning, we don't do that generally.

Germano Cavalcante (mano-wii) planned changes to this revision.Aug 24 2020, 3:37 PM
Germano Cavalcante (mano-wii) marked an inline comment as done.

I will have to postpone this project bcon4.

Great to see someone working on this, as the currently released code is indeed some kind of borderline incomprehensible mess of patches, hacks, and workarounds. A rewrite is well overdue, so, thanks @Germano Cavalcante (mano-wii)!

But what I can say is that such a change should be extensively tested. It would be really good to see this in some real world use-cases.
Collision response is extremely fiddly, and when I was working on it, despite being actively tested with the Agent 327 project, and later Next Gen, some serious issues still slipped through unnoticed.

I could not find those specific files, @Brecht Van Lommel (brecht), but I can contribute some files that test for specific (corner)cases. But keep in mind that this is by no means an exhaustive test set, so serious real world testing is still absolutely necessary.

A different bow simulation (note that this one should actually properly hold the knot prior to being untied).

In this test the cloth should stay between the two spheres, and not be pushed to one side (it works, but is very unstable in the current system, so an improvement here would be awesome, but it should at least not get worse).

Similar to the test above, but also tests friction. In the end, the cloth should be very close to the start position.

This tests that a small collision area can support a larger surface without causing instability.

A simple self-collision impact test.

This tests that several sheets can be stacked without intersections or instability. There should be minimal to no vibration in the end.

Feel free to add these files to Blender's test set.
I hope this helps a bit, and really look forward to seeing more results with this patch!

Thanks for the files @Luca Rood (LucaRood). They will definitely help a lot.

Locally I have already made other changes to the patch.
A change for example is in compute_collision_point_tri_tri that when the triangles intersect it returns now the shortest distance to "repel" the triangle (and not the points of intersection).
One idea that can be useful is to return the repulsion weight for each vertex in compute_collision_point_tri_tri, so that all vertices can be repelled at the same time.

This patch messed up the culling system a bit and needs to be rethought.