20-10-2017, 04:26 AM
(19-10-2017, 11:35 PM)niZmo Wrote: You can pin any object with an obiCollider on it with this. It can be update to support multiple partciles per collider if that whats needed by changing the dictionary to <ObiCollider, int[]> and a little bit more work.
Code:public Dictionary<ObiCollider, int> pinnedColliders = new Dictionary<ObiCollider, int>();
public ObiCloth actor;
public void PinObiColliderToParticle(ObiCollider obiCollider, int particleIndex, Vector3? pinOffset = null, int? pinStiffness = null)
{
if (pinnedColliders.ContainsKey(obiCollider))
return;
ObiPinConstraintBatch batch = actor.PinConstraints.GetBatches()[0] as ObiPinConstraintBatch;
actor.PinConstraints.RemoveFromSolver(null);
// Set the pin as the next availiable index in the batch
pinnedColliders.Add(obiCollider, batch.ConstraintCount);
batch.AddConstraint(particleIndex, obiCollider, pinOffset == null ? Vector3.zero : pinOffset.Value, pinStiffness == null ? 1 : pinStiffness.Value);
actor.PinConstraints.AddToSolver(null);
actor.PinConstraints.PushDataToSolver();
}
public void UnpinObiCollider(ObiCollider obiCollider)
{
if (!pinnedColliders.ContainsKey(obiCollider))
return;
ObiPinConstraintBatch batch = actor.PinConstraints.GetBatches()[0] as ObiPinConstraintBatch;
actor.PinConstraints.RemoveFromSolver(null);
int pinIndex = pinnedColliders[obiCollider];
batch.RemoveConstraint(pinIndex);
pinnedColliders.Remove(obiCollider);
List<ObiCollider> keys = new List<ObiCollider>(pinnedColliders.Keys);
// Decrement the pinned indices that are greater than the removed index
foreach (ObiCollider collider in keys)
{
int checkIndex = pinnedColliders[collider];
if (checkIndex >= 0 && checkIndex > pinIndex)
{
pinnedColliders[collider]--;
}
}
actor.PinConstraints.AddToSolver(null);
actor.PinConstraints.PushDataToSolver();
}
Wow! Thanks @niZmo. It sounds very promising and I will surely give it a try and let you know what happens