Help with ropes

Well i'm trying to understand how to work with inverse kinematics, so i serched how to do it and found a code that explain how to make, and i ended up with this:
2020.06.15-16.29.png
So i was trying to use gravity in the rope, and i was coing to try collisions, but i found a problem, when the botton of the rope if free from the static point, the rope gets a strange form.
like this:

//when static:
2020.06.15-16.28.png

//when free
2020.06.15-16.28_01.png

**The code**

//create
GML:
x = room_width/2;
y = room_height/2;



// Arm properties
arm_length = room_height/2-100;
arm_pinned = true;

// Segment properties
seg_amount = 20;
seg_length = arm_length/seg_amount;

//physics properties
weight = 10;

seg_x = [];
seg_y = [];

for(var i = 0; i < seg_amount; i++) {
    seg_x[i] = x + i*seg_length;
    seg_y[i] = y;
}
//step
GML:
var target_x = mouse_x;
var target_y = mouse_y;


seg_x[seg_amount] = target_x;
seg_y[seg_amount] = target_y;

for (var i = seg_amount-1; i >= 0; i--) {

    var dir = point_direction(seg_x[I],seg_y[I],seg_x[i+1],seg_y[i+1]);
 
    var ldx = lengthdir_x(seg_length, dir);
    var ldy = lengthdir_y(seg_length, dir);
 
    seg_x[I] = seg_x[i+1] - ldx;
    seg_y[I] = seg_y[i+1] - ldy;
 
    //add gravity
    seg_y[I] += weight;
 
}




if (arm_pinned) {
 
    seg_x[0] = x;
    seg_y[0] = y;
 
    for (var i = 1; i <= seg_amount; i++) {
        var dir = point_direction(seg_x[i-1], seg_y[i-1], seg_x[I], seg_y[I]);
     
        var ldx = lengthdir_x(seg_length, dir);
        var ldy = lengthdir_y(seg_length, dir);
     
        seg_x[I] = seg_x[i-1] + ldx;
        seg_y[I] = seg_y[i-1] + ldy;
     
    }
}
-Knowing this how can i fix the gravity when the rope is free?
-If i have collisions how could i check? Any ideas?
 
Top