- Home /
Collision detection with NVIDIA FLex ????
Hi,
I'm trying since several days now to get this working and I can't figure it out. I'm trying to mess around with the Nvidia flex plugin for Unity. I would like to detect collisions between the soft bodies I created and other elements (player, simple shapes....). But I can't make it work. I found online that there is suppose to be a way with a method : flexGetContact(), but I've never been able to use that, and the documentation is inexistant...... I tried to add a trigger collider to my soft body, but when I apply a force to it the collider is moved instantly to the aimed position and not the soft body.... (even when the soft body is a parent of the collider, cf the attached image....).
So if someone know how to detect collisions between soft bodies from the flex plugin.......
Answer by UNDERHILL · Oct 18, 2020 at 08:49 AM
Yes. I do. Kindof. I got particles working. Maybe you can adapt this for your needs.
I JUST got it working after several attempts over the past few years. And if you have been through the available tutorials carefully and understand flex and the unity particle system, this will all make sense to you;
Set up a flex actor container gameObject, add Flex Source Actor component to it.
On the flex source actor component check 'fluid' at a minimum. Configure like for your needs or like the tutorial.
Attach the 'flex particle controller' component to it.
Add the particle system component to it and configure;
Might want to set up a material or something, up to you.
Check 'Collision'.
Under collision select "World" as the "Type. Leave defaults for now.
Check 'send collision messages'.
Now the way that this works is not like you might be used to. A gameobject does not know that a particle hit it, but a particle system knows which gameObjects it hits.
Attach the script below to your flex actor gameobject.
The script will be logging the names of gameObjects with colliders hit by the particle system. You should be able to handle messaging, destroying or the collisions with those gameObjects from here. Don't forget you have many more particle collision settings, scale settings for particle size, you can make some super wicked stuff with this!!!
see also; https://www.youtube.com/watch?v=2IOXPnbHuvo
https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnParticleCollision.html
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class ParticleCollisionDetector : MonoBehaviour
{
public ParticleSystem part;
public List<ParticleCollisionEvent> collisionEvents;
void Start()
{
part = GetComponent<ParticleSystem>();
collisionEvents = new List<ParticleCollisionEvent>();
}
void OnParticleCollision(GameObject other)
{
Debug.Log("THIS PARTICLE SYSTEM HIT:" + other.name);
// int numCollisionEvents = part.GetCollisionEvents(other, collisionEvents);
// Rigidbody rb = other.GetComponent<Rigidbody>();
// int i = 0;
// while (i < numCollisionEvents)
// {
// if (rb)
// {
// Vector3 pos = collisionEvents[i].intersection;
// Vector3 force = collisionEvents[i].velocity * 10;
// rb.AddForce(force);
// }
// i++;
// }
}
}