void SpawnParticle() { if (particles.Count < particle_number) { GameObject b; b = Instantiate( particle_demo, new Vector3( transform.position.x + Random.Range(-width_create + 0.2f, width_create - 0.2f), transform.position.y + Random.Range(-height_create, height_create), 0), Quaternion.identity, transform); particle part = new particle(b.transform, 0.2f, d0_1); part.ApplyForce(new Vector3(Random.Range(-5, 5), 0, 0)); particles.Add(part); UpdateGrid(part); b = Instantiate( particle_2_demo, new Vector3( transform.position.x + Random.Range(-width_create + 0.2f, width_create - 0.2f), transform.position.y + Random.Range(-height_create, height_create), 0), Quaternion.identity, transform); part = new particle(b.transform, 0.2f, d0_2); part.ApplyForce(new Vector3(Random.Range(-5, 5), 0, 0)); particles.Add(part); UpdateGrid(part); } }
particle ManageBorders(particle part) { // floor (down) if (part.position.y - rayon_particule_real < -boundY_screen) { float deltaPos = part.position.y - rayon_particule_real - (-boundY_screen); part.position = new Vector3(part.position.x, -boundY_screen, part.position.z); part.position += (20f * Vector3.down * deltaPos) * Time.deltaTime * Time.deltaTime; } // left wall if (part.position.x - rayon_particule_real < -boundX_screen) { float deltaPos = part.position.x - rayon_particule_real + boundX_screen; part.position = new Vector3(-boundX_screen * 0.99f, part.position.y, part.position.z); part.position += (20f * Vector3.left * deltaPos) * Time.deltaTime * Time.deltaTime; } // right wall else if (part.position.x + rayon_particule_real > boundX_screen) { float deltaPos = part.position.x + rayon_particule_real - boundX_screen; part.position = new Vector3(boundX_screen * 0.99f, part.position.y, part.position.z); part.position += (20f * Vector3.left * deltaPos) * Time.deltaTime * Time.deltaTime; } return(part); }
private void Whipping(GameObject go, particle info) { Vector3 resPos = transform.TransformPoint(info.initPos); Vector3 d = resPos - info.curPos; info.curPos += d * parti_elastic; }
// Update is called once per frame void Update() { //SpawnParticle (); for (int k = 0; k < particles.Count; k++) { particle part = particles [k]; // apply gravity: vi = vi + dt*g part.velocity = part.velocity + Vector3.down * Time.deltaTime * gravity; // save previous position: xi_prev = xi Vector3 pos_old = part.position; // advance to predicted position: xi = xi + dt * vi part.position = part.position + Time.deltaTime * part.velocity; part = DoubleDensityRelaxation(part); part = ManageBorders(part); // use previous position to compute next velocity: vi = (xi - xi_prev) / dt part.velocity = (part.position - pos_old) / Time.deltaTime; } grid.Clear(); for (int k = 0; k < particles.Count; k++) { UpdateGrid(particles [k]); } }
void Start() { GameObject pixel_res = Resources.Load("pixel") as GameObject; for (int i = 0; i <= 50; i++) { GameObject pixel = Instantiate(pixel_res) as GameObject; particle particle = pixel.GetComponent <particle> (); int size = (int)(Random.value * 3) + 1; pixel.transform.localScale = new Vector2(size, size); int life_time = (int)(60 * Random.value + 60); int wait_time = (int)(15 * Random.value); float speed = 3.5f; Vector2 speed2 = new Vector2(Random.value * 2 - 1, Random.value * 2 - 1).normalized *speed; particle.init(life_time, wait_time, new Color(1, 0.8978f, 0, 1), new Color(1f, 0.259f, 0, 1)); particle.set_gravity(0.05f * (Random.value * 2 - 1) + 0.8f); particle.set_speed(speed2); particle.set_target(transform.parent.gameObject); particle.set_position_miu(transform.localPosition); } Destroy(transform.gameObject); }
private void CheckSpring(GameObject go, particle info, double iNormalLen, Vector3 delLen, Vector3 speed1, Vector3 speed2) { Vector3 curPrePos = transform.InverseTransformPoint(info.curPos); if (delLen.magnitude < Lmin) { // 弹簧异常 简单当做反弹处理 speed1 = -speed1; } else { // 阻尼力和弹力计算 Vector3 elasF; elasF = -(delLen.magnitude - (float)iNormalLen) * elastic * delLen / delLen.magnitude - damp * (speed1 - speed2); // 自身重力 // elasF1 = elasF1 + acceleration * info.mass * Vector3.down; // 加速度 Vector3 a1 = elasF / info.mass; speed1 += a1 * Time.deltaTime; info.speed = speed1; } Vector3 offset = speed1 * Time.deltaTime; go.transform.localPosition = curPrePos + offset; // 数据保存 info.prePos = info.curPos; info.curPos = go.transform.position; }
float densityList(Vector2 positionStep, particle part, int pow) { List <particle> parts; int hash = positionStep.GetHashCode(); if (grid.ContainsKey(hash)) { parts = grid [hash]; } else { return(0); } float density = 0; foreach (particle part2 in parts) { // distance h float dist = Vector3.Distance(part2.position, part.position); // di = sum{j in Neighbours(i)} ((1 - rij / h) ^ pow) if (dist < rayon_particule && dist > 0) { density += Mathf.Pow((1 - dist / rayon_particule), pow); } } return(density); }
void Start() { GameObject pixel_res = Resources.Load("pixel") as GameObject; for (int i = 0; i <= 3; i++) { if (transform.parent != null && transform.parent.gameObject != null) { GameObject pixel = Instantiate(pixel_res) as GameObject; particle particle = pixel.GetComponent <particle> (); int size = (int)(Random.value * 2) + 1; pixel.transform.localScale = new Vector2(size, size); int life_time = (int)(100 * Random.value + 100); int wait_time = (int)(3 * Random.value); particle.init(life_time, wait_time, new Color(1, 1, 1)); particle.set_speed(new Vector2(Random.value * 1 * transform.localScale.x, Random.value * 3)); particle.set_target(transform.parent.gameObject); particle.set_is_tigger(true); } } Destroy(transform.gameObject); }
/// <summary> /// constructor for Fireworks effect /// </summary> /// <param name="image">What TextureID to be used</param> public FireWorks(int image) { this.image = image; this.fade = 1.1f; float z = 0.4f; angel = 360.0f /(float)NFW; angel = (float) (angel * (Math.PI/180) ); part = new particle[NFW]; sfade = (0.5f + ( Util.Rnd.Next() / (Int32.MaxValue + 1.0f))) / 50.0f; float x = Util.Rnd.Next(-18,18)/10; float y = Util.Rnd.Next(4,18)/10; for (int i = 0; i < NFW; i++) { part[i] = new particle(); part[i].x = x; part[i].y = y; part[i].z = z; //v = (float)Math.PI * 2.0f * (Util.Rnd.Next() / (Int32.MaxValue + 1.0f)); xv = 0.7f + 0.1f * (Util.Rnd.Next() / (Int32.MaxValue + 1.0f)); part[i].sx = 0.005f * (float)Math.Cos(angel * i) * xv; part[i].sy = 0.005f * (float)Math.Sin(angel * i) * xv; z -= 0.0001f; } }
void Start() { GameObject pixel_res = Resources.Load("spirit") as GameObject; for (int i = 0; i <= 1; i++) { GameObject pixel = Instantiate(pixel_res) as GameObject; particle particle = pixel.GetComponent <particle> (); int size = (int)(Random.value * 3) + 1; pixel.transform.localScale = new Vector2(size, size); int life_time = (int)(100 * Random.value + 100); int wait_time = (int)(5 * Random.value); particle.init(life_time, wait_time, new Color(1f, 0.259f, 0, 1), new Color(0, 0, 0, 0)); particle.set_gravity(0.05f * (Random.value * 2 - 1)); particle.set_speed(new Vector2(Random.value * 0.5f * transform.localScale.x, Random.value * 0.5f)); if (transform.parent != null) { particle.set_target(transform.parent.gameObject); } else { particle.set_target(null); } } Destroy(transform.gameObject); }
public static double getLET(double x, particle getParticle) { if (x == 0) { return(0); } return(getParticle().input(x)); }
private void dFunc() { var tParticle = new particle(); tParticle._position._x = 5f; tParticle._position._y = 53f; Debug.Log(tParticle._position); }
public static void ShowVelocities(ref particle[] particleData) { for (int i = 0; i < particleData.Length; i++) { particle p = particleData[i]; Debug.DrawRay(p.position * GizmoPosScale, p.direction); } }
private void CheckInertia(GameObject go, particle info) { Vector3 v = info.curPos - info.prePos; Vector3 offsetVector3 = objMove * inertia; info.prePos = offsetVector3 + info.curPos; info.curPos += v * (1 - damp) + offsetVector3; Whipping(go, info); }
public static void shield(double s, double[] e, double[] f, particle getParticle) { for (int i = 0; i < e.Length; i++) { e[i] -= s * getParticle().input(e[i]); if (e[i] <= 0)//如果被完全屏蔽 { e[i] = 0; f[i] = 0; } } }
void UpdateGrid(particle part) { int j_pos = Mathf.FloorToInt(part.position.x / step); int i_pos = Mathf.FloorToInt(part.position.y / step); int hash = new Vector2(i_pos, j_pos).GetHashCode(); if (!grid.ContainsKey(hash)) { grid.Add(hash, new List <particle> ()); } grid [hash].Add(part); }
particle[] GetParticlePoints() { particle[] points = new particle[count]; Random.InitState(42); for (int i = 0; i < count; i++) { points[i].position = new Vector3(Random.Range(0, size), Random.Range(0, size), Random.Range(0, size)); points[i].direction = new Vector3(Random.Range(-size, size), Random.Range(-size, size), Random.Range(-size, size)); } return(points); }
public void TestPairST () { int t = 0, s = 1; particle p = new particle (t, s); particle q = new particle (t, s); pair_st pst = new pair_st (p, q); pair_st pstn = new pair_st (p.st, q.st); Assert.AreEqual (pst.a, pstn.a); Assert.AreEqual (pst.b, pstn.b); }
void Update() { if (is_active) { wait--; if (wait < 0) { if (transform.parent != null && transform.parent.gameObject != null) { GameObject pixel_res = Resources.Load("pixel") as GameObject; for (int i = 0; i <= 1; i++) { GameObject pixel = Instantiate(pixel_res) as GameObject; particle particle = pixel.GetComponent <particle> (); int size = (int)(Random.value * 2) + 1; pixel.transform.localScale = new Vector2(size, size); int life_time = (int)(20 * Random.value + 20); int wait_time = (int)(2 * Random.value); particle.init(life_time, wait_time, new Color(1, 1, 1, 1), new Color(1, 1, 1, 0)); particle.set_speed(random_vector(direction2) * (Random.value * 8 + 8)); particle.set_gravity(0.05f * (Random.value * 2 - 1) + 0.1f); particle.set_target(transform.parent.gameObject); } for (int i = 0; i <= 3; i++) { GameObject pixel = Instantiate(pixel_res) as GameObject; particle particle = pixel.GetComponent <particle> (); int size = (int)(Random.value * 2) + 1; pixel.transform.localScale = new Vector2(size, size); int life_time = (int)(35 * Random.value + 35); int wait_time = (int)(2 * Random.value); particle.init(life_time, wait_time, new Color(1, 1, 1, 1), new Color(1, 1, 1, 0)); particle.set_speed(random_vector(direction2) * (Random.value * 4 + 4)); particle.set_gravity(0.05f * (Random.value * 2 - 1) + 1.5f); particle.set_target(transform.parent.gameObject); } Destroy(transform.gameObject); } } } }
void SpringSimulation(GameObject go1, particle info1, GameObject go2, particle info2, double iNormalLen) { Vector3 delLen = transform.InverseTransformPoint(info1.curPos) - transform.InverseTransformPoint(info2.curPos); //go1.transform.localPosition - go2.transform.localPosition; Vector3 prePrePos1 = transform.InverseTransformPoint(info1.prePos); Vector3 prePrePos2 = transform.InverseTransformPoint(info2.prePos); Vector3 curPos1 = transform.InverseTransformPoint(info1.curPos); Vector3 curPos2 = transform.InverseTransformPoint(info2.curPos); Vector3 speed1 = (curPos1 - prePrePos1) / Time.deltaTime; Vector3 speed2 = (curPos2 - prePrePos2) / Time.deltaTime; CheckSpring(go1, info1, iNormalLen, delLen, speed1, speed2); CheckSpring(go2, info2, iNormalLen, -delLen, speed2, speed1); }
void LateUpdate() { for (int i = 0; i < gos.Length; i++) { //惯性 // CheckInertia(gos[i], infos[i]); } for (int i = 0; i < gos.Length; i++) { List <Tuple <int, double> > aroundList = new List <Tuple <int, double> >(); // 把当前点四周围的点点当做弹簧一一链接。 SingleAdd(aroundList, new Tuple <int, double>(i - width, L0)); if ((i + 1) % width != 1) { SingleAdd(aroundList, new Tuple <int, double>(i - width - 1, L0 * Math.Sqrt(2))); SingleAdd(aroundList, new Tuple <int, double>(i + width - 1, L0 * Math.Sqrt(2))); SingleAdd(aroundList, new Tuple <int, double>(i - 1, L0)); } if ((i + 1) % width != 0) { SingleAdd(aroundList, new Tuple <int, double>(i - width + 1, L0 * Math.Sqrt(2))); SingleAdd(aroundList, new Tuple <int, double>(i + width + 1, L0 * Math.Sqrt(2))); SingleAdd(aroundList, new Tuple <int, double>(i + 1, L0)); } SingleAdd(aroundList, new Tuple <int, double>(i + width, L0)); for (int j = 0; j < aroundList.Count; j++) { if (aroundList[j].Item1 >= 0 && aroundList[j].Item1 < gos.Length) { GameObject go1 = gos[i]; particle info1 = infos[i]; GameObject go2 = gos[aroundList[j].Item1]; particle info2 = infos[aroundList[j].Item1]; SpringSimulation(go1, info1, go2, info2, aroundList[j].Item2); } } } // HookPoints(); for (int i = 0; i < gos.Length; i++) { ApplyPosition(gos[i], infos[i]); } }
void rekebisha() //reset { particle[] ps = new particle[numParticles]; for (int i = 0; i < numParticles; i++) { particle p = new particle(); p.pos = new Vector2(Random.Range(10, resolution - 10), Random.Range(10, resolution - 10)); p.dir = new Vector2(Random.Range(-50, 50), Random.Range(-50, 50)); Color c = Random.ColorHSV(0, 1, 0.5f, 1, 0.5f, 1); p.color = new Vector3(c.r, c.g, c.b); p.alive = 0f; ps[i] = p; } particleBuffer.SetData(ps); }
public Smoke() { this.MouseActive = true; this.TextureFilter = Sprite.TextureFilters.High; this.Scaling = new EPointF(5, 5); this.Ink = RasterOps.ROPs.AddPin; this.Create(100, 100); lwidth = this.Width / res; lheight = this.Height / res; v = new vsquare[lwidth + 1][]; for (int i = 0; i < v.Length; i++) { v[i] = new vsquare[lheight + 1]; } vbuf = new vbuffer[lwidth + 1][]; for (int i = 0; i < vbuf.Length; i++) { vbuf[i] = new vbuffer[lheight + 1]; } p = new particle[pnum]; CurrentColor = Color.FromArgb(2, 255, 0, 0); ERectangle emitter = ERectangle.FromLTRB(this.Width / 2 - 20, this.Height - 20, this.Width / 2 + 20, this.Height); for (int i = 0; i < pnum; i++) { p[i] = new particle(emitter, this); p[i].Init(); } for (int i = 0; i <= lwidth; i++) { for (int u = 0; u <= lheight; u++) { v[i][u] = new vsquare(i * res, u * res, this); vbuf[i][u] = new vbuffer(i * res, u * res, this); } } RandomGust = new Gust(); }
Vector3 moveList(Vector2 positionStep, particle part, float dk, float d1k) { List <particle> parts; int hash = positionStep.GetHashCode(); if (grid.ContainsKey(hash)) { parts = grid [hash]; } else { return(Vector3.zero); } Vector3 move = Vector3.zero; foreach (particle part2 in parts) { float dist = Vector3.Distance(part2.position, part.position); if (dist < rayon_particule && dist > 0) { Vector3 rij = (part2.position - part.position).normalized; float q = dist / rayon_particule; // compute pressure and near-pressure float P = k0 * (dk - part.d0); float P2 = k0 * (dk - part2.d0); float P_near = k1 * (d1k); // apply displacements //D = dt^2 * (P(1-q)+Pnear(1-q)^2) * rij Vector3 D = Time.deltaTime * Time.deltaTime * (P * (1 - q) + P_near * (1 - q) * (1 - q)) * rij; Vector3 D2 = Time.deltaTime * Time.deltaTime * (P2 * (1 - q) + P_near * (1 - q) * (1 - q)) * rij; //dx = dx - D/2 move -= D / 2; //xj = xj + D/2 part2.position += D2 / 2; // change velocity of other particle also Vector3 V_other = Time.deltaTime * (P2 * (1 - q) + P_near * (1 - q) * (1 - q)) * rij; part2.velocity += V_other / 2; } } return(move); }
protected override void SolveInstance(Grasshopper.Kernel.IGH_DataAccess DA) { double v = -1.0; DA.GetData(2, ref v); if (!FriedChiken.isInitialized) { GH_Point[] pointList = new GH_Point[2]; DA.GetData(0, ref pointList[0]); DA.GetData(1, ref pointList[1]); cV = new constrainVolumeObject(v); mikity.NumericalMethodHelper.particle[] particles = new mikity.NumericalMethodHelper.particle[_nNodes]; for (int i = 0; i < _nNodes; i++) { particles[i] = new particle(pointList[i].Value.X, pointList[i].Value.Y, pointList[i].Value.Z); } pS = new GH_particleSystem(particles); cV.addElement(new isoparametricElement(0, 1)); pS.Value.addObject(cV); lGeometry.Clear(); lGeometry2.Clear(); lGeometry.Add(particles[0][0], particles[0][1], particles[0][2]); lGeometry.Add(particles[1][0], particles[1][1], particles[1][2]); lGeometry2.Add(particles[0][0], particles[0][1], particles[0][2]); lGeometry2.Add(particles[1][0], particles[1][1], particles[1][2]); this.DVPW = GetDVPW(lGeometry); this.BKGT = GetBKGT(lGeometry); pS.DVPW = GetDVPW(lGeometry2); pS.UPGR = GetUPGR(lGeometry2); pS.BKGT = GetBKGT(lGeometry2); } else { if (cV != null) { if (v > 0) { cV.refVolume = v; } } } DA.SetData(0, pS); }
particle DoubleDensityRelaxation(particle part) { Vector3 moveall = Vector3.zero; // get the indices of the virtual box int j = Mathf.FloorToInt(part.position.x / step); int i = Mathf.FloorToInt(part.position.y / step); float density = 0; float density2 = 2; Vector2 positionStep; // compute density and near-density for (int m = -1; m <= +1; m++) { for (int n = -1; n <= +1; n++) { positionStep = new Vector2(i + n, j + m); // density_i density += densityList(positionStep, part, 2); // density_i_near density2 += densityList(positionStep, part, 3); } } // particle has some neighbouring particles if (density > 0) { // compute pressure and near-pressure for (int m = -1; m <= +1; m++) { for (int n = -1; n <= +1; n++) { positionStep = new Vector2(i + n, j + m); moveall += moveList(positionStep, part, density, density2); } } } // xi = xi + dx part.position += moveall; return(part); }
public Smoke() { this.MouseActive = true; this.TextureFilter = Sprite.TextureFilters.High; this.Scaling = new EPointF(5,5); this.Ink = RasterOps.ROPs.AddPin; this.Create(100,100); lwidth = this.Width/res; lheight = this.Height/res; v = new vsquare[lwidth+1][]; for (int i=0; i<v.Length;i++) v[i] = new vsquare[lheight+1]; vbuf = new vbuffer[lwidth+1][]; for (int i=0; i<vbuf.Length;i++) vbuf[i] = new vbuffer[lheight+1]; p = new particle[pnum]; CurrentColor = Color.FromArgb(2,255,0,0); ERectangle emitter = ERectangle.FromLTRB(this.Width/2-20,this.Height-20,this.Width/2+20,this.Height); for(int i = 0; i < pnum; i++) { p[i] = new particle(emitter, this); p[i].Init(); } for(int i = 0; i <= lwidth; i++) { for(int u = 0; u <= lheight; u++) { v[i][u] = new vsquare(i*res,u*res, this); vbuf[i][u] = new vbuffer(i*res,u*res, this); } } RandomGust = new Gust(); }
// Start is called before the first frame update void Start() { objPrePosition = transform.position; for (int i = 0; i < gos.Length; i++) { particle p = new particle(); p.curPos = gos[i].transform.position; p.prePos = gos[i].transform.position; p.initPos = gos[i].transform.localPosition; infos.Add(p); if (i == gos.Length - 1) { hookPos2 = gos[i].transform.position; } else if (i == gos.Length - width) { hookPos1 = gos[i].transform.position; } } queue = new CoroutineQueue(2, StartCoroutine); queue.Run(DelayAddForce()); }
//---------------------------------------------------------------------------------------------------------------------- /// @brief calculates the smoothing kernal for density, used in our SPH equations /// @param _currentParticle the particle we wish to test for /// @param _neighbour the particle we wish to test our _current particle against /// @return the weighted viscosity vector the particle has on our _currentParticle protected Vector3 calcViscosityKern(particle _currentParticle, particle _neighbour) { Vector3 r = _currentParticle.getPos() - _neighbour.getPos(); if(r.length() > m_smoothingLength) { return Vector3.zero; } float viscosityKern = -(945/(32*Mathf.PI * Mathf.Pow(m_smoothingLength,9))) * Mathf.Pow(((m_smoothingLength*m_smoothingLength) - (r.length()*r.length())),3) * ((3*(m_smoothingLength*m_smoothingLength)) - 7*(m_smoothingLength*m_smoothingLength)); return r * viscosityKern; }
//---------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------- /// Calculates the forces for a particle using leap frog method of integration /// _currentParticle - The particle we are going to calculate the forces for /// _particleIndex - Vector of all our particles if we want to brut force or the neighbour particles for SPH /// _timeStep - the time step between updates public void calcForces(particle _currentParticle, List<particle> _particleIndex, float _timeStep) { float densityCurrent = 0; var gravitiy = new Vector3 (0f,-9.8f,0f); var pressureGrad = Vector3.zero; var viscosity = Vector3.zero; var viscosityVector = Vector3.zero; var Acceleration = Vector3.zero; for(int i = 0; i<_particleIndex.Count;i++){ // Calculate desity of all particles if(_currentParticle!=_particleIndex[i]){ densityCurrent += _particleIndex[i].getMass()*calcDensityKern(_currentParticle, _particleIndex[i]); } } _currentParticle.setDensity(densityCurrent); var pressTemp = Vector3.zero; var visTemp = Vector3.zero; float pressureCurrent, pressureNeighbour, pressure; for(int i=0; i<_particleIndex.Count;i++) { // Calcualate pressure pressureCurrent = m_gasConstant * (_currentParticle.getDensity() - _currentParticle.getRestDensity()); pressureNeighbour = m_gasConstant * (_particleIndex[i].getDensity() - _particleIndex[i].getRestDensity()); pressure = ((pressureCurrent /(pressureCurrent*pressureCurrent)) + (pressureNeighbour /(pressureNeighbour*pressureNeighbour)))*(_particleIndex[i].getMass()); pressTemp = calcPressureKern(_currentParticle,_particleIndex[i]); //pressTemp*= pressure; //!omfg not an assignment pressureGrad +=(pressTemp); // Calculate viscosiy vector viscosityVector = _particleIndex[i].getVel() - _currentParticle.getVel(); viscosityVector *=(_particleIndex[i].getMass()); viscosityVector /=(_particleIndex[i].getDensity()); // Calculate viscosiy visTemp = calcViscosityKern(_currentParticle,_particleIndex[i]); //visTemp = visTemp.cross(viscosityVector); //!omfg not an assignment viscosity +=(visTemp); } viscosity *=m_viscosityCoefficient; //Calcualate external force if the is one var Forces = Vector3.zero; if(m_externalForceStrenth != 0 && m_externalForceRadius != 0) { Forces = _currentParticle.getPos() - m_externalForcePos; if (Forces.length()<=m_externalForceRadius && Forces.length() > 0) { //find the direction the force is in Forces.Normalize(); //Forces *=((m_externalForceRadius-Forces.length())/m_externalForceRadius); Forces *=(m_externalForceStrenth); if(m_externalForcePush == false) { Forces =-Forces; } } else { Forces.set(0f,0f,0f); } } // Calculate our acceleration Acceleration = gravitiy - pressureGrad + viscosity + Forces; //tweak center //---------------leap frog integration------------------------ //Calculate velocity var VelHalfBack = _currentParticle.getVel() - _currentParticle.getAcc() * _timeStep/2; var VelHalfForward = VelHalfBack + Acceleration * _timeStep; var Velocity = (VelHalfBack + VelHalfForward) * 0.5f; //Calculate our new position var Position = _currentParticle.getPos() + VelHalfForward *(_timeStep); _currentParticle.setVel(Velocity); _currentParticle.setPos(Position); //---------------Debuging---------------- // Debug.Log("the viscosity is "+"["<<viscosity.m_x+","<<viscosity.m_y+","<<viscosity.m_z+"]"); // Debug.Log("the pressure grad is "+"["<<pressureGrad.m_x<<","<<pressureGrad.m_y<<","<<pressureGrad.m_z<<"]"); // Debug.Log("the accelleration is "+"["<<Acceleration.m_x<<","<<Acceleration.m_y<<","<<Acceleration.m_z<<"]"); // Debug.Log("the velocity is "+"["<<Velocity.m_x+","<<Velocity.m_y<<","<<Velocity.m_z<<"]"); }
public void update_new_state(particle a,type_state old_state, type_state new_state){ foreach (int pid in a.linked) { particle p = get_particle_id (pid); pair_st st1 = new pair_st (old_state, p.st); List<pair_part> l; if (pDict.ContainsKey (st1)) { l = pDict [st1]; // Console.WriteLine (st1); l.RemoveAll (x => x.id1 == a.id && x.id2 == p.id); l.RemoveAll (x => x.id2 == a.id && x.id1 == p.id); } pair_st st2 = new pair_st (p.st,old_state); if (pDict.ContainsKey(st2)) { l = pDict [st2]; // Console.WriteLine ("swap:"+st2); l.RemoveAll (x => x.id1 == a.id && x.id2 == p.id); l.RemoveAll (x => x.id2 == a.id && x.id1 == p.id); } // Console.WriteLine ("update :" + a + "O:" + old_state + "N:" + new_state + " " + p); pair_st pst = new pair_st (new_state, p.st); if(!pDict.ContainsKey(pst)) pDict.Add(pst,new List<pair_part>()); var ol = new pair_part (a.id, p.id); pDict [pst].Add (ol); //if (pDict.ContainsKey(ol.swap()) } // Console.WriteLine ("#-" + a.id + " " + stDict [old_state].Count+ " " + old_state); stDict [old_state].Remove (a.id); // Console.WriteLine ("#-" + a.id + " " + stDict [old_state].Count+ " " + old_state); a.st = new_state; if (!stDict.ContainsKey (new_state)) stDict [new_state] = new List<int> (); stDict [new_state].Add (a.id); }
public void link(particle a, particle b){ if (a == null) return; if (b == null) return; a.link (b); pair_st s = new pair_st (a, b); if (!pDict.ContainsKey (s)) { pDict.Add (s, new List<pair_part> ()); } var par = new pair_part(a.id,b.id); pDict [s].Add (par); }
//---------------------------------------------------------------------------------------------------------------------- /// @brief calculates the smoothing kernal for density, used in our SPH equations /// @param _currentParticle the particle we wish to test for /// @param _neighbour the particle we wish to test our _current particle against /// @return the weight of influence the particle has on our _currentParticle protected float calcDensityKern(particle _currentParticle, particle _neighbour) { Vector3 r = _currentParticle.getPos() - _neighbour.getPos(); if(r.length() > m_smoothingLength) { return 0; } float densityKern = (315 / (64 * Mathf.PI * Mathf.Pow(m_smoothingLength, 9))) * Mathf.Pow(((m_smoothingLength * m_smoothingLength) - (r.length() * r.length())), 3); return densityKern; }
public pair_st(particle a, particle b){ this.a = a.st; this.b = b.st; }
//---------------------------------------------------------------------------------------------------------------------- /// @brief calculates the smoothing kernal for density, used in our SPH equations /// @param _currentParticle the particle we wish to test for /// @param _neighbour the particle we wish to test our _current particle against /// @return the weighted pressued gradient the particle has on our _currentParticle protected Vector3 calcPressureKern(particle _currentParticle,particle _neighbour) { Vector3 r = _currentParticle.getPos() - _neighbour.getPos(); if (r.length()>m_smoothingLength) { return Vector3.zero; } float pressureKern = -(945/(32*Mathf.PI * Mathf.Pow(m_smoothingLength,9))) * Mathf.Pow(((m_smoothingLength*m_smoothingLength) - (r.length()*r.length())),3); return r *(pressureKern); }
public void add_particle(particle o){ if (!particles.Contains (o)) { particles.Add (o); ids.Add (o.id, o); if (!stDict.ContainsKey (o.st)) stDict.Add (o.st, new List<int> ()); stDict [o.st].Add (o.id); foreach (int pid in o.linked) { particle p = get_particle_id (pid); pair_st s = new pair_st (o, p); if (!pDict.ContainsKey (s)) { pDict.Add (s, new List<pair_part> ()); } var par = new pair_part (o.id, p.id); pDict [s].Add (par); } } }
public void insert_ensemble(ensemble e){ int s = e.size; particle[] p = new particle[s]; for (int i = 0; i < s; i++) { int ty = e.parts [i].type; int st = e.parts [i].state; p [i] = new particle (ty, st); add_particle (p [i]); } for (int i = 0; i < s; i++) { for (int j = i+1; j < s; j++) { if (e.links [i, j] == 1) { link (p [i], p [j]); } } } }
public override bool apply_rule(reactor rec, Random rd){ particle o = new particle (target_type, target_state); rec.add_particle(o); return true; }
///------------------------------------------------------------------------------------------------- /// @brief A functioin to run through all of the walls and test a selected particle for collision /// @param _testparticle The particle that we wish to test /// @param _timestep used in calculating the friction against the wall public void testCollisionWithWalls(particle _testParticle, float _timeStep) { //for all our walls calculate collision for(int i = 0; i < m_walls.Count; i++) { calculateWallCollision(_testParticle,_timeStep,m_walls[i].plane); } }
public void TestReactor () { reactor rec = new reactor (); /// new configuration tests Assert.AreEqual (0, rec.particles.Count); Assert.AreEqual (0, rec.rules.Count); int t = 0, s = 1; type_state key = new type_state(t,s); particle p = new particle (t, s); rec.add_particle (p); // test add particles Assert.AreEqual (1, rec.particles.Count); Assert.IsTrue(rec.stDict.ContainsKey(key)); Assert.AreEqual (1, rec.stDict [key].Count); // check link -> no links Assert.AreEqual (0, rec.pDict.Count); // double add rec.add_particle (p); Assert.AreEqual (1, rec.particles.Count); // two particles particle q = new particle (t, s); // test add rec.add_particle (q); Assert.AreEqual (2, rec.particles.Count); // test dictionnaries Assert.IsTrue(rec.stDict.ContainsKey(key)); Assert.AreEqual (2, rec.stDict [key].Count); // test remove rec.remove_particle (q); Assert.AreEqual (1, rec.particles.Count); Assert.IsTrue(rec.stDict.ContainsKey(key)); Assert.AreEqual (1, rec.stDict [key].Count); rec.remove_particle (p); Assert.AreEqual (0, rec.particles.Count); Assert.IsTrue(rec.stDict.ContainsKey(key)); Assert.AreEqual (0, rec.stDict [key].Count); // test link rec.add_particle (q); rec.add_particle (p); Assert.AreEqual (2, rec.particles.Count); Assert.IsTrue(rec.stDict.ContainsKey(key)); Assert.AreEqual (2, rec.stDict [key].Count); // create pair part pair_st pst = new pair_st (p, q); pair_st pstn = new pair_st (p.st, q.st); Assert.AreEqual (pst.a, pstn.a); Assert.AreEqual (pst.b, pstn.b); rec.link (p, q); Assert.AreEqual (2, rec.particles.Count); Assert.IsTrue(rec.stDict.ContainsKey(key)); Assert.AreEqual (2, rec.stDict [key].Count); Assert.IsTrue (rec.pDict.ContainsKey (pst)); Assert.AreEqual (1, rec.pDict[pst].Count); }
/// @brief calculates and updates a particles position and velocity depending on if it has a collision with the ground /// @param _testParticle the particle we wish to update /// @param _timeStep the time step between our calcuations, this is used in calculting the friction protected void calculateWallCollision(particle _testParticle, float _timeStep, Vector4 _ground) { Vector3 currentPos = _testParticle.getPos(); Vector3 currentVel = _testParticle.getVel(); float radius = _testParticle.getRadius(); Vector3 Vel = _testParticle.getVel(), Pos = _testParticle.getPos(); Vector3 normal = new Vector3(_ground.x,_ground.y,_ground.z); normal.Normalize(); //Test with ground if ((currentPos.dot(normal) -_ground.w) < radius) { //-----------------Calculate Velocity------------------ //Calculate our new velocity with momentum included Vel = -((currentVel.dot(normal))) * normal + (currentVel - (currentVel.cross(normal).cross(normal))); Vel+= m_coefOfRest * currentVel.dot(normal) * normal; //If moving parallel to our plane decrease speed due to friction unless it has already stopped if(currentVel.sqrMagnitude > 0 && currentVel.dot(normal) == 0) { Vector3 VelNormalized = Vel; VelNormalized.Normalize(); VelNormalized *=(-1); Vector3 friction = new Vector3((1 - normal.x) * VelNormalized.x,(1-normal.y) * VelNormalized.y,(1-normal.z) * VelNormalized.z); friction *=((m_coefOfFric*_timeStep)/(_testParticle.getMass())); if(friction.length()<Vel.length()) { Vel +=(friction); } else if(friction.length()>=Vel.length()) { Vel.set(0,0,0); } } _testParticle.setVel(Vel); //---------------Calculate Position---------------------- //If particle has a velocity which is not parallel to our plane find its new position if(currentVel.length() != 0 || currentVel.dot(normal) != 0) { Vector3 curPosRadius = currentPos - normal * (radius); float t = (_ground.w - normal.dot(curPosRadius)) / normal.dot(normal); Vector3 closestPoint = curPosRadius + normal *(t); if(t > 0 && t < 1) { Pos = closestPoint + normal *(radius); } else { Pos = closestPoint + normal *(radius); } _testParticle.setPos(Pos); } } }
private void ApplyPosition(GameObject go, particle info) { // go.transform.position = info.curPos; // info.prePos = info.curPos; }
public void remove_particle(particle o){ // Console.WriteLine ("delete: " + o+ " " + o.linked.Count); if (particles.Contains (o)) { if (o.linked.Count > 0) { foreach (int r in o.linked) { particle b = get_particle (r); // Console.Write ("d: " + b); unlink (o, b); b.linked.Remove (o.id); } // Console.WriteLine (); } o.linked.Clear (); // Console.WriteLine ("delete: " + o+ " " + o.linked.Count); if (stDict.ContainsKey (o.st)) stDict [o.st].Remove (o.id); ids.Remove(o.id); particles.Remove (o); } }
protected override void SolveInstance(Grasshopper.Kernel.IGH_DataAccess DA) { if (!FriedChiken.isInitialized) { List <GH_Point> pointList = new List <GH_Point>(); DA.GetDataList(0, pointList); bool x = true, y = true, z = true; if (!DA.GetData(1, ref x)) { return; } if (!DA.GetData(2, ref y)) { return; } if (!DA.GetData(3, ref z)) { return; } bool isGroup = true; if (!DA.GetData(4, ref isGroup)) { return; } _nNodes = pointList.Count; mikity.NumericalMethodHelper.particle[] particles = new mikity.NumericalMethodHelper.particle[_nNodes]; for (int i = 0; i < _nNodes; i++) { particles[i] = new particle(pointList[i].Value.X, pointList[i].Value.Y, pointList[i].Value.Z); } pS = new GH_particleSystem(particles); node[] lNodes = new node[_nNodes]; for (int i = 0; i < _nNodes; i++) { lNodes[i] = new node(i); lNodes[i].copyFrom(pS.Value.particles); } if (isGroup) { fixedNodes fN = new fixedNodes(x, y, z); for (int i = 0; i < _nNodes; i++) { fN.addNode(lNodes[i]); } pS.Value.addObject(fN); } else { for (int i = 0; i < _nNodes; i++) { fixedNodes fN = new fixedNodes(x, y, z); fN.addNode(lNodes[i]); pS.Value.addObject(fN); } } lGeometry = new Rhino.Geometry.Point3d[_nNodes]; lGeometry2 = new Rhino.Geometry.Point3d[_nNodes]; for (int i = 0; i < _nNodes; i++) { lGeometry[i] = new Rhino.Geometry.Point3d(particles[i][0], particles[i][1], particles[i][2]); } this.DVPW = GetDVPW(lGeometry); this.BKGT = GetBKGT(lGeometry); pS.DVPW = GetDVPW(lGeometry2); pS.UPGR = GetUPGR(lGeometry2); pS.BKGT = GetBKGT(lGeometry2); } DA.SetData(0, pS); }
protected override void SolveInstance(Grasshopper.Kernel.IGH_DataAccess DA) { if (!DA.GetData(2, ref v)) { return; } if (!FriedChiken.isInitialized) { GH_Point[] pointList = new GH_Point[2]; List <GH_Point> tmpPointList = new List <GH_Point>(); GH_Curve c = new GH_Curve(); int[] nEdgeNodes = new int[_dim]; DA.GetData(0, ref c); DA.GetData(1, ref nEdgeNodes[0]); for (int i = 0; i < _dim; i++) { if (nEdgeNodes[i] < 2) { AddRuntimeMessage(Grasshopper.Kernel.GH_RuntimeMessageLevel.Error, "Integers must be greater than or equal to 2"); return; } } //点群生成 int nNewNodes = nEdgeNodes[0]; nElements = nNewNodes - 1; mikity.NumericalMethodHelper.particle[] particles = new mikity.NumericalMethodHelper.particle[nNewNodes]; for (int i = 0; i < nNewNodes; i++) { Rhino.Geometry.Point3d p = c.Value.PointAt(c.Value.Domain.T0 + (c.Value.Domain.T1 - c.Value.Domain.T0) / nElements * i); particles[i] = new particle(p.X, p.Y, p.Z); } pS = new GH_particleSystem(particles); node[] lNodes = new node[nNewNodes]; for (int i = 0; i < nNewNodes; i++) { lNodes[i] = new node(i); lNodes[i].copyFrom(pS.Value.particles); } nF = new nodalForce(v.X, v.Y, v.Z); for (int i = 0; i < nNewNodes; i++) { nF.addNode(lNodes[i]); } pS.Value.addObject(nF); lGeometry.Clear(); for (int i = 0; i < nNewNodes; i++) { lGeometry.Add(new Rhino.Geometry.Point3d(particles[i][0], particles[i][1], particles[i][2])); } this.DVPW = GetDVPW(lGeometry); pS.DVPW = GetDVPW(lGeometry2); pS.UPGR = GetUPGR(lGeometry2); pS.BKGT = GetBKGT(lGeometry2); } else { nF.forceX = v.X; nF.forceY = v.Y; nF.forceZ = v.Z; } DA.SetData(0, pS); }
protected override void SolveInstance(Grasshopper.Kernel.IGH_DataAccess DA) { double v = -1.0; DA.GetData(2, ref v); if (!FriedChiken.isInitialized) { GH_Point[] pointList = new GH_Point[2]; List <GH_Point> tmpPointList = new List <GH_Point>(); GH_Curve c = new GH_Curve(); int[] nEdgeNodes = new int[_dim]; DA.GetData(0, ref c); DA.GetData(1, ref nEdgeNodes[0]); //cV = new constrainVolumeObject(v); for (int i = 0; i < _dim; i++) { if (nEdgeNodes[i] < 2) { AddRuntimeMessage(Grasshopper.Kernel.GH_RuntimeMessageLevel.Error, "Integers must be greater than or equal to 2"); return; } } //点群生成 int nNewNodes = nEdgeNodes[0]; nElements = nNewNodes - 1; mikity.NumericalMethodHelper.particle[] particles = new mikity.NumericalMethodHelper.particle[nNewNodes]; for (int i = 0; i < nNewNodes; i++) { Rhino.Geometry.Point3d p = c.Value.PointAt(c.Value.Domain.T0 + (c.Value.Domain.T1 - c.Value.Domain.T0) / nElements * i); particles[i] = new particle(p.X, p.Y, p.Z); } pS = new GH_particleSystem(particles); List <mikity.NumericalMethodHelper.elements.isoparametricElement> e = new List <mikity.NumericalMethodHelper.elements.isoparametricElement>(); for (int i = 0; i < nElements; i++) { e.Add(new mikity.NumericalMethodHelper.elements.isoparametricElement(i, i + 1)); } lCV.Clear(); for (int i = 0; i < e.Count; i++) { lCV.Add(new constrainVolumeObject(v / nElements)); lCV[i].addElement(e[i]); pS.Value.addObject(lCV[i]); } lGeometry.Clear(); for (int i = 0; i < nNewNodes; i++) { lGeometry.Add(pS.Value.particles[i, 0], pS.Value.particles[i, 1], pS.Value.particles[i, 2]); } this.DVPW = GetDVPW(lGeometry); pS.DVPW = GetDVPW(lGeometry2); pS.UPGR = GetUPGR(lGeometry2); pS.BKGT = GetBKGT(lGeometry2); } else { if (lCV != null && v > 0) { for (int i = 0; i < lCV.Count; i++) { lCV[i].refVolume = v / nElements; } } } DA.SetData(0, pS); }
public void unlink(particle a, particle b){ if (a == null) return; if (b == null) return; pair_st s = new pair_st (a, b); if (pDict.ContainsKey (s)) { pair_part n = new pair_part (-1, -1); foreach (pair_part l in pDict[s]) { if ((l.id1 == a.id) && (l.id2 == b.id)) { n = l; break; } if ((l.id2 == a.id) && (l.id1 == b.id)) { n = l; break; } } if (n.id1 != -1) pDict [s].Remove(n); } }
protected override void SolveInstance(Grasshopper.Kernel.IGH_DataAccess DA) { if (!FriedChiken.isInitialized) { GH_Point[] pointList = new GH_Point[2]; List <GH_Point> tmpPointList = new List <GH_Point>(); int[] nEdgeNodes = new int[_dim]; DA.GetData(0, ref pointList[0]); DA.GetData(1, ref pointList[1]); DA.GetData(2, ref nEdgeNodes[0]); bool x = true, y = true, z = true; if (!DA.GetData(3, ref x)) { return; } if (!DA.GetData(4, ref y)) { return; } if (!DA.GetData(5, ref z)) { return; } for (int i = 0; i < _dim; i++) { if (nEdgeNodes[i] < 2) { AddRuntimeMessage(Grasshopper.Kernel.GH_RuntimeMessageLevel.Error, "Integers must be greater than or equal to 2"); return; } } //点群生成 double[,] wt = mikity.MathUtil.bicubic(_dim, nEdgeNodes); int nNewNodes = wt.GetLength(0); mikity.NumericalMethodHelper.particle[] particles = new mikity.NumericalMethodHelper.particle[nNewNodes]; for (int i = 0; i < nNewNodes; i++) { particles[i] = new particle(0, 0, 0); for (int j = 0; j < _nNodes; j++) { particles[i][0] += pointList[j].Value.X * wt[i, j]; particles[i][1] += pointList[j].Value.Y * wt[i, j]; particles[i][2] += pointList[j].Value.Z * wt[i, j]; } } pS = new GH_particleSystem(particles); node[] lNodes = new node[nNewNodes]; for (int i = 0; i < nNewNodes; i++) { lNodes[i] = new node(i); lNodes[i].copyFrom(pS.Value.particles); } fixedNodes fN = new fixedNodes(x, y, z); for (int i = 0; i < nNewNodes; i++) { fN.addNode(lNodes[i]); } pS.Value.addObject(fN); lGeometry = new Rhino.Geometry.Point3d[nNewNodes]; for (int i = 0; i < nNewNodes; i++) { lGeometry[i] = new Rhino.Geometry.Point3d(particles[i][0], particles[i][1], particles[i][2]); } } DA.SetData(0, pS); }
//---------------------------------------------------------------------------------------------------------------------- /// @brief If particles are inside each other moves them apart /// @param _testParticle the particle we want to test /// @param _particles our array of particles we wish to test against public void initParticleCollision(particle _testParticle, List<particle> _particles) { Vector3 newPosP, newPosQ, currentPosQ,currentPosP,N; float radius = _testParticle.getRadius(); for (int i=0; i<_particles.Count;i++) { N = _testParticle.getPos() - _particles[i].getPos(); if (N.sqrMagnitude > 0 && N.sqrMagnitude < radius) { currentPosP = _testParticle.getPos(); currentPosQ = _particles[i].getPos(); newPosP = (currentPosP + (N * (0.5f))); newPosQ = (currentPosQ - (N * (0.5f))); _testParticle.setPos(newPosP); _particles[i].setPos(newPosQ); } } }