public static Vector2f MovementLeft(Vector2f _currentMovementVec, Dictionary <int, Vector2f> _movingObjVerts, Dictionary <int, Vector2f> _stationaryObjVerts) { Vector2f returnVec = _currentMovementVec; var movingObjVertsSorted = from entry in _movingObjVerts orderby entry.Value.X ascending select entry; var stationaryObjVertsSorted = from entry in _stationaryObjVerts orderby entry.Value.X descending select entry; //Console.WriteLine(string.Format("{0} {1} {2} {3}", movingObjVertsSorted.ToList()[0], movingObjVertsSorted.ToList()[1], movingObjVertsSorted.ToList()[2], movingObjVertsSorted.ToList()[3])); LinearEquation movingObjEqu = TackMath.GetLinearEquationFromPoints(movingObjVertsSorted.ToList()[0].Value, movingObjVertsSorted.ToList()[1].Value); LinearEquation stationaryObjEqu = TackMath.GetLinearEquationFromPoints(stationaryObjVertsSorted.ToList()[0].Value, stationaryObjVertsSorted.ToList()[1].Value); Console.WriteLine("vert: " + movingObjVertsSorted.ToList()[0].Value); Console.WriteLine(string.Format("{0} | {1}", movingObjEqu.ToString(), stationaryObjEqu.ToString())); if (TackMath.GetLinearIntersectionPoint(movingObjEqu, stationaryObjEqu, out Vector2f interestionPoint)) { Console.WriteLine("1 yes"); if (interestionPoint.X <= stationaryObjVertsSorted.ToList()[0].Value.X && interestionPoint.X >= stationaryObjVertsSorted.ToList()[1].Value.X) { Console.WriteLine("2 yes"); if (interestionPoint.Y <= stationaryObjVertsSorted.ToList()[0].Value.Y && interestionPoint.Y >= stationaryObjVertsSorted.ToList()[1].Value.Y) { Console.WriteLine("Intersection has been achieved: " + interestionPoint.ToString()); } } } Console.WriteLine("Point: " + interestionPoint.ToString()); return(returnVec); }
/// <summary> /// Calculates the actual vertex point with position and rotation factored in /// </summary> /// <param name="_vertIndex">The index of the vertex of which to find the position of (1 - 4)</param> /// <returns>The position, in Vector2f form, of the vertex</returns> public Vector2f FindVertexPoint(int _vertIndex) { /* * Multiplying matrix stuff * Formulas from: https://math.stackexchange.com/questions/384186/calculate-new-positon-of-rectangle-corners-based-on-angle * * x = x0 + (x - x0) * cos(angle) + (y - y0) * sin(angle) * y = y0 - (x - x0) * sin(angle) + (y - y0) * cos(angle) * * angle = angle of rotation in degrees * x/y = the current point if the vertex * x0/y0 = the centre point of the rectangle (the rotation point) * */ TackObject parentObject = GetParent(); if (_vertIndex < 1 || _vertIndex > 4) { TackConsole.EngineLog(EngineLogType.Error, string.Format("Cannot calculate the position of the vertex with index : {0}. Index values must be within the range (1-4, inclusive)", _vertIndex)); return(new Vector2f()); } RectangleShape objectShape = new RectangleShape() { X = ((parentObject.Position.X) - (parentObject.Scale.X / 2)), Y = ((parentObject.Position.Y) + (parentObject.Scale.Y / 2)), Width = (parentObject.Scale.X), Height = (parentObject.Scale.Y) }; if (_vertIndex == 1) { Vector2f vertPos = new Vector2f(objectShape.X + objectShape.Width, objectShape.Y); float x = parentObject.Position.X + (vertPos.X - parentObject.Position.X) * (float)System.Math.Cos(TackMath.DegToRad(parentObject.Rotation)) + (vertPos.Y - parentObject.Position.Y) * (float)System.Math.Sin(TackMath.DegToRad(parentObject.Rotation)); float y = parentObject.Position.Y - (vertPos.X - parentObject.Position.X) * (float)System.Math.Sin(TackMath.DegToRad(parentObject.Rotation)) + (vertPos.Y - parentObject.Position.Y) * (float)System.Math.Cos(TackMath.DegToRad(parentObject.Rotation)); return(new Vector2f(x, y)); } if (_vertIndex == 2) { Vector2f vertPos = new Vector2f(objectShape.X + objectShape.Width, objectShape.Y - objectShape.Height); float x = parentObject.Position.X + (vertPos.X - parentObject.Position.X) * (float)System.Math.Cos(TackMath.DegToRad(parentObject.Rotation)) + (vertPos.Y - parentObject.Position.Y) * (float)System.Math.Sin(TackMath.DegToRad(parentObject.Rotation)); float y = parentObject.Position.Y - (vertPos.X - parentObject.Position.X) * (float)System.Math.Sin(TackMath.DegToRad(parentObject.Rotation)) + (vertPos.Y - parentObject.Position.Y) * (float)System.Math.Cos(TackMath.DegToRad(parentObject.Rotation)); return(new Vector2f(x, y)); } if (_vertIndex == 3) { Vector2f vertPos = new Vector2f(objectShape.X, objectShape.Y - objectShape.Height); float x = parentObject.Position.X + (vertPos.X - parentObject.Position.X) * (float)System.Math.Cos(TackMath.DegToRad(parentObject.Rotation)) + (vertPos.Y - parentObject.Position.Y) * (float)System.Math.Sin(TackMath.DegToRad(parentObject.Rotation)); float y = parentObject.Position.Y - (vertPos.X - parentObject.Position.X) * (float)System.Math.Sin(TackMath.DegToRad(parentObject.Rotation)) + (vertPos.Y - parentObject.Position.Y) * (float)System.Math.Cos(TackMath.DegToRad(parentObject.Rotation)); return(new Vector2f(x, y)); } if (_vertIndex == 4) { Vector2f vertPos = new Vector2f(objectShape.X, objectShape.Y); float x = GetParent().Position.X + (vertPos.X - GetParent().Position.X) * (float)System.Math.Cos(TackMath.DegToRad(GetParent().Rotation)) + (vertPos.Y - GetParent().Position.Y) * (float)System.Math.Sin(TackMath.DegToRad(GetParent().Rotation)); float y = GetParent().Position.Y - (vertPos.X - GetParent().Position.X) * (float)System.Math.Sin(TackMath.DegToRad(GetParent().Rotation)) + (vertPos.Y - GetParent().Position.Y) * (float)System.Math.Cos(TackMath.DegToRad(GetParent().Rotation)); return(new Vector2f(x, y)); } return(new Vector2f(-1, -1)); }