public static void CopyTentSegments(Tentacle source, Tentacle dest) { dest.ClearSegments(); dest.segments = source.segments; dest.startWidth = source.startWidth; dest.endWidth = source.endWidth; dest.length = source.length; dest.GenerateSegments(); for (int i = 0; i < dest.tentJoints.Count; i++) { TentJoint destJoint = dest.tentJoints[i]; TentJoint sourceJoint = source.tentJoints[i]; destJoint.transform.position = dest.transform.position + source.transform.InverseTransformPoint(sourceJoint.transform.position); destJoint.gameObject.transform.position = dest.transform.position + source.transform.InverseTransformPoint(sourceJoint.gameObject.transform.position); //destJoint.gameObject.GetComponent<StretchTo>().RecalculateStart(); //destJoint.gameObject.GetComponent<StretchTo>().RunStretch(); //destJoint.lastPosition = sourceJoint.lastPosition; //destJoint.velocity = sourceJoint.velocity; ////public List<Vector2> targetPositions; //destJoint.tOffset = sourceJoint.tOffset; } //dest.t = source.t; }
void ForwardPass() { for (int i = tentJoints.Count - 1; i >= 0; i--) { //for (int i = 0; i < tentJoints.Count; i++) { TentJoint thisJoint = tentJoints[i]; Vector2 tailPoint = thisJoint.nextPoint.position; Vector2 thisPoint = thisJoint.transform.position; //float pull = (float)(segments - i) / (float)segments; //float taut = 0.99f; //float pull = (Mathf.Sin(WiggleTime + thisJoint.tOffset) + 1.0f) * 0.5f; //float s = segmentLength - segmentLength * pull * taut; float s = segmentLength; ////float d = Mathf.Min(s, Distance(headPoint, thisPoint)); ////Debug.Log(string.Format("s: {0}, d: {1}",s,d)); if (thisJoint.TailLength() > s) { float theta = CommonUtils.ThetaBetween(tailPoint, thisPoint); Vector2 newPos = new Vector2(tailPoint.x + Mathf.Cos(theta) * s, tailPoint.y + Mathf.Sin(theta) * s); thisJoint.targetPositions.Add(newPos); //joint.position.x = headPoint.x + Mathf.Cos(theta) * s; //joint.position.y = headPoint.y + Mathf.Sin(theta) * s; } else { thisJoint.targetPositions.Add(thisJoint.transform.position); } } }
public void GenerateSegments() { segmentLength = length / (float)segments; Transform emptyGameObject = new GameObject("Segments").transform; emptyGameObject.SetParent(transform); emptyGameObject.localPosition = Vector2.zero; for (int i = 0; i < segments; i++) { GameObject newSegment = new GameObject(string.Format("Joint {0}", i)); newSegment.transform.SetParent(emptyGameObject); newSegment.transform.localPosition = new Vector3(i * 0.1f, i * 0.01f, -0.001f); TentJoint newJoint = newSegment.AddComponent <TentJoint>(); newJoint.Initialize(this, i); tentJoints.Add(newJoint); } int o = 0; baseSegment = Object.Instantiate(pack.tentacleSprites[o], emptyGameObject); baseSegment.transform.parent = emptyGameObject; baseSegment.name = "Base Segment " + transform.name; baseSegment.transform.position = transform.position; baseSegment.transform.localScale = new Vector3(1.0f, startWidth, 1); baseSegment.GetComponent <StretchTo>().targetPoint = tentJoints[0].gameObject.transform; //baseSegment.transform.SetParent(basePoint); //baseSegment.transform.localPosition = Vector3.zero; //baseSegment.GetComponent<StretchTo>().RecalculateStart(); segmentsGenerated = true; }
public static void CopyTentSubSegments(Tentacle source, Tentacle dest, int start, int end) { //if (start < 0 || end > source.segments) { // throw new Exception("Can't make a sub segment with these bounds."); //} dest.ClearSegments(); dest.segments = end - start + 1; dest.pack = source.pack; dest.alternateSprites = source.alternateSprites; float width_dif = source.endWidth - source.startWidth; dest.startWidth = source.startWidth + width_dif * ((float)start / (float)source.segments); dest.endWidth = source.startWidth + width_dif * ((float)end / (float)source.segments); dest.length = source.length * ((float)dest.segments / (float)source.segments); dest.GenerateSegments(); if (start > 0) { dest.transform.position = source.tentJoints[start - 1].transform.position; } else { dest.transform.position = source.transform.position; } if (end < source.segments - 1) { dest.endPoint.position = source.tentJoints[end].transform.position; } else { dest.endPoint.position = source.endPoint.position; } dest.baseSegment.transform.localPosition = Vector2.zero; for (int i = 0; i < dest.tentJoints.Count; i++) { TentJoint destJoint = dest.tentJoints[i]; TentJoint sourceJoint = source.tentJoints[i + start]; destJoint.transform.position = sourceJoint.transform.position; //destJoint.transform.position = dest.transform.position + source.transform.InverseTransformPoint(sourceJoint.transform.position); //destJoint.gameObject.transform.position = dest.transform.position + source.transform.InverseTransformPoint(sourceJoint.gameObject.transform.position); //destJoint.lastPosition = sourceJoint.lastPosition; //destJoint.velocity = sourceJoint.velocity; ////public List<Vector2> targetPositions; //destJoint.tOffset = sourceJoint.tOffset; } //dest.AdjustJoints(); //dest.t = source.t; }
public void TeleportToBase() { for (int i = 0; i < tentJoints.Count; i++) { TentJoint joint = tentJoints[i]; joint.transform.position = transform.position; //joint.lastPosition = basePoint.transform.position; joint.velocity = Vector2.zero; } }
public void UpdateTentSegmentNextPointMaybe(TentJoint tentJoint) { int i = tentJoint.index; if (i > 0) { tentJoints[i - 1].nextPoint = tentJoint.transform; tentJoints[i - 1].stretchTo.targetPoint = tentJoint.transform; tentJoints[i - 1].stretchTo.RecalculateStart(); } if (i == segments - 1) { tentJoint.stretchTo.targetPoint = endPoint.transform; tentJoint.stretchTo.RecalculateStart(); } }
void WigglePass() { float t = CommonUtils.Distance(transform.position, endPoint.transform.position) / length; t = Mathf.Min(t, 1.0f); TentSlope = CommonUtils.NormalVector(transform.position, endPoint.position); for (int i = 0; i < tentJoints.Count; i++) { TentJoint thisJoint = tentJoints[i]; float w = Mathf.Sin(WiggleTime + thisJoint.tOffset) * wiggleStrength * t; v = thisJoint.Position; v.x += w * TentSlope.y; v.y -= w * TentSlope.x; thisJoint.targetPositions.Add(v); } }
void BackwardPass() { for (int i = 0; i < tentJoints.Count; i++) { TentJoint thisJoint = tentJoints[i]; Vector2 headPoint = thisJoint.lastPoint.position; float s = segmentLength; if (thisJoint.HeadLength() > s) { float theta = CommonUtils.ThetaBetween(headPoint, thisJoint.transform.position); Vector2 newPos = new Vector2(headPoint.x + Mathf.Cos(theta) * s, headPoint.y + Mathf.Sin(theta) * s); thisJoint.targetPositions.Add(newPos); } else { thisJoint.targetPositions.Add(thisJoint.transform.position); } } }