Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Change Shape Matching Constraints at Runtime
#1
Hi,
first of all, you created amazing engine, kudos.

I'm not able to change shape matchig constraints at runtime.
My scenario: 1 solver has 1 softbody

I want to be able to change max deformation and deformation resistance at runtime. I really tried to do this based on what you write in http://obi.virtualmethodstudio.com/tutor...aints.html, but no luck.
First question -> you did write this "there's only one skin constraint per particle", where can I get info on how many actor constraints are for particle in softbody?


I tried this:
Code:
var shapeConstraints = softbody.GetConstraintsByType(Oni.ConstraintType.ShapeMatching) as ObiConstraints<ObiShapeMatchingConstraintsBatch>;
var solverConstraints = softbody.solver.GetConstraintsByType(Oni.ConstraintType.ShapeMatching) as ObiConstraints<ObiShapeMatchingConstraintsBatch>;

var softbodyBatch = shapeConstraints.batches[0];
int offset = softbody.solverBatchOffsets[(int)Oni.ConstraintType.ShapeMatching][0];

for (int i = 0; i < shapeConstraints.batches.Count; i++) {
    var solverBatch = solverConstraints.batches[i];
    for (int j = 0; j < softbodyBatch.activeConstraintCount; ++j) {
        int index = i + offset;
        solverBatch.materialParameters[0] = 0.1f; //deformationResistance
        solverBatch.materialParameters[4] = 0.9f; //maxDeformation
    }
}


And quite few variants but, I can't get it right.
Reply
#2
Hi richienko,

You're retrieving the offset of the first batch only, then reusing it for all batches:

Code:
int offset = softbody.solverBatchOffsets[(int)Oni.ConstraintType.ShapeMatching][0];
for (int i = 0; i < shapeConstraints.batches.Count; i++) {

Each batch has its own offset, you should get it like so:

Code:
for (int i = 0; i < shapeConstraints.batches.Count; i++) {
    int offset = softbody.solverBatchOffsets[(int)Oni.ConstraintType.ShapeMatching][i];

The constraint index is wrong. You're using i (the batch index) instead of j when calculating it. Should be:

Code:
index = offset + j;

Also, there's 5 material parameters per constraint. You're accessing the material parameters of the first constraint only:

Code:
solverBatch.materialParameters[0] = 0.1f; //deformationResistance
solverBatch.materialParameters[4] = 0.9f; //maxDeformation

should be:

Code:
solverBatch.materialParameters[index*5+0] = 0.1f; //deformationResistance
solverBatch.materialParameters[index*5+4] = 0.9f; //maxDeformation

So the final, correct code is:

Code:
var shapeConstraints = softbody.GetConstraintsByType(Oni.ConstraintType.ShapeMatching) as ObiConstraints<ObiShapeMatchingConstraintsBatch>;
var solverConstraints = softbody.solver.GetConstraintsByType(Oni.ConstraintType.ShapeMatching) as ObiConstraints<ObiShapeMatchingConstraintsBatch>;

for (int i = 0; i < shapeConstraints.batches.Count; i++) {

    int offset = softbody.solverBatchOffsets[(int)Oni.ConstraintType.ShapeMatching][i];

    var softbodyBatch = shapeConstraints.batches[i];
    var solverBatch = solverConstraints.batches[i];

    for (int j = 0; j < softbodyBatch.activeConstraintCount; ++j) {
        int index = j + offset;
        solverBatch.materialParameters[index*5+0] = 0.1f; //deformationResistance
        solverBatch.materialParameters[index*5+4] = 0.9f; //maxDeformation
    }
}

Also note this only makes sense if you're going to apply different parameters to each individual constraint. If you want to set the same parameter for all constraints, just use the per-actor parameters as explained here:
http://obi.virtualmethodstudio.com/tutor...aints.html
Reply
#3
Oh my, I swear I tried that and maxDeformation field was not accessible.
Thanks a lot!
Reply