25-07-2024, 05:28 PM
Thank you for your answer.
I tried your suggestion. I'm encountering a strange situation as shown in the video I shared.
https://drive.google.com/file/d/1Bd3HLS5...sp=sharing
As shown in the video, there is a yellow cylinder that is already attached. After cutting, I attach one cylinder to the end of the rope fixed to the wall and another cylinder to the beginning of the falling piece.
Here is my code:
I tried your suggestion. I'm encountering a strange situation as shown in the video I shared.
https://drive.google.com/file/d/1Bd3HLS5...sp=sharing
As shown in the video, there is a yellow cylinder that is already attached. After cutting, I attach one cylinder to the end of the rope fixed to the wall and another cylinder to the beginning of the falling piece.
Here is my code:
Code:
private void ScreenSpaceCut(Vector2 lineStart, Vector2 lineEnd)
{
// keep track of whether the rope was cut or not.
bool cut = false;
int leftParticles = -1;
int rightParticle = -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))
{
leftParticles = rope.elements[i].particle1;
rightParticle = rope.elements[i].particle2;
cut = true;
rope.Tear(rope.elements[i]);
}
}
// If the rope was cut at any point, rebuilt constraints:
if (cut)
{
rope.RebuildConstraintsFromElements();
obiTest.AddNewSolidModel(leftParticles, rightParticle);
}
}
Code:
public void AddNewSolidModel(int leftParticle, int rightParticle)
{
AttachSolidModel(leftParticle, "Left");
AttachSolidModel(rightParticle, "Right");
}
private void AttachSolidModel(int particle, string groupName)
{
var go = Instantiate(solidModel);
go.transform.position = rope.solver.positions[particle];
go.transform.rotation = transform.rotation;
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 = go.transform;
}