Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help  Attaching gameobjects after tear up
#5
Yes I attached first cylinder as you mentioned. I used two attachments.

To do the same thing in code, I create two attachment and particleGroup for each seperated ropes. If element's particle1 = 6 and particle2 = 7 then after tear up left rope's last particle would be 6 and second rope's first particle would be newly created particle, lets say 10.

So I create two attachment for particle 5,6 and two attachment for 10,7 and I create particleGroup for each attachment. My code looks like this.
Code:
public void AddNewSolidModel((int, int) particlesleft, (int, int) particlesRight)
{
    var leftGo = Instantiate(solidModel);
    AttachSolidModel(particlesleft, "Left", leftGo);

    var rightGo = Instantiate(solidModel);
    AttachSolidModel(particlesRight, "Right", rightGo);
}

private void AttachSolidModel((int, int) particles, string groupName, GameObject go)
{
    go.transform.position = rope.solver.positions[particles.Item1];
    InstantiateAttachments(particles.Item1, go.transform, groupName + "1");
    InstantiateAttachments(particles.Item2, go.transform, groupName + "2");
}

private void InstantiateAttachments(int particle, Transform target, string groupName)
{
    var group = ScriptableObject.CreateInstance<ObiParticleGroup>();
    group.particleIndices.Add(particle);
    group.name = groupName;

    var particleAttachment = gameObject.AddComponent<ObiParticleAttachment>();
    particleAttachment.particleGroup = group;
    particleAttachment.attachmentType = ObiParticleAttachment.AttachmentType.Dynamic;
    particleAttachment.target = target;
}
Code:
private void ScreenSpaceCut(Vector2 lineStart, Vector2 lineEnd)
{
    // keep track of whether the rope was cut or not.
    bool cut = false;
    (int, int) particlesBefore = (-1, -1);
    (int, int) particlesAfter = (-1, -1);

    // iterate over all elements and test them for intersection with the line:
    for (int i = 0; i < rope.elements.Count; ++i)
    {
        // project the both ends of the element to screen space.
        Vector3 screenPos1 = cam.WorldToScreenPoint(rope.solver.positions[rope.elements[i].particle1]);
        Vector3 screenPos2 = cam.WorldToScreenPoint(rope.solver.positions[rope.elements[i].particle2]);

        // test if there's an intersection:
        if (SegmentSegmentIntersection(screenPos1, screenPos2, lineStart, lineEnd, out float r, out float s))
        {
            particlesBefore = (rope.elements[i].particle1, rope.elements[i].particle2);
            cut = true;
            rope.Tear(rope.elements[i]);
            particlesAfter = (rope.elements[i].particle1, rope.elements[i].particle2);
        }
    }

    // If the rope was cut at any point, rebuilt constraints:
    if (cut)
    {
        rope.RebuildConstraintsFromElements();
        obiTest.AddNewSolidModel(particlesBefore, particlesAfter);
    }
}
Here is ss of created attachments after cut:
https://drive.google.com/file/d/13z00AwX...sp=sharing
Here is new recorded video:
https://drive.google.com/file/d/1QqmHQR7...sp=sharing
For this video I add little delay before attaching gameobjects:
https://drive.google.com/file/d/1hvtCFxs...sp=sharing

Edit:
I was attaching gameobject to rope after I cut it. I needed to wait one frame to attach objects.
Code:
[Button("Tear")]
public void TearCable()
{
    rope.Tear(rope.elements[elementIndex]);
    rope.RebuildConstraintsFromElements();
    StartCoroutine(AddModelAfterCutEnumerator());
}

private IEnumerator AddModelAfterCutEnumerator()
{
    yield return new WaitForEndOfFrame();
    var element = rope.elements[elementIndex - 1];
    AddNewSolidModel((element.particle1, element.particle2));
}
Reply


Messages In This Thread
RE: Attaching gameobjects after tear up - by Ferhat123 - 25-07-2024, 07:28 PM