public SPoint ExitDistN(int iInd, SPoint origin, float dir, Ninja plr, int ifGrab) { // helper function for the Separating Axis Theorem that takes an axis defined by origin and dir //re-conditioned to handle the parameters of the attack box bool isHit = true; float projMin, projMax, hitMin, hitMax, distSq, projVal, cVal; float dHitMinSq = 1000; float dHitMaxSq = 0; float dPrMinSq = 1000; float dPrMaxSq = 0; SPoint projVec, nVec, rVec, cVec; projVec = new SPoint(); SPoint moveVec = new SPoint(); projMin = STAGE_LIMIT; projMax = -STAGE_LIMIT; hitMin = STAGE_LIMIT; hitMax = -STAGE_LIMIT; moveVec.x = plr.GetPos().x - plr.GetLastPos().x; moveVec.y = plr.GetPos().y - plr.GetLastPos().y; SPoint[] plBox; int hbLen = 4; if (ifGrab < 0) {//implies groundCheck plBox = plr.GetHitBox(); hbLen = 4; } else { plBox = plr.GetCurrentColBox(); hbLen = 6; } for (int i = 0; i < tBox.GetJlength(iInd); i++) { projVec.x = tBox.GetSPoint(iInd, i).x - origin.x; projVec.y = tBox.GetSPoint(iInd, i).y - origin.y; distSq = projVec.x * projVec.x + projVec.y * projVec.y; nVec = projVec.GetNormal(); cVec = new SPoint(Mathf.Cos(dir + Mathf.PI / 2), Mathf.Sin(dir + Mathf.PI / 2)); cVal = -cVec.Dot(nVec); //projVal = sin(rAng)*abs(sin(rAng))*distSq; projVal = cVal * Mathf.Abs(cVal) * distSq; if (projVal < projMin){ projMin = projVal; dPrMinSq = distSq; } if (projVal > projMax) { projMax = projVal; dPrMaxSq = distSq; } } if (dPrMinSq != 0) projMin = projMin / Mathf.Sqrt(dPrMinSq); if (dPrMaxSq != 0) projMax = projMax / Mathf.Sqrt(dPrMaxSq); if (dHitMinSq != 0) hitMin = hitMin / Mathf.Sqrt(dHitMinSq); if (dHitMaxSq != 0) hitMax = hitMax / Mathf.Sqrt(dHitMaxSq); for (int i = 0; i < hbLen; i++) { projVec.x = plBox[i].x - origin.x; projVec.y = plBox[i].y - origin.y; distSq = projVec.x * projVec.x + projVec.y * projVec.y; cVec = new SPoint(Mathf.Cos(dir + Mathf.PI / 2), Mathf.Sin(dir + Mathf.PI / 2)); nVec = projVec.GetNormal(); cVal = -cVec.Dot(nVec); //projVal = sin(rAng)*abs(sin(rAng))*distSq; projVal = cVal * Mathf.Abs(cVal) * distSq; if (projVal < hitMin){ hitMin = projVal; dHitMinSq = distSq; }if (projVal > hitMax){ hitMax = projVal; dHitMaxSq = distSq; } } if ((hitMax <= projMin) || (projMax <= hitMin)) isHit = false; if ((Mathf.Abs(projMax - hitMin) < EPS) || (Mathf.Abs(hitMax - projMin) < EPS)) isHit = false; //determine exit vector here float moveAng = Mathf.Atan2(moveVec.y, moveVec.x); float angDiff = Mathf.Abs(moveAng - (dir - Mathf.PI/2)) ; if(angDiff > Mathf.PI) angDiff = Mathf.Abs(angDiff - Mathf.PI*2); float moveDist=0; if(isHit){ if(angDiff < Mathf.PI/2) //moveVector in direction of exit vector moveDist = -Mathf.Abs(hitMax - projMin)/2; else moveDist = Mathf.Abs(projMax - hitMin)/2; } moveVec.x = moveDist*Mathf.Cos(dir - Mathf.PI/2); moveVec.y = moveDist*Mathf.Sin(dir - Mathf.PI/2); if(!isHit){ moveVec.x = 0; moveVec.y = 0; } return moveVec; }
public bool CheckAxis( SPoint origin, float dir, SPoint[] plBox, SPoint[] aBox, int pNum, int aNum) { // helper function for the Separating Axis Theorem that takes an axis defined by origin and dir //re-conditioned to handle the parameters of the attack box bool isHit = true; float projMin, projMax, hitMin, hitMax, distSq, projVal, cVal; SPoint projVec, nVec, rVec, cVec; projVec = new SPoint(); projMin = AXIS_LIMIT; projMax = -AXIS_LIMIT; hitMin = AXIS_LIMIT; hitMax = -AXIS_LIMIT; for(int i = 0; i < aNum; i++){ projVec.x = aBox[i].x - origin.x; projVec.y = aBox[i].y - origin.y; distSq = projVec.x*projVec.x + projVec.y*projVec.y; nVec=projVec.GetNormal(); cVec = new SPoint(Mathf.Cos(dir+Mathf.PI/2),Mathf.Sin(dir+Mathf.PI/2)); cVal = -cVec.Dot(nVec); //projVal = sin(rAng)*abs(sin(rAng))*distSq; projVal = cVal*Mathf.Abs(cVal)*distSq; if(projVal < projMin) projMin = projVal; if(projVal > projMax) projMax = projVal; } for(int i = 0; i < pNum; i++){ projVec.x = plBox[i].x - origin.x; projVec.y = plBox[i].y - origin.y; distSq = projVec.x*projVec.x + projVec.y*projVec.y; cVec = new SPoint(Mathf.Cos(dir+Mathf.PI/2),Mathf.Sin(dir+Mathf.PI/2)); nVec = projVec.GetNormal(); cVal = -cVec.Dot(nVec); //projVal = sin(rAng)*abs(sin(rAng))*distSq; projVal = cVal*Mathf.Abs(cVal)*distSq; if(projVal < hitMin) hitMin = projVal; if(projVal > hitMax) hitMax = projVal; } if((hitMax <= projMin)||(projMax <= hitMin)) isHit=false; if((Mathf.Abs(projMax - hitMin)< EPS)||(Mathf.Abs(hitMax - projMin)< EPS)) isHit=false; return isHit; }
public bool CheckAxis(SPoint origin, float dir, SPoint pCen, SPoint[] aBox, int aNum) { // helper function for the Separating Axis Theorem that takes an axis defined by origin and dir //re-conditioned to handle the parameters of the attack box bool isHit = true; float projMin, projMax, hitMin, hitMax, distSq, projVal, cVal; SPoint projVec, nVec, rVec, cVec; projVec = new SPoint(); projMin = 100; projMax = -100; hitMin = 100; hitMax = -100; int pNum = 1;//remove from param list! for (int i = 0; i < aNum; i++) { projVec.x = aBox[i].x - origin.x; projVec.y = aBox[i].y - origin.y; distSq = projVec.x * projVec.x + projVec.y * projVec.y; nVec = projVec.GetNormal(); cVec = new SPoint(Mathf.Cos(dir + Mathf.PI / 2), Mathf.Sin(dir + Mathf.PI / 2)); cVal = -cVec.Dot(nVec); //projVal = sin(rAng)*abs(sin(rAng))*distSq; projVal = cVal * Mathf.Abs(cVal) * distSq; if (projVal < projMin) projMin = projVal; if (projVal > projMax) projMax = projVal; } float eps = 0.00001f; projVec.x = pCen.x - origin.x; projVec.y = pCen.y - origin.y; distSq = projVec.x * projVec.x + projVec.y * projVec.y; cVec = new SPoint(Mathf.Cos(dir + Mathf.PI / 2), Mathf.Sin(dir + Mathf.PI / 2)); nVec = projVec.GetNormal(); cVal = -cVec.Dot(nVec); //projVal = sin(rAng)*abs(sin(rAng))*distSq; projVal = cVal * Mathf.Abs(cVal) * distSq; if (projVal < hitMin) hitMin = projVal; if (projVal > hitMax) hitMax = projVal; if ((projVal >= projMin) && (projVal <= projMax)) isHit = true; else isHit = false; return isHit; }