diff options
author | Neil Kollack <nkollack@gmail.com> | 2022-04-12 21:20:24 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-04-12 21:20:24 -0500 |
commit | f2a710c190ba95de184dcbd600a548f954675f60 (patch) | |
tree | ef880db26e6fd2fdd29eafc54a854c174e856aff /Assets/Scripts | |
parent | fb2703b646d22bd4502fab6f0ca58cb922d1ed2c (diff) | |
parent | 92c76d6d42512953c80e4250344f6694d9c4de56 (diff) |
Merge pull request #2 from MontanaJohns/wip-never-let-this-see-the-light-of-day-rope-prototype-DistanceHinge+simulated
rope prototype distance hinge+simulated
Diffstat (limited to 'Assets/Scripts')
-rw-r--r-- | Assets/Scripts/RopePrototype.cs | 133 | ||||
-rw-r--r-- | Assets/Scripts/RopePrototype.cs.meta | 11 | ||||
-rw-r--r-- | Assets/Scripts/RopeThrowPrototype.cs | 37 | ||||
-rw-r--r-- | Assets/Scripts/RopeThrowPrototype.cs.meta | 11 |
4 files changed, 192 insertions, 0 deletions
diff --git a/Assets/Scripts/RopePrototype.cs b/Assets/Scripts/RopePrototype.cs new file mode 100644 index 0000000..b2f37ab --- /dev/null +++ b/Assets/Scripts/RopePrototype.cs @@ -0,0 +1,133 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class RopePrototype : MonoBehaviour +{ + public int constraintIterations = 50; + public float gravityMultiplier = 1.0f; + public float lineWidth = 0.1f; + public float ropeSegmentLength = 0.25f; + public int segmentCount = 35; + + LineRenderer lr; + DistanceJoint2D dj; + GameObject player; + List<RopeSegment> ropeSegments = new List<RopeSegment>(); + Vector3[] ropePositions; + bool ropeCreated = false; + + // Start is called before the first frame update + void Start() + { + player = GameObject.FindGameObjectWithTag("Player"); + dj = GetComponent<DistanceJoint2D>(); + lr = GetComponent<LineRenderer>(); + lr.startWidth = lineWidth; + lr.endWidth = lineWidth; + ropePositions = new Vector3[segmentCount]; + lr.positionCount = segmentCount; + Vector2 ropeLoc = transform.position; + + for (int i = 0; i < segmentCount; i++) + { + ropeSegments.Add(new RopeSegment(ropeLoc)); + ropeLoc.y -= ropeSegmentLength; + } + } + + // Update is called once per frame + void Update() + { + if (!ropeCreated) + { + CreateRope(); + ropeCreated = true; + } + RenderLine(); + Simulate(); + } + + void RenderLine() + { + for(int i = 0; i < ropePositions.Length; i++) + { + ropePositions[i] = ropeSegments[i].posNow; + } + lr.SetPositions(ropePositions); + } + + void CreateRope() + { + dj.connectedBody = player.GetComponent<Rigidbody2D>(); + dj.maxDistanceOnly = true; + dj.distance = Vector2.Distance(player.transform.position, transform.position); + } + + void Simulate() + { + Vector2 gravityForce = new Vector2(0f, -gravityMultiplier); + + for(int i = 0; i < ropeSegments.Count; i++) + { + RopeSegment segment = ropeSegments[i]; + Vector2 velocity = segment.posNow - segment.posOld; + segment.posOld = segment.posNow; + segment.posNow += velocity; + segment.posNow += gravityForce * Time.deltaTime; + ropeSegments[i] = segment; + } + + for(int i = 0; i < constraintIterations; i++) + { + ApplyContraint(); + } + } + + void ApplyContraint() + { + RopeSegment endSegment1 = ropeSegments[0]; + endSegment1.posNow = transform.position; + ropeSegments[0] = endSegment1; + + RopeSegment endSegment2 = ropeSegments[ropeSegments.Count - 1]; + endSegment2.posNow = player.transform.position; + ropeSegments[ropeSegments.Count - 1] = endSegment2; + + for (int i = 0; i < ropeSegments.Count-1; i++) + { + RopeSegment segment1 = ropeSegments[i]; + RopeSegment segment2 = ropeSegments[i + 1]; + + float distance = (segment1.posNow - segment2.posNow).magnitude; + float error = distance - ropeSegmentLength; + Vector2 normalChange = (segment1.posNow - segment2.posNow).normalized; + Vector2 change = normalChange * error; + + if(i != 0) + { + segment1.posNow -= change * 0.5f; + ropeSegments[i] = segment1; + segment2.posNow += change * 0.5f; + ropeSegments[i + 1] = segment2; + } + else + { + segment2.posNow += change; + ropeSegments[i + 1] = segment2; + } + } + } + + public struct RopeSegment + { + public Vector2 posNow; + public Vector2 posOld; + + public RopeSegment(Vector2 pos) + { + this.posNow = pos; + this.posOld = pos; + } + } +} diff --git a/Assets/Scripts/RopePrototype.cs.meta b/Assets/Scripts/RopePrototype.cs.meta new file mode 100644 index 0000000..4ce3825 --- /dev/null +++ b/Assets/Scripts/RopePrototype.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: f828efbe27e65524c9d7e0d87b6a6f0f +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/RopeThrowPrototype.cs b/Assets/Scripts/RopeThrowPrototype.cs new file mode 100644 index 0000000..d828cca --- /dev/null +++ b/Assets/Scripts/RopeThrowPrototype.cs @@ -0,0 +1,37 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class RopeThrowPrototype : MonoBehaviour +{ + public GameObject hook; + public bool ropeExists = false; + + GameObject currentHook; + + // Start is called before the first frame update + void Start() + { + + } + + // Update is called once per frame + void Update() + { + if (Input.GetMouseButtonDown(0)) + { + if (!ropeExists) + { + Vector2 clickLocation = Camera.main.ScreenToWorldPoint(Input.mousePosition); + + currentHook = (GameObject)Instantiate(hook, clickLocation, Quaternion.identity); + ropeExists = true; + } + else + { + Destroy(currentHook); + ropeExists = false; + } + } + } +} diff --git a/Assets/Scripts/RopeThrowPrototype.cs.meta b/Assets/Scripts/RopeThrowPrototype.cs.meta new file mode 100644 index 0000000..3fccef5 --- /dev/null +++ b/Assets/Scripts/RopeThrowPrototype.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 4ddff3a67736d7a42a22a7590fc5b028 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: |