Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Beginner looking for a way to cut rope on swipe
#1
I apologize if this kind of question has been asked before, or if it is too vague. I wish to implement a cutting mechanic in my game where by if the user swipes across a rope, it will cut the rope at the point of touch. I am unsure on how to proceed with this as I am fairly new to coding. If someone could please take the time to explain how to do this, or if there's any existing code for a similar situation I could peruse, it would be highly appreciated. Thank you so much.
Reply
#2
(18-10-2020, 02:28 PM)CuriousJacob Wrote: I apologize if this kind of question has been asked before, or if it is too vague. I wish to implement a cutting mechanic in my game where by if the user swipes across a rope, it will cut the rope at the point of touch. I am unsure on how to proceed with this as I am fairly new to coding. If someone could please take the time to explain how to do this, or if there's any existing code for a similar situation I could peruse, it would be highly appreciated. Thank you so much.

Hi Jacob,

The steps to do this would look roughly like this:

1.- Find the rope element(s) intersected by the swipe.
2.- Call Tear() for each one of these elements.

The most complicated part of this is #1, as there's no ready-made solution and it involves a bit too much math for a beginner. Assuming this is 2D, the basic building block for this is determining whether 2 line segments intersect. You can find quite some info online and implementations in different languages, for instance:
https://www.geeksforgeeks.org/check-if-t...intersect/

Once you have a working segment intersection function, you can do:

Code:
// 1: find out rope elements intersecting the swipe, and put them in a list.
var cutElements = List<ObiStructuralElement>();
foreach(var element in rope.elements)
{
Vector3 elementStart = solver.positions[element.particle1];
Vector3 elementEnd = solver.positions[element.particle2];
if (SegmentsIntersect(elementStart, elementEnd, swipe.start, swipe.end)) //<---your intersection function
    cutElements.Add(element);
}

// 2: tear the elements:
foreach(var element in cutElements)
    rope.Tear(element);

rope.RebuildConstraintsFromElements();
Reply