Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Adding pin constraint to particle in runtime.
#1
Hi.
I have existing rope with couple on handlers on both of rope ends (attached to first and last rope particle).
I want to substitute them (or possibly only one of them with pin constraint).
I think handle can be removed/added with the help of disabling/enabling handle gameObject.
For adding ping constraint I'm launching some variations of following coroutine:
Code:
private IEnumerator AttachHook(){
        yield return 0;

        // Pin both ends of the rope (this enables two-way interaction between character and rope):
        ObiPinConstraints pinConstraints = obiRope.GetComponent<ObiPinConstraints>();
        ObiPinConstraintBatch pinBatch = pinConstraints.GetBatches()[0] as ObiPinConstraintBatch;

        // remove the constraints from the solver, because we cannot modify the constraints list while the solver is using it.
//        pinConstraints.RemoveFromSolver(null);
        
        // remove the first constraint.
//        pinBatch.RemoveConstraint(0);
        
        pinBatch.AddConstraint(obiRope.particleIndices[_obiParticleHandle.FirstParticleIndex], obiColliderToAttach, obiOffsetToAttach, 0);
        
        // Add the rope to the solver to begin the simulation:
        pinConstraints.AddToSolver(null);
//        pinConstraints.PushDataToSolver();
        
    }    

I was experimenting with Adding/Removing from solver and with pushing data back to solver without success.
I can see generated with script pin constraint in unity inspector, but it does nothing.
What am I missing?
Should I reinitialize rope somehow?

Thanks a lot.
Reply
#2
The handle class has public methods to let you add/remove particles. With your pin code, all I see that is weird is why you are calling RemoveConstaint(0) and using the particleHandle to get the first partcle. You may also access the PinConstraints by obiRope.PinConstraints. If you want there is this snippet which keeps track of the pinned particles to remove them later. And also you can use this to get the actual beginning and ending particles of the rope.
Reply
#3
(08-11-2017, 11:20 PM)niZmo Wrote: The handle class has public methods to let you add/remove particles. With your pin code, all I see that is weird is why you are calling RemoveConstaint(0) and using the particleHandle to get the first partcle. You may also access the PinConstraints by obiRope.PinConstraints. If you want there is this snippet which keeps track of the pinned particles to remove them later. And also you can use this to get the actual beginning and ending particles of the rope.

RemoveConstaint(0) line is commented out. I just experimented with it.
And I'm using particleHandle to get the actual particle that is used by some handler I'm interested in.
In general case it could be any particle. I'm using handler just for grabbing particle index.
Reply
#4
(09-11-2017, 12:09 AM)aaamarkin Wrote: RemoveConstaint(0) line is commented out. I just experimented with it.
And I'm using particleHandle to get the actual particle that is used by some handler I'm interested in.
In general case it could be any particle. I'm using handler just for grabbing particle index.

Oh i didn't see the // lol. And now i see the other code that's commented out that you need. Try to remove the particle from the particle handle first with RemoveParticle(index), then pin the particle. Look at the first link snippet i sent you, to see what methods you need to call before and after. You have them in there already, RemoveFromSolver and PushDataToSolver.

More info here.
Reply
#5
(09-11-2017, 12:57 AM)niZmo Wrote: Oh i didn't see the // lol. And now i see the other code that's commented out that you need. Try to remove the particle from the particle handle first with RemoveParticle(index), then pin the particle. Look at the first link snippet i sent you, to see what methods you need to call before and after. You have them in there already, RemoveFromSolver and PushDataToSolver.

More info here.

My current code:

Code:
private void AttachHook()
    {
        var particleIndex = _obiParticleHandle.FirstParticleIndex;


        _obiParticleHandle.RemoveParticle(particleIndex);

               // Disabling or enabling _obiParticleHandle has no affect
        _obiParticleHandle.enabled = false;

        ObiPinConstraintBatch pinBatch = obiRope.PinConstraints.GetBatches()[0] as ObiPinConstraintBatch;

        obiRope.PinConstraints.RemoveFromSolver(null);

        pinBatch.AddConstraint(obiRope.particleIndices[particleIndex], obiColliderToAttach, obiOffsetToAttach, 0);
        

        obiRope.PinConstraints.AddToSolver(null);
        obiRope.PinConstraints.PushDataToSolver();
    }

'ObiParticleHandle.RemoveParticle(index)' is working and was really helpfull, thanks.

But complete code is not working still.
After calling 'obiRope.PinConstraints.AddToSolver(null)' really weird things happen.
I am not sure how to describe it but rope starts moving up and down super fast, looks like it has infinite length.


When I comment 'obiRope.PinConstraints.AddToSolver(null)' line rope behaves ok. But again while I can see newly created PinConstraint in unity inspector rope behaviour looks like no particle has PinConstraint attached.
Reply