bool helperRegister(KParticle p) { var posRelative = p.transform.position - posInit; if (posRelative.x < 0 || posRelative.y < 0) return false; int x = (int)(posRelative.x / gridSize.x), y = (int)(posRelative.y / gridSize.y); if (x >= gridCount.x || y >= gridCount.y) return false; gridArray[x, y].register(p); return true; }
public void register(KParticle p) { if (!helperRegister(p)) { //GameObject.Destroy(p.gameObject); p.rigidbody2D.velocity = Vector3.zero; p.transform.position = new Vector3(-10.5f, 1.0f, 11.02f); gridArray[0, gridArray.GetLength(1) -1].register(p); } }
public void updateGrid(KParticle p) { }
void helperCalculatePressure(KParticle other) { var dis = (other.transform.position - transform.position).XY(); float disMagSq = dis.sqrMagnitude; if (disMagSq > disIdeal_SQRT) return; float disMag = Mathf.Sqrt(disMagSq); float ratio = 1- (disMag / disIdeal); pressureRatio += ratio * ratio; pressureNearRatio += ratio * ratio * ratio; }
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); }
void calculatePressure(KParticle other, int index) { var dis = (other.transform.position - transform.position).XY(); float disMagSq = dis.sqrMagnitude; if (disMagSq > disIdeal_SQRT) { arrayDis[index] = float.MaxValue; return; } arrayDis[index] = Mathf.Sqrt(disMagSq); float ratio = 1 - (arrayDis[index] / disIdeal); pressureRatio += ratio * ratio; pressureNearRatio += ratio * ratio*ratio *2.0f; }
void UpdateInteraction(KParticle a, KParticle b) { a.applyRepel(b,Time.fixedDeltaTime); b.applyRepel(a, Time.fixedDeltaTime); }
bool helperIsOut(KParticle p) { var pos = p.transform.position; return pos.x < cornerBottomLeft.x || pos.x > cornerTopRight.x || pos.y < cornerBottomLeft.y || pos.y > cornerTopRight.y; }
void EVENT_PARTICLE_OUT(KParticle p) { MANAGER_GRID.register(p); }
public void unRegister(KParticle p) { kList.Remove(p); }
public void register(KParticle p) { kList.Add(p); p.myGrid = this; renderer.material.color = new Color(kList.Count * .1f,0,0,1); }