bool checkCollision(Circle granade,Circle obstacle){ float dt = distance(granade.getX(),granade.getY(),obstacle.getX(),obstacle.getY()); if(obstacle.getRadius()+granade.getRadius() >= dt){ return true; } return false; }
bool Line::within(FigureType type, const double * coordinates, unsigned size) { if (type == FigureType::rect) { if (size < 4) return false; Rectangle polygon; polygon.setCoord(coordinates, size); if ( !(p1.getX() >= polygon.getX() && p1.getX() <= polygon.getX() + polygon.getWidth()) ) return false; if ( !(p2.getX() >= polygon.getX() && p2.getX() <= polygon.getX() + polygon.getWidth()) ) return false; if ( !(p1.getY() >= polygon.getY() && p1.getY() <= polygon.getY() + polygon.getHeight()) ) return false; if ( !(p2.getY() >= polygon.getY() && p2.getY() <= polygon.getY() + polygon.getHeight()) ) return false; return true; } else if (type == FigureType::circle) { if (size < 3) return false; Circle polygon; polygon.setCoord(coordinates, size); double x = p1.getX() - polygon.getX(); double y = p1.getY() - polygon.getY(); double radius = polygon.getRadius(); if ( !(x*x + y*y < radius*radius) ) return false; x = p2.getX() - polygon.getX(); y = p2.getY() - polygon.getY(); if ( !(x*x + y*y < radius*radius) ) return false; return true; } return false; }
/* @param object c calculates the distance between origin of one circle to other @returns the distance sqrt (x1-x2)^2 + (y1-y2)^2 */ double Circle::distanceTwoCircle(Circle& c){ double dx = getX() - c.getX(); //horizontal double dy = getY() - c.getY(); //vertical double dist = sqrt(dx*dx + dy*dy); //Pythagoras theorem return dist; }
bool Circle::overlaps(Circle c) { double radiiSum = pow(c.getRadius() + radius, 2); double radiiDiff = pow(c.getRadius() - radius, 2); double centerDist = pow(c.getX() - x, 2) + pow(c.getY() - y, 2); if(radiiDiff <= centerDist && centerDist <= radiiSum) { return true; } return false; }
int main(int argc, const char * argv[]) { Circle myCircle; cout << "Center:\t(" << myCircle.getX() << ", " << myCircle.getY() << ")" << endl; cout << "Radius:\t" << myCircle.getR() << endl; cout << "Area:\t" << myCircle.area << endl; myCircle.setX(1); myCircle.setY(2); myCircle.setR(3); cout << endl; cout << "Center:\t(" << myCircle.getX() << ", " << myCircle.getY() << ")" << endl; cout << "Radius:\t" << myCircle.getR() << endl; return 0; }
bool Circle::operator==(const Circle& circle) const { if (this->getX() == circle.getX() && this->getY() == circle.getY() && this->getRadius() == circle.getRadius() && this->isVisible() == circle.isVisible() && this->getRed() == circle.getRed() && this->getGreen() == circle.getGreen() && this->getBlue() == circle.getBlue()) return true; else return false; }
bool Circle::within(FigureType type, const double * coordinates, unsigned size) { if (type == FigureType::rect) { if (size < 4) return false; Rectangle polygon; polygon.setCoord(coordinates, size); if ( !(center.getX() - radius >= polygon.getX() && center.getX() + radius <= polygon.getX() + polygon.getWidth()) ) return false; if ( !(center.getY() - radius >= polygon.getY() && center.getY() + radius <= polygon.getY() + polygon.getHeight()) ) return false; return true; } else if (type == FigureType::circle) { if (size < 3) return false; Circle polygon; polygon.setCoord(coordinates, size); double distX = polygon.getX() - center.getX(); double distY = polygon.getY() - center.getY(); double radiusDifference = polygon.getRadius() - radius; if (radiusDifference < 0) return false; if ( !(distX*distX + distY*distY <= radiusDifference*radiusDifference) ) return false; return true; } return false; }
//Copy Constructor Circle :: Circle(const Circle &rhs){ radius = rhs.getRadius(); Area = rhs.getArea(); X = rhs.getX(); Y = rhs.getY(); }
Fighter* Projectile::checkHits(Fighter* other) { Hitbox temp = hit; Hitbox atkbox; vector<Circle> circles = temp.getCircles(); for (uint8 n = 0; n < circles.size(); n++) { Circle current = circles[n]; Circle newcircright(current.getX() + x, current.getY() + y, current.getRadius(), current.getKnockback(), current.damage); Circle newcircleft(64 - current.getX() + x, current.getY() + y, current.getRadius(), current.getKnockback(), current.damage); if (dx < 0) atkbox.addCircle(newcircright); else atkbox.addCircle(newcircleft); } if(TYPE == THUNDER1 || TYPE == THUNDER2 || TYPE == THUNDER3 || TYPE == THUNDER4) { vector<Projectile> projs = *((vector<Projectile>*)getProj()); for(int n = 0; n < (int)projs.size(); n++) { if(projs[n].owner == owner && (projs[n].TYPE == THUNDER1 || projs[n].TYPE == THUNDER2 || projs[n].TYPE == THUNDER3 || projs[n].TYPE == THUNDER4)) { if(!projs[n].enabled) enabled = false; } } } if (!enabled) { if(TYPE == THUNDER1 || TYPE == THUNDER2 || TYPE == THUNDER3 || TYPE == THUNDER4) { vector<Projectile> projs = *((vector<Projectile>*)getProj()); for(int n = 0; n < (int)projs.size(); n++) { if(projs[n].owner == owner && (projs[n].TYPE == THUNDER1 || projs[n].TYPE == THUNDER2 || projs[n].TYPE == THUNDER3 || projs[n].TYPE == THUNDER4)) { projs[n].enabled = false; } } } return other; } if (other -> respawntimer > 0) return other; if (other -> invincibility > 0) return other; if(atkbox.hits(other -> getAtkbox())) { if(atkbox.getHitCircle(other -> getAtkbox()).priority < other -> getAtkbox().getHitCircle(atkbox).priority) { if (dx < 0) other -> takeDamage(atkbox.getHitCircle(other -> getDefbox(PA_GetSpriteAnimFrame(MAIN_SCREEN, other -> SPRITENUM))), 1, owner, 0); else other -> takeDamage(atkbox.getHitCircle(other -> getDefbox(PA_GetSpriteAnimFrame(MAIN_SCREEN, other -> SPRITENUM))), -1, owner, 0); } else if(atkbox.getHitCircle(other -> getAtkbox()).priority == other -> getAtkbox().getHitCircle(atkbox).priority) { removeProj(num); } else { removeProj(num); return other; // they win priority } } else if(atkbox.hits(other -> getDefbox(PA_GetSpriteAnimFrame(MAIN_SCREEN, other -> SPRITENUM)))) { if(other -> MYCHAR == KIRBY && PA_GetSpriteAnimFrame(MAIN_SCREEN, other -> SPRITENUM) == 189) { if(TYPE != THUNDER1 && TYPE != THUNDER2 && TYPE != THUNDER3 && TYPE != THUNDER4 && TYPE != IKESWORD) removeProj(num); else atkbox.enabled = false; } else if (other -> action == AIRDODGE || other -> action == ROLL || other -> action == DODGE) { /*doesn't hit*/ } else if (other -> action == SHIELD) { other -> shieldstr -= (int)((atkbox.getHitCircle(other -> getDefbox(PA_GetSpriteAnimFrame(MAIN_SCREEN, other -> SPRITENUM))).damage)); if(TYPE != THUNDER1 && TYPE != THUNDER2 && TYPE != THUNDER3 && TYPE != THUNDER4 && TYPE != IKESWORD) removeProj(num); else atkbox.enabled = false; if(TYPE == THUNDER1 || TYPE == THUNDER2 || TYPE == THUNDER3 || TYPE == THUNDER4) { vector<Projectile> projs = *((vector<Projectile>*)getProj()); for(int n = 0; n < (int)projs.size(); n++) { if(projs[n].owner == owner && (projs[n].TYPE == THUNDER1 || projs[n].TYPE == THUNDER2 || projs[n].TYPE == THUNDER3 || projs[n].TYPE == THUNDER4)) projs[n].enabled = false; } } } else if(other -> COUNTER) { if(TYPE != THUNDER1 && TYPE != THUNDER2 && TYPE != THUNDER3 && TYPE != THUNDER4 && TYPE != IKESWORD) removeProj(num); else atkbox.enabled = false; other -> COUNTER = false; if(other -> MYCHAR == IKE && (PA_GetSpriteAnimFrame(MAIN_SCREEN, other -> SPRITENUM) == 139 || PA_GetSpriteAnimFrame(MAIN_SCREEN, other -> SPRITENUM) == 140)) { PA_StartSpriteAnimEx(MAIN_SCREEN, other -> SPRITENUM, 143, 144, 10, ANIM_LOOP, -1); other -> action = ATTACK; other -> delay = 60/10 * 2; other -> freeze(30); other -> dx = 0; PA_StartSpriteAnimEx(MAIN_SCREEN, other -> SPRITENUM, 139, 139, 1, ANIM_LOOP, -1); } if(TYPE == THUNDER1 || TYPE == THUNDER2 || TYPE == THUNDER3 || TYPE == THUNDER4) { vector<Projectile> projs = *((vector<Projectile>*)getProj()); for(int n = 0; n < (int)projs.size(); n++) { if(projs[n].owner == owner && (projs[n].TYPE == THUNDER1 || projs[n].TYPE == THUNDER2 || projs[n].TYPE == THUNDER3 || projs[n].TYPE == THUNDER4)) projs[n].enabled = false; } } } else if(other -> CAPE) { dx *= -1; owner = other -> charnum; vector<Circle> temp = hit.getCircles(); hit.reset(); for(int n = 0; n < (int)temp.size();n++) { hit.addCircle(Circle(temp[n].getX(), temp[n].getY(), temp[n].getRadius(), Knockback(temp[n].getKnockback().dx * -1, temp[n].getKnockback().dy, temp[n].getKnockback().length), temp[n].damage)); } if(dx > 0) PA_SetSpriteHflip(MAIN_SCREEN, num, 0); if(dx < 0) PA_SetSpriteHflip(MAIN_SCREEN, num, 1); } else if (other -> ABSORB && (TYPE != IKESWORD)) { other -> percentage -= atkbox.getHitCircle(other -> getDefbox(PA_GetSpriteAnimFrame(MAIN_SCREEN, other -> SPRITENUM))).damage; if(other -> percentage < 0) other -> percentage = 0; if(TYPE != THUNDER1 && TYPE != THUNDER2 && TYPE != THUNDER3 && TYPE != THUNDER4) removeProj(num); else atkbox.enabled = false; if(TYPE == THUNDER1 || TYPE == THUNDER2 || TYPE == THUNDER3 || TYPE == THUNDER4) { vector<Projectile> projs = *((vector<Projectile>*)getProj()); for(int n = 0; n < (int)projs.size(); n++) { if(projs[n].owner == owner && (projs[n].TYPE == THUNDER1 || projs[n].TYPE == THUNDER2 || projs[n].TYPE == THUNDER3 || projs[n].TYPE == THUNDER4)) projs[n].enabled = false; } } } else { other -> takeDamage(atkbox.getHitCircle(other -> getDefbox(PA_GetSpriteAnimFrame(MAIN_SCREEN, other -> SPRITENUM))), 1, owner, 0); if (TYPE != FINALCUTTER && TYPE != IKESWORD && TYPE != THUNDER1 && TYPE != THUNDER2 && TYPE != THUNDER3 && TYPE != THUNDER4) removeProj(num); else enabled = false; if(TYPE == THUNDER1 || TYPE == THUNDER2 || TYPE == THUNDER3 || TYPE == THUNDER4) { vector<Projectile> projs = *((vector<Projectile>*)getProj()); for(int n = 0; n < (int)projs.size(); n++) { if(projs[n].owner == owner && (projs[n].TYPE == THUNDER1 || projs[n].TYPE == THUNDER2 || projs[n].TYPE == THUNDER3 || projs[n].TYPE == THUNDER4)) projs[n].enabled = false; } } } } return other; }
// gets values for the circle bool Circle::intersects(Circle other) { double dx = other.getX() - x; // change in x double dy = other.getY() - y; // change in y double dist = sqrt(dx * dx + dy * dy); return dist < (radius + other.getRadius()); } // checks if this circle intersects Circle other
Circle::Circle(const Circle& circle) : Shape() { this->setLocation(circle.getX(), circle.getY()); this->setRadius(circle.getRadius()); this->setVisible(circle.isVisible()); this->setColor(circle.getRed(), circle.getGreen(), circle.getBlue()); }
bool Circle::collision(Shape* s) { //Check the type of the shape to compare to //If circle then check if there is a circle-circle collision if (!s->getType().compare(CIRCLE)) { //If the shape is another circle then cast to circle Circle* c = static_cast<Circle*>(s); //Then do a collision detection if(pythagoras( fabs (getX() - c->getX()), fabs(getY() - c->getY())) < getRadius() + c->getRadius()) { return true; } return false; } //If square then check if there is a square-circle collision if(!s->getType().compare(SQUARE)) { //If the shape is a square then cast to square Square* sq = static_cast<Square*>(s); //In the diagram below E is inside the square, B D F H are alongside the square and are nearest an edge // A C G I are nearest the corner of the square // A|B|C // -+-+- // D|E|F // -+-+- // G|H|I //Check if the centre of the circle is within the bounds of the square //In the above diagram this would mean the centre of the circle is within the box E if ((getX() > sq->getX() - (sq->getWidth() / 2) && getX() < sq->getX() + (sq->getWidth() / 2)) && (getY() > sq->getY() - (sq->getHeight() / 2) && getY() < sq->getY() + (sq->getHeight() / 2))) { cout << "HERE"; return true; } //Check if the centre of the circle is above, below, or next to the square //Do this by checking if the x value is within the width of the square but outside the height //Or is the y value inside the height of the square but outside the width //In the diagram above this would mean the centre of the circle is either in B D F or H //The number of methods could have been reduced improving efficiency by including the check for above/below and the check //for left/right in one if statement, halving the number of tests required //However for readibility of the code I have kept this split up as the nested if statements were starting to look //even more complicated than they already are //Check if the centre of the circle is above the square in position B if ((getX() > sq->getX() - (sq->getWidth() / 2) && getX() < sq->getX() + (sq->getWidth() / 2)) && getY() > sq->getY() + (sq->getHeight() / 2)) { //Check if the circle overlaps the top of the square if(fabs(getY() - sq->getY()) < (getRadius()) + (sq->getHeight()/2)) { return true; } } //Check if the centre of the circle is below the square in position H if ((getX() > sq->getX() - (sq->getWidth() / 2) && getX() < sq->getX() + (sq->getWidth() / 2)) && getY() < sq->getY() - (sq->getHeight() / 2)) { //Check if the circle overlaps the bottom of the square if(fabs(getY() - sq->getY()) < (getRadius()) + (sq->getHeight()/2)) { return true; } } //Check if the centre of the circle is left of the square in position D if ((getY() > sq->getY() - (sq->getHeight() / 2) && getY() < sq->getY() + (sq->getHeight() / 2)) && getX() < sq->getX() - (sq->getWidth() / 2)) { //Check if the circle overlaps the left of the square if(fabs(getX() - sq->getX()) < (getRadius()) + (sq->getWidth()/2)) { return true; } } //Check if the centre of the circle is right of the square in position D if ((getY() > sq->getY() - (sq->getHeight() / 2) && getY() < sq->getY() + (sq->getHeight() / 2)) && getX() > sq->getX() + (sq->getWidth() / 2)) { //Check if the circle overlaps the right of the square if(fabs(getX() - sq->getX()) < (getRadius()) + (sq->getWidth()/2)) { return true; } } //Check if the centre of the circle is diagonally above or below the square //Do this by checking if the x and y values are below and/or above the perimeter //values of the square //In the diagram above this would mean the centre of the circle lies in A C G I //Then check whether the distance between the centre point of the circle and the corner of the square that //it is closest to is less than the radius. //Check if the centre of the circle is left of and above the square if( ( getX() < sq->getX() - (sq->getWidth() / 2) ) && ( getY() < sq->getY() - (sq->getHeight() / 2) ) ) { //Check if the distance from the circle midpoint to the top left corner of the square is less than the radius if ( pythagoras ( abs(getX() - ( sq->getX() - (sq->getWidth()/2)) ) , abs(getY() - ( sq->getY() - (sq->getHeight()/2)) ) ) < getRadius() ) { return true; } } //Check if the centre of the circle is right of and above the square if( ( getX() > sq->getX() + (sq->getWidth() / 2) ) && ( getY() < sq->getY() - (sq->getHeight() / 2) ) ) { //Check if the distance from the circle midpoint to the top right corner of the square is less than the radius if ( pythagoras ( abs(getX() - ( sq->getX() + (sq->getWidth()/2)) ) , abs(getY() - ( sq->getY() - (sq->getHeight()/2)) ) ) < getRadius() ) { return true; } } //Check if the centre of the circle is left of and below the square if( ( getX() < sq->getX() - (sq->getWidth() / 2) ) && ( getY() > sq->getY() + (sq->getHeight() / 2) ) ) { //Check if the distance from the circle midpoint to the bottom left corner of the square is less than the radius if ( pythagoras ( abs(getX() - ( sq->getX() - (sq->getWidth()/2)) ) , abs(getY() - ( sq->getY() + (sq->getHeight()/2)) ) ) < getRadius() ) { return true; } } //Check if the centre of the circle is right of and below the square if( ( getX() > sq->getX() + (sq->getWidth() / 2) ) && ( getY() > sq->getY() + (sq->getHeight() / 2) ) ) { //Check if the distance from the circle midpoint to the bottom right corner of the square is less than the radius if ( pythagoras ( abs(getX() - ( sq->getX() + (sq->getWidth()/2)) ) , abs(getY() - ( sq->getY() + (sq->getHeight()/2)) ) ) < getRadius() ) { return true; } } } return false; }
void QtGraphicsViewVisitor::drawCircle(Circle &shape) { int x = shape.getX(); int y = shape.getY(); int r = shape.getRadius(); scene->addEllipse(x - r, y - r, r * 2, r * 2, *blackPen); }