Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- - (void)startFly
- {
- [self runAction:[CCSequence actions:
- [CCBezierBy actionWithDuration:timeFlying bezier:[self getPathWithDirection:currentDirection]],
- [CCCallFuncN actionWithTarget:self selector:@selector(endFly)],
- nil]];
- }
- for (int i = 0; i < 10; i++)
- t = t + (L / 10) / length(t * v1 + v2);
- public sealed class CurveMap<TCurve> where TCurve : struct, ICurve
- {
- private readonly float[] _arcLengths;
- private readonly float _ratio;
- public float length { get; private set; }
- public TCurve curve { get; private set; }
- public bool isSet { get { return !length.isNaN(); } }
- public int resolution { get { return _arcLengths.Length; } }
- public CurveMap(int resolution)
- {
- _arcLengths = new float[resolution];
- _ratio = 1f / resolution;
- length = float.NaN;
- }
- public void set(TCurve c)
- {
- curve = c;
- Vector2 o = c.sample(0);
- float ox = o.X;
- float oy = o.Y;
- float clen = 0;
- int nSamples = _arcLengths.Length;
- for(int i = 0; i < nSamples; i++)
- {
- float t = (i + 1) * _ratio;
- Vector2 p = c.sample(t);
- float dx = ox - p.X;
- float dy = oy - p.Y;
- clen += (dx * dx + dy * dy).sqrt();
- _arcLengths[i] = clen;
- ox = p.X;
- oy = p.Y;
- }
- length = clen;
- }
- public Vector2 sample(float u)
- {
- if(u <= 0) return curve.sample(0);
- if(u >= 1) return curve.sample(1);
- int index = 0;
- int low = 0;
- int high = resolution - 1;
- float target = u * length;
- float found = float.NaN;
- // Binary search to find largest value <= target
- while(low < high)
- {
- index = (low + high) / 2;
- found = _arcLengths[index];
- if (found < target)
- low = index + 1;
- else
- high = index;
- }
- // If the value we found is greater than the target value, retreat
- if (found > target)
- index--;
- if(index < 0) return curve.sample(0);
- if(index >= resolution - 1) return curve.sample(1);
- // Linear interpolation for index
- float min = _arcLengths[index];
- float max = _arcLengths[index + 1];
- Debug.Assert(min <= target && max >= target);
- float interp = (target - min) / (max - min);
- Debug.Assert(interp >= 0 && interp <= 1);
- return curve.sample((index + interp + 1) * _ratio);
- }
- }
- public float speed; // target linear speed
- // determine an initial value by checking where speedFactor converges
- float speedFactor = speed / 10;
- float targetStepSize = speed / 60f; // divide by fixedUpdate frame rate
- float lastStepSize;
- void Update ()
- {
- // Take a note of your previous position.
- Vector3 previousPosition = transform.position;
- // Advance on the curve to the next t;
- transform.position = BezierOrOtherCurveFunction(p0, p1, ..., t);
- // Measure your movement length
- lastStepSize = Vector3.Magnitude(transform.position - previousPosition);
- // Accelerate or decelerate according to your latest step size.
- if (lastStepSize < targetStepSize)
- {
- speedFactor *= 1.1f;
- }
- else
- {
- speedFactor *= 0.9f;
- }
- t += speedFactor * Time.deltaTime;
- }
Add Comment
Please, Sign In to add comment