Obi Official Forum

Full Version: What is the proper way to add distance constraint?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I've only able to find example for adding pin constraint, and before I do anything dumb myself, I'd like to get an example for adding distance constraint through script.

Is it as simple as adding two indices for the pair of particles in the new distance constraint to the batch array and mark the constraint type as dirty?

When I look into the Tear() method it looks like I also need to apply some properties to the new constraint, but there seem to be other things going on which I'm not sure if it's only for the reconstruction after the tearing operation...
(21-06-2022, 03:06 PM)snowtv Wrote: [ -> ]I've only able to find example for adding pin constraint, and before I do anything dumb myself, I'd like to get an example for adding distance constraint through script.

Is it as simple as adding two indices for the pair of particles in the new distance constraint to the batch array and mark the constraint type as dirty?

Yes, it's basically the same as with pin constraints. The only difference among different constraint types is the arguments of the AddConstraint function in the batch.

Do keep in mind that constraints that involve the same particles must be put in separate batches. Otherwise, race conditions (multiple threads writing concurrently to the same memory positions) will arise during simulation. This is explained here:
http://obi.virtualmethodstudio.com/manua...aints.html

Constraints are sorted into batches during blueprint generation (when you click "Generate" in the editor or call the Generate() coroutine at runtime). If you're adding new constraints at runtime, you have to find an appropriate batch for them. This is a non-issue for pin constraints since generally each pin only affects 1 particle, they all can go in the same batch.

(21-06-2022, 03:06 PM)snowtv Wrote: [ -> ]When I look into the Tear() method it looks like I also need to apply some properties to the new constraint, but there seem to be other things going on which I'm not sure if it's only for the reconstruction after the tearing operation...

Tearing is a whole different beast: it is a logic layer added on top of constraints, that modifies constraints of different types during topology changes in the mesh. The StructuralElement is a wrapper over a single distance constraint, that stores the batch the constraint lives in, its index in the batch, and its stress force. During ApplyTearing(), an element is created for every active distance constraint whose stress force is over a threshold. Then, all resulting elements are sorted by stress, and the N with higher stress are considered for tearing.

Tearing a structural element updates both the cloth topology (stored in the blueprint) and activates new constraints/particles (preallocated during blueprint generation).

Looks to me like you're doing custom modifications to tearing or some other sort of advanced stuff involving constraints. This requires deep understanding of how it all works as a whole, otherwise it's rather easy to blow things up. Let me know if you need further help.