/// <summary> /// Rotates a <c>Vector</c> <c>Absolute or Relative</c> /// to the defined <c>Angle</c> /// </summary> /// <param name="v">The Vector to Rotate</param> /// <param name="rad">The Angle</param> /// <param name="absolute">Determines if the Angle is absolute or not</param> /// <returns>The rotated Vector2</returns> public static Vector2 Rotate(this Vector2 v, float rad, bool absolute) { Vector2 vOut = new Vector2(v.X, v.Y); vOut = Vector2.Transform( vOut, Matrix.CreateRotationZ( rad - (absolute ? vOut.GetRotation() : 0))); return vOut; }
public static Vector2 Rotate(this Vector2 src, Vector2 pivot, float amount) { var rot = pivot.GetRotation(src); rot += amount; var dir = new Vector2((float)Math.Cos(rot), (float)Math.Sin(rot)); return(dir * (src - pivot).Length() + pivot); }
private void lookAt(Vector2 from, Vector2 target) { desiredRotation = from.GetRotation(target);// -MathHelper.PiOver2; //transform.Orientation = desiredRotation; var rotation = MathHelper.WrapAngle(transform.DerivedOrientation); var rotationAmount = MathHelper.WrapAngle(desiredRotation - rotation); if (Math.Abs(rotationAmount) < maxTurnSpeed) { transform.Orientation = desiredRotation; } else { //rotationAmount = MathHelper.Clamp(rotationAmount, -0.05f, 0.05f); transform.Orientation += MathHelper.Clamp(rotationAmount, -maxTurnSpeed, maxTurnSpeed);; } }
//public float maxTurnSpeed = 0.065f; private void lookAt(Vector2 from, Vector2 target) { desiredRotation = from.GetRotation(target);// -MathHelper.PiOver2; //transform.Orientation = desiredRotation; var rotation = MathHelper.WrapAngle(transform.DerivedOrientation); var rotationAmount = MathHelper.WrapAngle(desiredRotation - rotation); //if (Math.Abs(rotationAmount) < maxTurnSpeed) // transform.Orientation = desiredRotation; //else if (Math.Abs(rotationAmount) < 1.5f) { var mtsq = ((Math.Abs(rotationAmount) / 1.5f)); //var mtsq = ((Math.Abs(rotationAmount) / 1.5f) + 1) / 2f; //var mstq = random.Next(0.5f, 0.75f); transform.Orientation += (MathHelper.Clamp(rotationAmount, -maxTurnSpeed, maxTurnSpeed) * mtsq); } else { //rotationAmount = MathHelper.Clamp(rotationAmount, -0.05f, 0.05f); transform.Orientation += MathHelper.Clamp(rotationAmount, -maxTurnSpeed, maxTurnSpeed); } }
protected override void InitializeObject(string data) { string[] temp = data.Split(','); values = new float[Enum.GetValues(typeof(JumpPadValues)).Length]; for(int i = 0; i< temp.Length;i++) { try { values[i] = float.Parse(temp[i], CultureInfo.CreateSpecificCulture("en-us")); } catch (Exception e) { DebugManager.AddItem(e.Message, this.ToString(), new StackTrace(e), System.Drawing.Color.Red); FileManager.WriteInErrorLog(this, "Failed to load Data of "+ Type +": \n" + e.Message, e.GetType()); } } timeThreshold = values[(int)JumpPadValues.Fallof]; blank = Content.Load<Texture2D>("Stuff\\Blank"); jumpPadTex = Content.Load<Texture2D>(GameParameters.CurrentGraphXPackPath + "Level/InteractiveObjects/JumpPad"); drawRects = new ExtendedRectangle[5]; drawRectsAlpha = new byte[5]; drawRectsTime = new float[5]; ActionRectangle = new Rectangle( (int)values[(int)JumpPadValues.RectangleX], (int)values[(int)JumpPadValues.RectangleY], (int)values[(int)JumpPadValues.RectangleWidth], (int)values[(int)JumpPadValues.RectangleHeight]); force = new Vector2(values[(int)JumpPadValues.SpeedX],values[(int)JumpPadValues.SpeedY]); float rotation = force.GetRotation(); for (int i = 0; i < drawRects.Length; i++) { drawRectsTime[i] = (float)i / drawRects.Length * AnimationTimeThreshold; Vector2 offset = (force / MaxForceLength) * (((float)i / drawRects.Length) * MaxLengthDrawRects); drawRects[i] = ExtendedRectangle.Transform(new Rectangle( (int)values[(int)JumpPadValues.RectangleX], (int)values[(int)JumpPadValues.RectangleY], 32, 2), new Vector2(blank.Width,blank.Height), new Vector2((int)values[(int)JumpPadValues.RectangleWidth] / 2, (int)values[(int)JumpPadValues.RectangleHeight] / 2), offset, force.GetRotation() - (float)Math.PI/2); } particles = new ParticleSystem(); particles.Initialize(false, SpawnType.Fontaine, 50, 100, 3, Color.White, new Vector2(values[(int)JumpPadValues.RectangleX] , values[(int)JumpPadValues.RectangleY]), new Vector2(values[(int)JumpPadValues.RectangleX] + values[(int)JumpPadValues.RectangleWidth], values[(int)JumpPadValues.RectangleY] + values[(int)JumpPadValues.RectangleHeight]), SpawnDirections.Angle, force /MaxForceLength * 1000, blank, GravityType.OverallForce, Vector2.Zero, 1, XNAParticleSystem.CollisionType.None); Options.InitObjectHolder.particleManagers[EGameState.Game].Add(particles); particles.Reset(); particles.UseMinSpeed = true; particles.MinSpeed = force / MaxForceLength * 500; particles.SpawnAngle = 0.0f; particles.AirFriction = 0.95f; //particles.UseCuda(true); }
//Finds the angle (radians) between two vectors by considering two lines drawn from each vector to a shared pivot point. //Rotation is calculated clockwise starting at the first vector. //The end result x always satisfies 0 <= x < TwoPi. //Note: the length of each line drawn to the pivot matters unless the angle is an exact multiple of PiOver2 (90 degrees). // // Examples (P = pivot point, v1 = first vector, v2 = second vector): // // |[v1] \[v2] =(rotated 1/3 left into quadrant 2) \[v1] =(rotated 1/3 left into quadrant // | \ \ // | \ \ // | \ \ // | x=PiOver2=90degrees \ x=(5/3)Pi=240degrees \ x=(4/3)Pi=120degrees // P---------------------------------[v2] P-------------[v1] =(along x axis) P-------------[v2] =(along x axis) // public static float GetAngleBetweenVectors(this Vector2 point, Vector2 other, Vector2 pivot) { float angle = point.GetRotation(pivot) - other.GetRotation(pivot); return(angle < 0 ? angle + MathHelper.TwoPi : angle); }