I have been working for a few weeks on creating a rope simulation for a project in Godot. No in engine features had the functionality I wanted, so I’ve been trying to create it myself, using verlet integration to create the rope behavior.
However, the collision handling is what I’ve been having issues with. I’m currently looking for solutions to get my rope simulation to collide with stability, being able to rest and slide off of surfaces with little jittering. Currently, my code is vaguely functional, but the rope does not rest in place on a surface and it is constantly clipping and bouncing on the edge of the surface it’s colliding with. I’m not looking for extreme accuracy, just stability, performance, and being generally believable.
for contact_index in range(0, contact_points.size(), 2):
var contact_1 = p.position
var contact_2 = contact_points[contact_index + 1]
var contact_axis = contact_2 - contact_1
var movement_float = (p.radius * 1.05) - contact_axis.length()
var percentage = abs(movement_float) / contact_axis.length()
var movement_vector = contact_axis * percentage
var new_pos = p.position - (movement_vector * 5)
point_positions[point] = new_pos
point_prev_positions[point] = contact_2
Currently the program finds all the collision points in a separate method and then sends them to the above loop. contact_1 is the position of the colliding point (An Area2D with CollisionShape2D) and contact_2 is the point on the surface it is colliding with. point_positions and point_prev_positions are the collection of Vector2 positions that represent the points on the rope. The body_shape_entered and body_shape_exited signals are what I’m using for collision detection.
I’ve tried moving the points along the vector the exact distance away to leave the colliding body, However, this does not function as I want. It results in the points remaining inside the body and continuing to jitter and adjust itself, because it is being affected by gravity which pulls it right back into the body if it is above it.
There are some constants that are being used in the code. They have just been attempts to adjust the distance moved by different amounts on collision. They aren’t necessarily representative of anything else in the program.
I have done a lot of research on vector math and collision handling for this project, but I understand that my knowledge is still amateur, so feel free to send me any resources that you think would be helpful. Thanks!
coopy c is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.