I’m playing with Swift - Apple’s programming language - and I have got two apps on the go. One is a for stereo photos, the other is a nonsense doodle that I did to see if I could do it. It’s actually something I did on RISC OS back in the 1990s, but I wondered if I can do it in Mac OSX.
![Image](https://www.evernote.com/l/AKx8DnJQdoxBqaxQ9MD-OUEry_oVCzof62sB/image.png)
I can. It’s a “train set” - randomly locos and coaches/trucks run along the bottom of the screen. In the Mac version, there’s even some smoke and the wheels rotate. That‘s the problem I have.
I know the distance the train has to cover - that’s the screen width + 500 pixels (to give the smoke some clearance). I want the wheels to rotate at a speed that makes it look as if they are moving with the train.
The speed of the train is random, and I am thinking in pixels per second.
I know the radius of the wheels, so I can calculate the circumference.
OK, so train moving across the screen is achieved like this:
After this, I kick off the animation. Basically, it should be travelling at variable
speed per second.
OK, so rotating the wheels is confusing. I’ve done some research and tried a few things, but it seems to be off by a bit. This is what I’m doing:
At this point, I believe that to rotate the wheel realistically, I need to rotate it so that it covers the same number of pixels per second as the train is moving. However, this is where I think I am getting unstuck
I’m not sure why *2 is working, but it seems to be a bit closer than not multiplying it.
Then in a CABasicAnimation, I use that wheelRotateSpeed to rotate the wheel
I’m not asking for help with the code - I’m OK there, I think (just giving examples as it shows my working....). It‘s the underlying maths I’m not sure about. It’s either the wheel spinning is correct, and the speed of the horizontal movement is wrong, or the other way round.
I am having far too much fun creating the graphics for this, BTW. I have 4 locos (one has two variations), as well as some trucks and a load of coaches. Stuff just rolls across the screen is a rather satisfying fashion. The connecting rods on the locos move too - though I haven’t worked out how to do connecting rods for the really big engines yet (A4 class, etc.).