void verletParticle(int start, int count) { //return; int index = start; for (int i = 0; i < count; i++) { Particle2D pp = base.sim.getParticle(index); if (pp.IsFree) { //lock(pp) { Vector2 tempv2 = pp.Position; pp.Position += (pp.Position - pp.PositionOld) * base.sim.Settings.damping; pp.PositionOld = tempv2; //} } index++; } }
void TraverseBinaryTreeForCircle(BinaryTree branch) { if (branch.branchA != null && branch.branchB != null) { if (branch.depth < this.minTraverseDepth) { TraverseBinaryTreeForCircle(branch.branchA); TraverseBinaryTreeForCircle(branch.branchB); } else { searchCount++; if (branch.boundingCircle.Overlaps(this.targetPos, this.cc.radius)) { TraverseBinaryTreeForCircle(branch.branchA); TraverseBinaryTreeForCircle(branch.branchB); } } } else if (branch.branchA == null && branch.branchB == null) { Vector2 dir; searchCount++; if (branch.boundingCircle.OverlapsResults(this.targetPos, this.cc.radius, out dir)) { if (isDebugCollidingOn) { branch.boundingCircle.DebugDraw(transform.localToWorldMatrix, branch.depth, Color.magenta); } //apply collision Particle2D pp = sim.getParticle(branch.leafIndex); pp.Position -= dir * leafForceFeedback; dir = transform.TransformDirection(dir); targetRb2D.AddForce(dir * targetForceFeedback, ForceMode2D.Force); } } }
// Original Verlet: // xi+1 = xi + (xi - xi-1) + a * dt * dt // Time-Corrected Verlet: // xi+1 = xi + (xi - xi-1) * (dti / dti-1) + a * dti * dti void verlet() { for (int i = 0; i < base.sim.numberOfParticles(); i++) { p = base.sim.getParticle(i); if (p.IsFree) { //This is a simplified version temp = p.Position; p.Position += (p.Position - p.PositionOld) * setting.damping; p.PositionOld = temp; /* Different versions * temp = p.Position; * //p.Position = p.Position + (p.Position - p.PositionOld); * //p.Position = p.Position + (p.Position - p.PositionOld) * s.damping * (t / dt) + p.Force / p.Mass * t * t; //With force,mass and time-corrected * //p.Position = p.Position + (p.Position - p.PositionOld) * s.damping + p.Force; * p.Position += (p.Position - p.PositionOld) * s.damping; * p.PositionOld = temp; * //dt = t;//dt is the previous deltaTime */ } } }
public Spring2D(Simulation sim, Particle2D a, Particle2D b, float restLength) : this(sim, sim.getParticleIndex(a), sim.getParticleIndex(b), restLength) { }
/// <summary> /// Calculates the convergence ID of the springs and angles that are connected to particle specified by particleIndex /// </summary> /// <param name="particleIndex">Particle index.</param> private void CalcConvergenceID(int particleIndex) { Particle2D pp = this.getParticle(particleIndex); //Spring List <Spring2D> sp = new List <Spring2D> (10); //the conencted spring for particle index int springNum = this.numberOfSprings(); //get the connected spring for (int i = 0; i < springNum; i++) { Spring2D s = this.getSpring(i); Particle2D a = s.ParticleA; Particle2D b = s.ParticleB; if (a == pp || b == pp) { sp.Add(s); } } //calc the convergence id for the springs which connects to this particle List <int> IDinUse = new List <int> (10); for (int i = 0; i < sp.Count; i++) { if (sp[i].convergenceGroupID != 0) { IDinUse.Add(sp[i].convergenceGroupID); } } for (int i = 0; i < sp.Count; i++) { if (sp[i].convergenceGroupID == 0) { int id = 1; while (IDinUse.Contains(id)) { id++; } sp[i].convergenceGroupID = id; IDinUse.Add(id); if (id > this.maxSpringConvergenceID) { this.maxSpringConvergenceID = id; } } } //angle List <AngleConstraint2D> ag = new List <AngleConstraint2D> (10); int angleNum = this.numberOfAngleConstraints(); //get the connected angles for (int i = 0; i < angleNum; i++) { AngleConstraint2D angle = this.getAngleConstraint(i); Particle2D b = angle.ParticleB; Particle2D m = angle.ParticleM; if (b == pp || m == pp) { ag.Add(angle); } } //recalc the convergence id for the angles that are connected to this particle IDinUse.Clear(); for (int i = 0; i < ag.Count; i++) { if (ag[i].convergenceGroupID != 0) { IDinUse.Add(ag[i].convergenceGroupID); } } for (int i = 0; i < ag.Count; i++) { if (ag[i].convergenceGroupID == 0) { int id = 1; while (IDinUse.Contains(id)) { id++; } ag[i].convergenceGroupID = id; IDinUse.Add(id); if (id > this.maxAngleConvergenceID) { this.maxAngleConvergenceID = id; } } } }
bool ContainParticle(Particle2D p) { return(particleA == p || particleB == p || particleM == p); }
public float distanceSquaredTo(Particle2D p) { return((this.Position - p.Position).sqrMagnitude); }
public float distanceTo(Particle2D p) { return((this.Position - p.Position).magnitude); }