Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using System.Linq;
- using System;
- public class anythingidk : MonoBehaviour
- {
- Bounds bounds = new Bounds(Vector2.zero, new Vector2(100f, 37f));
- public int a = 100;
- public float r = 0.1f;
- public int s = 2;
- public bool isawake = false;
- public float[] densities;
- public float pi = (float)3.141592653589793238463;
- public float mu = (float)0.03;
- public float smoothingradius = 0.2f;
- public float pressuremultiplier = 225;
- public float targetdensity = 234;
- public float k = 0.1f;
- public float mass = 0;
- Vector2[] positions;
- Vector2[] velocities;
- Vector2[] accelerations;
- Vector2 acceleration;
- Vector2 position;
- Vector2 velocity;
- Vector2 gravity;
- void updateproperties(){
- smoothingradius = r/4;
- mass = 4*r*pi*targetdensity/(3*a);
- }
- void vectorscache(){
- positions = new Vector2[a];
- velocities = new Vector2[a];
- accelerations = new Vector2[a];
- position = new Vector2(0.0f, 0.0f);
- velocity = new Vector2(0.0f, 0.0f);
- gravity = new Vector2(0.0f, -9.8f);
- acceleration = new Vector2(0.0f, 0.0f);
- for (int i = 0; i < a; i++){
- velocities[i] = velocity;
- }
- for (int i = 0; i < a; i++){
- float x = UnityEngine.Random.Range(-47.0f+r*s, 47.0f-r*s);
- float y = UnityEngine.Random.Range(-19.0f+r*s, 19.0f-r*s);
- positions[i] = new Vector2(x, y);
- }
- }
- static float smoothingkernel(float r, float h){
- if (h>=r) return 0;
- if (0>=r) return 0;
- double pi = 3.141592653589793238463;
- double normalization = 315 / (64 * pi * Mathf.Pow(r, 9));
- double result = (r*r-h*h)*(r*r-h*h)*(r*r-h*h)*normalization;
- return (float)result;
- }
- float densityatr(Vector2 r){
- float density = 0;
- for (int i = 0; i < a; i++){
- float distance = (positions[i] - r).magnitude;
- float sum = mass * smoothingkernel(distance, smoothingradius);
- density += sum;
- }
- return density;
- }
- void densitycache(){
- densities = new float[a];
- for (int i = 0; i < a; i++){
- densities[i] = densityatr(positions[i]);
- }
- }
- float densitytopressure(float density){
- float densityerror = density - targetdensity;
- float pressure = densityerror * pressuremultiplier;
- return pressure;
- }
- Vector2 smoothingkernelgradient(float r, float h){
- Vector2 exception = new Vector2(0, 0);
- if (h>=r) return exception;
- if (0>=r) return exception;
- Vector2 result;
- double pi = 3.141592653589793238463;
- double normalization = 315 / (64 * pi * Mathf.Pow(r, 9));
- double dr = 6*r*(r*r-h*h)*(r*r-h*h)*normalization;
- double dh = -6*h*(r*r-h*h)*(r*r-h*h)*normalization;
- result = new Vector2((float)dr, (float)dh);
- return result;
- }
- Vector2 smoothingkernelgradient2(float r, float h){
- Vector2 exception = new Vector2(0, 0);
- if (h>=r) return exception;
- if (0>=r) return exception;
- Vector2 result;
- double pi = 3.141592653589793238463;
- double normalization = 315 / (64 * pi * Mathf.Pow(r, 9));
- double dr = 6*(h*h*h*h-6*h*h*r*r+5*r*r*r*r)*normalization;
- double dh = -6*(5*h*h*h*h-6*h*h*r*r+r*r*r*r)*normalization;
- result = new Vector2((float)dr, (float)dh);
- return result;
- }
- Vector2 findvatr(int index){
- Vector2 v = Vector2.zero;
- Vector2 posofi = positions[index];
- for (int i = 0; i < a; i++){
- float distance = (posofi - positions[i]).magnitude;
- v += mu * mass * (velocities[i] - velocities[index]) * smoothingkernelgradient2(distance, smoothingradius) / densities[i];
- }
- return v;
- }
- Vector2 findpatr(int index){
- Vector2 p = Vector2.zero;
- float[] pressure = new float[a];
- float pressurer = densitytopressure(densities[index]);
- Vector2[] dst = new Vector2[a];
- for (int i = 0; i < a; i++){
- dst[i] = positions[i]-positions[index];
- pressure[i] = densitytopressure(densities[i]);
- Vector2 slope = smoothingkernelgradient(dst[i].magnitude, smoothingradius);
- p += -mass * (pressurer+pressure[i]) * slope/(2*densities[i]);
- }
- return p;
- }
- Vector2 findfatr(int index){
- return findpatr(index) + findvatr(index);
- }
- Vector2 findaatr(int index){
- return findfatr(index)/densities[index];
- }
- void collisions(int i, Bounds bounds){
- Vector2 pos = positions[i];
- Vector2 vel = velocities[i];
- Vector2 halfsize = bounds.size*0.5f;
- Vector2 absolute = new Vector2(Mathf.Abs(pos[0]), Mathf.Abs(pos[1]));
- Vector2 edgedst = halfsize - absolute;
- if (edgedst[0] <= 0){
- vel[0] = -velocities[i][0];
- }
- if (edgedst[1] <= 0){
- vel[1] = -velocities[i][1];
- }
- velocities[i] = vel;
- positions[i] = pos;
- }
- void Awake()
- {
- updateproperties();
- vectorscache();
- densitycache();
- isawake = true;
- }
- void Update()
- {
- for (int i = 0; i < a; i++){
- accelerations[i] = findaatr(i);
- if (velocities[i].magnitude <= 10){
- velocities[i] += (accelerations[i]+gravity) * Time.deltaTime;
- }
- positions[i] += velocities[i] * Time.deltaTime;
- transform.position = positions[i];
- collisions(i, bounds);
- }
- }
- void OnDrawGizmos()
- {
- if (Application.isPlaying){
- Gizmos.color = Color.blue;
- for (int i = 0; i < a; i++){
- Gizmos.DrawSphere(positions[i], r);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement