Jan 26

The program this simulates the snake moving around is finished and it can be downloaded here. When it starts the code “reads” the servo positions, based on these the previous tail position is calculated as explained in a previous post. Also two points in front of the snake are calculated and the spline of the snake is drawn. This spline is used as the path to follow.

The most difficult part was to define a good algorithm how to advance the full snake for a specific distance over this path. It finally turned out to be very simple; the function that executes this is called FindNextPosition.

The same formula for calculating the spline between two points using two control points is used. This requires an Index value that ranges from 1 down to 0 and calculating the length of the vector between the start point and the point on the spline at a given Index value. The value of Index is decreased with 0.01 until the length of the vector matches the required distance. The (X, Y) position of the point at the found Index is the point to which the snake will move forward for that specific segment of the snake.

This process is done for all segments from head to tail. As a result all servo positions are known on the spline with a specific distance to the previous stored servo positions. The servo positions are only stored when the snake moved one serve length forward. The snake stops moving until you click with the mouse in front of the snake. The angle between the tip of the head and the location you clicked is used for calculating the new target position to reach one servo length in front of the snake.

I made a screen capture of the program using a trail version of Camtasia Studio while using it and uploaded the video to YouTube. If you watch the movie, please give it a five star rating when finished so it scores higher in the search results!

When watching the movements it looks to be a vary promising method, the resulting spline looks vary natural and it almost makes me wonder if snakes can calculate splines as well….

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.