Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help  Custom Force to Match Object
#4
(26-10-2022, 12:22 AM)docgonzzo Wrote: I was wondering if you could please post a good example of how to use the solver externalForces array. I tried the code below, but this is throwing a NullReferenceException runtime error on the for loop line.
Code:
    public ObiRope myRope; //this is assigned in Start()

    private void FixedUpdate()
    {

        for (int i = 0; i < myRope.solverIndices.Length; ++i)
            myRope.solver.externalForces[i] = myForce;

    }

The manual contains examples of accessing per-particle properties for any actor:
http://obi.virtualmethodstudio.com/manua...icles.html

Your code does not check whether the rope is null (could potentially be if you're manually assigning it from some other script) or if it has been loaded or not. So add this at the beginning of FixedUpdate():

Code:
if (myRope == null || !myRope.isLoaded)
return;

Also "myRope.solverIndices" and "myRope.solver.externalForces" don't have the same length, so naturally you'll get an out of bounds access exception accessing them that way.
All data arrays in the solver (positions, velocities, externalForces, invMasses, etc) have one entry per particle loaded in the solver, if there's multiple ropes/actors loaded then they'll have many more entries than particles in each individual rope. The index at which data for each particle in stored in the solver arrays is provided by the rope.solverIndices array:

Code:
for (int i = 0; i < myRope.solverIndices.Length; ++i){

// retrieve index of the particle in the solver:
int solverIndex = myRope.solverIndices[i];

// set the external force of this particle:
myRope.solver.externalForces[solverIndex] = myForce;
}

See: http://obi.virtualmethodstudio.com/manua...icles.html
"Actor particle indices run from 0 to the amount of particles in that actor. However, since particle data for an actor might be scattered across the solver arrays (see architecture), you need to map from actor index to solver index. You can map from actor particle indices to solver particle indices using the actor's solverIndices array."

Also note that generally, you want to add forces instead of forcing the force to a specific value:
Quote:myRope.solver.externalForces[solverIndex] += myForce;

If you just set the force to a value, your script won't work well with other scripts that might add additional forces to the particles. For instance if you have a directional force script and a gravity well script, the one that gets executed last will overwrite the forces set by the first one.


(26-10-2022, 12:22 AM)docgonzzo Wrote: In fact, this experiment below produced both a NullReferenceException runtime error, and the actual length of the array in the console Confundido

Code:
    private void FixedUpdate()
    {

        Debug.Log("myRope.solverIndices.Length: " + myRope.solverIndices.Length);

    }

NullReferenceException: Object reference not set to an instance of an object
ObiRopePlanetGravity.FixedUpdate () (at Assets/ObiRopePlanetGravity.cs:55)


myRope.solverIndices.Length: 10
UnityEngine.Debug:Log (object)
ObiRopePlanetGravity:FixedUpdate () (at Assets/ObiRopePlanetGravity.cs:55)

Nothing weird about this: NullRefException on the first frame because the rope isn't loaded yet, then correct length on subsequent frames. Thing is you're not checking whether the rope is loaded into the solver yet. If it isn't, the solverIndices array will be null since particles are not included in any solver. You can check whether the rope is loaded like this:

Code:
if (rope.isLoaded){
// do something with the particles
}

As a side note, you might want to use the solver's callbacks instead of FixedUpdate. This is important because during a single FixedUpdate(), Obi's physics engine will be updated more than once if you're using more than 1 substep. See: http://obi.virtualmethodstudio.com/manua...olver.html

let me know if you need further help,

cheers
Reply


Messages In This Thread
Custom Force to Match Object - by docgonzzo - 25-10-2022, 12:31 AM
RE: Custom Force to Match Object - by josemendez - 25-10-2022, 07:21 AM
RE: Custom Force to Match Object - by docgonzzo - 26-10-2022, 12:22 AM
RE: Custom Force to Match Object - by josemendez - 26-10-2022, 07:52 AM
RE: Custom Force to Match Object - by docgonzzo - 27-10-2022, 12:06 AM
RE: Custom Force to Match Object - by josemendez - 27-10-2022, 07:39 AM