public static Func<float, float> FromCurve(Curve c) { return (f) => { return c.Evaluate(f); }; }
public void EvaluateNoKeys() { var curve = new Curve(); var result = curve.Evaluate(1.25f); Assert.AreEqual(0f, result); }
public void EvaluateOneKey() { var curve = new Curve(); curve.Keys.Add(new CurveKey(1, -1)); var result = curve.Evaluate(1.25f); Assert.AreEqual(-1f, result); }
public void Evaluate() { var curve = new Curve(); curve.Keys.Add(new CurveKey(1, -1)); curve.Keys.Add(new CurveKey(2, 2)); curve.Keys.Add(new CurveKey(3, 4)); var result = curve.Evaluate(1.25f); Assert.AreEqual(-0.53125f, result); }
/// <summary> /// Draws a line based on a Curve object /// </summary> /// <param name="curve">The Curve object (can only have 1 value for each Position value)</param> public void drawCurve(Curve curve) { Vector2 first = new Vector2(curve.Keys[0].Position, curve.Keys[0].Value); Vector2 second = new Vector2(curve.Keys[curve.Keys.Count - 1].Position, curve.Keys[curve.Keys.Count - 1].Value); int numVertices = (int)Vector2.Distance(first, second); List<VertexPositionColor> vertices = new List<VertexPositionColor>(); for (int i = 0; i < numVertices; ++i) { Vector3 pos = new Vector3(curve.Evaluate(i), i, 0); vertices.Add(new VertexPositionColor(Vector3.Transform(pos, transformMatrix), color)); } basicEffect.CurrentTechnique.Passes[0].Apply(); graphics.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.LineStrip, vertices.ToArray(), 0, vertices.Count - 1, VertexPositionColor.VertexDeclaration); }
public Platform(int x, int y, int width, int height, int minH, int maxH, int res) : base(null, new Vector2(x, y), new Vector2(width, height)) { heights = new int[(int)dim.X]; heights[heights.Length - 1] = 0; heights[0] = 0; Curve c=new Curve(); for (int i = res; i< heights.Length; i += res) { heights[i] = RNG.Next(minH, maxH); c.Keys.Add(new CurveKey(i, heights[i])); } for (int i = 0; i < heights.Length; i++) { heights[i] = (int)c.Evaluate(i); } redoCurve(); }
/// <summary> /// Initialises fields <c>shadowVertexData</c> and <c>shadowIndexData</c> /// to a shadow 3D model that fills the current client bounds. /// </summary> private void InitializeShadow() { var alphaCurve = new Curve(); // value of alpha as a function of distance in pixels from shadow origin alphaCurve.Keys.Add(new CurveKey(0, 0)); alphaCurve.Keys.Add(new CurveKey(300, 0.07f)); alphaCurve.Keys.Add(new CurveKey(500, 0.32f)); alphaCurve.Keys.Add(new CurveKey(700, 0.65f)); alphaCurve.Keys.Add(new CurveKey(900, 0.91f)); alphaCurve.Keys.Add(new CurveKey(1200, 0.97f)); alphaCurve.PreLoop = CurveLoopType.Constant; alphaCurve.PostLoop = CurveLoopType.Constant; alphaCurve.ComputeTangents(CurveTangent.Smooth); _shadowSize = new Point(ViewportWidth, ViewportHeight); // The shadow is a rectangle that spans a grid of vertices, each // of them black but with different levels of alpha. // The origin of the shadow 3D model is at the view center. var shadowDimensions = new Vector2(ViewportWidth, ViewportHeight); int gridWidth = (int)shadowDimensions.X / 30; int gridHeight = (int)shadowDimensions.Y / 30; var vertexData = new List<VertexPositionColor>(); var indexData = new List<short>(); for (int y = 0; y < gridHeight; ++y) for (int x = 0; x < gridWidth; ++x) { var posInShadow = shadowDimensions * new Vector2((float)x / (gridWidth - 1) - 0.5f, (float)y / (gridHeight - 1) - 0.5f); float curvePos = (posInShadow * new Vector2(1, 1.5f)).Length(); vertexData.Add(new VertexPositionColor( new Vector3(posInShadow, 0), Color.Multiply(Color.Black, alphaCurve.Evaluate(curvePos)))); if (y > 0) checked { if (x > 0) { indexData.Add((short)(y * gridWidth + x)); indexData.Add((short)((y - 1) * gridWidth + x)); indexData.Add((short)(y * gridWidth + x - 1)); } if (x < gridWidth - 1) { indexData.Add((short)(y * gridWidth + x)); indexData.Add((short)((y - 1) * gridWidth + x + 1)); indexData.Add((short)((y - 1) * gridWidth + x)); } } } _shadowVertexData = vertexData.ToArray(); _shadowIndexData = indexData.ToArray(); }
/// <summary> /// Performs a complete update of the path. /// NOTE: should not be performed on a path /// in simulation. /// </summary> public void Update() { float distance = 0.0f; float k; Body tempBody; Vector2 tempVectorA = new Vector2(); Vector2 tempVectorB; Vector2 tempVectorC = new Vector2(); if (_recalculate) // only do the update if something has changed { // first we get our curve ready Curve xCurve = new Curve(); Curve yCurve = new Curve(); float curveIncrement = 1.0f/_controlPoints.Count; for (int i = 0; i < _controlPoints.Count; i++) // for all the control points { k = curveIncrement*(i + 1); xCurve.Keys.Add(new CurveKey(k, _controlPoints[i].X)); // set the keys for x and y yCurve.Keys.Add(new CurveKey(k, _controlPoints[i].Y)); // with a time from 0-1 } k = 0.0f; xCurve.ComputeTangents(CurveTangent.Smooth); // compute x tangents yCurve.ComputeTangents(CurveTangent.Smooth); // compute y tangents // next we find the first point at 1/2 the width because we are finding where the body's center will be placed while (distance < (_width/2.0f)) // while the distance along the curve is <= to width / 2 { k += _precision; // we increment along the line at this precision coeffient tempVectorA = new Vector2(xCurve.Evaluate(k), yCurve.Evaluate(k)); distance = Vector2.Distance(_controlPoints[0], tempVectorA); // get the distance } while (distance < _width) // while the distance along the curve is <= to width / 2 { k += _precision; // we increment along the line at this precision coeffient tempVectorC = new Vector2(xCurve.Evaluate(k), yCurve.Evaluate(k)); distance = Vector2.Distance(_controlPoints[0], tempVectorC); // get the distance } tempBody = BodyFactory.Instance.CreateRectangleBody(_width, _height, _mass); // create the first body tempBody.Position = tempVectorA; tempBody.Rotation = FindNormalAngle(FindVertexNormal(_controlPoints[0], tempVectorA, tempVectorC)); // set the angle _bodies.Add(tempBody); // add the first body tempVectorB = tempVectorA; // now that our first body is done we can start on all our other body's // since the curve was created with a time of 0-1 we can just stop creating bodies when k is 1 while (k < 1.0f) { distance = 0.0f; // next we find the first point at the width because we are finding where the body's center will be placed while ((distance < _width) && (k < 1.0f)) // while the distance along the curve is <= to width { k += _precision; // we increment along the line at this precision coeffient tempVectorA = new Vector2(xCurve.Evaluate(k), yCurve.Evaluate(k)); distance = Vector2.Distance(tempVectorA, tempVectorB); // get the distance } distance = 0.0f; while ((distance < _width) && (k < 1.0f)) // while the distance along the curve is <= to width { k += _precision; // we increment along the line at this precision coeffient tempVectorC = new Vector2(xCurve.Evaluate(k), yCurve.Evaluate(k)); distance = Vector2.Distance(tempVectorA, tempVectorC); // get the distance } tempBody = BodyFactory.Instance.CreateRectangleBody(_width, _height, _mass); // create the first body tempBody.Position = tempVectorA; tempBody.Rotation = FindNormalAngle(FindVertexNormal(tempVectorB, tempVectorA, tempVectorC)); // set the angle _bodies.Add(tempBody); // add all the rest of the bodies tempVectorB = tempVectorA; } MoveControlPoint(tempVectorC, _controlPoints.Count - 1); _recalculate = false; } }
/// <param name="bonusBoxDirection"> Entry or Exit Direction</param> /// <param name="bonusBoxEffect"> Avoid or Close</param> /// <param name="adjustment"> Place where the adjustment starts</param> private Vector2 GetScale(Curve bonusBoxDirection, Curve bonusBoxEffect, Vector2 adjustment) { return new Vector2((adjustment.X - bonusBoxDirection.Keys[bonusBoxDirection.Keys.Count - 1].Value) / (bonusBoxDirection.Evaluate(0) - bonusBoxDirection.Keys[bonusBoxDirection.Keys.Count - 1].Value), (adjustment.Y - bonusBoxEffect.Keys[bonusBoxEffect.Keys.Count - 1].Value) / (bonusBoxEffect.Evaluate(0) - bonusBoxEffect.Keys[bonusBoxEffect.Keys.Count - 1].Value)); }
/// <param name="bonusBoxDirection"> Entry or Exit Direction</param> /// <param name="bonusBoxEffect"> Avoid or Close</param> /// <param name="adjustment"> Place where the adjustment starts</param> private Vector2 GetExitShift(Curve bonusBoxDirection, Curve bonusBoxEffect, Vector2 adjustment) { return new Vector2(adjustment.Y - bonusBoxDirection.Evaluate(0), adjustment.Y - bonusBoxEffect.Evaluate(0)); }
/// <param name="bonusBoxDirection"> Entry or Exit Direction</param> /// <param name="bonusBoxEffect"> Avoid or Close</param> /// <param name="slideTime"> Time passed since entry or exit</param> private Vector2 GetCurvePos(Curve bonusBoxDirection, Curve bonusBoxEffect, float slideTime) { return new Vector2(bonusBoxDirection.Evaluate(slideTime), bonusBoxEffect.Evaluate(slideTime)); }
public void redoCurve() { Curve c = new Curve(); for (int i=0; i<heights.Length; i+=25) c.Keys.Add(new CurveKey(i, heights[i])); for (int i = 0; i < heights.Length; i++) heights[i] = (int)c.Evaluate(i); }