public virtual void CopyToSpringBody(JelloBody oldBody, JelloSpringBody newBody)
    {
        newBody.affectedByGravity    = oldBody.affectedByGravity;
        newBody.disabled             = oldBody.disabled;
        newBody.gravity              = oldBody.gravity;
        newBody.IsAwake              = oldBody.IsAwake;
        newBody.IsKinematic          = oldBody.IsKinematic;
        newBody.IsStatic             = false;
        newBody.IsTrigger            = oldBody.IsTrigger;
        newBody.overrideWorldGravity = oldBody.overrideWorldGravity;
        newBody.pivotOffset          = oldBody.pivotOffset;
        newBody.setComponentReferences();
        newBody.polyCollider = oldBody.polyCollider;

        if (oldBody.meshLink != null)
        {
            newBody.meshLink      = oldBody.meshLink;
            newBody.meshLink.body = newBody;
        }

        if (oldBody.Shape != null)
        {
            newBody.setShape(oldBody.Shape, shapeSettingOptions);
            newBody.Mass = oldBody.Mass != Mathf.Infinity ? oldBody.Mass : 1f;
        }

        for (int i = 0; i < oldBody.EdgePointMassCount; i++)
        {
            newBody.setEdgePointMass(oldBody.getEdgePointMass(i), i);
            newBody.getEdgePointMass(i).body = newBody;

            if (oldBody.Mass == Mathf.Infinity)
            {
                newBody.getEdgePointMass(i).Mass = 1f;
            }
        }
        for (int i = 0; i < oldBody.InternalPointMassCount; i++)
        {
            newBody.setInternalPointMass(oldBody.getInternalPointMass(i), i);
            newBody.getInternalPointMass(i).body = newBody;

            if (oldBody.Mass == Mathf.Infinity)
            {
                newBody.getInternalPointMass(i).Mass = 1f;
            }
        }
        for (int i = 0; i < oldBody.AttachPointCount; i++)
        {
            newBody.AddAttachPoint(oldBody.GetAttachPoint(i));
            newBody.GetAttachPoint(i).body = newBody;
        }
        for (int i = 0; i < oldBody.JointCount; i++)
        {
            newBody.AddJoint(oldBody.GetJoint(i));
            newBody.GetJoint(i).bodyA = newBody;
        }

        EditorUtility.SetDirty(newBody);
    }
	/// <summary>
	/// Grabs the point mass.
	/// </summary>
	public void GrabPointMass()
	{
		//i am now pulling the body.
		pulling = true;
	
		//clear old values from last pull
		adjacentPointMasses.Clear();
		oldMultipliers.Clear();

		//set the position to lock it in place while pulling the body
		position = body.Position;

		//get the closest point mass to the mouse positions and cache some information about it.
		mousePosInWorld = Camera.main.ScreenToWorldPoint(Input.mousePosition);
		pmIndex = body.getClosestPointMass(mousePosInWorld, false); //only grab edge point masses
		pointmass = body.getEdgePointMass(pmIndex);
		oldMultipliers.Add (pointmass.shapeMatchingMultiplier);
		pointmass.shapeMatchingMultiplier = 0f;
		adjacentPointMasses.Add (pointmass);

		//Set the body to kinematic to keep it in place while pulling.
		body.IsKinematic = true; 
		
		int adjIndex = 0;
		int numFromStart = 0;
		//grab adjacent point masses further in the array.
		for(int i = pmIndex; i < pmIndex + numAdjacentPoints; i++)
		{
			adjIndex = i > body.EdgePointMassCount - 1 ? i - body.EdgePointMassCount : i;
			
			if(adjIndex > body.EdgePointMassCount - 1 || adjacentPointMasses.Contains(body.getEdgePointMass(adjIndex)))
				continue;

			//cache the point mass info and modify the shape matching values by how close it is to the grabbed point mass
			adjacentPointMasses.Add (body.getEdgePointMass(adjIndex));
			oldMultipliers.Add(body.getEdgePointMass(adjIndex).shapeMatchingMultiplier);
			numFromStart++;
			body.getEdgePointMass(adjIndex).shapeMatchingMultiplier = numFromStart / (numAdjacentPoints + 1 + adjacentDropoff);
		}
		numFromStart = 0;
		//grab adjacent point masses before the current index in the array.
		for(int i = pmIndex; i > pmIndex - numAdjacentPoints; i--)
		{
			adjIndex = i < 0 ? i + body.EdgePointMassCount: i;
			if(adjIndex < 0 || adjacentPointMasses.Contains(body.getEdgePointMass(adjIndex)))
				continue;

			//cache the point mass info and modify the shape matching values by how close it is to the grabbed point mass
			adjacentPointMasses.Add (body.getEdgePointMass(adjIndex));
			oldMultipliers.Add(body.getEdgePointMass(adjIndex).shapeMatchingMultiplier);
			numFromStart++;
			body.getEdgePointMass(adjIndex).shapeMatchingMultiplier = numFromStart / (numAdjacentPoints + 1 + adjacentDropoff);
		}
		//find any internal point masses connected to the selected point mass (via spring) and cache/modify its shape matching multiplier.
		for(int i = 0; i < body.SpringCount; i++)
		{
			if(body.getSpring(i).pointMassA == pmIndex || body.getSpring(i).pointMassB == pmIndex)
			{
				if(body.getSpring(i).pointMassA >= body.EdgePointMassCount)
				{
					adjacentPointMasses.Add (body.getPointMass(body.getSpring(i).pointMassA));
					oldMultipliers.Add (body.getPointMass(body.getSpring(i).pointMassA).shapeMatchingMultiplier);
					body.getPointMass(body.getSpring(i).pointMassA).shapeMatchingMultiplier = 1 / (2 + adjacentDropoff);
				}
				else if(body.getSpring(i).pointMassB >= body.EdgePointMassCount)
				{
					adjacentPointMasses.Add (body.getPointMass(body.getSpring(i).pointMassB));
					oldMultipliers.Add (body.getPointMass(body.getSpring(i).pointMassB).shapeMatchingMultiplier);
					body.getPointMass(body.getSpring(i).pointMassB).shapeMatchingMultiplier = 1 / (2 + adjacentDropoff);
				}
			}
		}
	}
Esempio n. 3
0
    public virtual void CopyToSpringBody(JelloBody oldBody, JelloSpringBody newBody)
    {
        newBody.affectedByGravity = oldBody.affectedByGravity;
        newBody.disabled = oldBody.disabled;
        newBody.gravity = oldBody.gravity;
        newBody.IsAwake = oldBody.IsAwake;
        newBody.IsKinematic = oldBody.IsKinematic;
        newBody.IsStatic = false;
        newBody.IsTrigger = oldBody.IsTrigger;
        newBody.overrideWorldGravity = oldBody.overrideWorldGravity;
        newBody.pivotOffset = oldBody.pivotOffset;
        newBody.setComponentReferences();
        newBody.polyCollider = oldBody.polyCollider;

        if(oldBody.meshLink != null)
        {
            newBody.meshLink = oldBody.meshLink;
            newBody.meshLink.body = newBody;
        }

        if(oldBody.Shape != null)
        {
            newBody.setShape(oldBody.Shape, shapeSettingOptions);
            newBody.Mass = oldBody.Mass != Mathf.Infinity ? oldBody.Mass : 1f;
        }

        for(int i = 0; i < oldBody.EdgePointMassCount; i++)
        {
            newBody.setEdgePointMass(oldBody.getEdgePointMass(i), i);
            newBody.getEdgePointMass(i).body = newBody;

            if(oldBody.Mass == Mathf.Infinity)
                newBody.getEdgePointMass(i).Mass = 1f;
        }
        for(int i = 0; i < oldBody.InternalPointMassCount; i++)
        {
            newBody.setInternalPointMass(oldBody.getInternalPointMass(i), i);
            newBody.getInternalPointMass(i).body = newBody;

            if(oldBody.Mass == Mathf.Infinity)
                newBody.getInternalPointMass(i).Mass = 1f;
        }
        for(int i = 0; i < oldBody.AttachPointCount; i++)
        {
            newBody.AddAttachPoint(oldBody.GetAttachPoint(i));
            newBody.GetAttachPoint(i).body = newBody;
        }
        for(int i = 0; i < oldBody.JointCount; i++)
        {
            newBody.AddJoint(oldBody.GetJoint(i));
            newBody.GetJoint(i).bodyA = newBody;
        }

        EditorUtility.SetDirty(newBody);
    }