Simulate damping motion and simulate Damping

Source: Internet
Author: User

Simulate damping motion and simulate Damping

There are often turntable activities in the game. In order to make the turntable behave a little more naturally, you need to simulate the damping movement by yourself. There are three processes: Uniform Acceleration, constant speed, and even deceleration.

Set the maximum speed to MaxSpeed, SpeedUp1 (acceleration of uniformly accelerated motion), SpeedUp2 (acceleration of uniformly slow motion), secondary CT (expected radian point)

In fact, the simulation only needs to simulate the two motion ranges, and the rest is the motion interval at a constant speed.

 

1 public struct DampingMotion 2 {3 // <param name = "maxSpeed"> maximum speed </param> 4 /// <param name = "speedUp1"> acceleration 1 </param> 5 // <param name = "speedUp2"> deceleration motion 2 </param> 6 // <param name = "expected"> expect to stay in a radian </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 distane3 = speedUp2 * Math. pow (time3, 2)/2; 18 distance2 = expected-(distance1 + distane3) % (Math. PI * 2) + Math. PI * 4; 19 time2 = distance2/maxSpeed; 20} 21 22 // unit: seconds 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 + distane3; 42} 43 44 public double maxSpeed; 45 public double speedUp1; 46 public double speedUp2; 47 public double expected; 48 49 public double time1; // The First Time for uniform acceleration is 50 public double time2; // The second stage of the uniform motion time 51 public double time3; // the third stage of the uniform deceleration motion time 52 private double distance1; 53 private double distance2; 54 private double distane3; 55}

 

After the construction, you only need to call the GetRotate function to obtain the radians of a time turntable.

Such a code with less than 50 lines can be used as an interview question.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.