10-01-2023, 02:05 PM
(This post was last modified: 10-01-2023, 02:06 PM by josemendez.)
(10-01-2023, 12:48 PM)Seahorse Wrote:One mystery: Why is the count of m_IDs and m_IDToIndex is 0? solver batchCode:public void RemoveConstraint(int constraintIndex)
{
SwapConstraints(constraintIndex, constraintCount - 1);
m_IDs.RemoveAt(constraintCount - 1);
m_IDToIndex.RemoveAt(constraintCount - 1);
m_ConstraintCount--;
m_ActiveConstraintCount = Mathf.Min(m_ActiveConstraintCount, m_ConstraintCount);
}
Because these are only populated for actor/blueprint batches.
I think I misunderstood what you were trying to do here. Typically, you can only destroy constraints from the solver when they're not part of any specific actor, for instance pin or stitch constraints. If you're dealing with shape matching constraints that haven't been manually added to the solver (but added as part of an existing actor), you have two options:
A) - Take the entire actor out of the solver, destroy the constraints in the actor's blueprint (or add new constraints, reorder them, anything goes), then put the actor back into the solver. This is the most flexible approach, but also the less performant.
When using this method, typically you want to create an instance of the blueprint at runtime to avoid modifying the one stored as an asset in your project.
So, something like this:
Code:
// create an instance of the original blueprint:
softbody.softbodyBlueprint = Instantiate(softbody.softbodyBlueprint);
// take the actor out of the solver:
softbody.RemoveFromSolver();
// do anything you want with the blueprint, add, remove, deactivate constraints, etc. Here I'm just regenerating it from scratch.
softbody.softbodyBlueprint.GenerateImmediate();
//place the actor back into the solver:
softbody.AddToSolver();
B) - Instead of completely removing the constraints, deactivate them. Then, just flag them dirty so that the solver batches are updated. This is closer to what you were originally doing. For instance:
Code:
var dc = m_Actor.GetConstraintsByType(Oni.ConstraintType.ShapeMatching) as ObiConstraints<ObiShapeMatchingConstraintsBatch>;
for (int j = 0; j < dc.GetBatchCount(); j++)
{
var batch = dc.batches[j];
for (int i = batch.activeConstraintCount-1; i >= 0; --i)
{
for (int k = 0; k < batch.numIndices[i]; ++k)
{
int p = batch.particleIndices[batch.firstIndex[i] + k];
int or = m_Actor.solverIndices[p];
Vector3 shapePos = m_Actor.solver.positions[or];
if (box.bounds.Contains(shapePos))
{
batch.DeactivateConstraint(i);
}
}
}
}
m_Actor.SetConstraintsDirty(Oni.ConstraintType.ShapeMatching);
let me know if you need further help,
cheers!