/// <summary> /// Reads a curve from the specified CSV file. /// </summary> public static Curve <TValue, CurveKey <TValue> > ReadCurveFromCsv <TValue>(String path, Ultraviolet.ICurveSampler <TValue, CurveKey <TValue> > sampler) where TValue : new() { using (var file = File.OpenRead(path)) using (var reader = new StreamReader(file)) using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture)) { csv.Read(); csv.ReadHeader(); var header = csv.Context.HeaderRecord.Select((item, ix) => new { Item = item, Index = ix }) .ToDictionary(x => x.Item, x => x.Index); var keys = new List <CurveKey <TValue> >(); while (csv.Read()) { var keyPosition = csv.GetField <Single>(header["Position"]); var keyValue = (Object) new TValue(); ReadCsvFields <TValue>(csv, "Value_", keyValue, header); var key = new CurveKey <TValue>(keyPosition, (TValue)keyValue); keys.Add(key); } return(new Curve <TValue, CurveKey <TValue> >(CurveLoopType.Cycle, CurveLoopType.Cycle, sampler, keys)); } }
public void Properties() { var curveKeyCollection = new CurveKeyCollection { new CurveKey(0, 0), new CurveKey(-1, 1), new CurveKey(-1, 1) }; // Count property Assert.AreEqual(3, curveKeyCollection.Count); // IsReadOnly property Assert.AreEqual(false, curveKeyCollection.IsReadOnly); // Item indexer var key1 = new CurveKey(-1, 1); var key2 = curveKeyCollection[1]; var key3 = curveKeyCollection[2]; Assert.AreEqual(true, key1 == key2); Assert.AreEqual(false, key2 == key3); Assert.AreEqual(key1, key2); Assert.AreNotEqual(key2, key3); Assert.AreNotEqual(key1, key3); }
public IntersectionDecider(CurveKey key, float sampleLeftValue, float sampleRightValue, IntersectionChoice choice) { Key = key; SampleLeftValue = sampleLeftValue; SampleRightValue = sampleRightValue; Choice = choice; }
public void ComputeTangent() { var key1 = new CurveKey(-0.5f, 1.5f); var key2 = new CurveKey(1.1f, 2.3f); var key3 = new CurveKey(2.25f, 4.4f); var curve1 = new Curve(); curve1.Keys.Add(key1); curve1.Keys.Add(key2); curve1.Keys.Add(key3); curve1.ComputeTangent(0, CurveTangent.Smooth); curve1.ComputeTangent(1, CurveTangent.Smooth); curve1.ComputeTangent(2, CurveTangent.Smooth); Assert.AreEqual(0.0f, curve1.Keys[0].TangentIn); Assert.AreEqual(0.799999952f, curve1.Keys[0].TangentOut); Assert.AreEqual(1.68727279f, curve1.Keys[1].TangentIn); Assert.AreEqual(1.21272731f, curve1.Keys[1].TangentOut); Assert.AreEqual(2.10000014f, curve1.Keys[2].TangentIn); Assert.AreEqual(0.0f, curve1.Keys[2].TangentOut); var curve2 = new Curve(); curve2.Keys.Add(key1); curve2.Keys.Add(key2); curve2.Keys.Add(key3); curve2.ComputeTangent(0, CurveTangent.Flat); curve2.ComputeTangent(1, CurveTangent.Flat); curve2.ComputeTangent(2, CurveTangent.Flat); Assert.AreEqual(0.0f, curve2.Keys[0].TangentIn); Assert.AreEqual(0.0f, curve2.Keys[0].TangentOut); Assert.AreEqual(0.0f, curve2.Keys[1].TangentIn); Assert.AreEqual(0.0f, curve2.Keys[1].TangentOut); Assert.AreEqual(0.0f, curve2.Keys[2].TangentIn); Assert.AreEqual(0.0f, curve2.Keys[2].TangentOut); var curve3 = new Curve(); curve3.Keys.Add(key1); curve3.Keys.Add(key2); curve3.Keys.Add(key3); curve3.ComputeTangent(0, CurveTangent.Linear); curve3.ComputeTangent(1, CurveTangent.Linear); curve3.ComputeTangent(2, CurveTangent.Linear); Assert.AreEqual(0.0f, curve3.Keys[0].TangentIn); Assert.AreEqual(0.799999952f, curve3.Keys[0].TangentOut); Assert.AreEqual(0.799999952f, curve3.Keys[1].TangentIn); Assert.AreEqual(2.10000014f, curve3.Keys[1].TangentOut); Assert.AreEqual(2.10000014f, curve3.Keys[2].TangentIn); Assert.AreEqual(0.0f, curve3.Keys[2].TangentOut); }
public EditCurveKey(long id, CurveKey key) { Id = id; OriginalKey = key; TangentOutType = TangentInType = EditCurveTangent.Smooth; }
public void SetTangents() { for (int i = 0; i < this.curveX.Keys.Count; i++) { int num = i - 1; if (num < 0) { num = i; } int num2 = i + 1; if (num2 == this.curveX.Keys.Count) { num2 = i; } CurveKey curveKey = this.curveX.Keys[num]; CurveKey curveKey2 = this.curveX.Keys[num2]; CurveKey value = this.curveX.Keys[i]; CameraMotion.Curve3D.SetCurveKeyTangent(ref curveKey, ref value, ref curveKey2); this.curveX.Keys[i] = value; curveKey = this.curveY.Keys[num]; curveKey2 = this.curveY.Keys[num2]; value = this.curveY.Keys[i]; CameraMotion.Curve3D.SetCurveKeyTangent(ref curveKey, ref value, ref curveKey2); this.curveY.Keys[i] = value; curveKey = this.curveZ.Keys[num]; curveKey2 = this.curveZ.Keys[num2]; value = this.curveZ.Keys[i]; CameraMotion.Curve3D.SetCurveKeyTangent(ref curveKey, ref value, ref curveKey2); this.curveZ.Keys[i] = value; } }
public void CopyTo() { CurveKey[] array = new CurveKey[c.Count]; c.CopyTo(array, 0); Assert.AreEqual(k3.Position, array[0].Position, "#1"); Assert.AreEqual(k2.Position, array[1].Position, "#2"); Assert.AreEqual(k1.Position, array[2].Position, "#3"); }
public static void AreEqual <TParam, TPoint>(CurveKey <TParam, TPoint> expected, CurveKey <TParam, TPoint> curveKey) { Assert.AreEqual(expected.Interpolation, curveKey.Interpolation); Assert.AreEqual(expected.Parameter, curveKey.Parameter); Assert.AreEqual(expected.Point, curveKey.Point); Assert.AreEqual(expected.TangentIn, curveKey.TangentIn); Assert.AreEqual(expected.TangentOut, curveKey.TangentOut); }
public void ComputeTangents() { var key1 = new CurveKey(-0.5f, 1.5f); var key2 = new CurveKey(1.1f, 2.3f); var key3 = new CurveKey(2.25f, 4.4f); var curve1 = new Curve(); curve1.Keys.Add(key1); curve1.Keys.Add(key2); curve1.Keys.Add(key3); curve1.ComputeTangents(CurveTangent.Smooth); Assert.AreEqual(0.0f, curve1.Keys[0].TangentIn); Assert.AreEqual(0.799999952f, curve1.Keys[0].TangentOut); Assert.AreEqual(1.68727279f, curve1.Keys[1].TangentIn); Assert.AreEqual(1.21272731f, curve1.Keys[1].TangentOut); Assert.AreEqual(2.10000014f, curve1.Keys[2].TangentIn); Assert.AreEqual(0.0f, curve1.Keys[2].TangentOut); var curve2 = new Curve(); curve2.Keys.Add(key1); curve2.Keys.Add(key2); curve2.Keys.Add(key3); curve2.ComputeTangents(CurveTangent.Flat); Assert.AreEqual(0.0f, curve2.Keys[0].TangentIn); Assert.AreEqual(0.0f, curve2.Keys[0].TangentOut); Assert.AreEqual(0.0f, curve2.Keys[1].TangentIn); Assert.AreEqual(0.0f, curve2.Keys[1].TangentOut); Assert.AreEqual(0.0f, curve2.Keys[2].TangentIn); Assert.AreEqual(0.0f, curve2.Keys[2].TangentOut); var curve3 = new Curve(); curve3.Keys.Add(key1); curve3.Keys.Add(key2); curve3.Keys.Add(key3); curve3.ComputeTangents(CurveTangent.Linear); Assert.AreEqual(0.0f, curve3.Keys[0].TangentIn); Assert.AreEqual(0.799999952f, curve3.Keys[0].TangentOut); Assert.AreEqual(0.799999952f, curve3.Keys[1].TangentIn); Assert.AreEqual(2.10000014f, curve3.Keys[1].TangentOut); Assert.AreEqual(2.10000014f, curve3.Keys[2].TangentIn); Assert.AreEqual(0.0f, curve3.Keys[2].TangentOut); }
public void UpdateSide(Side side, CurveKey keyToUpdate, float sampleValue, bool newChoice) { var sideDeciders = PickSide(side); var index = sideDeciders.FindIndex(decider => decider.Key == keyToUpdate); sideDeciders[index] = new SideDecider(keyToUpdate, sampleValue, newChoice); RegenerateCombinedPreview(); }
public void Properties() { var key = new CurveKey(1, 2, 3, 4, CurveContinuity.Step); Assert.AreEqual(1, key.Position); Assert.AreEqual(2, key.Value); Assert.AreEqual(3, key.TangentIn); Assert.AreEqual(4, key.TangentOut); Assert.AreEqual(CurveContinuity.Step, key.Continuity); }
/// <summary> /// This is the method to use when adding a key in the editor. It will create /// the corresponding KeyWrapper and add it to the control. /// </summary> public KeyWrapper AddKey(CurveKey key, long id = -1) { Curve.Keys.Add(key); KeyWrapper newKey = new KeyWrapper(key, this, KeyTangentMode.Smooth, id); Control.Keys.Add(newKey); ComputeTangents(); return(newKey); }
public void Setup() { k1 = new CurveKey(12, 123); k2 = new CurveKey(11, 11); k3 = new CurveKey(-1, -11); c = new CurveKeyCollection(); c.Add(k1); c.Add(k2); c.Add(k3); }
private void MutateClipUsing(AnimationClip source, AnimationClip destination, IEnumerable <CurveKey> curvesToKeep) { AnimationUtility.GetCurveBindings(source) .Where(binding => curvesToKeep.Contains(CurveKey.FromBinding(binding))) .ToList() .ForEach(binding => { var curve = AnimationUtility.GetEditorCurve(source, binding); AnimationUtility.SetEditorCurve(destination, binding, curve); }); }
/// <summary> /// Initializes a KeyWrapper /// </summary> /// <param name="curveKey">The key to wrap</param> /// <param name="curve">The curve that owns curveKey</param> /// <param name="id">Pass -1 to autogenerate a new id</param> public KeyWrapper(CurveKey curveKey, CurveWrapper curve, KeyTangentMode tangentMode, long id = -1) { if (id < 0) Id = latestId++; else Id = id; Curve = curve; Key = curveKey; tangentInMode = tangentMode; tangentOutMode = tangentMode; }
public void Add() { CurveKey k = new CurveKey(123, 123); c.Add(k); Assert.AreEqual(4, c.Count, "#1"); Assert.AreEqual(k, c[3], "#2"); c.Add(k); Assert.AreEqual(5, c.Count, "#3"); Assert.AreEqual(k, c[4], "#4"); }
public Curve(CurveLoopType preLoop, CurveLoopType postLoop, IEnumerable<CurveKey> keys) { this.preLoop = preLoop; this.postLoop = postLoop; this.keys = new CurveKeyCollection(keys); this.keyFirst = (keys == null) ? null : this.keys[0]; this.keyLast = (keys == null) ? null : this.keys[this.keys.Count - 1]; this.length = (keys == null) ? 0f : keyLast.Position - keyFirst.Position; this.easingFunction = Evaluate; }
public void Clone() { CurveKey clone1 = c1.Clone(); CurveKey clone2 = c2.Clone(); // Make sure they arent the same instance Assert.AreNotSame(c1, clone1, "#1"); Assert.AreNotSame(c2, clone2, "#2"); Assert.AreEqual(c1, clone1, "#3"); Assert.AreEqual(c2, clone2, "#4"); }
/// <summary> /// Returns the KeyWrapper of the provided key. This is a relatively /// expensive O(n) method because it iterates over the values of a dict. /// </summary> public KeyWrapper GetWrapper(CurveKey key) { foreach (KeyWrapper wrapper in keys.Values) { if (Object.ReferenceEquals(wrapper.Key, key)) { return(wrapper); } } return(null); }
/// <summary> /// Moves the key by the given offset. It will actually remove the old key /// from the curve and add a new offseted curve. /// </summary> public void MoveKey(float positionOffset, float valueOffset) { // Copy the old key with offseted values. CurveKey newKey = new CurveKey(Key.Position + positionOffset, Key.Value + valueOffset); newKey.Continuity = Key.Continuity; newKey.TangentIn = Key.TangentIn; newKey.TangentOut = Key.TangentOut; // Add it to the curve. Curve.Curve.Keys.Remove(Key); Curve.Curve.Keys.Add(newKey); Key = newKey; }
/// <summary> /// Initializes a KeyWrapper /// </summary> /// <param name="curveKey">The key to wrap</param> /// <param name="curve">The curve that owns curveKey</param> /// <param name="id">Pass -1 to autogenerate a new id</param> public KeyWrapper(CurveKey curveKey, CurveWrapper curve, KeyTangentMode tangentMode, long id = -1) { if (id < 0) { Id = latestId++; } else { Id = id; } Curve = curve; Key = curveKey; tangentInMode = tangentMode; tangentOutMode = tangentMode; }
private static void SetCurveKeyTangent(ref CurveKey Prev, ref CurveKey Current, ref CurveKey Next) { float dt = Next.Position - Prev.Position; float dv = Next.Value - Prev.Value; if (Math.Abs(dv) < float.Epsilon) { Current.TangentIn = 0.0f; Current.TangentOut = 0.0f; } else { Current.TangentIn = dv * (Current.Position - Prev.Position) / dt; Current.TangentOut = dv * (Current.Position - Prev.Position) / dt; } }
/// <summary> /// Select intersected tangents. /// </summary> /// <param name="editCurveKeys">Selection target EditCurveKeys.</param> /// <param name="selectRegion">Select region in unit coordinate.</param> /// <param name="tangentLength">Tangent length in unit coordinate.</param> /// <param name="singleSelect">It select only one EditCurveKey /// if this value is true.</param> /// <returns>It returns true if any EditCurveKey selected; /// otherwise it returns false.</returns> public bool SelectTangents(ICollection <EditCurveKey> editCurveKeys, BoundingBox selectRegion, Vector2 tangentScale, bool singleSelect) { bool selected = false; foreach (EditCurveKey key in editCurveKeys) { CurveKey orgKey = key.OriginalKey; // User can't select stepped continuity key tangents. if (orgKey.Continuity == CurveContinuity.Step) { continue; } Vector3 rayOrigin = new Vector3(orgKey.Position, orgKey.Value, 0); for (int i = 0; i < 2; ++i) { float tsx = tangentScale.X; float tsy = tangentScale.Y; Vector3 dir = (i == 0) ? new Vector3(-tsx, -orgKey.TangentIn * tsy, 0) : new Vector3(tsx, orgKey.TangentOut * tsy, 0); float length = dir.Length(); dir.Normalize(); Ray ray = new Ray(rayOrigin, dir); Nullable <float> result; selectRegion.Intersects(ref ray, out result); if (result.HasValue && result.Value < length) { Select(key.Id, (i == 0)? EditCurveSelections.TangentIn: EditCurveSelections.TangentOut); selected = true; if (singleSelect) { break; } } } } return(selected); }
private static void SetCurveKeyTangent(ref CurveKey prev, ref CurveKey cur, ref CurveKey next) { float num = next.Position - prev.Position; float num2 = next.Value - prev.Value; if (Math.Abs(num2) < 1.401298E-45f) { cur.TangentIn = 0f; cur.TangentOut = 0f; } else { cur.TangentIn = num2 * (cur.Position - prev.Position) / num; cur.TangentOut = num2 * (next.Position - cur.Position) / num; } }
public static Curve ReadCurve(this BinaryReader reader) { Curve curve = new Curve(); int num = reader.ReadInt32(); for (int i = 0; i < num; i++) { float value = reader.ReadSingle(); float position = reader.ReadSingle(); float tangentIn = reader.ReadSingle(); float tangentOut = reader.ReadSingle(); CurveKey item = new CurveKey(position, value, tangentIn, tangentOut); curve.Keys.Add(item); } return(curve); }
private void SetCurveKeyTangent(ref CurveKey prev, ref CurveKey cur, ref CurveKey next) { float dt = next.Position - prev.Position; float dv = next.Value - prev.Value; if (Math.Abs(dv) < float.Epsilon) { cur.TangentIn = 0; cur.TangentOut = 0; } else { // The in and out tangents should be equal to the slope between the adjacent keys cur.TangentIn = dv * (cur.Position - prev.Position) / dt; cur.TangentOut = dv * (next.Position - cur.Position) / dt; } }
static void SetCurveKeyTangent(ref CurveKey prev, ref CurveKey cur, ref CurveKey next) { float dt = next.Position - prev.Position; float dv = next.Value - prev.Value; if (Math.Abs(dv) < float.Epsilon) { cur.TangentIn = 0; cur.TangentOut = 0; } else { // The in and out tangents should be equal to the slope between the adjacent keys. cur.TangentIn = dv * (cur.Position - prev.Position) / dt; cur.TangentOut = dv * (next.Position - cur.Position) / dt; } }
private void DrawTangents(Graphics g, EditCurve curve) { System.Drawing.Color[] colors = new System.Drawing.Color[] { System.Drawing.Color.Yellow, System.Drawing.Color.Brown }; float len = 50.0f; Vector2 pt1 = new Vector2(); EditCurveSelections tangentSelection = EditCurveSelections.Key | EditCurveSelections.TangentIn | EditCurveSelections.TangentOut; for (int i = 0; i < curve.Keys.Count; ++i) { EditCurveKey editKey = curve.Keys[i]; CurveKey key = editKey.OriginalKey; EditCurveSelections selection = editKey.Selection; bool isSelecting = (selection & tangentSelection) != 0; if (((isSelecting && curve.Editable) || curveTangentsViewMode == EditCurveView.Always) && key.Continuity == CurveContinuity.Smooth) { pt1 = curveView.ToPixelCoordinate(key.Position, key.Value); int colIdx = 0; float screen_tangent; // tan-in. colIdx = (selection & EditCurveSelections.TangentIn) != 0 ? 0 : 1; screen_tangent = curveView.UnitToScreenTangentAngle( key.TangentIn / curve.GetDistanceOfKeys(i, 0)); DrawArrow(g, pt1, (float)Math.PI + (float)Math.Atan(screen_tangent), len, colors[colIdx]); // tan-out. colIdx = (selection & EditCurveSelections.TangentOut) != 0 ? 0 : 1; screen_tangent = curveView.UnitToScreenTangentAngle( key.TangentOut / curve.GetDistanceOfKeys(i, 1)); DrawArrow(g, pt1, (float)Math.Atan(screen_tangent), len, colors[colIdx]); } } }
protected internal override CurveKeyCollection Deserialize( IntermediateReader input, ContentSerializerAttribute format, CurveKeyCollection existingInstance) { var result = existingInstance ?? new CurveKeyCollection(); if (input.Xml.HasValue) { var elements = PackedElementsHelper.ReadElements(input); if (elements.Length > 0) { // Each CurveKey consists of 5 elements if (elements.Length % 5 != 0) { throw new InvalidContentException( "Elements count in CurveKeyCollection is inncorect!"); } try { // Parse all CurveKeys for (int i = 0; i < elements.Length; i += 5) { // Order: Position, Value, TangentIn, TangentOut and Continuity var curveKey = new CurveKey (XmlConvert.ToSingle(elements[i]), XmlConvert.ToSingle(elements[i + 1]), XmlConvert.ToSingle(elements[i + 2]), XmlConvert.ToSingle(elements[i + 3]), (CurveContinuity)Enum.Parse( typeof(CurveContinuity), elements[i + 4], true)); result.Add(curveKey); } } catch (Exception e) { throw new InvalidContentException ("Error parsing CurveKey", e); } } } return(result); }
private static string ToFormattedName(CurveKey key, bool value, bool showFullPath) { var propertyName = key.PropertyName .Replace("blendShape.", "::") .Replace("localEulerAnglesRaw.", "rotation:") .Replace("m_LocalScale.", "scale:") .Replace("m_LocalPosition.", "position:"); var keyPath = showFullPath ? key.Path : SimplifyPath(key.Path); var niceName = propertyName + " @ " + keyPath; if (value) { return("<b>" + niceName + "</b>"); } return(niceName); }
/// <summary>Serializes a curve into a binary data stream</summary> /// <param name="writer">BinaryWriter to serialize the curve into</param> /// <param name="curve">Curve to be serialized</param> public static void Save(BinaryWriter writer, Curve curve) { // Save the curve's loop settings writer.Write((byte)curve.PreLoop); writer.Write((byte)curve.PostLoop); // Save the key frames contained in the curve writer.Write(curve.Keys.Count); for (int keyIndex = 0; keyIndex < curve.Keys.Count; ++keyIndex) { CurveKey key = curve.Keys[keyIndex]; writer.Write(key.Position); writer.Write(key.Value); writer.Write(key.TangentIn); writer.Write(key.TangentOut); writer.Write((byte)key.Continuity); } // for }
public void TestEquals() { #pragma warning disable 1718 CurveKey clone1 = new CurveKey(1f, 2f, 3f, -3f, CurveContinuity.Smooth); Assert.IsTrue(c1.Equals(c1), "#1"); Assert.IsTrue(c1.Equals(clone1), "#2"); Assert.IsFalse(c1.Equals(null), "#3"); Assert.IsFalse(c1.Equals(c2), "#5"); Assert.IsTrue(c1 == c1, "#6"); Assert.IsTrue(c1 == clone1, "#7"); Assert.IsFalse(c1 != c1, "#8"); Assert.IsFalse(c1 != clone1, "#9"); int a = 5; Assert.AreNotEqual(c1, a, "#10"); #pragma warning restore 1718 }
/// <summary> /// LoadContent will be called once per game and is the place to load /// all of your content. /// </summary> protected override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); Random rand = new Random(); Grafika.Init(Content, this.GraphicsDevice); GlobalAcc.Init(); Wyposazenie.Inicjalizacja(Content); Mapy.Init(this.GraphicsDevice); TestowyFont = Content.Load <SpriteFont>("testfont"); float[] pozycjeX = { 100, 100, 400, 400, 100 }; float[] pozycjeY = { 100, 300, 300, 100, 100 }; float [] czas = { 0f, 20f, 70f, 100f, 120f }; CurveKey [] KurwyX = new CurveKey[5]; CurveKey [] KurwyY = new CurveKey[5]; for (int i = 0; i < 5; i++) { KurwyX[i] = new CurveKey(czas[i], pozycjeX[i]); KurwyY[i] = new CurveKey(czas[i], pozycjeY[i]); } kolizja = new Obiekt(new Vector2(750f, 320f), "GuzikN", Content, 0f); GlobalAcc.ListaKolizji.Add(kolizja); GlobalAcc.ListaObiektow3.Add(kolizja); SpriteAnimated SA = new SpriteAnimated(300, 300, Grafika.Eksplozja3, 60); GlobalAcc.ListaAnimacji1.Add(SA); // TODO: use this.Content to load your game content here }
public void CreateSpileData(float Length, bool IsLoop) { TotalLength = Length; if (!IsLoop) { return; } var Key_p1 = new CurveKey() { Length = -(Keys[Keys.Count - 1].Value - Keys[0].Value).magnitude, Value = Keys[Keys.Count - 1].Value, }; var Key_p2 = new CurveKey() { Length = Key_p1.Length - (Keys[Keys.Count - 2].Value - Keys[Keys.Count - 1].Value).magnitude, Value = Keys[Keys.Count - 2].Value, }; var Key_n1 = new CurveKey() { Length = TotalLength + (Keys[Keys.Count - 1].Value - Keys[0].Value).magnitude, Value = Keys[0].Value, }; TotalLength = Key_n1.Length; var Key_n2 = new CurveKey() { Length = TotalLength + (Keys[0].Value - Keys[1].Value).magnitude, Value = Keys[1].Value, }; Keys.Insert(0, Key_p1); Keys.Insert(0, Key_p2); Keys.Add(Key_n1); Keys.Add(Key_n2); }
/// <summary> /// Evaluates a position outside of the curve. /// </summary> private Single EvaluateOutside(CurveLoopType loop, CurveKey loopKey, Single loopKeyTangent, Single position) { var offset = 0f; switch (loop) { case CurveLoopType.Constant: { return loopKey.Value; } case CurveLoopType.Linear: { return loopKey.Value - loopKeyTangent * (loopKey.Position - position); } case CurveLoopType.Cycle: { var cycle = GetCycleIndex(position); position = position - (cycle * length); } break; case CurveLoopType.CycleOffset: { var cycle = GetCycleIndex(position); position = position - (cycle * length); offset = (keyLast.Value - keyFirst.Value) * cycle; } break; case CurveLoopType.Oscillate: { var cycle = GetCycleIndex(position); position = position - (cycle * length); if (cycle % 2 != 0) { position = keyFirst.Position + (length - (position - keyFirst.Position)); } } break; } return offset + EvaluateInside(position); }
/// <summary> /// Finds the pair of keys which surrounds the specified position on the curve. /// </summary> private void FindKeysAtCurvePosition(Single position, out CurveKey key1, out CurveKey key2) { key1 = keys[0]; key2 = keys[keys.Count - 1]; for (var i = 1; i < keys.Count; i++) { if (keys[i].Position >= position && keys[i - 1].Position <= position) { key1 = keys[i - 1]; key2 = keys[i]; return; } } }
public void Clone() { var key = new CurveKey(1, 2, 3, 4, CurveContinuity.Step); Assert.AreEqual(key,key.Clone()); }
/// <summary> /// Perform move on found path /// </summary> /// <param name="Nodes">List of found path nodes</param> /// <param name="StartPosition">Point from where to start moving</param> /// <param name="EndPosition">Moving end point</param> public void PathMove(ref List<PathReturnNode> Nodes, Vector2 StartPosition, Vector2 EndPosition) { mMovingStatus = MovingType.PathSmooth; mCurveX.Keys.Clear(); mCurveY.Keys.Clear(); mNodePos = Nodes.Count - 1; //This needed to get directions mStartPosOnPath = new Point(Nodes[mNodePos].PosX, Nodes[mNodePos].PosY); mPrevPosOnPath = mStartPosOnPath; mCurrentPosOnPath = mStartPosOnPath; mEndPosOnPath = new Point(Nodes[0].PosX, Nodes[0].PosY); mDirFrom = GetDirection(mNodePos, ref Nodes); CurveKey key; //Loop throught all nodes for (int n = mNodePos - 1; n >= 0; n -= 1) { //Prew direction mDirTo = mDirFrom; //Get new direction mDirFrom = GetDirection(n, ref Nodes); //Add nodes in curves, LinearMoves array and DirTo are used to get the rigt spot key = new CurveKey((mNodePos - n) * mGridWidth, mCurrentPosOnPath.X * mGridWidth + mLinearMoves[mDirTo].X); mCurveX.Keys.Add(key); key = new CurveKey((mNodePos - n) * mGridWidth, mCurrentPosOnPath.Y * mGridWidth + mLinearMoves[mDirTo].Y); mCurveY.Keys.Add(key); mPrevPosOnPath = mCurrentPosOnPath; mCurrentPosOnPath = new Point(Nodes[n].PosX, Nodes[n].PosY); } //First and last nodes are inserted and are not path node based //We should get distance form first to second node to insert in in right time float firstDist = mGridWidth - Vector2.Distance(StartPosition, new Vector2(mCurveX.Keys[0].Value, mCurveY.Keys[0].Value)); key = new CurveKey(firstDist, StartPosition.X); mCurveX.Keys.Add(key); key = new CurveKey(firstDist, StartPosition.Y); mCurveY.Keys.Add(key); float secondDist = Vector2.Distance(EndPosition, new Vector2(mCurveX.Keys[mCurveX.Keys.Count - 1].Value, mCurveY.Keys[mCurveY.Keys.Count - 1].Value)) - mGridWidth; key = new CurveKey(Nodes.Count * mGridWidth + secondDist, EndPosition.X); mCurveX.Keys.Add(key); key = new CurveKey(Nodes.Count * mGridWidth + secondDist, EndPosition.Y); mCurveY.Keys.Add(key); if (mNodePos > 2) { mCurveX.Keys.RemoveAt(1); mCurveX.Keys.RemoveAt(mCurveX.Keys.Count - 2); mCurveY.Keys.RemoveAt(1); mCurveY.Keys.RemoveAt(mCurveY.Keys.Count - 2); } //Compute tangents for both curves mCurveX.ComputeTangents(CurveTangent.Smooth); mCurveY.ComputeTangents(CurveTangent.Smooth); //Set start and end positions on curve, used in Update mTime = firstDist; mTotalTime = Nodes.Count * mGridWidth + secondDist; }
/// <summary> /// Calculates a value between the specified curve keys based on the specified interpolation factor. /// </summary> private Single InterpolateValue(CurveKey key1, CurveKey key2, Single t) { switch (key1.Continuity) { case CurveContinuity.Step: return t >= 1 ? key2.Value : key1.Value; default: { var t2 = t * t; var t3 = t2 * t; var key1Value = key1.Value; var key2Value = key2.Value; var tangentIn = key2.TangentIn; var tangentOut = key1.TangentOut; var polynomial1 = (2.0 * t3 - 3.0 * t2 + 1.0); // (2t^3 - 3t^2 + 1) var polynomial2 = (t3 - 2.0 * t2 + t); // (t3 - 2t^2 + t) var polynomial3 = (-2.0 * t3 + 3.0 * t2); // (-2t^2 + 3t^2) var polynomial4 = (t3 - t2); // (t^3 - t^2) return (float)(key1Value * polynomial1 + tangentOut * polynomial2 + key2Value * polynomial3 + tangentIn * polynomial4); } } }
public void Setup() { c1 = new CurveKey(1f, 2f, 3f, -3f, CurveContinuity.Smooth); c2 = new CurveKey(2f, 4f, 6f, 6.7f, CurveContinuity.Step); }
private float FindSegment(float t, ref CurveKey k0, ref CurveKey k1) { float num1 = t; k0 = this.keys[0]; for (int index = 1; index < this.keys.Count; ++index) { k1 = this.keys[index]; if ((double)k1.position >= (double)t) { double num2 = (double)k0.position; double num3 = (double)k1.position; double num4 = (double)t; double num5 = num3 - num2; num1 = 0.0f; if (num5 > 0.0 / 1.0) { num1 = (float)((num4 - num2) / num5); break; } else break; } else k0 = k1; } return num1; }
private static float Hermite(CurveKey k0, CurveKey k1, float t) { if (k0.Continuity == CurveContinuity.Step) { if ((double)t >= 1.0) return k1.internalValue; else return k0.internalValue; } else { float num1 = t * t; float num2 = num1 * t; float num3 = k0.internalValue; float num4 = k1.internalValue; float num5 = k0.tangentOut; float num6 = k1.tangentIn; return (float)((double)num3 * (2.0 * (double)num2 - 3.0 * (double)num1 + 1.0) + (double)num4 * (-2.0 * (double)num2 + 3.0 * (double)num1) + (double)num5 * ((double)num2 - 2.0 * (double)num1 + (double)t) + (double)num6 * ((double)num2 - (double)num1)); } }
private float FindSegment(float t, ref CurveKey k0, ref CurveKey k1) { float num2 = t; k0 = this.keys[0]; for (int i = 1; i < this.keys.Count; i++) { k1 = this.keys[i]; if (k1.position >= t) { double position = k0.position; double num6 = k1.position; double num5 = t; double num3 = num6 - position; num2 = 0f; if (num3 > 1E-10) { num2 = (float) ((num5 - position) / num3); } return num2; } k0 = k1; } return num2; }
/// <summary> /// This is the method to use when adding a key in the editor. It will create /// the corresponding KeyWrapper and add it to the control. /// </summary> public KeyWrapper AddKey(CurveKey key, long id = -1) { Curve.Keys.Add(key); var newKey = new KeyWrapper(key, this, KeyTangentMode.Smooth, id); Control.Keys.Add(newKey); ComputeTangents(); return newKey; }
private static float Hermite(CurveKey k0, CurveKey k1, float t) { if (k0.Continuity == CurveContinuity.Step) { if (t >= 1f) { return k1.internalValue; } return k0.internalValue; } float num = t * t; float num2 = num * t; float internalValue = k0.internalValue; float num5 = k1.internalValue; float tangentOut = k0.tangentOut; float tangentIn = k1.tangentIn; return ((((internalValue * (((2f * num2) - (3f * num)) + 1f)) + (num5 * ((-2f * num2) + (3f * num)))) + (tangentOut * ((num2 - (2f * num)) + t))) + (tangentIn * (num2 - num))); }