Just some inverse kinematics based off of a coding train video I saw a while back. The main logic was coded while I was in a plane (my brother challenged me to an in-flight game jam, which kicked off the project), so I couldn't look it up at the time lol but I guess I remembered enough to have it function convincingly enough.
For those unfamiliar with inverse kinematics, (or at least how I implemented it)... Basically, at the start of each update, it just moves the first node to the car's position, then it moves the next node so its distance to the first node is constrained to the chain segment's set length. Then it repeats that move + distance constraint for all subsequent nodes.
Here's a some pseudo-code of the the function Player::updateChains() in Player.cpp
for all chain nodes (starting from the car, iterating until the ball):
node.position += node.velocity
node.velocity += frictionForce
if distance(node.position, node.parent.position) > chainSegmentLength:
// segment := vector pointing from parent to node, with length set to chainSegmentLength
Vector2 segment = (node.position - node.parent.position).setLength(chainSegmentLength)
// node.velocity = new position - old position
node.velocity = node.parent.position + segment - node.position
node.position = node.parent.position + segment
As I write this, I'm realizing I'm not applying delta time correctly... Yeah, I kind of took the mentality of "There are two types of code. Good code, and production code."
But it works well enough! I might come back to this project later when I've got more time to rewrite all the code and implement some of the features I withheld to combat scope creep.