Created
July 14, 2018 12:43
-
-
Save ditzel/ae6ebc115d767da9a5a1e1f70dee27e5 to your computer and use it in GitHub Desktop.
Rope Physics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
public class DistanceJoint3D : MonoBehaviour { | |
public Transform ConnectedRigidbody; | |
public bool DetermineDistanceOnStart = true; | |
public float Distance; | |
public float Spring = 0.1f; | |
public float Damper = 5f; | |
protected Rigidbody Rigidbody; | |
void Awake() | |
{ | |
Rigidbody = GetComponent<Rigidbody>(); | |
} | |
void Start() | |
{ | |
if (DetermineDistanceOnStart && ConnectedRigidbody != null) | |
Distance = Vector3.Distance(Rigidbody.position, ConnectedRigidbody.position); | |
} | |
void FixedUpdate() | |
{ | |
var connection = Rigidbody.position - ConnectedRigidbody.position; | |
var distanceDiscrepancy = Distance - connection.magnitude; | |
Rigidbody.position += distanceDiscrepancy * connection.normalized; | |
var velocityTarget = connection + (Rigidbody.velocity + Physics.gravity * Spring); | |
var projectOnConnection = Vector3.Project(velocityTarget, connection); | |
Rigidbody.velocity = (velocityTarget - projectOnConnection) / (1 + Damper * Time.fixedDeltaTime); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class FrictionJoint3D : MonoBehaviour { | |
[Range(0,1)] | |
public float Friction; | |
protected Rigidbody Rigidbody; | |
void Awake() | |
{ | |
Rigidbody = GetComponent<Rigidbody>(); | |
} | |
void FixedUpdate() | |
{ | |
Rigidbody.velocity = Rigidbody.velocity * (1 - Friction); | |
Rigidbody.angularVelocity = Rigidbody.angularVelocity * (1 - Friction); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class RopeRoot : MonoBehaviour { | |
public float RigidbodyMass = 1f; | |
public float ColliderRadius = 0.1f; | |
public float JointSpring = 0.1f; | |
public float JointDamper = 5f; | |
public Vector3 RotationOffset; | |
public Vector3 PositionOffset; | |
protected List<Transform> CopySource; | |
protected List<Transform> CopyDestination; | |
protected static GameObject RigidBodyContainer; | |
void Awake() | |
{ | |
if(RigidBodyContainer == null) | |
RigidBodyContainer = new GameObject("RopeRigidbodyContainer"); | |
CopySource = new List<Transform>(); | |
CopyDestination = new List<Transform>(); | |
//add children | |
AddChildren(transform); | |
} | |
private void AddChildren(Transform parent) | |
{ | |
for (int i = 0; i < parent.childCount; i++) | |
{ | |
var child = parent.GetChild(i); | |
var representative = new GameObject(child.gameObject.name); | |
representative.transform.parent = RigidBodyContainer.transform; | |
//rigidbody | |
var childRigidbody = representative.gameObject.AddComponent<Rigidbody>(); | |
childRigidbody.useGravity = true; | |
childRigidbody.isKinematic = false; | |
childRigidbody.freezeRotation = true; | |
childRigidbody.mass = RigidbodyMass; | |
//collider | |
var collider = representative.gameObject.AddComponent<SphereCollider>(); | |
collider.center = Vector3.zero; | |
collider.radius = ColliderRadius; | |
//DistanceJoint | |
var joint = representative.gameObject.AddComponent<DistanceJoint3D>(); | |
joint.ConnectedRigidbody = parent; | |
joint.DetermineDistanceOnStart = true; | |
joint.Spring = JointSpring; | |
joint.Damper = JointDamper; | |
joint.DetermineDistanceOnStart = false; | |
joint.Distance = Vector3.Distance(parent.position, child.position); | |
//add copy source | |
CopySource.Add(representative.transform); | |
CopyDestination.Add(child); | |
AddChildren(child); | |
} | |
} | |
public void Update() | |
{ | |
for (int i = 0; i < CopySource.Count; i++) | |
{ | |
CopyDestination[i].position = CopySource[i].position + PositionOffset; | |
CopyDestination[i].rotation = CopySource[i].rotation * Quaternion.Euler(RotationOffset); | |
} | |
} | |
} |
How is the "RotationOffset" being calculated? The rope appears as a zig zag without segments being rotated. If the segments are very small the zig zag may be hard to see.
Hello. Where should the three rows be placed ?
please make another video again and this time give us the 3d model too so we dont need to make it so size issues will not be there and please this time how to use the scripts where to attach them and how to modify if it startst to fly tell these too
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you! One other question: Can the ropes collide with the environment and with each other? And can they be attached to rigid bodies? thanks