Slithering SnakeHXCTCreations - Custom level - from Android
Simple snake, rope, or whatever you call it. Each segments always keep a distance against another segments.
1 function seg_update(segments, range, cspd)
2 for i = 2, #segments do
3 local dx = segments[i].x - segments[i-1].x
4 local dy = segments[i].y - segments[i-1].y
5 local dist = math.sqrt(dx ^ 2 + dy ^ 2)
6 segments[i].x = segments[i].x - (dist - range) * cspd * dx
7 segments[i].y = segments[i].y - (dist - range) * cspd * dy
1: update function declaration
-segments is the table containing position of each segments.
-range is the distance of two segments in normal condition.
-cspd is the correction speed.
2: do the process on each but first segment.
3 and 4: calculate the segment's position relative to rlthe previous segment.
5: calculate the absolute distance to the previous segment.
6 and 7: move the segment to/away the previous segments, depending on the distance and correction speed.
Views: 467 Downloads: 169 Unique objects: 2 Total objects: 10
LEVEL ID: 26853