Obi Official Forum
Help Rope extension, vibration and collision - Printable Version

+- Obi Official Forum (https://obi.virtualmethodstudio.com/forum)
+-- Forum: Obi Users Category (https://obi.virtualmethodstudio.com/forum/forum-1.html)
+--- Forum: Obi Rope (https://obi.virtualmethodstudio.com/forum/forum-4.html)
+--- Thread: Help Rope extension, vibration and collision (/thread-4479.html)



Rope extension, vibration and collision - 0hsyn1 - 19-04-2025

Hello,
I am developing a project using the system in your Hook project as a reference. I can lengthen and shorten the rope, but the rope has problems interacting with colliders, as far as I understand the particle count is low. How can I increase this? If my method is wrong, please tell me.
When I wrap it around an obstacle, the rope vibrates.

Example of the project I am trying to make: https://play.google.com/store/apps/details?id=com.dalakgames.ziplinevalley&pli=1

Point A is fixed, point B is moveable.

Thank you Sonrisa

[attachment=2325][attachment=2326][attachment=2327]
Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Obi;
using System;

public class RopeCreate : MonoBehaviour
{
 

    [Header("Obi")]
    [SerializeField] Transform ropeEnd;
    public ObiSolver solver;
    public ObiCollider2D character;
    public float hookExtendRetractSpeed = 2;
    public Material material;
    public ObiRopeSection section;

    private ObiRope rope;
    private ObiRopeBlueprint blueprint;
    private ObiRopeExtrudedRenderer ropeRenderer;

    private ObiRopeCursor cursor;

    private RaycastHit hookAttachment;

    public Rigidbody2D characterRb;
    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;
    }

    private void Start()
    {
      

        if (!rope.isLoaded)
            LaunchHook();

        Debug.Log(GetRopeLength());
    }

    private void OnDestroy()
    {
        DestroyImmediate(blueprint);
    }

    /**
     * Raycast against the scene to see if we can attach the hook to something.
     */
    private void LaunchHook()
    {


        StartCoroutine(AttachHook());


    }

    private IEnumerator AttachHook()
    {

        yield return null;

        Vector3 startLocal = transform.position;
        Vector3 endLocal = ropeEnd.position;
        Vector3 direction = (endLocal - startLocal).normalized;

       
        blueprint.path.Clear();
        blueprint.path.AddControlPoint(startLocal, -direction, direction, Vector3.up, 0.1f, 0.1f, 1, 1, Color.white, "Hook start");
        blueprint.path.AddControlPoint(endLocal, -direction, direction, Vector3.up, 0.1f, 0.1f, 1, 1, Color.white, "Hook end");
        blueprint.path.FlushEvents();

       
        yield return blueprint.Generate();

        rope.ropeBlueprint = blueprint;
        rope.GetComponent<MeshRenderer>().enabled = true;

       
        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],
                            ropeEnd.GetComponent<ObiColliderBase>(),
                            //ropeEnd.position,
                            //ropeEnd.InverseTransformPoint(ropeStart.position),
                            Vector3.zero,
                            Quaternion.identity, 0, 0, float.PositiveInfinity);

        batch.activeConstraintCount = 2;
        pinConstraints.AddBatch(batch);

        rope.SetConstraintsDirty(Oni.ConstraintType.Pin);
    } 

  
    public void RopeIncrease()
    {
        cursor.ChangeLength(rope.restLength + hookExtendRetractSpeed * Time.deltaTime);
    }
    public void RopeDecrease()
    {
        cursor.ChangeLength(rope.restLength - hookExtendRetractSpeed * Time.deltaTime );

    }

    public float GetStrain()
    {
        float strain = rope.restLength / rope.CalculateLength();
        return strain;
    }

    public float GetRopeLength()
    {
        return rope.CalculateLength();
    }
}

Code:
public class CapsuleMove : MonoBehaviour
{
    [SerializeField]RopeCreate ropeCreate;
    [Header("Move")]
    const float speed = 500f;
   
    private Vector3 mOffset;
    private float mZCoord;
    private bool active;
    private Rigidbody2D rb;
    float startRotation;

    [SerializeField] public bool isRopeLengthMax = false;

    public Vector2 startPos;

    private Vector2 previousPosition;
    private void Start()
    {
        rb = GetComponent<Rigidbody2D>();

      
    }

    private void OnMouseDown()
    {
       

        mZCoord = Camera.main.WorldToScreenPoint(transform.position).z;
        mOffset = transform.position - GetMouseAsWorldPoint();
        active = true;
        rb.velocity = Vector2.zero;
        rb.gravityScale = 0f;
       
        rb.constraints &= ~RigidbodyConstraints2D.FreezePositionX;
        rb.constraints &= ~RigidbodyConstraints2D.FreezePositionY;

    }

    private void OnMouseUp()
    {
        MouseDownEnded();
    }
    private void MouseDownEnded()
    {
        active = false;
        rb.gravityScale = 1f;
        rb.velocity = Vector2.zero;
        // rb.freezeRotation = true;
        rb.constraints = RigidbodyConstraints2D.FreezePositionX | RigidbodyConstraints2D.FreezePositionY | RigidbodyConstraints2D.FreezeRotation;       
    }

    private Vector3 GetMouseAsWorldPoint()
    {
        Vector3 mousePoint = Input.mousePosition;
        mousePoint.z = mZCoord;
        return Camera.main.ScreenToWorldPoint(mousePoint);
    }

    private void FixedUpdate()
    {
       
        if (isRopeLengthMax)
        {
            MouseDownEnded();

            return;
        }
        if (active)
        {

            Vector2 desiredPosition = (Vector2)(GetMouseAsWorldPoint() + mOffset);
            Vector2 direction = desiredPosition - rb.position;
            rb.velocity = direction.magnitude > 0.1f ? direction * Time.fixedDeltaTime * speed : Vector2.zero;

           

            float distanceMoved = Vector2.Distance(rb.position, previousPosition);
            if (distanceMoved < 0.05f)
            {
                return;
            }

            if (ropeCreate.GetStrain() > .9)
            {
                ropeCreate.RopeDecrease();
            }
            else
            {
                ropeCreate.RopeIncrease();
               // Debug.Log("Lenght" + ropeCreate.GetRopeLength());
            }

            previousPosition = rb.position;

        }
    }
}



RE: Rope extension, vibration and collision - josemendez - 19-04-2025

(19-04-2025, 08:00 AM)0hsyn1 Wrote: Hello,
I am developing a project using the system in your Hook project as a reference. I can lengthen and shorten the rope, but the rope has problems interacting with colliders, as far as I understand the particle count is low. How can I increase this?

Hi,

Increase the rope blueprint's resolution, which in your code is currently set to 0.5f. See the manual for details:
https://obi.virtualmethodstudio.com/manual/7.0/ropesetup.html

You can also enable surface collisions. See:
https://obi.virtualmethodstudio.com/manual/7.0/surfacecollisions.html

kind regards,


RE: Rope extension, vibration and collision - 0hsyn1 - 19-04-2025

(19-04-2025, 09:09 AM)josemendez Wrote: Hi,

Increase the rope blueprint's resolution, which in your code is currently set to 0.5f. See the manual for details:
https://obi.virtualmethodstudio.com/manual/7.0/ropesetup.html

You can also enable surface collisions. See:
https://obi.virtualmethodstudio.com/manual/7.0/surfacecollisions.html

kind regards,
Thanks for your quick reply, I was able to increase the number of particles.

But when I check the collision section and start wrapping the rope around the obstacle, vibrations occur. Can particles be affected by each other?