08-08-2022, 07:44 AM
(This post was last modified: 08-08-2022, 07:46 AM by josemendez.)
(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.