| List<InteractibleObj> lBBoxes = new List<InteractibleObj>(); |
| |
| //*********************TESTING |
| List<Vector2> rotatedcaller = new List<Vector2>(); |
| List<Vector2> rotatedcandidate = new List<Vector2>(); |
| //****************************/TESTING |
| |
| //jsut check for the first iObj(palyer ) right now |
| if(true) |
| { |
| InteractibleObj caller = iObjs[0]; |
| lBBoxes = GetBoundingBoxCollisions(caller); |
| |
| foreach (InteractibleObj intbbox in lBBoxes) |
| if (intbbox != null) |
| { |
| int edges; int foundAxes; |
| List<Vector2> lProjectionAxes = new List<Vector2>(); |
| List<Vector2> lCallerPolyVertices = caller.GetPolygonVertices(); |
| List<Vector2> lCandidatePolyVertices = intbbox.GetPolygonVertices(); |
| Vector2 workVec, flippedWorkVec; |
| |
| #region Get projection axes |
| //Get projection axes for calling interactibleobj |
| edges = lCallerPolyVertices.Count; |
| |
| for (int i = 0; i < edges ; ++i) |
| { |
| workVec = lCallerPolyVertices[(i + 1) % edges] - lCallerPolyVertices[i]; |
| workVec.Normalize(); |
| flippedWorkVec = new Vector2(-workVec.X, -workVec.Y); |
| |
| for (int j = 0; j < lProjectionAxes.Count; ++j) |
| if (workVec == lProjectionAxes[j] || flippedWorkVec == lProjectionAxes[j]) |
| { |
| workVec = Vector2.Zero; |
| break; |
| } |
| |
| if(workVec != Vector2.Zero) |
| lProjectionAxes.Add(workVec); |
| |
| } |
| |
| foundAxes = lProjectionAxes.Count-1; |
| //Get projection axes for colliding interactibleobj |
| edges = lCandidatePolyVertices.Count; |
| |
| |
| |
| for (int i = 0; i < edges; ++i) |
| { |
| workVec = lCandidatePolyVertices[(i + 1) % edges] - lCandidatePolyVertices[i]; |
| workVec.Normalize(); |
| flippedWorkVec = new Vector2(-workVec.X, -workVec.Y); |
| |
| for (int j = 0; j < lProjectionAxes.Count; ++j) |
| if (workVec == lProjectionAxes[j] || flippedWorkVec == lProjectionAxes[j]) |
| { |
| workVec = Vector2.Zero; |
| break; |
| } |
| |
| if(workVec != Vector2.Zero) |
| lProjectionAxes.Add(workVec); |
| } |
| |
| #endregion |
| |
| |
| #region Project polygon onto each axes and test for collisions |
| |
| float candidateDP, callerDP; |
| double axisRotationAmount; |
| Vector2 callerPolyProj, candidatePolyProj, tempVector, tempVector2; |
| |
| List< Vector2> callerProjectionOut = new List<Vector2>(); |
| List<Vector2> lTest = new List<Vector2>(); |
| |
| //Project each poloygon onto each axis and then check if they overlap. |
| foreach (Vector2 pAxes in lProjectionAxes) |
| { |
| callerPolyProj = new Vector2(10000000000, -100000); |
| candidatePolyProj = callerPolyProj; |
| |
| //Axes that are not 0,1 or 1,0 need their points rotated onto these axis. |
| tempVector = (Vector2.Dot(lCallerPolyVertices[0], pAxes) * pAxes); |
| tempVector2 = tempVector - (Vector2.Dot(lCandidatePolyVertices[0], pAxes) * pAxes); |
| axisRotationAmount = Math.PI - Math.Atan2(tempVector2.Y, tempVector2.X) + Math.PI; |
| |
| |
| |
| //project caller onto the current axis |
| for (int i = 0; i < lCallerPolyVertices.Count; ++i) |
| { |
| callerDP = Vector2.Dot(lCallerPolyVertices[i], pAxes); |
| |
| if (pAxes != Vector2.UnitX && pAxes != Vector2.UnitY) |
| { |
| tempVector = callerDP * pAxes; |
| rotatedcaller.Add(tempVector); |
| callerDP = Vector2.Transform(tempVector, Matrix.CreateRotationZ((float)axisRotationAmount)).X; |
|
| } |
| |
| |
| if (callerDP < callerPolyProj.X) |
| callerPolyProj.X = callerDP; |
| if (callerDP > callerPolyProj.Y) |
| callerPolyProj.Y = callerDP; |
| } |
| |
| //project candidate onto the current axis |
| for (int i = 0; i < lCandidatePolyVertices.Count; ++i) |
| { |
| candidateDP = Vector2.Dot(lCandidatePolyVertices[i], pAxes); |
| |
| if (pAxes != Vector2.UnitX && pAxes != Vector2.UnitY) |
| { |
| tempVector = candidateDP * pAxes; |
| rotatedcandidate.Add(tempVector); |
| candidateDP = Vector2.Transform(tempVector, Matrix.CreateRotationZ((float)axisRotationAmount)).X; |
| } |
| |
| |
| if (candidateDP < candidatePolyProj.X) |
| candidatePolyProj.X = candidateDP; |
| if (candidateDP > candidatePolyProj.Y) |
| candidatePolyProj.Y = candidateDP; |
| } |
| |
| |
| |
| |
| //Comparison check to see if the projections overlap |
| if (callerPolyProj.X < callerPolyProj.Y && callerPolyProj.Y < candidatePolyProj.X |
| || |
| candidatePolyProj.X < candidatePolyProj.Y && candidatePolyProj.Y < callerPolyProj.X) |
| { |
| //No overlap, meaning no collision, so exit loop |
| callerProjectionOut.Clear(); |
| callerProjectionOut.Add(Vector2.Zero); |
| break; |
| |
| } |
| else //calculate vector to get caller unstuck |
| { |
| if (callerPolyProj.X <= candidatePolyProj.X) |
| { |
| callerProjectionOut.Add((candidatePolyProj.X - callerPolyProj.Y) * pAxes); |
| } |
| if (candidatePolyProj.X <= callerPolyProj.X) |
| { |
| |
| callerProjectionOut.Add((candidatePolyProj.Y - callerPolyProj.X) * pAxes); |
| } |
| |
| |
| //callerProjectionOut.Clear(); |
| //callerProjectionOut.Add(new Vector2(-mx,-my)); |
| |
| |
| } |
| |
| |
| |
| |
| } |
| |
| #endregion |
| |
| |
| callerProjectionOut.Sort(intbbox.vecCom); |
| caller.CurrPosition += callerProjectionOut[0]; |
| |
| } |
| |
| |
| |
| } |