Working On A Sudo Fluid System

I am working on a “fluid system” based on particles. I have the particles worked out but I am having trouble with a couple of things. The biggest is connecting the particles into a face, and the second is having spacing in between each particle. This is so that less particles can be used, and to help with volume. Here is the code I have at the moment:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Fluid: MonoBehaviour {

public float bonddist = 1;
public float bondStrength = 10;
public float repulsion = 10;
public float repulsionDist = 50;
public float damp = 1;

void Update () 
{
	//Destroy (gameObject, 15);
	Collider[] obj = Physics.OverlapSphere (transform.position, bonddist);
	rigidbody.interpolation = RigidbodyInterpolation.Interpolate;

	foreach(Collider OBJ in obj)
	{
		GameObject temp = OBJ.gameObject; 
		Vector3 offset = transform.position - temp.transform.position;
		Rigidbody rb = temp.rigidbody;

		if((OBJ.tag == "Fluid") && Vector3.Distance(temp.transform.position, transform.position) > repulsionDist)
		{
			rb.AddForce(offset / bondStrength);
			Debug.DrawLine(transform.position, temp.transform.position, Color.red);
		}
		else if(OBJ.tag == "Fluid")
		{
			rb.AddForce(-(offset / repulsion) / ((Vector3.Distance(temp.transform.position, transform.position)) + 0.0001f));
		}
	}
}

}

To create something as you describe is going to be quite a challenge. Are you operating in 2D or 3D?

A 2D simulation is relatively approachable, assuming you’re pretty darn familiar with procedural mesh generation. I assume you intend that each “particle” acts as a vertex of a face or group of faces. Your real-time mesh generator would examine a collection of particles, building a new mesh each N frames.

The first thing that comes to mind is determining each particle’s nearest N neighbors and creating a new abstract “edge” between each one, filtering out duplicate edges. Another pass examines the completed edge collection and creates faces for each triangle. Even with this simple method, you’ve got your work cut out for you. :slight_smile:

A low resolution 3D system might be feasible, but will require intimate familiarity with mesh creation. The traditional approach in 3D applications (and presumably some games) involves voxels and an established mesh generation algorithm like Marching Cubes.