// Can have a one that has a range to keep in public void ApplyLengthConstraint2D(MegaSoft2D soft) { if (active && soft.applyConstraints) { //calculate direction Vector2 direction = soft.masses[p2].pos - soft.masses[p1].pos; //calculate current length float currentLength = direction.magnitude; //check for zero vector //if ( direction != Vector3.zero ) if (currentLength != 0.0f) //direction.x != 0.0f || direction.y != 0.0f || direction.y != 0.0f ) { //normalize direction vector //direction.Normalize(); direction.x /= currentLength; direction.y /= currentLength; //move to goal positions Vector2 moveVector = 0.5f * (currentLength - length) * direction; soft.masses[p1].pos.x += moveVector.x; soft.masses[p1].pos.y += moveVector.y; soft.masses[p2].pos.x += -moveVector.x; soft.masses[p2].pos.y += -moveVector.y; //soft.masses[p1].pos += moveVector; //soft.masses[p2].pos += -moveVector; } } }
// Can have a one that has a range to keep in public void ApplyLengthConstraint2D(MegaSoft2D soft) { if ( active && soft.applyConstraints ) { //calculate direction Vector2 direction = soft.masses[p2].pos - soft.masses[p1].pos; //calculate current length float currentLength = direction.magnitude; //check for zero vector //if ( direction != Vector3.zero ) if ( currentLength != 0.0f ) //direction.x != 0.0f || direction.y != 0.0f || direction.y != 0.0f ) { //normalize direction vector //direction.Normalize(); direction.x /= currentLength; direction.y /= currentLength; //move to goal positions Vector2 moveVector = 0.5f * (currentLength - length) * direction; soft.masses[p1].pos.x += moveVector.x; soft.masses[p1].pos.y += moveVector.y; soft.masses[p2].pos.x += -moveVector.x; soft.masses[p2].pos.y += -moveVector.y; //soft.masses[p1].pos += moveVector; //soft.masses[p2].pos += -moveVector; } } }
// apply this constraint to a verlet (usually the one that owns this constraint). void Satisfy(MegaSoft2D soft) { // Get vector from other verlet to me Vector2 to_me = soft.masses[p1].pos - soft.masses[p2].pos; Vector2 mid = (soft.masses[p1].pos + soft.masses[p2].pos) / 2.0f; // handle case where points are the same if (to_me.magnitude == 0.0f) { to_me = new Vector2(1.0f, 0.0f); } float radius = to_me.magnitude; if (radius < min) { radius = min; } if (radius > max) { radius = max; } // Scale it to the required radius to_me = radius * to_me.normalized; // and set the point soft.masses[p2].pos = mid - to_me / 2.0f; }
public void ApplyAngleConstraint(MegaSoft2D soft) { //Vector2 d1 = soft.masses[p1].pos - soft.masses[p1 + 1].pos; //Vector2 d2 = soft.masses[p1 + 2].pos - soft.masses[p1 + 1].pos; //float ang = Vector2.Dot(d1, d2); }
public override void Apply(MegaSoft2D soft) { if (active) { soft.masses[p1].pos = pos; } }
public void ApplyPointConstraint2D(MegaSoft2D soft) { if (active) { soft.masses[p1].pos = pos; } }
public void Apply(MegaSoft2D soft) { switch ( contype ) { case 0: ApplyLengthConstraint2D(soft); break; case 1: ApplyPointConstraint2D(soft); break; //case 2: ApplyPointTargetConstraint2D(soft); break; } }
public Spring2D(int _p1, int _p2, float _ks, float _kd, MegaSoft2D mod) { p1 = _p1; p2 = _p2; ks = _ks; kd = _kd; restLen = Vector2.Distance(mod.masses[p1].pos, mod.masses[p2].pos); len = restLen; }
public void Apply(MegaSoft2D soft) { switch (contype) { case 0: ApplyLengthConstraint2D(soft); break; case 1: ApplyPointConstraint2D(soft); break; //case 2: ApplyPointTargetConstraint2D(soft); break; } }
Vector2 GetForce(MegaSoft2D soft) //CVerletPoint* p_verlet) { Vector2 to_me = soft.masses[p1].pos - soft.masses[p2].pos; // handle case where points are the same if (to_me.magnitude < 0.000001) { to_me = new Vector2(1.0f, 0.0f); } Vector2 mid = soft.masses[p2].pos + to_me.normalized * mid; Vector2 to_mid = mid - soft.masses[p1].pos; return(to_mid * force); // --- ???? }
// Should have a softbody lib public void doCalculateSpringForce(MegaSoft2D mod) { Vector2 deltaP = mod.masses[p1].pos - mod.masses[p2].pos; float dist = deltaP.magnitude; //VectorLength(&deltaP); // Magnitude of deltaP float Hterm = (dist - restLen) * ks; // Ks * (dist - rest) Vector2 deltaV = mod.masses[p1].vel - mod.masses[p2].vel; float Dterm = (Vector2.Dot(deltaV, deltaP) * kd) / dist; // Damping Term Vector2 springForce = deltaP * (1.0f / dist); springForce *= -(Hterm + Dterm); mod.masses[p1].force += springForce; mod.masses[p2].force -= springForce; }
public void doCalculateSpringForce1(MegaSoft2D mod) { //get the direction vector Vector2 direction = mod.masses[p1].pos - mod.masses[p2].pos; //check for zero vector if (direction != Vector2.zero) { //get length float currLength = direction.magnitude; //normalize direction = direction.normalized; //add spring force Vector2 force = -ks * ((currLength - restLen) * direction); //add spring damping force //float v = (currLength - len) / mod.timeStep; //force += -kd * v * direction; //apply the equal and opposite forces to the objects mod.masses[p1].force += force; mod.masses[p2].force -= force; len = currLength; } }
public override void Apply(MegaSoft2D soft) { if (active) { //calculate direction soft.masses[p1].pos = obj.position; Vector2 direction = soft.masses[p2].pos - soft.masses[p1].pos; //calculate current length float currentLength = direction.magnitude; //check for zero vector if (direction != Vector2.zero) { //normalize direction vector direction.Normalize(); //move to goal positions Vector2 moveVector = 1.0f * (currentLength - length) * direction; //soft.masses[p1].pos += moveVector; soft.masses[p2].pos += -moveVector; } } }
public void ApplyPointConstraint2D(MegaSoft2D soft) { if ( active ) soft.masses[p1].pos = pos; }
public override void Apply(MegaSoft2D soft) { if ( active ) { //calculate direction soft.masses[p1].pos = obj.position; Vector2 direction = soft.masses[p2].pos - soft.masses[p1].pos; //calculate current length float currentLength = direction.magnitude; //check for zero vector if ( direction != Vector2.zero ) { //normalize direction vector direction.Normalize(); //move to goal positions Vector2 moveVector = 1.0f * (currentLength - length) * direction; //soft.masses[p1].pos += moveVector; soft.masses[p2].pos += -moveVector; } } }
public override void Apply(MegaSoft2D soft) { if ( active ) soft.masses[p1].pos = pos; }
public void doCalculateSpringForce1(MegaSoft2D mod) { //get the direction vector Vector2 direction = mod.masses[p1].pos - mod.masses[p2].pos; //check for zero vector if ( direction != Vector2.zero ) { //get length float currLength = direction.magnitude; //normalize direction = direction.normalized; //add spring force Vector2 force = -ks * ((currLength - restLen) * direction); //add spring damping force //float v = (currLength - len) / mod.timeStep; //force += -kd * v * direction; //apply the equal and opposite forces to the objects mod.masses[p1].force += force; mod.masses[p2].force -= force; len = currLength; } }
public void doCalculateSpringForce2(MegaSoft2D mod) { Vector2 deltaP = mod.masses[p1].pos - mod.masses[p2].pos; float dist = deltaP.magnitude; //VectorLength(&deltaP); // Magnitude of deltaP float Hterm = (dist - restLen) * ks; // Ks * (dist - rest) //Vector2 deltaV = mod.masses[p1].vel - mod.masses[p2].vel; float v = (dist - len); // / mod.timeStep; float Dterm = (v * kd) / dist; // Damping Term Vector2 springForce = deltaP * (1.0f / dist); springForce *= -(Hterm + Dterm); mod.masses[p1].force += springForce; mod.masses[p2].force -= springForce; }
// apply this constraint to a verlet (usually the one that owns this constraint). void Satisfy(MegaSoft2D soft) { // Get vector from other verlet to me Vector2 to_me = soft.masses[p1].pos - soft.masses[p2].pos; Vector2 mid = (soft.masses[p1].pos + soft.masses[p2].pos) / 2.0f; // handle case where points are the same if ( to_me.magnitude == 0.0f ) { to_me = new Vector2(1.0f, 0.0f); } float radius = to_me.magnitude; if ( radius < min) radius = min; if ( radius > max) radius = max; // Scale it to the required radius to_me = radius * to_me.normalized; // and set the point soft.masses[p2].pos = mid - to_me / 2.0f; }
public void ApplyAngleConstraint(MegaSoft2D soft) { }
//CVerletPoint* p_verlet) Vector2 GetForce(MegaSoft2D soft) { Vector2 to_me = soft.masses[p1].pos - soft.masses[p2].pos; // handle case where points are the same if ( to_me.magnitude < 0.000001) { to_me = new Vector2(1.0f, 0.0f); } Vector2 mid = soft.masses[p2].pos + to_me.normalized * mid; Vector2 to_mid = mid - soft.masses[p1].pos; return to_mid * force; // --- ???? }