/// <summary> /// Creates a straight rope anchored to a transform at the top. /// Transform may or may not move around and may or may not have a rigidbody. /// When you call this the rope will appear in the scene and immediately interact with gravity and objects with ObiColliders. /// Called from anywhere (main thread only) /// </summary> public IEnumerator MakeRope(Transform anchoredTo, Vector3 attachmentOffset, float ropeLength) { // create a new GameObject with the required components: a solver, a rope, and a curve. // we also throw a cursor in to be able to change its length. GameObject ropeObject = new GameObject("rope", typeof(ObiSolver), typeof(ObiRope), typeof(ObiCurve), typeof(ObiRopeCursor)); // get references to all components: rope = ropeObject.GetComponent <ObiRope>(); cursor = ropeObject.GetComponent <ObiRopeCursor>(); solver = ropeObject.GetComponent <ObiSolver>(); ObiCurve path = ropeObject.GetComponent <ObiCurve>(); // set up component references (see ObiRopeHelper.cs) rope.Solver = solver; rope.ropePath = path; //rope.section = Resources.Load<ObiRopeSection>("DefaultRopeSection"); // set path control points (duplicate end points, to set curvature as required by CatmullRom splines): path.controlPoints.Clear(); path.controlPoints.Add(new ObiCurve.ControlPoint(Vector3.zero, Vector3.up)); path.controlPoints.Add(new ObiCurve.ControlPoint(Vector3.zero, Vector3.up)); path.controlPoints.Add(new ObiCurve.ControlPoint(Vector3.down * ropeLength, Vector3.up)); path.controlPoints.Add(new ObiCurve.ControlPoint(Vector3.down * ropeLength, Vector3.up)); rope.pooledParticles = 2000; // parent the rope to the anchor transform: rope.transform.SetParent(anchoredTo, false); rope.transform.localPosition = attachmentOffset; // generate particles/constraints and add them to the solver (see ObiRopeHelper.cs) yield return(rope.StartCoroutine(rope.GeneratePhysicRepresentationForMesh())); rope.AddToSolver(null); // get the last particle in the rope at its rest state. pinnedParticle = rope.UsedParticles - 1; // add a tethers batch: ObiTetherConstraintBatch tetherBatch = new ObiTetherConstraintBatch(true, false, 0, 1); rope.TetherConstraints.AddBatch(tetherBatch); //UpdateTethers(); // fix first particle in place (see http://obi.virtualmethodstudio.com/tutorials/scriptingparticles.html) rope.Solver.invMasses[rope.particleIndices[0]] = rope.invMasses[0] = 0; }
void Awake() { ObiCurve path = GetComponent <ObiCurve>(); path.controlPoints.Clear(); float ang = 0; float height = 0; for (int i = 0; i < points; ++i) { Vector3 point = new Vector3(Mathf.Cos(ang) * radius, height, Mathf.Sin(ang) * radius); // optimal handle length for circle approximation: 4/3 tan(pi/(2n)) Vector3 tangent = new Vector3(-point.z, heightStep, point.x).normalized *(4.0f / 3.0f) * Mathf.Tan(radialStep / 4.0f) * radius; path.controlPoints.Add(new ObiCurve.ControlPoint(point, Vector3.up, -tangent, tangent, ObiCurve.ControlPoint.BezierCPMode.Mirrored)); ang += radialStep; height += heightStep; } }
void Awake() { // Create both the rope and the solver: rope = gameObject.AddComponent <ObiRope>(); renderer = gameObject.AddComponent <ObiRopeExtrudedRenderer>(); curve = gameObject.AddComponent <ObiCurve>(); solver = gameObject.AddComponent <ObiSolver>(); // Provide a solver and a curve: rope.Solver = solver; rope.ropePath = curve; rope.GetComponent <MeshRenderer>().material = material; // Configure rope and solver parameters: rope.resolution = 0.1f; rope.BendingConstraints.stiffness = 0.2f; renderer.section = section; renderer.uvScale = new Vector2(1, 5); renderer.normalizeV = false; renderer.uvAnchor = 1; solver.substeps = 3; solver.distanceConstraintParameters.iterations = 5; solver.pinConstraintParameters.iterations = 5; solver.bendingConstraintParameters.iterations = 1; solver.particleCollisionConstraintParameters.enabled = false; solver.volumeConstraintParameters.enabled = false; solver.densityConstraintParameters.enabled = false; solver.stitchConstraintParameters.enabled = false; solver.skinConstraintParameters.enabled = false; solver.tetherConstraintParameters.enabled = false; // Add a cursor to be able to change rope length: cursor = rope.gameObject.AddComponent <ObiRopeCursor>(); cursor.normalizedCoord = 0; cursor.direction = true; }