02-01-2025, 06:11 PM
I know it's an old thread, but it comes up for the topic of killing fluid particles on contact with colliders.
The API has changed, so this seems to be useful for Obi 7 Fluids.
The API has changed, so this seems to be useful for Obi 7 Fluids.
Code:
// FluidAbsorber.cs
//
using UnityEngine;
using Obi;
public class FluidAbsorber: MonoBehaviour
{
[Tooltip("The solver for Obi Fluid particles")]
public ObiSolver solver;
[Tooltip("The Obi Collider that will destroy fluid particles")]
public ObiColliderBase absorber;
void Awake()
{
// unlike particle actors, this absorber
// does not have to be a child of the solver,
// but if left null we assume it is
//
if (solver == null)
solver = GetComponentInParent<ObiSolver>();
if (absorber == null)
absorber = GetComponent<ObiColliderBase>();
}
void OnEnable()
{
if (solver != null)
solver.OnCollision += Solver_OnCollision;
}
void OnDisable()
{
if (solver != null)
solver.OnCollision -= Solver_OnCollision;
}
void Solver_OnCollision(object sender,
ObiNativeContactList e)
{
ObiColliderWorld world = ObiColliderWorld.GetInstance();
foreach (Oni.Contact contact in e)
{
// real contact or speculative?
if (contact.distance > 0.01f)
continue;
// was it our specified Obi Collider?
ObiColliderBase collider =
world.colliderHandles[contact.bodyB].owner;
if (collider != absorber)
continue;
// "kill" the particle by setting its life to zero
int particleIndex = solver.simplices[contact.bodyA];
solver.life[particleIndex] = 0;
}
}
}