Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help  Some questions & requests
#6
(06-08-2022, 02:59 PM)landosilva Wrote: I don’t think it is something with my code because the new ropes are fine, the problem is with the ones coming from the pool, and they pass through exactly the same code. For some reason the positions are not properly set.

Thanks! I understand the problem now. How are you getting ropes out of the pool? I've quickly tried a pooling scheme but it works fine for me, here's the pool script:

Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Obi;

public class RopePool : MonoBehaviour
{
    public List<GameObject> pool;
    public GameObject source;

    public Transform originTransform;
    public Transform endTransform;

    void Start()
    {
        pool = new List<GameObject>();
        GameObject tmp;

        for (int i = 0; i < 5; i++)
        {
            tmp = Instantiate(source);
            tmp.transform.parent = source.transform.parent;
            tmp.SetActive(false);
            pool.Add(tmp);
        }
    }

    GameObject GetPooledRope()
    {
        for (int i = 0; i < 5; i++)
        {
            if (!pool[i].activeInHierarchy)
            {
                return pool[i];
            }
        }
        return null;
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            var obj = GetPooledRope();
            if (obj != null)
            {
                obj.SetActive(true);
                var rope = obj.GetComponent<ObiRope>();
                var attach = obj.GetComponent<RopeAttach>();

                attach.SetStartPosition(rope.solver.transform.InverseTransformPoint(originTransform.position));
                attach.SetLastPosition(rope.solver.transform.InverseTransformPoint(endTransform.position));
                attach.Connect(originTransform, endTransform);
            }
        }
    }    
}

And the script to attach the rope:

Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Obi;

public class RopeAttach : MonoBehaviour
{
    ObiRope rope;
    public ObiParticleAttachment attachment1;
    public ObiParticleAttachment attachment2;

    void Awake()
    {
        rope = GetComponent<ObiRope>();
    }

    public void Connect(Transform origin, Transform end)
    {
        attachment1.target = origin;
        attachment2.target = end;
    }

    public void SetStartPosition(Vector3 position)
    {
        int firstParticle = rope.elements[0].particle1;
        rope.solver.positions[firstParticle] = position;
    }

    public void SetLastPosition(Vector3 position)
    {
        int lastParticle = rope.elements[rope.elements.Count - 1].particle2;
        rope.solver.positions[lastParticle] = position;
    }
}

The only thing I think could be causing trouble is passing the positions to SetStartPosition and SetLastPosition, all actor properties in Obi are expressed in solver space (the solver's local space, that is) so if you're passing world space positions and moving the solver in-between adding ropes, a problem similar to the one you describe would take place. You need to pass positions expressed in solver space, you can use InverseTransformPoint as I do in the code above. See:
http://obi.virtualmethodstudio.com/manua...icles.html

Quote:Each ObiSolver has an array for each one of these properties, that stores the current data for all particles in use by any actor managed by the solver. All spatial properties (positions, orientations, velocities, vorticities, etc) are expressed in the solver's local space.
Reply


Messages In This Thread
Some questions & requests - by landosilva - 04-08-2022, 10:58 PM
RE: Some questions & requests - by josemendez - 05-08-2022, 07:57 AM
RE: Some questions & requests - by landosilva - 05-08-2022, 03:34 PM
RE: Some questions & requests - by josemendez - 05-08-2022, 04:17 PM
RE: Some questions & requests - by landosilva - 06-08-2022, 02:59 PM
RE: Some questions & requests - by josemendez - 08-08-2022, 07:44 AM
RE: Some questions & requests - by landosilva - 08-08-2022, 09:46 AM
RE: Some questions & requests - by josemendez - 08-08-2022, 10:54 AM
RE: Some questions & requests - by landosilva - 08-08-2022, 02:09 PM
RE: Some questions & requests - by josemendez - 08-08-2022, 02:24 PM
RE: Some questions & requests - by landosilva - 08-08-2022, 02:56 PM
RE: Some questions & requests - by josemendez - 08-08-2022, 03:21 PM
RE: Some questions & requests - by landosilva - 08-08-2022, 03:44 PM
RE: Some questions & requests - by josemendez - 08-08-2022, 03:46 PM
RE: Some questions & requests - by landosilva - 09-08-2022, 04:51 PM
RE: Some questions & requests - by josemendez - 10-08-2022, 07:18 AM
RE: Some questions & requests - by landosilva - 10-08-2022, 10:03 AM
RE: Some questions & requests - by josemendez - 10-08-2022, 10:15 AM
RE: Some questions & requests - by josemendez - 10-08-2022, 10:39 AM