I thought I might write down some points about how the simulator works for future reference and to help me think about it and make improvements. The linkage program is a bit experimental so I try to get something working and then worry about making it better later.

There are links and connectors. Two links that are connected are actually sharing a connector. The connector has a pointer to each link and the links have pointers to each connector. A “fixed” connector is fixed in it’s position because it is anchor to the ground or because the simulator has figured out a new location for it for it and has made it fixed in position just for the specific step of simulation.

All simulation is done by starting with the original locations of the connectors and the new position is generated from there. The exception is the input, or rotating, anchors. Their current rotation angle is maintained during the simulation and is not calculated from an original angle for each step. Otherwise, I would need to maintain an actual simulation step counter and calculate the new angle from the original. Either way would end up working the same.

For each link, the code makes a determination as to what action to take. A link that has a rotating input anchor ends up fully rotated to it’s new position. A link that is already fixed in place is skipped. A link that has a single fixed connector and other that are not fixed is then processed further to see what can be done with it.

The connectors of the link in question are examined and some other link is found that has some other fixed connector. The current link and the other link are then rotated so that the common connector of the two is in the right location. This is done by finding the intersection of two circles that are centered on the fixed connectors are have a radius that is the distance to the common connector. There are almost always two intersection points and the one closest to the previous simulated location of the common connector is used. I’ve recently added momentum so that the two previous locations are saved and a new third location is predicted based on these. The intersection of the two circles that is closest to the predicted location is used. This keeps some special mechanisms moving as expected like the wheels and connecting rod of locomotive drive wheels.

Now that I’ve added sliding connectors, I’m finding a bunch of special cases that are much less generic than the one case handled for normal connectors. There is the case where a link can be moved based on some sliding connector that is already fixed or the case where a link can be moved so that it’s sliding connector is on the segment between connectors of another link. There is also the case of two fixed sliding connectors or two non-fixed sliding connectors that can be rotated into the proper position. I’ve shown some pictures in older blog entires of these special cases and they are not complex problems, just numerous.

The simulator does the link and connector analysis 30 times a second and the results are drawn. If there is some connector that doesn’t get moved to a new location during the simulation step, the current step of the simulation is discarded and the previous step is displayed with some indicators of what connectors were skipped.

The only complexity in all of the simulation code is keeping track of what link is the “current” link and what other links and connectors are being analyzed and moved. it’s just hard-to-read code even with comments.

My description here hasn’t revealed any serious flaws or shortcuts that I can take so I will proceed with the current plan of handling the special cases until the entire set of possible mechanisms is handled. then I might add gears, pulleys, input pistons or other interesting mechanism parts.

My blog needs more pictures and maybe some mechanism animations!