Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Is it able to move softbody with script?
#11
(14-11-2019, 10:39 AM)josemendez Wrote: Hi Manu,

The video you sent causes VLC, Quicktime and Media Player to crash, so I'm unable to see it.

When setting positions directly you also need to set the inverse mass and the velocity of these particles to zero, as I pointed out before. Setting the inverse mass to zero deactivates dynamics for that particle (as you will be setting its position directly, and you don't want the simulation to overwrite it), and removing all its velocity will make sure it doesn't drift away from the position you set.

Once you "release" the particle, simply set its inverse mass to whatever it was. The simulation will take over and begin setting both its position and velocity.

From the manual:
http://obi.virtualmethodstudio.com/tutor...icles.html

You don't need to download the video to watch it, it supports online play.
I already did it with 

solver.invMasses[pickArgs.particleIndex] = 0f;
solver.velocities[pickArgs.particleIndex]= Vector4.zero;

saving the last invMass, but I'm getting this:

transform.position assign attempt for 'Dragon' is not valid. Input position is { NaN, NaN, NaN }.
UnityEngine.TransformConfundidoet_position(Vector3)
Obi.ObiSoftbody:OnSolverStepEnd(Single) (at Assets/Obi/Scripts/Actors/ObiSoftbody.cs:65)
Obi.ObiSolver:AllSolversStepEnd() (at Assets/Obi/Scripts/Solver/ObiSolver.cs:877)
Obi.ObiArbiter:WaitForAllSolvers() (at Assets/Obi/Scripts/Solver/ObiArbiter.cs:45)
Obi.ObiSolver:WaitForAllSolvers() (at Assets/Obi/Scripts/Solver/ObiSolver.cs:784)
Obi.ObiSolver:SimulateStep(Single) (at Assets/Obi/Scripts/Solver/ObiSolver.cs:672)
Obi.ObiSolver:FixedUpdate() (at Assets/Obi/Scripts/Solver/ObiSolver.cs:866)

And multiple clusters errors like:

transform.position assign attempt for 'Cluster102' is not valid. Input position is { NaN, NaN, NaN }.
UnityEngine.TransformConfundidoet_position(Vector3)
Obi.ObiSoftbodySkinner:UpdateBones(Object, EventArgs) (at Assets/Obi/Rendering/ObiSoftbodySkinner.cs:122)
Obi.ObiSolver:EndFrame(Single) (at Assets/Obi/Scripts/Solver/ObiSolver.cs:727)
Obi.ObiSolver:LateUpdate() (at Assets/Obi/Scripts/Solver/ObiSolver.cs:905)

The dragon disappears.

Btw, the examples from the manual do nothing
Reply
#12
(14-11-2019, 10:51 AM)manurocker95 Wrote: You don't need to download the video to watch it, it supports online play.
I already did it with 

solver.invMasses[pickArgs.particleIndex] = 0f;
solver.velocities[pickArgs.particleIndex]= Vector4.zero;

saving the last invMass, but I'm getting this:

transform.position assign attempt for 'Dragon' is not valid. Input position is { NaN, NaN, NaN }.
UnityEngine.TransformConfundidoet_position(Vector3)
Obi.ObiSoftbody:OnSolverStepEnd(Single) (at Assets/Obi/Scripts/Actors/ObiSoftbody.cs:65)
Obi.ObiSolver:AllSolversStepEnd() (at Assets/Obi/Scripts/Solver/ObiSolver.cs:877)
Obi.ObiArbiter:WaitForAllSolvers() (at Assets/Obi/Scripts/Solver/ObiArbiter.cs:45)
Obi.ObiSolver:WaitForAllSolvers() (at Assets/Obi/Scripts/Solver/ObiSolver.cs:784)
Obi.ObiSolver:SimulateStep(Single) (at Assets/Obi/Scripts/Solver/ObiSolver.cs:672)
Obi.ObiSolver:FixedUpdate() (at Assets/Obi/Scripts/Solver/ObiSolver.cs:866)

And multiple clusters errors like:

transform.position assign attempt for 'Cluster102' is not valid. Input position is { NaN, NaN, NaN }.
UnityEngine.TransformConfundidoet_position(Vector3)
Obi.ObiSoftbodySkinner:UpdateBones(Object, EventArgs) (at Assets/Obi/Rendering/ObiSoftbodySkinner.cs:122)
Obi.ObiSolver:EndFrame(Single) (at Assets/Obi/Scripts/Solver/ObiSolver.cs:727)
Obi.ObiSolver:LateUpdate() (at Assets/Obi/Scripts/Solver/ObiSolver.cs:905)

The dragon disappears.

Btw, the examples from the manual do nothing

Hi there,

Going to write an example script and post it here, for reference. Sonrisa
Reply
#13
(14-11-2019, 11:48 AM)josemendez Wrote: Hi there,

Going to write an example script and post it here, for reference. Sonrisa

Okay, my script is something like 
Code:
using System;
using UnityEngine;

namespace Obi
{
   [RequireComponent(typeof(LineRenderer))]
   [RequireComponent(typeof(ObiParticlePicker))]
   public class OBIExampleDragger : MonoBehaviour
   {
       public bool drawSpring = true;

       private LineRenderer lineRenderer;
       private ObiParticlePicker picker;
       private ObiParticlePicker.ParticlePickEventArgs pickArgs;

       public UnityEngine.Events.UnityEvent OnParticlePicked;
       public UnityEngine.Events.UnityEvent OnParticleDragged;
       public UnityEngine.Events.UnityEvent OnParticleReleased;

       public float draggedInMass = 0f;

       void OnEnable()
       {
           lineRenderer = GetComponent<LineRenderer>();
           picker = GetComponent<ObiParticlePicker>();

   
           picker.OnParticlePicked.AddListener(Picker_OnParticleDragged);
           picker.OnParticleDragged.AddListener(Picker_OnParticleDragged);
           picker.OnParticleReleased.AddListener(Picker_OnParticleReleased);
       }

       void OnDisable()
       {
           picker.OnParticlePicked.RemoveListener(Picker_OnParticleDragged);
           picker.OnParticleDragged.RemoveListener(Picker_OnParticleDragged);
           picker.OnParticleReleased.RemoveListener(Picker_OnParticleReleased);
           lineRenderer.positionCount = 0;
       }

       private void FixedUpdate()
       {
           ObiSolver solver = picker.solver;
           if (solver != null && pickArgs != null)
           {
               // Calculate picking position in solver space:
               Vector4 targetPosition = pickArgs.worldPosition;
               if (solver.simulateInLocalSpace)
                   targetPosition = solver.transform.InverseTransformPoint(targetPosition);

               // Calculate effective inverse mass:
               float invMass = solver.invMasses[pickArgs.particleIndex];
               draggedInMass = invMass;

               if (invMass > 0)
               {
                   // Calculate and apply spring force:
                   solver.positions[pickArgs.particleIndex] = (targetPosition);
                   solver.invMasses[pickArgs.particleIndex] = 0f;
                   solver.velocities[pickArgs.particleIndex] = Vector4.zero;

                   if (drawSpring)
                   {
                       lineRenderer.positionCount = 2;
                       lineRenderer.SetPosition(0, targetPosition);
                       lineRenderer.SetPosition(1, position);
                   }
                   else
                   {
                       lineRenderer.positionCount = 0;
                   }
               }
           }
       }

       void Picker_OnParticleDragged(ObiParticlePicker.ParticlePickEventArgs e)
       {
           pickArgs = e;

           OnParticlePicked?.Invoke();
           OnParticleDragged?.Invoke();
       }

       void Picker_OnParticleReleased(ObiParticlePicker.ParticlePickEventArgs e)
       {
           pickArgs = null;
           lineRenderer.positionCount = 0;


           OnParticleReleased?.Invoke();
       }

       public void OnReleased()
       {
           ObiSolver solver = picker.solver;

           if (solver != null && pickArgs != null)
               solver.invMasses[pickArgs.particleIndex] = draggedInMass;
       }
   }
}
Reply
#14
(14-11-2019, 11:58 AM)manurocker95 Wrote: Okay, my script is something like 
Code:
using System;
using UnityEngine;

namespace Obi
{
   [RequireComponent(typeof(LineRenderer))]
   [RequireComponent(typeof(ObiParticlePicker))]
   public class OBIExampleDragger : MonoBehaviour
   {
       public bool drawSpring = true;

       private LineRenderer lineRenderer;
       private ObiParticlePicker picker;
       private ObiParticlePicker.ParticlePickEventArgs pickArgs;

       public UnityEngine.Events.UnityEvent OnParticlePicked;
       public UnityEngine.Events.UnityEvent OnParticleDragged;
       public UnityEngine.Events.UnityEvent OnParticleReleased;

       public float draggedInMass = 0f;

       void OnEnable()
       {
           lineRenderer = GetComponent<LineRenderer>();
           picker = GetComponent<ObiParticlePicker>();

   
           picker.OnParticlePicked.AddListener(Picker_OnParticleDragged);
           picker.OnParticleDragged.AddListener(Picker_OnParticleDragged);
           picker.OnParticleReleased.AddListener(Picker_OnParticleReleased);
       }

       void OnDisable()
       {
           picker.OnParticlePicked.RemoveListener(Picker_OnParticleDragged);
           picker.OnParticleDragged.RemoveListener(Picker_OnParticleDragged);
           picker.OnParticleReleased.RemoveListener(Picker_OnParticleReleased);
           lineRenderer.positionCount = 0;
       }

       private void FixedUpdate()
       {
           ObiSolver solver = picker.solver;
           if (solver != null && pickArgs != null)
           {
               // Calculate picking position in solver space:
               Vector4 targetPosition = pickArgs.worldPosition;
               if (solver.simulateInLocalSpace)
                   targetPosition = solver.transform.InverseTransformPoint(targetPosition);

               // Calculate effective inverse mass:
               float invMass = solver.invMasses[pickArgs.particleIndex];
               draggedInMass = invMass;

               if (invMass > 0)
               {
                   // Calculate and apply spring force:
                   solver.positions[pickArgs.particleIndex] = (targetPosition);
                   solver.invMasses[pickArgs.particleIndex] = 0f;
                   solver.velocities[pickArgs.particleIndex] = Vector4.zero;

                   if (drawSpring)
                   {
                       lineRenderer.positionCount = 2;
                       lineRenderer.SetPosition(0, targetPosition);
                       lineRenderer.SetPosition(1, position);
                   }
                   else
                   {
                       lineRenderer.positionCount = 0;
                   }
               }
           }
       }

       void Picker_OnParticleDragged(ObiParticlePicker.ParticlePickEventArgs e)
       {
           pickArgs = e;

           OnParticlePicked?.Invoke();
           OnParticleDragged?.Invoke();
       }

       void Picker_OnParticleReleased(ObiParticlePicker.ParticlePickEventArgs e)
       {
           pickArgs = null;
           lineRenderer.positionCount = 0;


           OnParticleReleased?.Invoke();
       }

       public void OnReleased()
       {
           ObiSolver solver = picker.solver;

           if (solver != null && pickArgs != null)
               solver.invMasses[pickArgs.particleIndex] = draggedInMass;
       }
   }
}

Hi, it's *almost* correct.

Whenever you manually change the inverse mass (and thus, the mass) of a particle involved in a shape matching constraint, you need to recalculate the shape matching cluster's rest state. I really should have pointed this out before, kinda took it for granted it but it's not entirely obvious. My apologies.

Lastly, it only works for 1 particle (like the default dragger). There's cases where you want to grab multiple particles, but well that's just an additional bonus.

My next post contains a complete example that works via contact callbacks, so you can grab multiple particles that are in touch with a collider (or inside a trigger, for that matter).
Reply
#15
Here's a complete example with all the bells and whistles. You can also find it attached for convenience.

Code:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Obi;

/**
* Sample component that makes a collider "grab" any particle it touches (regardless of which Actor it belongs to).
*/
[RequireComponent(typeof(ObiCollider))]
public class Grabber : MonoBehaviour
{

   public ObiSolver solver;

   /**
    * Helper class that stores the index of a particle in the solver, its position in the grabber's local space, and its inverse mass previous to being grabbed.
    * This makes it easy to tell if a particle has been grabbed, update its position while grabbing, and restore its mass after being released.
    */
   private class GrabbedParticle : IEqualityComparer<GrabbedParticle>
   {
       public int index;
       public float invMass;
       public Vector3 localPosition;

       public GrabbedParticle(int index, float invMass)
       {
           this.index = index;
           this.invMass = invMass;
       }

       public bool Equals(GrabbedParticle x, GrabbedParticle y)
       {
           return x.index == y.index;
       }

       public int GetHashCode(GrabbedParticle obj)
       {
           return index;
       }
   }

   private Obi.ObiSolver.ObiCollisionEventArgs collisionEvent;                                  /**< store the current collision event*/
   private ObiCollider localCollider;                                                           /**< the collider on this gameObject.*/
   private HashSet<GrabbedParticle> grabbedParticles = new HashSet<GrabbedParticle>();          /**< set to store all currently grabbed particles.*/
   private HashSet<ObiSoftbody> grabbedSoftbodies = new HashSet<ObiSoftbody>();                 /**< set of softbodies grabbed during this step.*/
   private Matrix4x4 grabber2Solver;
   private Matrix4x4 solver2Grabber;

   private void Awake()
   {
       localCollider = GetComponent<ObiCollider>();
   }

   private void OnEnable()
   {
       solver.OnCollision += Solver_OnCollision;
   }

   private void OnDisable()
   {
       solver.OnCollision -= Solver_OnCollision;
   }

   private void Solver_OnCollision(object sender, Obi.ObiSolver.ObiCollisionEventArgs e)
   {
       // Calculate transform matrix from grabber to world space (Note: if using local space simulation, postmultiply with solver.transform.localToWorldMatrix)
       solver2Grabber = transform.worldToLocalMatrix;

       // and its inverse:
       grabber2Solver = solver2Grabber.inverse;

       collisionEvent = e;
   }

   private void UpdateRestShapeMatching()
   {
       // Update rest shape matching of all grabbed softbodies:
       foreach (ObiSoftbody softbody in grabbedSoftbodies)
           foreach (ObiShapeMatchingConstraintBatch batch in softbody.ShapeMatchingConstraints.GetBatches())
               Oni.CalculateRestShapeMatching(solver.OniSolver, batch.OniBatch);
   }

   /**
    * Creates and stores a GrabbedParticle from the particle at the given index.
    * Returns true if we sucessfully grabbed a particle, false if the particle was already grabbed.
    */
   private bool GrabParticle(int index)
   {
       GrabbedParticle p = new GrabbedParticle(index, solver.invMasses[index]);

       // in case this particle has not been grabbed yet:
       if (!grabbedParticles.Contains(p))
       {
           // record the particle's position relative to the grabber, and store it.
           p.localPosition = solver2Grabber.MultiplyPoint3x4(solver.positions[index]);
           grabbedParticles.Add(p);

           // Set inv mass and velocity to zero:
           solver.invMasses[index] = 0;
           solver.velocities[index] = Vector4.zero;

           return true;
       }

       return false;
   }

   /**
    * Grabs all particles currently touching the grabber.
    */
   public void Grab()
   {
       grabbedSoftbodies.Clear();

       foreach (Oni.Contact contact in collisionEvent.contacts)
       {
           // this one is an actual collision:
           if (contact.distance < 0.01f)
           {
               Component contactCollider;
               if (ObiCollider.idToCollider.TryGetValue(contact.other, out contactCollider))
               {
                   // if the current contact references our collider, proceed to grab the particle.
                   if (contactCollider == localCollider.SourceCollider)
                   {
                       // try to grab the particle, if not already grabbed.
                       if (GrabParticle(contact.particle))
                       {
                           // we want to know if we grabbed a softbody, to update its rest shape.
                           var softbody = solver.particleToActor[contact.particle].actor as ObiSoftbody;
                           if (softbody != null)
                               grabbedSoftbodies.Add(softbody);
                       }
                   }
               }
           }
       }

       UpdateRestShapeMatching();
   }

   /**
    * Releases all currently grabbed particles. This boils down to simply resetting their invMass.
    */
   public void Release()
   {
       // Restore the inverse mass of all grabbed particles, so dynamics affect them.
       foreach (GrabbedParticle p in grabbedParticles)
           solver.invMasses[p.index] = p.invMass;

       grabbedParticles.Clear();

       // Also update rest shape matching:
       UpdateRestShapeMatching();
       grabbedSoftbodies.Clear();
   }

   /**
    * Updates the position of the grabbed particles.
    */
   private void FixedUpdate()
   {
       foreach (GrabbedParticle p in grabbedParticles)
           solver.positions[p.index] = grabber2Solver.MultiplyPoint3x4(p.localPosition);
   }

   /**
    * Just for convenience. Ideally, this should not be part of this component.
    * You're expected to control the Grabber from outside.
    */
   public void Update()
   {
       if (Input.GetKeyDown(KeyCode.G))
       {
           Grab();
       }

       if (Input.GetKeyDown(KeyCode.R))
       {
           Release();
       }
           
   }
}

It is a component that you can add to any ObiCollider, and it takes a reference to a ObiSolver as input.

It has two methods: Grab() and Release():
- Grab() detects all particles in touch with the collider, stores them in a set together with their invMass and local space position. Then sets its invMass and velocity to zero. Also updates rest shape matching in case any of the particles belonged to a softbody.
- Release() resets all the currently grabbed particle's invMass to whatever it was before grabbing them. This makes them be driven by dynamics once again.

I've included code in Update() that calls Grab() upon pressing "G" in the keyboard, and Release() when pressing "R", for demonstration purposes. Modify it to your taste.

Edit: a video of it in action:


Attached Files
.cs   Grabber.cs (Size: 6.06 KB / Downloads: 4)
Reply
#16
(14-11-2019, 12:52 PM)josemendez Wrote: Here's a complete example with all the bells and whistles. You can also find it attached for convenience.

Code:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Obi;

/**
* Sample component that makes a collider "grab" any particle it touches (regardless of which Actor it belongs to).
*/
[RequireComponent(typeof(ObiCollider))]
public class Grabber : MonoBehaviour
{

   public ObiSolver solver;

   /**
    * Helper class that stores the index of a particle in the solver, its position in the grabber's local space, and its inverse mass previous to being grabbed.
    * This makes it easy to tell if a particle has been grabbed, update its position while grabbing, and restore its mass after being released.
    */
   private class GrabbedParticle : IEqualityComparer<GrabbedParticle>
   {
       public int index;
       public float invMass;
       public Vector3 localPosition;

       public GrabbedParticle(int index, float invMass)
       {
           this.index = index;
           this.invMass = invMass;
       }

       public bool Equals(GrabbedParticle x, GrabbedParticle y)
       {
           return x.index == y.index;
       }

       public int GetHashCode(GrabbedParticle obj)
       {
           return index;
       }
   }

   private Obi.ObiSolver.ObiCollisionEventArgs collisionEvent;                                  /**< store the current collision event*/
   private ObiCollider localCollider;                                                           /**< the collider on this gameObject.*/
   private HashSet<GrabbedParticle> grabbedParticles = new HashSet<GrabbedParticle>();          /**< set to store all currently grabbed particles.*/
   private HashSet<ObiSoftbody> grabbedSoftbodies = new HashSet<ObiSoftbody>();                 /**< set of softbodies grabbed during this step.*/
   private Matrix4x4 grabber2Solver;
   private Matrix4x4 solver2Grabber;

   private void Awake()
   {
       localCollider = GetComponent<ObiCollider>();
   }

   private void OnEnable()
   {
       solver.OnCollision += Solver_OnCollision;
   }

   private void OnDisable()
   {
       solver.OnCollision -= Solver_OnCollision;
   }

   private void Solver_OnCollision(object sender, Obi.ObiSolver.ObiCollisionEventArgs e)
   {
       // Calculate transform matrix from grabber to world space (Note: if using local space simulation, postmultiply with solver.transform.localToWorldMatrix)
       solver2Grabber = transform.worldToLocalMatrix;

       // and its inverse:
       grabber2Solver = solver2Grabber.inverse;

       collisionEvent = e;
   }

   private void UpdateRestShapeMatching()
   {
       // Update rest shape matching of all grabbed softbodies:
       foreach (ObiSoftbody softbody in grabbedSoftbodies)
           foreach (ObiShapeMatchingConstraintBatch batch in softbody.ShapeMatchingConstraints.GetBatches())
               Oni.CalculateRestShapeMatching(solver.OniSolver, batch.OniBatch);
   }

   /**
    * Creates and stores a GrabbedParticle from the particle at the given index.
    * Returns true if we sucessfully grabbed a particle, false if the particle was already grabbed.
    */
   private bool GrabParticle(int index)
   {
       GrabbedParticle p = new GrabbedParticle(index, solver.invMasses[index]);

       // in case this particle has not been grabbed yet:
       if (!grabbedParticles.Contains(p))
       {
           // record the particle's position relative to the grabber, and store it.
           p.localPosition = solver2Grabber.MultiplyPoint3x4(solver.positions[index]);
           grabbedParticles.Add(p);

           // Set inv mass and velocity to zero:
           solver.invMasses[index] = 0;
           solver.velocities[index] = Vector4.zero;

           return true;
       }

       return false;
   }

   /**
    * Grabs all particles currently touching the grabber.
    */
   public void Grab()
   {
       grabbedSoftbodies.Clear();

       foreach (Oni.Contact contact in collisionEvent.contacts)
       {
           // this one is an actual collision:
           if (contact.distance < 0.01f)
           {
               Component contactCollider;
               if (ObiCollider.idToCollider.TryGetValue(contact.other, out contactCollider))
               {
                   // if the current contact references our collider, proceed to grab the particle.
                   if (contactCollider == localCollider.SourceCollider)
                   {
                       // try to grab the particle, if not already grabbed.
                       if (GrabParticle(contact.particle))
                       {
                           // we want to know if we grabbed a softbody, to update its rest shape.
                           var softbody = solver.particleToActor[contact.particle].actor as ObiSoftbody;
                           if (softbody != null)
                               grabbedSoftbodies.Add(softbody);
                       }
                   }
               }
           }
       }

       UpdateRestShapeMatching();
   }

   /**
    * Releases all currently grabbed particles. This boils down to simply resetting their invMass.
    */
   public void Release()
   {
       // Restore the inverse mass of all grabbed particles, so dynamics affect them.
       foreach (GrabbedParticle p in grabbedParticles)
           solver.invMasses[p.index] = p.invMass;

       grabbedParticles.Clear();

       // Also update rest shape matching:
       UpdateRestShapeMatching();
       grabbedSoftbodies.Clear();
   }

   /**
    * Updates the position of the grabbed particles.
    */
   private void FixedUpdate()
   {
       foreach (GrabbedParticle p in grabbedParticles)
           solver.positions[p.index] = grabber2Solver.MultiplyPoint3x4(p.localPosition);
   }

   /**
    * Just for convenience. Ideally, this should not be part of this component.
    * You're expected to control the Grabber from outside.
    */
   public void Update()
   {
       if (Input.GetKeyDown(KeyCode.G))
       {
           Grab();
       }

       if (Input.GetKeyDown(KeyCode.R))
       {
           Release();
       }
           
   }
}

It is a component that you can add to any ObiCollider, and it takes a reference to a ObiSolver as input.

It has two methods: Grab() and Release():
- Grab() detects all particles in touch with the collider, stores them in a set together with their invMass and local space position. Then sets its invMass and velocity to zero. Also updates rest shape matching in case any of the particles belonged to a softbody.
- Release() resets all the currently grabbed particle's invMass to whatever it was before grabbing them. This makes them be driven by dynamics once again.

I've included code in Update() that calls Grab() upon pressing "G" in the keyboard, and Release() when pressing "R", for demonstration purposes. Modify it to your taste.

Edit: a video of it in action:

Awesome! Thanks! Could you add it to the store package?
Reply
#17
(14-11-2019, 03:16 PM)manurocker95 Wrote: Awesome! Thanks! Could you add it to the store package?

Yep, I will do Sonrisa.
Reply