Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help  Adding & Removing ObiParticleAttachments at runtime
#1
I want to dynamically add and remove ObiParticleAttachments to individual particles of Softbodies at runtime.
My current implemenation looks like this:


Code:
private void Grasp(ObiActor actor, int index)
        {
          attachment = actor.gameObject.AddComponent<ObiParticleAttachment>();
            attachment.target = transform;
            attachment.particleGroup = actor.blueprint.AppendNewParticleGroup(groupName);
            groupIndex = actor.blueprint.groups.Count;
            attachment.particleGroup.particleIndices = new List<int> { index };
            attachment.compliance = 0;
            attachment.attachmentType = ObiParticleAttachment.AttachmentType.Dynamic;
            IsGrasping = true;

            solver.RemoveActor(actor);
            solver.AddActor(actor);
        }

private void Release()
        {
            Destroy(attachment);
            actor.blueprint.RemoveParticleGroupAt(groupIndex);
            solver.RemoveActor(actor);
            solver.AddActor(actor);
        }




I can succesfully preform Grasp() and Release() once. The second time I call Grasp() the actor moves only halfway to the attachments target. It seems like the solver did not fully forget the previous attachments.

I experimented several hours with the methods suggested in Scripting Constraints tutorial without success.

I don't want to add forces to particles in this script as it is done in ObiParticleDragger but rather activate & deactivate a pin/attachment constraint.

Thank you so much for your support!


tl/dr:
How do I properly register and unregister ObiParticleAttachments at runtime?
Reply
#2
Hi Martin,

Tested your code, and it works as it's supposed to: attachments work by binding the particle at its current position, relative to the transform they're being bound to. So if you destroy the attachment, let the actor simulate, and then re-create the attachment, the particle will be attached at whenever it is at that moment.

If you need to attach the particle to a specific point in an object's local space, you have several options:

1.- Set the particle position before creating the attachment.

2.- Set the particle position and manually set its inverse mass to 0 (this is what a static attachment does under the hood). Also, see the ObiContactGrabber.cs utility script for an example of how to do this: the contact grabber detects collisions between particles and colliders, then attaches the particle to the collider.

3.- Manually create a pin constraint, passing the offset in local space to the AddConstraint() method.



Let me know if you need further help with any of these.
Reply