Exemplo n.º 1
0
    // 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;
            }
        }
    }
Exemplo n.º 2
0
	// 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;
			}
		}
	}
Exemplo n.º 3
0
    // 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;
    }
Exemplo n.º 4
0
    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);
    }
Exemplo n.º 5
0
 public override void Apply(MegaSoft2D soft)
 {
     if (active)
     {
         soft.masses[p1].pos = pos;
     }
 }
Exemplo n.º 6
0
 public void ApplyPointConstraint2D(MegaSoft2D soft)
 {
     if (active)
     {
         soft.masses[p1].pos = pos;
     }
 }
Exemplo n.º 7
0
 public void Apply(MegaSoft2D soft)
 {
     switch ( contype )
     {
         case 0:	ApplyLengthConstraint2D(soft);	break;
         case 1: ApplyPointConstraint2D(soft); break;
         //case 2: ApplyPointTargetConstraint2D(soft); break;
     }
 }
Exemplo n.º 8
0
 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;
 }
Exemplo n.º 9
0
    public void Apply(MegaSoft2D soft)
    {
        switch (contype)
        {
        case 0: ApplyLengthConstraint2D(soft);  break;

        case 1: ApplyPointConstraint2D(soft); break;
            //case 2: ApplyPointTargetConstraint2D(soft); break;
        }
    }
Exemplo n.º 10
0
    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);         // --- ????
    }
Exemplo n.º 11
0
    // 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;
    }
Exemplo n.º 12
0
    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;
        }
    }
Exemplo n.º 13
0
    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;
            }
        }
    }
Exemplo n.º 14
0
 public void ApplyPointConstraint2D(MegaSoft2D soft)
 {
     if ( active )
         soft.masses[p1].pos = pos;
 }
Exemplo n.º 15
0
    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;
            }
        }
    }
Exemplo n.º 16
0
 public override void Apply(MegaSoft2D soft)
 {
     if ( active )
         soft.masses[p1].pos = pos;
 }
Exemplo n.º 17
0
 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;
 }
Exemplo n.º 18
0
    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;
        }
    }
Exemplo n.º 19
0
    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;
    }
Exemplo n.º 20
0
    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);
    }
Exemplo n.º 21
0
    // 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;
    }
Exemplo n.º 22
0
 public void ApplyAngleConstraint(MegaSoft2D soft)
 {
 }
Exemplo n.º 23
0
    //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;  // --- ????
    }