bool Collision::CircleTest(const sf::Sprite& Object1, const sf::Sprite& Object2) { //Simplest circle test possible //Distance between points <= sum of radius float Radius1 = (Object1.GetSize().x + Object1.GetSize().y) / 4; float Radius2 = (Object2.GetSize().x + Object2.GetSize().y) / 4; float xd = Object1.GetPosition().x - Object2.GetPosition().x; float yd = Object1.GetPosition().y - Object2.GetPosition().y; return sqrt(xd * xd + yd * yd) <= Radius1 + Radius2; }
//returns true if (x,y) is inside sprite fiure bool contains(float x, float y, const sf::Sprite& sprite) { float minX = sprite.GetPosition().x; float minY = sprite.GetPosition().y; float maxX = sprite.GetPosition().x + sprite.GetSize().x; float maxY = sprite.GetPosition().y + sprite.GetSize().y; //return if x and y falls in between sprite's range return isBetween(x,minX, maxX) && isBetween(y,minY, maxY); }
bool Mouse::isInside(sf::Sprite &sprite) { float x1, x2, y1, y2; x1 = sprite.GetPosition().x; x2 = x1 + sprite.GetSize().x; y1 = sprite.GetPosition().y; y2 = y1 + sprite.GetSize().y; if ((this->x < x2) && (this->x > x1)) { if ((this->y < y2) && (this->y > y1)) { return true; } } return false; }
bool sourisAuDessusBoutons(sf::Event &Event, sf::Sprite &Bouton) { //test la position actuelle du curseur ainsi que sa position future //si le curseur est au-dessus du sprite, alors on l'affiche ! if((Event.MouseMove.X < Bouton.GetPosition().x+Bouton.GetSize().x && Event.MouseMove.X>Bouton.GetPosition().x && Event.MouseMove.Y<Bouton.GetPosition().y+Bouton.GetSize().y && Event.MouseMove.Y>Bouton.GetPosition().y) || (Event.MouseButton.X < Bouton.GetPosition().x+Bouton.GetSize().x && Event.MouseButton.X>Bouton.GetPosition().x && Event.MouseButton.Y<Bouton.GetPosition().y+Bouton.GetSize().y && Event.MouseButton.Y>Bouton.GetPosition().y)) { return true; } else return false; }
bool np::Collision::BoundingBoxColl(sf::Sprite &object1, sf::Sprite &object2) { leftA = object1.GetPosition().x; rightA = object1.GetPosition().x + object1.GetSubRect().GetWidth(); topA = object1.GetPosition().y; bottomA = object1.GetPosition().y + object1.GetSubRect().GetHeight(); leftB = object2.GetPosition().x; rightB = object2.GetPosition().x + object2.GetSubRect().GetWidth(); topB = object2.GetPosition().y; bottomB = object2.GetPosition().y + object2.GetSubRect().GetHeight(); if(bottomA <= topB) return false; if(topA >= bottomB) return false; if(leftA >= rightB) return false; if(rightA <= leftB) return false; return true; }
bool Utility::contains(sf::Sprite sp, int x, int y){ float posX1 = sp.GetPosition().x - sp.GetSize().x/2; float posY1 = sp.GetPosition().y - sp.GetSize().y/2; //sf::Vector2f center = sp.GetCenter(); float posX2 = sp.GetPosition().x + sp.GetSize().x/2; float posY2 = sp.GetPosition().y + sp.GetSize().y/2; //sf::Vector2f //if(s if(x > posX1 && x < posX2 && y > posY1 && y < posY2) return true; return false; }
/*! * \param Obj The object * \return The bounding box of the object */ sf::IntRect Collision::GetBoundingBox(const sf::Sprite &Obj) { const sf::Vector2f &Position(Obj.GetPosition()); const sf::IntRect &SubRect(Obj.GetSubRect()); return sf::IntRect(Position.x, Position.y, Position.x + SubRect.GetWidth(), Position.y + SubRect.GetHeight()); }
Bala::Bala(float _v, sf::Sprite& _cannon, sf::Image& imagen) { float cannonA, cannonX, cannonY, cannonH; //Cargo sprite sprite.SetImage(imagen); sprite.SetCenter(imagen.GetWidth()/2, imagen.GetHeight()/2); //Inicializo variables dt = 0.0f; cannonA = _cannon.GetRotation(); cannonX = _cannon.GetPosition().x; cannonY = _cannon.GetPosition().y; //Tomo el tamaño sobre el eje x como el largo del cañon //porque sin rotar la misma se encuentra en horizontal cannonH = _cannon.GetSize().x; //Descompongo la velocidad inicial vx = _v * (cos(cannonA/180*PI)); vy = _v * (sin(cannonA/180*PI)); //Posicion inicial bala según la posición del cañon //usando trigonometria xi = cannonX + ((cos(cannonA/180*PI) * cannonH)); yi = cannonY - ((sin(cannonA/180*PI) * cannonH)); sprite.SetPosition(xi, yi); }
bool cliqueBouton(sf::Event &Event, sf::Sprite &BoutonJ) { if ((Event.Type == sf::Event::MouseButtonPressed) && (Event.MouseButton.X > BoutonJ.GetPosition().x) && (Event.MouseButton.X < BoutonJ.GetPosition().x+BoutonJ.GetSize().x) && (Event.MouseButton.Y > BoutonJ.GetPosition().y) && (Event.MouseButton.Y < BoutonJ.GetPosition().y+BoutonJ.GetSize().y)) return true; else return false; }
sf::IntRect Colision::getIntRect(const sf::Sprite &sprite) { const sf::Vector2f &pos(sprite.GetPosition()); const sf::IntRect &sub_rect(sprite.GetSubRect()); sf::IntRect int_rect((int)pos.x, (int)pos.y, (int)(pos.x + sub_rect.GetWidth()), (int)(pos.y + sub_rect.GetHeight())); return (int_rect); }
void setXYCheckeur(sf::Sprite &Checkeur, Fenetre Fen, sf::Sprite &Case1, sf::Sprite &Case2, sf::Sprite &Case3) { if(Fen.getLargeur()==1152) { Checkeur.SetX(Case1.GetPosition().x); Checkeur.SetY(Case1.GetPosition().y); } else if(Fen.getLargeur()==1024) { Checkeur.SetX(Case2.GetPosition().x); Checkeur.SetY(Case2.GetPosition().y); } else { Checkeur.SetX(Case3.GetPosition().x); Checkeur.SetY(Case3.GetPosition().y); } }
sf::Sprite RectangleBlock::CopySprite(sf::Sprite& sprite,sf::Vector2<float> offSet) { sf::Sprite ret = sf::Sprite(sprite); sf::Vector2<float> new_coord = sprite.GetPosition() - offSet; ret.SetPosition(new_coord); return ret; }
void Fenetre::boutonValiderParametres(sf::RenderWindow &App, sf::Event &Event, sf::Sprite &Checkeur, sf::Sprite &ValiderReso, sf::Sprite &Case1, sf::Sprite &Case2, sf::Sprite &Case3, string &code) { int x, y; if (cliqueBouton(Event, ValiderReso)){ if(Checkeur.GetPosition().y==Case1.GetPosition().y){ x=1152; y=864; }else if(Checkeur.GetPosition().y==Case2.GetPosition().y){ x=1024; y=768; }else{ x=800; y=600; } code="accueil"; ecrireResolutionRD(x, y); //App.Create(sf::VideoMode(x, y), "Jeu du morpion par Romain Dauby"); //changerResolution(x, y); //si l'on décommente ces deux lignes, il faudrait relancer le gestionnaire de ressources. } }
void cCollider::getCoordinates(sf::Sprite& thisSprite) { setMinX(thisSprite.GetPosition().x); setMaxY(thisSprite.GetPosition().y); if (maxY > 763) { setOnGround(true); //rThisSprite.SetY(763); } else { setOnGround(false); } maxX = minX + width; minY = maxY - height; }
bool boxCollision(const sf::Sprite& a, const sf::Sprite& b) { sf::IntRect aRect(a.GetPosition().x - a.GetSize().x/2, a.GetPosition().y - a.GetSize().y/2, a.GetPosition().x + a.GetSize().x/2, a.GetPosition().y + a.GetSize().y/2); sf::IntRect bRect(b.GetPosition().x - b.GetSize().x/2, b.GetPosition().y - b.GetSize().y/2, b.GetPosition().x + b.GetSize().x/2, b.GetPosition().y + b.GetSize().y/2); return aRect.Intersects(bRect); }
void caseResolution(sf::RenderWindow &App, sf::Event &Event, sf::Sprite &Case1, sf::Sprite &Case2, sf::Sprite &Case3, sf::Sprite &Checkeur, int ¶m) { if ((Event.Type == sf::Event::MouseButtonPressed) && (Event.MouseButton.X > Case1.GetPosition().x) && (Event.MouseButton.X < Case1.GetPosition().x+Case1.GetSize().x) && (Event.MouseButton.Y > Case1.GetPosition().y) && (Event.MouseButton.Y < Case1.GetPosition().y+Case1.GetSize().y)) { Checkeur.SetX(Case1.GetPosition().x); Checkeur.SetY(Case1.GetPosition().y); } else if ((Event.Type == sf::Event::MouseButtonReleased) && (Event.MouseButton.X > Case2.GetPosition().x) && (Event.MouseButton.X < Case2.GetPosition().x+Case2.GetSize().x) && (Event.MouseButton.Y > Case2.GetPosition().y) && (Event.MouseButton.Y < Case2.GetPosition().y+Case2.GetSize().y)) { Checkeur.SetX(Case2.GetPosition().x); Checkeur.SetY(Case2.GetPosition().y); } else if ((Event.Type == sf::Event::MouseButtonReleased) && (Event.MouseButton.X > Case3.GetPosition().x) && (Event.MouseButton.X < Case3.GetPosition().x+Case3.GetSize().x) && (Event.MouseButton.Y > Case3.GetPosition().y) && (Event.MouseButton.Y < Case3.GetPosition().y+Case3.GetSize().y)) { Checkeur.SetX(Case3.GetPosition().x); Checkeur.SetY(Case3.GetPosition().y); } param=1; }
bool Collision::collide(sf::Sprite& obj, float dx, float dy, CollisionResult& result) { //assert(_map->tileFromPixel(obj.GetPosition().x, obj.GetPosition().y)->isWalkable()); //assert(_map->tileFromPixel(obj.GetPosition().x + obj.GetSize().x, obj.GetPosition().y)->isWalkable()); std::cout << "posx: " << obj.GetPosition().x << std::endl; std::cout << "posy: " << obj.GetPosition().y << std::endl; std::cout << "dx: " << dx << std::endl; std::cout << "dy: " << dy << std::endl; result.left = false; result.right = false; result.floor = false; result.ceil = false; result.dx = dx; result.dy = dy; if(dx != 0.f) { //if moving horizontal bool left = dx < 0; float forwardX; if(left) forwardX = obj.GetPosition().x; else forwardX = obj.GetPosition().x + obj.GetSize().x; float newX = forwardX + dx; float topY = obj.GetPosition().y + constants::COLLISION_MARGIN; float botY = obj.GetPosition().y + obj.GetSize().y - constants::COLLISION_MARGIN; Tile* topTile = _map->tileFromPixel(newX, topY); Tile* botTile = _map->tileFromPixel(newX, botY); if(!topTile->isWalkable() || !botTile->isWalkable()) { result.left = left; result.right = !left; float limit; if(left) limit = topTile->GetPosition().x + topTile->GetSize().x; else limit = topTile->GetPosition().x; result.dx = limit - forwardX; } std::cout << "res.dx=" << result.dx << std::endl; assert(left || result.dx <= dx); assert(!left || result.dx >= dx); //assert(result.dx * dx >= 0); //same direction } float objPosX = obj.GetPosition().x + result.dx; if(dy != 0.f) { //if moving vertically bool down = dy > 0; float forwardY; if(down) forwardY = obj.GetPosition().y + obj.GetSize().y; else forwardY = obj.GetPosition().y; float newY = forwardY + dy; Tile* leftTileY = _map->tileFromPixel(objPosX + constants::COLLISION_MARGIN, newY); Tile* rightTileY = _map->tileFromPixel(objPosX + obj.GetSize().x - constants::COLLISION_MARGIN, newY); if(!leftTileY->isWalkable() || !rightTileY->isWalkable()) { result.floor = down; result.ceil = !down; float limit; if(down) limit = leftTileY->GetPosition().y;// - constants::COLLISION_MARGIN; else limit = leftTileY->GetPosition().y + leftTileY->GetSize().y;// + constants::COLLISION_MARGIN; result.dy = limit - forwardY; } std::cout << "res.dy=" << result.dy << std::endl; assert(down || result.dy >= dy); assert(!down || result.dy <= dy); //assert(result.dy * dy >= 0); //same direction } return result.floor || result.ceil || result.left || result.right; }
bool Table::isPointOverSprite(const sf::Vector2f Position, sf::Sprite &Sprite){ return (Position.x < Sprite.GetPosition().x + Sprite.GetSize().x) && (Sprite.GetPosition().x < Position.x) && (Position.y < Sprite.GetPosition().y + Sprite.GetSize().y) && (Sprite.GetPosition().y < Position.y); }
bool Collision::BoundingBoxTest(const sf::Sprite& Object1, const sf::Sprite& Object2) { sf::Vector2f A, B, C, BL, TR; sf::Vector2f HalfSize1 = Object1.GetSize(); sf::Vector2f HalfSize2 = Object2.GetSize(); //For somereason the Vector2d divide by operator //was misbehaving //Doing it manually HalfSize1.x /= 2; HalfSize1.y /= 2; HalfSize2.x /= 2; HalfSize2.y /= 2; //Get the Angle we're working on float Angle = Object1.GetRotation() - Object2.GetRotation(); float CosA = cos(Angle * RADIANS_PER_DEGREE); float SinA = sin(Angle * RADIANS_PER_DEGREE); float t, x, a, dx, ext1, ext2; //Normalise the Center of Object2 so its axis aligned an represented in //relation to Object 1 C = Object2.GetPosition(); C -= Object1.GetPosition(); C = RotatePoint(C, Object2.GetRotation()); //Get the Corners BL = TR = C; BL -= HalfSize2; TR += HalfSize2; //Calculate the vertices of the rotate Rect A.x = -HalfSize1.y*SinA; B.x = A.x; t = HalfSize1.x*CosA; A.x += t; B.x -= t; A.y = HalfSize1.y*CosA; B.y = A.y; t = HalfSize1.x*SinA; A.y += t; B.y -= t; t = SinA * CosA; // verify that A is vertical min/max, B is horizontal min/max if (t < 0) { t = A.x; A.x = B.x; B.x = t; t = A.y; A.y = B.y; B.y = t; } // verify that B is horizontal minimum (leftest-vertex) if (SinA < 0) { B.x = -B.x; B.y = -B.y; } // if rr2(ma) isn't in the horizontal range of // colliding with rr1(r), collision is impossible if (B.x > TR.x || B.x > -BL.x) return false; // if rr1(r) is axis-aligned, vertical min/max are easy to get if (t == 0) { ext1 = A.y; ext2 = -ext1; }// else, find vertical min/max in the range [BL.x, TR.x] else { x = BL.x - A.x; a = TR.x - A.x; ext1 = A.y; // if the first vertical min/max isn't in (BL.x, TR.x), then // find the vertical min/max on BL.x or on TR.x if (a * x > 0) { dx = A.x; if (x < 0) { dx -= B.x; ext1 -= B.y; x = a; } else { dx += B.x; ext1 += B.y; } ext1 *= x; ext1 /= dx; ext1 += A.y; } x = BL.x + A.x; a = TR.x + A.x; ext2 = -A.y; // if the second vertical min/max isn't in (BL.x, TR.x), then // find the local vertical min/max on BL.x or on TR.x if (a * x > 0) { dx = -A.x; if (x < 0) { dx -= B.x; ext2 -= B.y; x = a; } else { dx += B.x; ext2 += B.y; } ext2 *= x; ext2 /= dx; ext2 -= A.y; } } // check whether rr2(ma) is in the vertical range of colliding with rr1(r) // (for the horizontal range of rr2) return !((ext1 < BL.y && ext2 < BL.y) || (ext1 > TR.y && ext2 > TR.y)); }