Following up on this question, I have created a snakelike enemy which consists of a head followed by segments. I get the segments to follow the head by constraining each segment's position relative to the segment in front of it. I get the difference between the actual distances between the segments and the desired distance, and correct that error 30% per frame.
The GDScript code looks like this:
for segment in [segment1, segment2, segment3, segment4, segment5, segment6]:
# get actual distance between segment and its target
var distance = segment.global_position.distance_to(segment.target.global_position)
# find difference with desired distance
var error = distance - desired_distance
# move segment a percentage of the error toward its target segment
var vector_to_target = (segment.target.global_position - segment.global_position).normalized()
segment.global_position += vector_to_target * error * 0.30
I am happy with the result.
Now I want to create a similar enemy also composed of segments, but whose final segment is a kind of base, rooted to one position. Similar to the Chain Chomp from Mario.
I then want to code movement of the head, but for everything to be constrained. I'm guessing all intermediate segments would be doubly constrained by both of their neighbor segments, and the head itself would also have to be constrained by its neighbor segment, even though I'd be coding movement for it.
I'm not sure how to set up constraints like these which have to look in both directions, and would appreciate any advice?
Although I work in Godot, an engine-neutral explanation of the technique would suffice.
segment6.global_position = (x,y)
andif ((head.postion - segment6.global_position).lenght() <= range) move();
\$\endgroup\$ – Anonymous Anonymous Sep 28 '18 at 17:27