Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help  Creating and Removing Pin Constraints from ObiPinConstraintsBatch
#3
(18-08-2023, 07:15 PM)sebjf Wrote: Hi, 

As a test, I updated my code to create an ObiPinConstraintsBatch for each sticking particle, and remove the batch entirely when its single constraint exceeds the threshold.

This works successfully, however I am not sure of the efficiency of creating and destroying entire batches this way for one particle each.

As this demonstrates the problem is indeed in trying to update an ObiPinConstraintsBatch, what is the recommended approach to do so?

This:

Code:
for (int i = 0; i < adhesionBatch.constraintCount; i++)
{
    [...]
    adhesionBatch.RemoveConstraint(i);
}

Will invariably lead to bugs since you're iterating a collection while removing items from it, skipping items in it. You have several options, listed from "cleanest" to "hackiest":

- Iterate the batch backwards.
- Store the indices of the items you want to remove as you iterate the list, then remove them all at the end.
- Use a while loop instead.
- Decrement the counter variable "i" every time you remove an item.

Note this applies to all programming languages since removing an item from a compact, array-like data structure moves all following items back one position. (see: https://stackoverflow.com/questions/1582...ng-over-it)

How you're dealing with particle indices looks correct, but can't really tell for sure since the code omits how "particlesToStick" is populated during collisions. Could you share this bit of code as well?

kind regards,
Reply


Messages In This Thread
RE: Creating and Removing Pin Constraints from ObiPinConstraintsBatch - by josemendez - 21-08-2023, 06:30 AM