Answer the question
In order to leave comments, you need to log in
How to stop rotation in the right place?
I make a wheel of fortune, the rotation is primitive:
Slowing down or speeding up is also normal:angle += 10 * speed;
if(play){
speed += 0.1;
}
if(stop) speed -= 0.1;
speed = Math.min(1,Math.max(0, speed));
var speed= 1 - ( (angle-EndAngle) % 6.28318531 ) / 6.28318531 ;
Answer the question
In order to leave comments, you need to log in
Well, at the moment of pressing the "stop" I would determine the angle where the wheel should stop. For example, using a random number generator.
Let's say that at the moment you press "stop" the angle of the wheel alpha
is , and we want it to stop at the angle beta
, after making about five revolutions. Then, if alpha > beta, then we need to rotate the wheel by theta = 360 * 5 + beta - alpha
degrees.
If alpha < beta, then you need for example theta = 360 * 6 - beta + alpha
degrees.
In total, we know the rotation speed speed
, we know the number of degrees that need to be passed - theta
. We know that at the end the speed should be 0. We need to calculate the deceleration а
.
It's time for math. In time, the t
wheel will rotate by speed * t - a * t * t /2
degrees. We need it to turn on theta
.
So we have an equation speed * t - a * t * t /2 = theta
. We also remember that the speed at the end should be equal to 0. So speed - a * t = 0
.
Substitute, shorten. We get a = speed * speed / ( 2 * theta)
. Now you know the deceleration by which you need to reduce the speed each cycle.
Just keep in mind that your time is discrete, so don't mess with the units for t
, speed
and а
.
You shouldn't count the angle using a formula like "+=" - it accumulates errors. It is better to consider "angle = time * speed".
Suppose, at the moment of the beginning of the stop, the angular velocity was speed, and it is necessary to stop through the angle fi.
If the speed is constant, then the wheel will pass the angle fi in the time t1=fi/speed. If the speed decreases evenly to zero, then during this time the wheel will pass the angle fi / 2.
Suppose at the moment t0 the wheel has angle angle0. And we start to slow down according to the formula
angle = angle0 + (time * speed) - (uskr * time^2 / 2)
The wheel slows down - the acceleration is negative.
The speed will be reset at the moment
d_time1 = speed / uskr
At this moment angle1 will be equal to - according to the formula above. And he d.b. such as we need, i.e. He is known.
angle1 = angle0 + (time1 * speed) - (uskr * time1^2 / 2)
Substitute time1 :
angle1 = angle0 + (speed / uskr * speed) - (uskr * (speed / uskr)^2 / 2)
angle1 = angle0 + (speed^2 / uskr) - (speed^2 / uskr / 2) = angle0 + (speed^2 / uskr /2)
(speed^2 / uskr /2) = angle1 - angle0
uskr = (speed^2 / 2 / (angle1 - angle0))
Well, that's all. Compute:
angle0 = time0 * speed
uskr = (speed^2 / 2 / (angle1 - angle0))
d_time1 = speed / uskr
if time < time0 then {
angle = time * speed;
} else if time < time0 + d_time1 then
angle = angle0 + (time * speed) - (uskr * time^2 / 2)
} else {
angle = angle1
}
Something like this...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question