private IEnumerator AttachHook() { Vector3 localHit = curve.transform.InverseTransformPoint(hookAttachment.point); // Procedurally generate the initial rope shape (a simple straight line): curve.controlPoints.Clear(); curve.controlPoints.Add(Vector3.zero); curve.controlPoints.Add(Vector3.zero); curve.controlPoints.Add(localHit); curve.controlPoints.Add(localHit); // Generate the particle representation of the rope (wait until it has finished): yield return(rope.GeneratePhysicRepresentationForMesh()); // Pin both ends of the rope (this enables two-way interaction between character and rope): /*ObiPinConstraintBatch pinConstraints = rope.PinConstraints.GetBatches()[0] as ObiPinConstraintBatch; * pinConstraints.AddConstraint(0,character,transform.localPosition,0); * pinConstraints.AddConstraint(rope.UsedParticles-1,hookAttachment.collider,hookAttachment.collider.transform.InverseTransformPoint(hookAttachment.point),0);*/ // Add the rope to the solver to begin the simulation: rope.AddToSolver(null); rope.GetComponent <MeshRenderer>().enabled = true; attached = true; }
private IEnumerator AttachHook() { yield return(0); Vector3 localHit = rope.transform.InverseTransformPoint(hookAttachment.point); // Procedurally generate the rope path (a simple straight line): blueprint.path.Clear(); blueprint.path.AddControlPoint(Vector3.zero, -localHit.normalized, localHit.normalized, Vector3.up, 0.1f, 0.1f, 1, 1, Color.white, "Hook start"); blueprint.path.AddControlPoint(localHit, -localHit.normalized, localHit.normalized, Vector3.up, 0.1f, 0.1f, 1, 1, Color.white, "Hook end"); blueprint.path.FlushEvents(); // Generate the particle representation of the rope (wait until it has finished): yield return(blueprint.Generate()); // Set the blueprint (this adds particles/constraints to the solver and starts simulating them). rope.ropeBlueprint = blueprint; rope.GetComponent <MeshRenderer>().enabled = true; // Pin both ends of the rope (this enables two-way interaction between character and rope): var pinConstraints = rope.GetConstraintsByType(Oni.ConstraintType.Pin) as ObiConstraints <ObiPinConstraintsBatch>; pinConstraints.Clear(); var batch = new ObiPinConstraintsBatch(); batch.AddConstraint(rope.solverIndices[0], character, transform.localPosition, Quaternion.identity, 0, 0, float.PositiveInfinity); batch.AddConstraint(rope.solverIndices[blueprint.activeParticleCount - 1], hookAttachment.collider.GetComponent <ObiColliderBase>(), hookAttachment.collider.transform.InverseTransformPoint(hookAttachment.point), Quaternion.identity, 0, 0, float.PositiveInfinity); batch.activeConstraintCount = 2; pinConstraints.AddBatch(batch); rope.SetConstraintsDirty(Oni.ConstraintType.Pin); }
void Awake() { // Create both the rope and the solver: rope = gameObject.AddComponent <ObiRope>(); curve = gameObject.AddComponent <ObiCatmullRomCurve>(); 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; rope.UVScale = new Vector2(1, 5); rope.NormalizeV = false; rope.UVAnchor = 1; solver.distanceConstraintParameters.iterations = 15; solver.pinConstraintParameters.iterations = 15; solver.bendingConstraintParameters.iterations = 1; // Add a cursor to change rope length: cursor = rope.gameObject.AddComponent <ObiRopeCursor>(); cursor.rope = rope; cursor.normalizedCoord = 0; cursor.direction = true; }
void Awake() { // Create both the rope and the solver: rope = gameObject.AddComponent <ObiRope>(); curve = gameObject.AddComponent <ObiCatmullRomCurve>(); 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; rope.uvScale = new Vector2(1, 5); rope.normalizeV = false; rope.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; }
public void AttachHookStatic(Transform _target) { //print("Hook to " + _target + " "+ _target.position); rope.GetComponent <MeshRenderer>().enabled = true; targetHandle.transform.position = _target.position; attached = true; }
void Awake() { // Create both the rope and the solver: rope = gameObject.AddComponent <ObiRope>(); ropeRenderer = gameObject.AddComponent <ObiRopeExtrudedRenderer>(); ropeRenderer.section = section; ropeRenderer.uvScale = new Vector2(1, 5); ropeRenderer.normalizeV = false; ropeRenderer.uvAnchor = 1; rope.GetComponent <MeshRenderer>().material = material; // Setup a blueprint for the rope: blueprint = ScriptableObject.CreateInstance <ObiRopeBlueprint>(); blueprint.resolution = 0.5f; // Tweak rope parameters: rope.maxBending = 0.02f; // Add a cursor to be able to change rope length: cursor = rope.gameObject.AddComponent <ObiRopeCursor>(); cursor.cursorMu = 0; cursor.direction = true; }
public IEnumerator MakeRope() { print("Load Extraction Rope"); ropeObject = new GameObject("rope", typeof(ObiSolver), typeof(ObiRope), typeof(ObiCatmullRomCurve), typeof(ObiRopeCursor)); // get references to all components: ObiSolver solver = ropeObject.GetComponent <ObiSolver>(); rope = ropeObject.GetComponent <ObiRope>(); ObiCatmullRomCurve path = ropeObject.GetComponent <ObiCatmullRomCurve>(); //ObiRopeCursor cursor = ropeObject.GetComponent<ObiRopeCursor>(); // set up component references rope.Solver = solver; rope.ropePath = path; rope.section = (ObiRopeSection)Resources.Load("DefaultRopeSection"); rope.GetComponent <MeshRenderer>().material = material; //cursor.rope = rope; // Calculate rope start/end and direction in local space: (plus offset) Vector3 localStart = transform.InverseTransformPoint(chutes[0].transform.position + new Vector3(0, 0.2f, 0)); Vector3 localEnd = transform.InverseTransformPoint(payloads[0].transform.position + new Vector3(0, 0.5f, 0)); Vector3 direction = (localEnd - localStart).normalized; // Generate rope path: path.controlPoints.Clear(); path.controlPoints.Add(localStart - direction); path.controlPoints.Add(localStart); path.controlPoints.Add(localEnd); path.controlPoints.Add(localEnd + direction); //correct thickness, particle resolution, and parenting rope.thickness = 0.025f; rope.resolution = 0.05f; rope.transform.parent = payloads[0].transform; yield return(rope.StartCoroutine(rope.GeneratePhysicRepresentationForMesh())); rope.AddToSolver(null); //extractionCollider = new ObiCollider(); //cargoCollider = new ObiCollider(); BoxCollider extractionBox = chutes[0].AddComponent <BoxCollider>(); extractionBox.size = Vector3.zero; BoxCollider cargoBox = payloads[0].AddComponent <BoxCollider>(); cargoBox.center = new Vector3(0.0f, 0.25f, 0.0f); cargoBox.size = new Vector3(0.5f, 0.5f, 0.5f); extractionCollider = chutes[0].AddComponent <ObiCollider>(); cargoCollider = payloads[0].AddComponent <ObiCollider>(); // remove the constraints from the solver, because we cannot modify the constraints list while the solver is using it. rope.PinConstraints.RemoveFromSolver(null); ObiPinConstraintBatch constraintsBatch = rope.PinConstraints.GetFirstBatch(); constraintsBatch.AddConstraint(0, extractionCollider, Vector3.zero, 0.0f); constraintsBatch.AddConstraint(rope.UsedParticles - 1, cargoCollider, new Vector3(0, 0.5f, -0.25f), 0.0f); rope.PinConstraints.AddToSolver(null); rope.PinConstraints.PushDataToSolver(); }
IEnumerator Setup() { extractionObject = (GameObject)Instantiate(models[0]); heavyCargoObject = (GameObject)Instantiate(models[1]); extractionObject.transform.position = initialExtractionPos; heavyCargoObject.transform.position = initialHeavyCargoPos; start = extractionObject.transform; end = heavyCargoObject.transform; // Get all needed components and interconnect them: rope = GetComponent <ObiRope>(); path = GetComponent <ObiCatmullRomCurve>(); rope.Solver = (ObiSolver)Instantiate(solver); rope.ropePath = path; rope.section = section; GetComponent <MeshRenderer>().material = material; // Calculate rope start/end and direction in local space: (plus offset) Vector3 localStart = transform.InverseTransformPoint(start.position + new Vector3(0, 0.2f, 0)); Vector3 localEnd = transform.InverseTransformPoint(end.position + new Vector3(0, 0.5f, 0)); Vector3 direction = (localEnd - localStart).normalized; // Generate rope path: path.controlPoints.Clear(); path.controlPoints.Add(localStart - direction); path.controlPoints.Add(localStart); path.controlPoints.Add(localEnd); path.controlPoints.Add(localEnd + direction); //correct thickness, particle resolution, and parenting rope.thickness = 0.025f; rope.resolution = 0.05f; rope.transform.parent = heavyCargoObject.transform; // Generate particles and add them to solver: yield return(StartCoroutine(rope.GeneratePhysicRepresentationForMesh())); rope.AddToSolver(null); // Fix first and last particle in place: //rope.invMasses[0] = 0; //rope.invMasses[rope.UsedParticles-1] = 0; //Oni.SetParticleInverseMasses(solver.OniSolver,new float[]{0},1,rope.particleIndices[0]); //Oni.SetParticleInverseMasses(solver.OniSolver,new float[]{0},1,rope.particleIndices[rope.UsedParticles-1]); constraints = rope.GetComponent <ObiPinConstraints>(); constraintsBatch = rope.PinConstraints.GetFirstBatch(); extractionCollider = new ObiCollider(); cargoCollider = new ObiCollider(); BoxCollider extractionBox = extractionObject.AddComponent <BoxCollider>(); extractionBox.size = Vector3.zero; BoxCollider cargoBox = heavyCargoObject.AddComponent <BoxCollider>(); cargoBox.center = new Vector3(0.0f, 0.5f, 0.0f); extractionCollider = extractionObject.AddComponent(typeof(ObiCollider)) as ObiCollider; cargoCollider = heavyCargoObject.AddComponent(typeof(ObiCollider)) as ObiCollider; // remove the constraints from the solver, because we cannot modify the constraints list while the solver is using it. constraints.RemoveFromSolver(null); constraintsBatch.AddConstraint(0, extractionCollider, Vector3.zero, 0.0f); constraintsBatch.AddConstraint(rope.UsedParticles - 1, cargoCollider, Vector3.zero, 0.0f); constraints.AddToSolver(null); constraints.PushDataToSolver(); }