Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
RESET Obi Softbody
#5
(23-06-2023, 06:00 PM)swancollective Wrote: Thank you so much for the quick reply and your code - IT WORKS!!
Only thing, since adding the script, Unity editor keeps crashing on me. Maybe weird coincidence, but it's the only thing I changed, and it never crashed before. Maybe any clue why this could be?

The code above simply iterates trough a couple arrays setting values internally, there's no reason I can think for it to crash. Could you share your editor log, so that I may see the crash stack trace?

(23-06-2023, 06:00 PM)swancollective Wrote: As to your question, this is GPT-4s best attempt of resetting the softbody (which also didn't work, after hours of iterating):

The most likely outcome of that script is a NullRefException at runtime. It doesn't check whether the blueprint is loaded into a solver and simply assumes it to be by the time Awake() is called - which is often not the case, hence causing a null ref when it tries to access the length of the solverIndices. It also omits particle velocities and orientations which should also be reset for the softbody to correctly go back to its initial state, and assumes the solver is at the scene origin.

Here's the closest correct version of it (still assuming the solver is at the origin) even then it's far more verbose and complex than it needs to be - ResetParticles() and Teleport() do a better job in just two lines.

Code:
using UnityEngine;
using Obi;

[RequireComponent(typeof(ObiSoftbody))]
public class ResetSoftbody : MonoBehaviour
{
    ObiSoftbody softbody;
    Vector4[] originalPositions;
    Quaternion[] originalOrientations;

    void OnEnable()
    {
        softbody = GetComponent<ObiSoftbody>();
        softbody.OnBlueprintLoaded += Softbody_OnBlueprintLoaded;
        if (softbody.isLoaded)
            Softbody_OnBlueprintLoaded(softbody, softbody.softbodyBlueprint);
    }

    void OnDisable()
    {
        softbody.OnBlueprintLoaded -= Softbody_OnBlueprintLoaded;
    }

    private void Softbody_OnBlueprintLoaded(ObiActor actor, ObiActorBlueprint blueprint)
    {
        originalPositions = new Vector4[softbody.solverIndices.count];
        originalOrientations = new Quaternion[softbody.solverIndices.count];

        for (int i = 0; i < softbody.solverIndices.count; ++i)
        {
            int solverIndex = softbody.solverIndices[i];
            originalPositions[i] = softbody.solver.positions[solverIndex];
            originalOrientations[i] = softbody.solver.orientations[solverIndex];
        }
    }

    public void ResetToOriginalPosition()
    {
        for (int i = 0; i < softbody.solverIndices.count; ++i)
        {
            int solverIndex = softbody.solverIndices[i];
            softbody.solver.positions[solverIndex] = originalPositions[i];
            softbody.solver.orientations[solverIndex] = originalOrientations[i];
            softbody.solver.velocities[solverIndex] = softbody.solver.angularVelocities[solverIndex] = Vector4.zero;
        }
    }
}
Reply


Messages In This Thread
RESET Obi Softbody - by swancollective - 22-06-2023, 08:25 PM
RE: RESET Obi Softbody - by swancollective - 23-06-2023, 11:17 AM
RE: RESET Obi Softbody - by josemendez - 23-06-2023, 02:19 PM
RE: RESET Obi Softbody - by swancollective - 23-06-2023, 06:00 PM
RE: RESET Obi Softbody - by josemendez - 23-06-2023, 06:42 PM
RE: RESET Obi Softbody - by swancollective - 24-06-2023, 11:52 AM
RE: RESET Obi Softbody - by josemendez - 24-06-2023, 02:48 PM
RE: RESET Obi Softbody - by swancollective - 25-06-2023, 10:15 AM
RE: RESET Obi Softbody - by josemendez - 25-06-2023, 11:06 AM
RE: RESET Obi Softbody - by swancollective - 25-06-2023, 12:39 PM
RE: RESET Obi Softbody - by swancollective - 03-07-2023, 07:20 AM
RE: RESET Obi Softbody - by spikebor - 09-05-2024, 06:12 AM