views:

98

answers:

1

Hi, I am trying to spin a wheel on its axis -- setting it in motion using the mouse. Specifically, I am trying to spin a roulette wheel.

I calculate a delta x and delta y by getting the difference of x1 x2 and y1 y2. I can see in console via NSLOG msgs that it is ok. I use these values to calculate velocity.

CABasicAnimation *fullRotation; fullRotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"]; fullRotation.fromValue = [ NSNumber numberWithFloat:0 ] ; fullRotation.toValue = [NSNumber numberWithFloat:(((360*M_PI)/180)) ; fullRotation.duration = duration ; fullRotation.repeatCount = repeat ;

[myview.layer addAnimation:fullRotation forKey:@"360"];

The above code gives me fluid animation, but the wheel always completes a full circle, which would be not very realistic.

Can anyone suggest a method for spinning the wheel by smaller amounts that still gives fluid animation? When I use the above, the wheel doesn't move at all for smaller amounts.

Thank you!

Piesia

+1  A: 

It sounds like you need a little physics modeling to get the right action on the wheel. I'm not familiar with the APIs and I don't see where the code takes into account the velocity imparted by the finger movement, but I will dust off my physics lessons from 20 years ago with the hope it will help.

The initial velocity of the wheel is going to be determined by the motion of the finger at the time of release. We know that the linear velocity of the wheel at some point will be equal to the angular velocity multiplied by the radius to the point where we're measuring the velocity.

linear velocity = angular velocity * radius

As the wheel spins, it's going to encounter friction from the spindle against the wheel and this will cause it to slow down. Friction is a force applied over time until the wheel comes to a stop.

Friction = Moment of Inertia of the Wheel * angular acceleration

or

Friction / Moment of Inertia of the Wheel = angular acceleration

For the purposes of your application this will be a constant and you'll probably want to experiment with different values for this until it "feels right."

To calculate the amount of time, solve the following equation for time:

0 = angular velocity + angular acceleration * time

Angular acceleration will be negative because you're slowing down. Once you have the amount of time, you're going to calculate the number of revolutions by the following:

revolutions = ((angular velocity * time) + (0.5 * angular acceleration * time^2)) / (2 * pi)

time^2 is time squared, or time * time.

I hope this helps.

David Smith
David, thanks so much! I will look into this (it might take a week or so as I got pulled onto another project), but I definitely find your information informative and helpful!Best regards, Piesia
Piesia