void Character::checkCollision() { isGrounded=false; for (Rect* g: *grounds) { if (g->getMidX() - draw_Sprite->getPositionX()<1500 && g->getMidY() - draw_Sprite->getPositionY()<1500 ) { checkGround(g); checkTop(g); checkSide(true,g); checkSide(false,g); } } if (collidingWith==nullptr){ } else if (rCollider.intersectsRect(*collidingWith)){ } else if (lCollider.intersectsRect(*collidingWith)){ } else { collidingWith==nullptr; } }
//------------------------------------------------------------------------------ void Game::checkBorders() { checkLeft(); checkRight(); checkTop(); checkBottom(); }
bool BoundingBox::pointInBounds(int xp, int yp) { // Check if the point is in between the top bottom and side lines (all return 0 or negative) if(checkTop(xp,yp)<= 0 && checkBottom(xp,yp) <= 0 && checkLeft(xp,yp)<=0 && checkRight(xp,yp) <= 0) return true; return false; }
void BoundingBox::calculateResize(int xp, int yp,bool locked, int corner, double *ans) { double xn = 0, yn = 0, dh = 0, dw = 0, xO1 = 0, yO1 = 0; if(corner == CBL) { // Find intersection (xn, yn) of two lines // bottom of element and perpendicular line going through (xp, yp) xn = (mainSlope * xp - sideSlope * xO - yp + yO) / (mainSlope - sideSlope); yn = mainSlope * (xn - xp) + yp; // Calculate the change in width and height (dw, dh) dh = sqrt( pow((xO - xn),2) + pow((yO - yn),2) ); dw = sqrt( pow((xp - xn),2) + pow((yp - yn),2) ); // If the point is inside the box (negative value from checkLeft/Bottom) // The size is getting smaller if(checkLeft(xp, yp) < 0) dw = dw * -1; if(checkBottom(xp, yp) < 0) dh = dh * -1; // if width/ height is negative invert the dw/dh if(width < 0) dw = dw*-1; if(height < 0) dh = dh*-1; // If the aspect ratio is locked choose the bigger change and scale the other appropriately if(locked) { if(fabs(dw) > fabs(dh)) { // Use dw, scale height proportionaly to find dh double scale = (double)(dw+width)/width; double h = height*scale; // The new dh dh = h - height; }else { double scale = (double)(dh+height)/height; double w = width*scale; // The new dw dw = w - width; } } // New origin is (xp,yp) xO1 = xp; yO1 = yp; } if(corner == CBR) { // Find intersection (xn, yn) of two lines // bottom of element and perpendicular line going through (xp, yp) xn = (mainSlope * xp - sideSlope * xBR - yp + yBR) / (mainSlope - sideSlope); yn = mainSlope * (xn - xp) + yp; // Calculate the change in width and height (dw, dh) dh = sqrt( (xBR - xn)*(xBR - xn) + (yBR - yn)*(yBR - yn) ); dw = sqrt( (xp - xn)*(xp - xn) + (yp - yn)*(yp - yn) ); // If the point is inside the right or bottom then the shape is getting smaller dw/dh should be negative if(checkRight(xp, yp) < 0) dw = dw * -1; if(checkBottom(xp, yp) < 0) dh = dh * -1; // if width/ height is negative invert the dw/dh if(width < 0) dw = dw*-1; if(height < 0) dh = dh*-1; // If the aspect ratio is locked choose the bigger change and scale the other appropriately if(locked) { if(fabs(dw) > fabs(dh)) { // Use dw, scale height proportionaly to find dh double scale = (double)(dw+width)/width; double h = height*scale; // The new dh dh = h - height; }else { double scale = (double)(dh+height)/height; double w = width*scale; // The new dw dw = w - width; } } // Calculate the new origin (xO1, yO1) xO1 = xO + dh * cos(angle - PI/2); // angle minus 90 degrees yO1 = yO + dh * sin(angle - PI/2); // angle minus 90 degrees } if(corner == CTR) { // Find intersection (xn, yn) of two lines // bottom of element and perpendicular line going through (xp, yp) xn = (mainSlope * xp - sideSlope * xTR - yp + yTR) / (mainSlope - sideSlope); yn = mainSlope * (xn - xp) + yp; // Calculate the change in width and height (dw, dh) dh = sqrt( (xTR - xn)*(xTR - xn) + (yTR - yn)*(yTR - yn) ); dw = sqrt( (xp - xn)*(xp - xn) + (yp - yn)*(yp - yn) ); // Retain the direction of the change // Acts opposite if angle is below the x axis if(checkRight(xp, yp) < 0)// && angle > 0 && angle < 180 || checkRight(xp, yp) > 0 && angle >180) dw = dw * -1; if(checkTop(xp, yp) < 0 )//&& angle > 0 && angle < 180) dh = dh * -1; // if width/ height is negative invert the dw/dh if(width < 0) dw = dw*-1; if(height < 0) dh = dh*-1; // If the aspect ratio is locked choose the bigger change and scale the other appropriately if(locked) { if(fabs(dw) > fabs(dh)) { // Use dw, scale height proportionaly to find dh double scale = (double)(dw+width)/width; double h = height*scale; // The new dh dh = h - height; }else { double scale = (double)(dh+height)/height; double w = width*scale; // The new dw dw = w - width; } } // Origin remains the same xO1 = xO; yO1 = yO; } if(corner == CTL) { // Find intersection (xn, yn) of two lines // bottom of element and perpendicular line going through (xp, yp) xn = (sideSlope * xTL - mainSlope * xp + yp - yTL) / (sideSlope - mainSlope); yn = yp - mainSlope * (xp - xn); // Calculate the change in width and height (dw, dh) dh = sqrt( (xn - xTL)*(xn - xTL) + (yn - yTL)*(yn - yTL) ); dw = sqrt( (xp - xn)*(xp - xn) + (yp - yn)*(yp - yn) ); // If the point is inside the box (negative value from checkLeft/Top) // The size is getting smaller if(checkLeft(xp, yp) < 0) dw = dw * -1; if(checkTop(xp, yp) < 0) dh = dh * -1; // if width/ height is negative invert the dw/dh if(width < 0) dw = dw*-1; if(height < 0) dh = dh*-1; // If the aspect ratio is locked choose the bigger change and scale the other appropriately if(locked) { if(fabs(dw) > fabs(dh)) { // Use dw, scale height proportionaly to find dh double scale = (double)(dw+width)/width; double h = height*scale; // The new dh dh = h - height; }else { double scale = (double)(dh+height)/height; double w = width*scale; // The new dw dw = w - width; } } // Calculate the new origin (xO1, yO1) xO1 = xO + dw * cos(angle + PI); // 180 plus angle yO1 = yO + dw * sin(angle + PI); // 180 plus angle } /// final new origin width and height ans[0] = xO1; ans[1] = yO1; ans[2] = width + dw; ans[3] = height + dh; }