Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
RESET Obi Softbody
#12
Propagate this code to other users when you see them need to reset any ObiActor (Rope, Cloth,Softbody or whatever)
This is needed when teleport, or optimize disable > move solver to new pos > enable back.

Code:
    public class ObiActorResetor {
        public ObiActor actor;
        Vector3 localPosition;
        Quaternion localRotation;

        public ObiActorResetor(ObiActor actor) {
            Setup(actor);
        }
        //do not do this in Awake, since at the time the obi solver still not done setup
        public void Setup(ObiActor actor) {
            this.actor = actor;
            var parent = actor.solver.transform;
            localPosition = parent.InverseTransformPoint(actor.transform.position);
            localRotation = Quaternion.Inverse(parent.rotation) * actor.transform.rotation;
        }
       
        //the action teleport can not do instantly, should sub it on next frame of the sim
        public void MarkShouldReset() {
            actor.solver.OnSimulationStart -= DoReset;
            actor.solver.OnSimulationStart += DoReset;
        }

        void DoReset(ObiSolver solver, float simulatedTime, float substepTime){
            var parent = actor.solver.transform;
            actor.Teleport(parent.TransformPoint(localPosition), parent.rotation * localRotation);
            actor.ResetParticles();
            actor.solver.OnSimulationStart -= DoReset;
        }
    }

How to use:
Setup on Start (softbody is a reference that you wired in inspector, this is for example, it can be anything: rope, obibone, cloth)
Code:
        ObiActorResetor softbodyResetor;

        private void Start()
        {
            softbodyResetor = new ObiActorResetor(softbody);
        }

When want to Reset / Restore it to starting local position related to the solver

Code:
        softbodyResetor.MarkShouldReset();
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