Advertisement
Guest User

uuuuuuuuuuuh

a guest
Jun 9th, 2024
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.05 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.Linq;
  5. using System;
  6.  
  7. public class anythingidk : MonoBehaviour
  8. {
  9. Bounds bounds = new Bounds(Vector2.zero, new Vector2(100f, 37f));
  10. public int a = 100;
  11. public float r = 0.1f;
  12. public int s = 2;
  13. public bool isawake = false;
  14. public float[] densities;
  15. public float pi = (float)3.141592653589793238463;
  16. public float mu = (float)0.03;
  17. public float smoothingradius = 0.2f;
  18. public float pressuremultiplier = 225;
  19. public float targetdensity = 234;
  20. public float k = 0.1f;
  21. public float mass = 0;
  22. Vector2[] positions;
  23. Vector2[] velocities;
  24. Vector2[] accelerations;
  25. Vector2 acceleration;
  26. Vector2 position;
  27. Vector2 velocity;
  28. Vector2 gravity;
  29. void updateproperties(){
  30. smoothingradius = r/4;
  31. mass = 4*r*pi*targetdensity/(3*a);
  32. }
  33. void vectorscache(){
  34. positions = new Vector2[a];
  35. velocities = new Vector2[a];
  36. accelerations = new Vector2[a];
  37. position = new Vector2(0.0f, 0.0f);
  38. velocity = new Vector2(0.0f, 0.0f);
  39. gravity = new Vector2(0.0f, -9.8f);
  40. acceleration = new Vector2(0.0f, 0.0f);
  41. for (int i = 0; i < a; i++){
  42. velocities[i] = velocity;
  43. }
  44.  
  45. for (int i = 0; i < a; i++){
  46. float x = UnityEngine.Random.Range(-47.0f+r*s, 47.0f-r*s);
  47. float y = UnityEngine.Random.Range(-19.0f+r*s, 19.0f-r*s);
  48. positions[i] = new Vector2(x, y);
  49. }
  50. }
  51.  
  52. static float smoothingkernel(float r, float h){
  53. if (h>=r) return 0;
  54. if (0>=r) return 0;
  55. double pi = 3.141592653589793238463;
  56. double normalization = 315 / (64 * pi * Mathf.Pow(r, 9));
  57. double result = (r*r-h*h)*(r*r-h*h)*(r*r-h*h)*normalization;
  58. return (float)result;
  59.  
  60. }
  61. float densityatr(Vector2 r){
  62. float density = 0;
  63.  
  64. for (int i = 0; i < a; i++){
  65. float distance = (positions[i] - r).magnitude;
  66. float sum = mass * smoothingkernel(distance, smoothingradius);
  67. density += sum;
  68. }
  69. return density;
  70. }
  71. void densitycache(){
  72. densities = new float[a];
  73. for (int i = 0; i < a; i++){
  74. densities[i] = densityatr(positions[i]);
  75. }
  76. }
  77. float densitytopressure(float density){
  78. float densityerror = density - targetdensity;
  79. float pressure = densityerror * pressuremultiplier;
  80. return pressure;
  81. }
  82. Vector2 smoothingkernelgradient(float r, float h){
  83. Vector2 exception = new Vector2(0, 0);
  84. if (h>=r) return exception;
  85. if (0>=r) return exception;
  86. Vector2 result;
  87. double pi = 3.141592653589793238463;
  88. double normalization = 315 / (64 * pi * Mathf.Pow(r, 9));
  89. double dr = 6*r*(r*r-h*h)*(r*r-h*h)*normalization;
  90. double dh = -6*h*(r*r-h*h)*(r*r-h*h)*normalization;
  91. result = new Vector2((float)dr, (float)dh);
  92. return result;
  93. }
  94. Vector2 smoothingkernelgradient2(float r, float h){
  95. Vector2 exception = new Vector2(0, 0);
  96. if (h>=r) return exception;
  97. if (0>=r) return exception;
  98. Vector2 result;
  99. double pi = 3.141592653589793238463;
  100. double normalization = 315 / (64 * pi * Mathf.Pow(r, 9));
  101. double dr = 6*(h*h*h*h-6*h*h*r*r+5*r*r*r*r)*normalization;
  102. double dh = -6*(5*h*h*h*h-6*h*h*r*r+r*r*r*r)*normalization;
  103. result = new Vector2((float)dr, (float)dh);
  104. return result;
  105. }
  106. Vector2 findvatr(int index){
  107. Vector2 v = Vector2.zero;
  108. Vector2 posofi = positions[index];
  109. for (int i = 0; i < a; i++){
  110. float distance = (posofi - positions[i]).magnitude;
  111. v += mu * mass * (velocities[i] - velocities[index]) * smoothingkernelgradient2(distance, smoothingradius) / densities[i];
  112. }
  113. return v;
  114. }
  115. Vector2 findpatr(int index){
  116. Vector2 p = Vector2.zero;
  117. float[] pressure = new float[a];
  118. float pressurer = densitytopressure(densities[index]);
  119. Vector2[] dst = new Vector2[a];
  120. for (int i = 0; i < a; i++){
  121. dst[i] = positions[i]-positions[index];
  122. pressure[i] = densitytopressure(densities[i]);
  123. Vector2 slope = smoothingkernelgradient(dst[i].magnitude, smoothingradius);
  124. p += -mass * (pressurer+pressure[i]) * slope/(2*densities[i]);
  125. }
  126. return p;
  127. }
  128.  
  129. Vector2 findfatr(int index){
  130. return findpatr(index) + findvatr(index);
  131. }
  132. Vector2 findaatr(int index){
  133. return findfatr(index)/densities[index];
  134. }
  135. void collisions(int i, Bounds bounds){
  136. Vector2 pos = positions[i];
  137. Vector2 vel = velocities[i];
  138. Vector2 halfsize = bounds.size*0.5f;
  139. Vector2 absolute = new Vector2(Mathf.Abs(pos[0]), Mathf.Abs(pos[1]));
  140. Vector2 edgedst = halfsize - absolute;
  141. if (edgedst[0] <= 0){
  142. vel[0] = -velocities[i][0];
  143. }
  144. if (edgedst[1] <= 0){
  145. vel[1] = -velocities[i][1];
  146. }
  147. velocities[i] = vel;
  148. positions[i] = pos;
  149. }
  150. void Awake()
  151. {
  152. updateproperties();
  153. vectorscache();
  154. densitycache();
  155. isawake = true;
  156.  
  157. }
  158. void Update()
  159. {
  160.  
  161.  
  162. for (int i = 0; i < a; i++){
  163. accelerations[i] = findaatr(i);
  164. if (velocities[i].magnitude <= 10){
  165. velocities[i] += (accelerations[i]+gravity) * Time.deltaTime;
  166. }
  167. positions[i] += velocities[i] * Time.deltaTime;
  168. transform.position = positions[i];
  169. collisions(i, bounds);
  170.  
  171. }
  172. }
  173. void OnDrawGizmos()
  174. {
  175. if (Application.isPlaying){
  176. Gizmos.color = Color.blue;
  177. for (int i = 0; i < a; i++){
  178. Gizmos.DrawSphere(positions[i], r);
  179. }
  180. }
  181. }
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement