Using inverse kinematics, this project draws 2D, snake-like objects which can follow a target.

This project is based (fairly heavily) on Coding Challenge #64.2 by The Coding Train.

The code is written in Processing, a Java based language not designed to run in a web browser. To run it here, I made use of the processing.js library (now archived). THe only issues I came across were to do with PVector methods. The copy() method was never implemented so I changed it to get(), and some calculation methods don’t return a value and therefore couldn’t be chained together. This resulted in the following function:

PVector dir = PVector.sub(a, target);
angle = dir.heading();
//This line contains a few unsupported features
b.add(target.copy().sub(b).div(parent == null ? 5 : 1));
calculateA();

Becoming this:

PVector dir = PVector.sub(a, target);
angle = dir.heading();
//get() must be used instead of copy()
PVector v = target.get();
//Each operation method must be called from the PVector object, not chained together
v.sub(b);
v.div(parent == null ? 5 : 1);
b.add(v);
calculateA();

The Processing sketch files can be downloaded as a zip file here.