Reply to topic  [ 4 posts ] 
Rotating wheels - a maths question 
Author Message
What's a life?
User avatar

Joined: Thu Apr 23, 2009 6:27 pm
Posts: 12251
Reply with quote
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
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:

Code:
// Position train it’s length off the left of the screen.
self.myTrainSet.frame.origin.x = -trainLength

// The finish - that’s the width of the screen + length of the train, minus the smoke adjustment.
let myTrainSpeed = ((screenWidth + trainLength) - 500 ) / speed


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:

Code:
// Get the radius of the wheel
let myRadius = wheelBox.width

// Calculate the circumference
let myPerimeter = (2.0 * Double.pi) * myRadius


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

Code:
wheelRotateSpeed = (speed/myPerimeter) * 2

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
Code:
let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation")
rotateAnimation.duration = timeToRotate


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.).

_________________
All the best,
Paul
brataccas wrote:
your posts are just combo chains of funny win

I’m on Twitter, tweeting away... My Photos Random Avatar Explanation


Thu May 17, 2018 8:24 am
Profile
Doesn't have much of a life
User avatar

Joined: Wed Apr 29, 2009 9:33 am
Posts: 667
Reply with quote
I can't help you with the maths, because I think you are on the right track and I have no idea why the *2 appears to be giving you a better result.

However on thing you are also going to have to consider is the frame rate effect which at certain speeds will give you the classic reverse turning wheels optical illusion, so you will have to work out which speed ranges give that effect and make the wheels turn in the "wrong" direction for those.

_________________
UltraSonic f***erPhonic ZombieShockin TrailerRockin BabyBoomin GaitorGroomin InterStellar LadyRaiders


Thu May 17, 2018 9:36 am
Profile WWW
I haven't seen my friends in so long
User avatar

Joined: Fri Apr 24, 2009 6:37 am
Posts: 6954
Location: Peebo
Reply with quote
I think I can see one problem. In setting the myRadius bit you appear to actually be specifying the diameter, assuming the wheelBox.width refers to the total width of the wheel. If that's the case you are effectively doing 4*pi*r (or 2*pi*d) which also explains why you need the *2 in the wheelRotateSpeed part.

_________________
When they put teeth in your mouth, they spoiled a perfectly good bum.
-Billy Connolly (to a heckler)


Thu May 17, 2018 9:52 am
Profile
What's a life?
User avatar

Joined: Thu Apr 23, 2009 6:27 pm
Posts: 12251
Reply with quote
Thanks all! Yes, I think I may have got it now. The *2 and /2 was causing some confusion. It seems that I could have been doing my calculation for the speed the wrong way.

I changed it to this
Code:
self.wheelRotateSpeed = myPerimeter/speed

Which seems to be creating results that are more in keeping with what I want. Asking the question and reading answers seems to have helped me re-examine what I’ve done and think about it a little more clearly.

As for wheel turning and frequencies - yes, that’s something I have seen a bit. I’m not sure yet how to get the screen refresh rate. Noting that reversing the wheel rotation will cause some features on the wheel to definitely rotate backwards! There are the counterweights at the bottom of the wheel, as well as the lugs (not sure of the correct name for those - Heather will know) that connect to the connection rod. These would always need to rotate in the direction of travel, and that would mean having each wheel made up of two (or more) components depending on the construction. That would possibly add two components for each wheel (rim & hub, spokes which would rotate according to frequency patterns).

_________________
All the best,
Paul
brataccas wrote:
your posts are just combo chains of funny win

I’m on Twitter, tweeting away... My Photos Random Avatar Explanation


Thu May 17, 2018 11:45 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 4 posts ] 

Who is online

Users browsing this forum: No registered users and 21 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software.