/// <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); } } } }