Obi Official Forum
Help Changing the rope's position - Printable Version

+- Obi Official Forum (https://obi.virtualmethodstudio.com/forum)
+-- Forum: Obi Users Category (https://obi.virtualmethodstudio.com/forum/forum-1.html)
+--- Forum: Obi Rope (https://obi.virtualmethodstudio.com/forum/forum-4.html)
+--- Thread: Help Changing the rope's position (/thread-4211.html)



Changing the rope's position - Crystalius - 08-05-2024

Hello. I've been struggling with moving the rope where I want to. My goal at the moment is to disable the solver along with the rope when I don't need them for performance reasons, but when I re-enable them the position is kept old, which is understandable of course. What would be the simplest way to move the rope after I enable it? I've read your discussion about that in another thread from 2020 but I couldn't recreate their script in Unity's visual scripting. 

I can easily get the positions with ObiRope GetParticlePosition. But how would I set them? If it was the same as getting them and iterating through all the particles I probably wouldn't be writing here.

I would very much appreciate your support here


RE: Changing the rope's position - spikebor - 09-05-2024

(08-05-2024, 05:33 PM)Crystalius Wrote: Hello. I've been struggling with moving the rope where I want to. My goal at the moment is to disable the solver along with the rope when I don't need them for performance reasons, but when I re-enable them the position is kept old, which is understandable of course. What would be the simplest way to move the rope after I enable it? I've read your discussion about that in another thread from 2020 but I couldn't recreate their script in Unity's visual scripting. 

I can easily get the positions with ObiRope GetParticlePosition. But how would I set them? If it was the same as getting them and iterating through all the particles I probably wouldn't be writing here.

I would very much appreciate your support here

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 > enable back like you said.

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();



RE: Changing the rope's position - josemendez - 09-05-2024

(08-05-2024, 05:33 PM)Crystalius Wrote: Hello. I've been struggling with moving the rope where I want to. My goal at the moment is to disable the solver along with the rope when I don't need them for performance reasons, but when I re-enable them the position is kept old, which is understandable of course. What would be the simplest way to move the rope after I enable it? I've read your discussion about that in another thread from 2020 but I couldn't recreate their script in Unity's visual scripting. 

Hi!

rope.Teleport(position, rotation); should work. See Spikebor's code above for what seems like a complete solution that supports parent transforms and resetting particles as well.

(08-05-2024, 05:33 PM)Crystalius Wrote: I can easily get the positions with ObiRope GetParticlePosition. But how would I set them? If it was the same as getting them and iterating through all the particles I probably wouldn't be writing here.

GetParticlePosition() is mostly used from editor, as it abstracts away the differences between play mode /editor when it comes to rendering particles. At runtime usually you would directly access the solver's data arrays, that contain all per-particle data: positions, velocities, etc. This is what Teleport() does internally: it iterates trough all particle positions and rotates/translates them.

See:
http://obi.virtualmethodstudio.com/manual/6.3/scriptingparticles.html

kind regards,


RE: Changing the rope's position - Crystalius - 09-05-2024

(09-05-2024, 08:05 AM)josemendez Wrote: Hi!

rope.Teleport(position, rotation); should work. See Spikebor's code above for what seems like a complete solution that supports parent transforms and resetting particles as well.


GetParticlePosition() is mostly used from editor, as it abstracts away the differences between play mode /editor when it comes to rendering particles. At runtime usually you would directly access the solver's data arrays, that contain all per-particle data: positions, velocities, etc. This is what Teleport() does internally: it iterates trough all particle positions and rotates/translates them.

See:
http://obi.virtualmethodstudio.com/manual/6.3/scriptingparticles.html

kind regards,

Thank you for both of your replies! 

I've never come across teleport anywhere yet. No idea such functionality existed. Will do some tinkering with it and will come back again if I can't climb out of this.

Kind regards!