# Slithering Snake

HXCTCreations - Custom level - from AndroidSimple snake, rope, or whatever you call it. Each segments always keep a distance against another segments.

Explanation:

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

8 end

9 end

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: 350 Downloads: 152 Unique objects: 2 Total objects: 10

LEVEL ID: 26853

## Discuss this level