MegaFlowSpline.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. 
  2. #if false
  3. using UnityEngine;
  4. public class MegaFlowForce : MonoBehaviour
  5. {
  6. public virtual Vector3 GetForce(Vector3 p)
  7. {
  8. return Vector3.zero;
  9. }
  10. public virtual Vector3 GetVel(Vector3 p)
  11. {
  12. return Vector3.zero;
  13. }
  14. }
  15. // Option to bake all the forces into a Vector Field
  16. public class MegaFlowSpline : MegaFlowForce
  17. {
  18. public MegaShape shape;
  19. public int curve;
  20. public float force;
  21. public float radius = 1.0f;
  22. public AnimationCurve radiuscrv = new AnimationCurve(new Keyframe(0, 1), new Keyframe(1, 1));
  23. public AnimationCurve forcecrv = new AnimationCurve(new Keyframe(0, 1), new Keyframe(1, 1));
  24. Vector3 tan;
  25. float alpha;
  26. int kn;
  27. [Range(0.001f, 0.1f)]
  28. public float mass = 1.0f;
  29. public Vector3 vel = Vector3.zero;
  30. public Vector3 pos = Vector3.zero;
  31. public float constrainfrc = 1.0f;
  32. public float drag = 0.1f;
  33. // Break line up into segments then can do quick segment check
  34. // Start with spline for proof concept
  35. public float velfrc = 1.0f;
  36. public override Vector3 GetForce(Vector3 p)
  37. {
  38. Vector3 np = shape.FindNearestPointWorld(p, 4, ref kn, ref tan, ref alpha);
  39. //Debug.Log("np " + np);
  40. float dist = Vector3.Distance(np, p);
  41. if ( dist < radius )
  42. {
  43. Vector3 frc = tan.normalized * force * (1.0f - (dist / radius));
  44. frc += (np - p).normalized * constrainfrc * (dist / radius);
  45. frc -= vel * drag;
  46. //Debug.Log("tan " + tan);
  47. //vel = tan.normalized * velfrc;
  48. return frc; //tan.normalized * force * (1.0f - (dist / radius));
  49. }
  50. return Vector3.zero;
  51. }
  52. public float flowvel = 1.0f;
  53. public override Vector3 GetVel(Vector3 p)
  54. {
  55. Vector3 np = shape.FindNearestPointWorld(p, 4, ref kn, ref tan, ref alpha);
  56. float dist = Vector3.Distance(np, p);
  57. if ( dist < radius )
  58. {
  59. Vector3 vel1 = tan.normalized * flowvel; // * (1.0f - (dist / radius));
  60. return vel1; //tan.normalized * force * (1.0f - (dist / radius));
  61. }
  62. return vel; //Vector3.zero;
  63. }
  64. void Start()
  65. {
  66. pos = transform.position;
  67. }
  68. public float gravity = -9.81f;
  69. void Update()
  70. {
  71. UpdateVel();
  72. return;
  73. Vector3 frc = GetForce(pos);
  74. frc.y += gravity;
  75. vel += (frc / mass) * Time.deltaTime;
  76. pos += vel * Time.deltaTime;
  77. if ( pos.y < 0.0f )
  78. pos.y = 0.0f;
  79. transform.position = pos;
  80. }
  81. void UpdateVel1()
  82. {
  83. //UpdateVel();
  84. //return;
  85. Vector3 tvel = GetVel(pos);
  86. Vector3 delta = tvel - vel;
  87. Vector3 imp = mass * (delta / Time.deltaTime);
  88. Vector3 frc = imp;
  89. frc.y += gravity;
  90. vel += (frc / mass) * Time.deltaTime;
  91. pos += vel * Time.deltaTime;
  92. if ( pos.y < 0.0f )
  93. pos.y = 0.0f;
  94. transform.position = pos;
  95. }
  96. void UpdateVel2()
  97. {
  98. //UpdateVel();
  99. //return;
  100. Vector3 tvel = GetVel(pos);
  101. Vector3 delta = tvel - vel;
  102. Vector3 imp = mass * (delta / Time.deltaTime);
  103. Vector3 frc = imp;
  104. frc.y += gravity;
  105. vel += (frc / mass) * Time.deltaTime;
  106. pos += vel * Time.deltaTime;
  107. if ( pos.y < 0.0f )
  108. pos.y = 0.0f;
  109. transform.position = pos;
  110. }
  111. public float Area = 1.0f;
  112. public float Reynolds = 20.0f;
  113. public float Density = 1.22f;
  114. void UpdateVel()
  115. {
  116. //if ( source )
  117. {
  118. Vector3 Fshape = Vector3.zero; // Random force due to particle shape
  119. Vector3 Fgrv = new Vector3(0.0f, gravity, 0.0f);
  120. float duration = Time.deltaTime;
  121. pos = transform.position;
  122. bool inbounds = true;
  123. float p = Density; //->GetFloat(unityflow_density, t); //1.22f;
  124. float A = Area; //pblock->GetFloat(unityflow_area, t); //0.01f; //1.0f;
  125. float Re = Reynolds; //->GetFloat(unityflow_reynolds, t); //20.0f;
  126. //float mass = mass; //pblock->GetFloat(unityflow_mass, t); //0.001f;
  127. float dt = 0.01f;
  128. float coef = 1.0f * p * A * Mathf.Pow(Re, -0.5f);
  129. Vector3 flowpos = pos; //source.transform.worldToLocalMatrix.MultiplyPoint3x4(pos);
  130. while ( duration > 0.0f )
  131. {
  132. //Vector3 flowpos = source.transform.worldToLocalMatrix.MultiplyPoint(pos);
  133. Vector3 airvel = GetVel(flowpos); //source.GetGridVel(flowpos, ref inbounds) * source.Scale;
  134. if ( !inbounds )
  135. {
  136. //airvel = new Vector3(scale, 0.0f, 0.0f);
  137. //flowpos += vel * dt;
  138. }
  139. else
  140. {
  141. Vector3 tvel = airvel - vel;
  142. float U = tvel.sqrMagnitude; //magnitude;
  143. //float df = 1.0f * p * (U * U) * A * Mathf.Pow(Re, -0.5f);
  144. float df = coef * U; //(U * U);
  145. Vector3 dir = tvel.normalized;
  146. Vector3 Fdrag = dir * df;
  147. Vector3 Fp = Fdrag + Fshape + Fgrv;
  148. Vector3 acc = Fp / mass;
  149. vel += acc * dt;
  150. //vel = airvel;
  151. flowpos += vel * dt;
  152. }
  153. duration -= dt;
  154. //break;
  155. }
  156. if ( flowpos.y < 0.0f )
  157. {
  158. flowpos.y = 0.0f;
  159. }
  160. transform.position = flowpos; //source.transform.localToWorldMatrix.MultiplyPoint3x4(flowpos);
  161. //rot += rotspeed * Time.deltaTime;
  162. //transform.rotation = Quaternion.Euler(rot);
  163. }
  164. }
  165. }
  166. #endif