Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help  Controlling specific particle force / custom constraints.
#14
That isn't a crash (as in Unity didn't close, right?), but an exception.

This is due to the snake sample scene not being designed with softbody interaction in mind, so it doesn't handle calculating traction against other actors. It assumes there's only a snake in the scene, as it is written ad-hoc for this specific example. You can modify the sample script (SnakeController) to add support for this, in case you need:

Line 61 reads:
Code:
traction[particleInActor.indexInActor] = 1;

There's only one traction array, and its length is equal to the amount of particles in the snake (line 22):

Code:
traction = new float[rope.activeParticleCount];

So when you add a softbody to the scene, particleInActor.indexInActor can be larger than the amount of particles in the snake (since the script assumes there's only one snake) thus throwing an out of bounds access exception.

It should be simple to fix, just check:

Quote:if (particleInActor.actor == rope)

To make sure the particle belongs to the snake, before accessing the traction array. Should look like this:

Code:
private void AnalyzeContacts(object sender, ObiSolver.ObiCollisionEventArgs e)
    {
        // iterate trough all contacts:
        for (int i = 0; i < e.contacts.Count; ++i)
        {
            var contact = e.contacts.Data[i];
            if (contact.distance < 0.005f)
            {
                int simplexIndex = solver.simplices[contact.bodyA];
                var particleInActor = solver.particleToActor[simplexIndex];

                if (particleInActor.actor == rope)
                {
                    // using 1 here, could calculate a traction value based on the type of terrain, friction, etc.
                    traction[particleInActor.indexInActor] = 1;

                    // accumulate surface normal:
                    surfaceNormal[particleInActor.indexInActor] += (Vector3)contact.normal;
                }
            }
        }
    }

Note it is your responsibility to modify sample scripts to work in the context of your game and fit your specific needs, as they're only that: samples Guiño.
Reply


Messages In This Thread
RE: Controlling specific particle force / custom constraints. - by josemendez - 27-07-2021, 11:38 AM