public void applyRepel(KParticle other, float timeElapsed) { var dis = (other.transform.position - transform.position).XY() + new Vector2(.001f, .001f); float disMag = dis.magnitude; if (disMag > disIdeal) return; Vector2 dir; if (disMag != 0) dir = new Vector2(dis.x / disMag, dis.y / disMag); else { float angleRan = Random.Range(0, 3.14f * 2); dir = new Vector2(Mathf.Cos(angleRan), Mathf.Sin(angleRan)); } float ratio = 1 - disMag / disIdeal; float factor = ratio * (pressureRatio + pressureNearRatio * ratio) / (2 * disMag); Vector2 d = dis * factor; Vector2 veloDiff = other.rigidbody2D.velocity - rigidbody2D.velocity; factor = VISCOSITY * ratio * DT; d -= veloDiff * factor * timeElapsed; //Debug.Log(d); applyForce(d * -1.0f); other.applyForce(d); }
void applyPressure_5_01(KParticle other, int index, float timeElapsed = .001f) { if (arrayDis[index] > disIdeal) return; var dis = (other.transform.position - transform.position).XY() + new Vector2(.001f, .001f); float disMag = arrayDis[index]; Vector2 dir = (arrayDis[index] > .01f) ? new Vector2(dis.x / disMag, dis.y / disMag) : helperRandomDir(); float ratio = 1.0f - disMag / disIdeal; float factor = ratio * (pressureRatio + pressureNearRatio * ratio) / (2 * disMag); // / (2 * disMag); Vector2 d = dis * factor; Vector2 veloDiff = other.rigidbody2D.velocity - rigidbody2D.velocity; d -= veloDiff * factor * timeElapsed; //Debug.Log(pressureRatio + " " + pressureNearRatio + " " + ratio + " = " + factor); d = dir * factor; var vChange = (other.rigidbody2D.velocity - rigidbody2D.velocity) * ratio; //Debug.Log(d); applyForce(d * -1.0f + vChange); other.applyForce(d - vChange); }