Guest User

Untitled

a guest
Dec 16th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. - (void)startFly
  2. {
  3. [self runAction:[CCSequence actions:
  4. [CCBezierBy actionWithDuration:timeFlying bezier:[self getPathWithDirection:currentDirection]],
  5. [CCCallFuncN actionWithTarget:self selector:@selector(endFly)],
  6. nil]];
  7.  
  8. }
  9.  
  10. for (int i = 0; i < 10; i++)
  11. t = t + (L / 10) / length(t * v1 + v2);
  12.  
  13. public sealed class CurveMap<TCurve> where TCurve : struct, ICurve
  14. {
  15. private readonly float[] _arcLengths;
  16. private readonly float _ratio;
  17. public float length { get; private set; }
  18. public TCurve curve { get; private set; }
  19. public bool isSet { get { return !length.isNaN(); } }
  20. public int resolution { get { return _arcLengths.Length; } }
  21.  
  22. public CurveMap(int resolution)
  23. {
  24. _arcLengths = new float[resolution];
  25. _ratio = 1f / resolution;
  26. length = float.NaN;
  27. }
  28.  
  29. public void set(TCurve c)
  30. {
  31. curve = c;
  32. Vector2 o = c.sample(0);
  33. float ox = o.X;
  34. float oy = o.Y;
  35. float clen = 0;
  36. int nSamples = _arcLengths.Length;
  37. for(int i = 0; i < nSamples; i++)
  38. {
  39. float t = (i + 1) * _ratio;
  40. Vector2 p = c.sample(t);
  41. float dx = ox - p.X;
  42. float dy = oy - p.Y;
  43. clen += (dx * dx + dy * dy).sqrt();
  44. _arcLengths[i] = clen;
  45. ox = p.X;
  46. oy = p.Y;
  47. }
  48. length = clen;
  49. }
  50.  
  51. public Vector2 sample(float u)
  52. {
  53. if(u <= 0) return curve.sample(0);
  54. if(u >= 1) return curve.sample(1);
  55.  
  56. int index = 0;
  57. int low = 0;
  58. int high = resolution - 1;
  59. float target = u * length;
  60. float found = float.NaN;
  61.  
  62. // Binary search to find largest value <= target
  63. while(low < high)
  64. {
  65. index = (low + high) / 2;
  66. found = _arcLengths[index];
  67. if (found < target)
  68. low = index + 1;
  69. else
  70. high = index;
  71. }
  72.  
  73. // If the value we found is greater than the target value, retreat
  74. if (found > target)
  75. index--;
  76.  
  77. if(index < 0) return curve.sample(0);
  78. if(index >= resolution - 1) return curve.sample(1);
  79.  
  80. // Linear interpolation for index
  81. float min = _arcLengths[index];
  82. float max = _arcLengths[index + 1];
  83. Debug.Assert(min <= target && max >= target);
  84. float interp = (target - min) / (max - min);
  85. Debug.Assert(interp >= 0 && interp <= 1);
  86. return curve.sample((index + interp + 1) * _ratio);
  87. }
  88. }
  89.  
  90. public float speed; // target linear speed
  91.  
  92. // determine an initial value by checking where speedFactor converges
  93. float speedFactor = speed / 10;
  94.  
  95. float targetStepSize = speed / 60f; // divide by fixedUpdate frame rate
  96. float lastStepSize;
  97.  
  98. void Update ()
  99. {
  100. // Take a note of your previous position.
  101. Vector3 previousPosition = transform.position;
  102.  
  103. // Advance on the curve to the next t;
  104. transform.position = BezierOrOtherCurveFunction(p0, p1, ..., t);
  105.  
  106. // Measure your movement length
  107. lastStepSize = Vector3.Magnitude(transform.position - previousPosition);
  108.  
  109. // Accelerate or decelerate according to your latest step size.
  110. if (lastStepSize < targetStepSize)
  111. {
  112. speedFactor *= 1.1f;
  113. }
  114. else
  115. {
  116. speedFactor *= 0.9f;
  117. }
  118.  
  119. t += speedFactor * Time.deltaTime;
  120. }
Add Comment
Please, Sign In to add comment