# 模拟阻尼运动

1     public struct DampingMotion
2     {
3         /// <param name="maxSpeed">最大速度</param>
4         /// <param name="speedUp1">加速运动1</param>
5         /// <param name="speedUp2">减速运动2</param>
6         /// <param name="expected">期望停留在某个弧度</param>
7         public DampingMotion(double maxSpeed, double speedUp1, double speedUp2, double expected)
8         {
9             this.maxSpeed = maxSpeed;
10             this.speedUp1 = speedUp1;
11             this.speedUp2 = speedUp2;
12             this.expected = expected;
13
14             time1 = maxSpeed / speedUp1;
15             time3 = maxSpeed / speedUp2;
16             distance1 = speedUp1 * Math.Pow(time1, 2) / 2;
17             distance3 = speedUp2 * Math.Pow(time3, 2) / 2;
18             distance2 = expected - (distance1 + distance3) % (Math.PI * 2) + Math.PI * 4;
19             time2 = distance2 / maxSpeed;
20         }
21
22         //单位是秒
23         public double GetRotate(double time)
24         {
25             if (time >= 0 && time < time1)
26             {
27                 return Math.Pow(time, 2) / 2 * speedUp1;
28             }
29             if (time >= time1 && time < time1 + time2)
30             {
31                 return distance1 + (time - time1) * maxSpeed;
32             }
33             if (time >= time1 + time2 && time < time1 + time2 + time3)
34             {
35                 var f = time - (time1 + time2);
36                 return distance1 + distance2 + (
37                     speedUp2 * Math.Pow(time3, 2) / 2 -
38                     speedUp2 * Math.Pow((time3 - f), 2) / 2
39                     );
40             }
41             return distance1 + distance2 + distance3;
42         }
43
44         public double maxSpeed;
45         public double speedUp1;
46         public double speedUp2;
47         public double expected;
48
49         public double time1; //第一段匀加速的时间
50         public double time2; //第二段匀速运动时间
51         public double time3; //第三段匀减速运动时间
52         private double distance1;
53         private double distance2;
54         private double distance3;
55     }

posted @ 2017-08-29 17:49  egmkang  阅读(745)  评论(0编辑  收藏