void doMouseMove(){ if (rect1.getGlobalBounds().contains(sf::Mouse::getPosition(window).x, sf::Mouse::getPosition(window).y)){ if (!sf::Mouse::isButtonPressed(sf::Mouse::Button::Left)) rect1.setFillColor(sf::Color(100,100,100)); } else { rect1.setFillColor(sf::Color(200,200,200)); } if (rect2.getGlobalBounds().contains(sf::Mouse::getPosition(window).x, sf::Mouse::getPosition(window).y)){ if (!sf::Mouse::isButtonPressed(sf::Mouse::Button::Left)) rect2.setFillColor(sf::Color(100,100,100)); } else { rect2.setFillColor(sf::Color(200,200,200)); } if (rect3.getGlobalBounds().contains(sf::Mouse::getPosition(window).x, sf::Mouse::getPosition(window).y)){ if (!sf::Mouse::isButtonPressed(sf::Mouse::Button::Left)) rect3.setFillColor(sf::Color(100,100,100)); } else { rect3.setFillColor(sf::Color(200,200,200)); } if (rect4.getGlobalBounds().contains(sf::Mouse::getPosition(window).x, sf::Mouse::getPosition(window).y)){ if (!sf::Mouse::isButtonPressed(sf::Mouse::Button::Left)) rect4.setFillColor(sf::Color(100,100,100)); } else { rect4.setFillColor(sf::Color(200,200,200)); } }
void DropDown::arrange(sf::Text &text,sf::RectangleShape &shape) { float ratiox = shape.getGlobalBounds().width/text.getGlobalBounds().width; float ratioy = (shape.getGlobalBounds().height/text.getGlobalBounds().height); ratioy*=0.5f; text.scale(ratiox,ratioy); }
//Method to check collision between 2 rectangles. bool CollisionManager::CheckRectangleCollision(sf::RectangleShape entity1, sf::RectangleShape entity2) { if (entity1.getGlobalBounds().intersects(entity2.getGlobalBounds())) return true; else return false; }
bool CollisionChecker::IsPossibleCollisionWithTopOrBottomSide(sf::RectangleShape &r1, sf::RectangleShape &r2) { if(r1.getPosition().x+r1.getGlobalBounds().width > r2.getPosition().x && r1.getPosition().x < r2.getPosition().x+r2.getGlobalBounds().width){ return true; } return false; }
bool CollisionChecker::IsPossibleCollisionWithLeftOrRightSide(sf::RectangleShape &r1, sf::RectangleShape &r2) { if(r1.getPosition().y+r1.getGlobalBounds().height > r2.getPosition().y && r1.getPosition().y < r2.getPosition().y+r2.getGlobalBounds().height){ return true; } return false; }
bool CollisionChecker::IsCollidingWithRightSide(sf::RectangleShape &r1, sf::RectangleShape &r2) { if(r1.getPosition().x <= r2.getPosition().x+r2.getGlobalBounds().width && r1.getPosition().x >= r2.getPosition().x && r1.getPosition().y+r1.getGlobalBounds().height > r2.getPosition().y && r1.getPosition().y < r2.getPosition().y+r2.getGlobalBounds().height){ return true; } return false; }
bool tr::CColisiometro::Interseccion(sf::RectangleShape obj1, sf::RectangleShape obj2, sf::RectangleShape* choque = nullptr) { sf::FloatRect rect2 = obj2.getGlobalBounds(); sf::FloatRect rect1 = obj1.getGlobalBounds(); if (rect2.intersects(rect1)) { return true; }else { return false; } }
bool ifCollide(const sf::RectangleShape& A, const sf::RectangleShape& B) { float aRad = A.getGlobalBounds().height; float bRad = B.getGlobalBounds().height; float length = (aRad + bRad)/2.f; sf::Vector2f aPos = A.getPosition(); sf::Vector2f bPos = B.getPosition(); sf::Vector2f diff = aPos - bPos; float magn = sqrt(diff.x*diff.x + diff.y*diff.y); return magn <= length; }
void doMouseButton(sf::Mouse::Button button){ if (button == sf::Mouse::Button::Left){ if (rect1.getGlobalBounds().contains(sf::Mouse::getPosition(window).x, sf::Mouse::getPosition(window).y)){ if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left)){ rect1.setFillColor(sf::Color::Yellow); doSwitchMode(Mode::SolidWhite); } else { rect1.setFillColor(sf::Color(100,100,100)); } } else { rect1.setFillColor(sf::Color(200,200,200)); } if (rect2.getGlobalBounds().contains(sf::Mouse::getPosition(window).x, sf::Mouse::getPosition(window).y)){ if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left)){ rect2.setFillColor(sf::Color::Yellow); doSwitchMode(Mode::Marquee); } else { rect2.setFillColor(sf::Color(100,100,100)); } } else { rect2.setFillColor(sf::Color(200,200,200)); } if (rect3.getGlobalBounds().contains(sf::Mouse::getPosition(window).x, sf::Mouse::getPosition(window).y)){ if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left)){ rect3.setFillColor(sf::Color::Yellow); doSwitchMode(Mode::ColorCycle); } else { rect3.setFillColor(sf::Color(100,100,100)); } } else { rect3.setFillColor(sf::Color(200,200,200)); } if (rect4.getGlobalBounds().contains(sf::Mouse::getPosition(window).x, sf::Mouse::getPosition(window).y)){ if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left)){ rect4.setFillColor(sf::Color::Yellow); doSwitchMode(Mode::Pew); } else { rect4.setFillColor(sf::Color(100,100,100)); } } else { rect4.setFillColor(sf::Color(200,200,200)); } } }
bool Ray::ClipVector(int axis, sf::RectangleShape& boxIn) { float tempClipFractionLow = 0; float tempClipFractionHigh = 1;//temp Fractions //gets fraction of vector that fits inside of bounding box if(axis == 0)//x axis { tempClipFractionLow = (((boxIn.getPosition().x - boxIn.getGlobalBounds().width/2) - startPos.x)/(endPos.x - startPos.x)); tempClipFractionHigh = (((boxIn.getPosition().x + boxIn.getGlobalBounds().width/2) - startPos.x)/(endPos.x - startPos.x)); } else if(axis == 1)//y axis { tempClipFractionLow = (((boxIn.getPosition().y + boxIn.getGlobalBounds().height/2) - startPos.y)/(endPos.y - startPos.y)); tempClipFractionHigh = (((boxIn.getPosition().y - boxIn.getGlobalBounds().height/2) - startPos.y)/(endPos.y - startPos.y)); } else { return false; } if(tempClipFractionHigh < tempClipFractionLow) { swap(tempClipFractionHigh,tempClipFractionLow); } if(tempClipFractionHigh < clipFractionLow) { return false; } else if(tempClipFractionLow > clipFractionHigh) { return false; } clipFractionLow = max(tempClipFractionLow,clipFractionLow); clipFractionHigh = min(tempClipFractionHigh,clipFractionHigh); if(clipFractionLow > clipFractionHigh) { return false; } return true; }
//============================================================================================ bool Enemy::checkCollision(sf::RectangleShape& playerBounds) { if(boundingBox.getGlobalBounds().intersects(playerBounds.getGlobalBounds())){ stateChange = true; currentState = attack; } return stateChange; }
//check for collisions bool Collision(sf::RectangleShape object1, sf::RectangleShape object2) { if (object1.getGlobalBounds().intersects(object2.getGlobalBounds())) { //if in collision with the second passed object return true; } else { //if not in collision with the second passed object return false; } }
void PongGame::handleBallHitsTopOrBottom(sf::RectangleShape &border) { sf::FloatRect rectangleBounds = border.getGlobalBounds(); sf::FloatRect ballRectangleBounds = ball.getGlobalBounds(); ballSpeed.y = -ballSpeed.y; float ballYPosition = 0; if (&bottomRectangle == &border) { // ball hits bottom rectangle ballYPosition = rectangleBounds.top - rectangleBounds.height - ballRectangleBounds.top; } else if (&topRectangle == &border) { // ball hits top rectangle ballYPosition = rectangleBounds.top + rectangleBounds.height - ballRectangleBounds.top; } ball.move(sf::Vector2f(0, ballSpeed.y + 2 * ballYPosition) * timePerFrame.asSeconds()); }
void IsSelected(Selection s, sf::RenderWindow &Window) { if(sf::Mouse::isButtonPressed(sf::Mouse::Left)) { if(rect.getGlobalBounds().intersects(s.rect.getGlobalBounds()) || (sf::Mouse::getPosition(Window).x > rect.getPosition().x && sf::Mouse::getPosition(Window).x < rect.getPosition().x + rect.getSize().x && sf::Mouse::getPosition(Window).y > rect.getPosition().y && sf::Mouse::getPosition(Window).y < rect.getPosition().y + rect.getSize().y)) { selected = true; rect.setFillColor(sf::Color::Magenta); } else { selected = false; rect.setFillColor(sf::Color::Green); } } }
void update(sf::Time timeChange) { sf::Vector2f p1Movement(0, 0); sf::Vector2f p2Movement(0, 0); if (p1MovingUp) p1Movement.y -= paddleSpeed; if (p1MovingDown) p1Movement.y += paddleSpeed; if (p2MovingUp) p2Movement.y -= paddleSpeed; if (p2MovingDown) p2Movement.y += paddleSpeed; p1Paddle.move(p1Movement * timeChange.asSeconds()); p2Paddle.move(p2Movement * timeChange.asSeconds()); // Check collision if (ball.getPosition().y < 0 || ball.getPosition().y > 480){ ballMovement.y *= -1; } if (ball.getGlobalBounds().intersects(p1Paddle.getGlobalBounds()) || ball.getGlobalBounds().intersects(p2Paddle.getGlobalBounds())){ ballMovement.x *= -1; } // Scoring if (ball.getPosition().x > 600){ // P1 scores ball.setPosition(300, 240); p1Score++; p1ScoreText.setString(std::to_string(p1Score)); } else if (ball.getPosition().x < 0){ // P2 scores ball.setPosition(300, 240); p2Score++; p2ScoreText.setString(std::to_string(p2Score)); } ball.move(ballMovement * timeChange.asSeconds()); }
void PongGame::handleBallCollidesWithPlayer(sf::RectangleShape &player) { sf::FloatRect playerBounds = player.getGlobalBounds(); sf::FloatRect ballBounds = ball.getGlobalBounds(); ballSpeed.x = -ballSpeed.x; ballSpeed.y = (ballBounds.top + ballBounds.height / 2 - playerBounds.top - playerBounds.height / 2) / 1; float correction = 0; if (&player2 == &player) { correction = ballBounds.left + ballBounds.width - playerBounds.left; ballBounds.left = playerBounds.left - ballBounds.width - correction; } else if (&player1 == &player) { correction = playerBounds.left + playerBounds.width - ballBounds.left; ballBounds.left = playerBounds.left + playerBounds.width + correction; } ball.setPosition(ballBounds.left, ballBounds.top); increaseBallSpeed(); }
bool tr::CColisiometro::Interseccion(sf::RectangleShape rectangle, sf::CircleShape circle, sf::RectangleShape* choque = nullptr) { sf::FloatRect rect2 = rectangle.getGlobalBounds(); sf::FloatRect rect1 = circle.getGlobalBounds(); sf::FloatRect intersection; if (rect2.intersects(rect1, intersection)) { if (choque != nullptr) { //le regreso la zona del choque choque->setPosition(sf::Vector2f(intersection.left, intersection.top)); choque->setSize(sf::Vector2f(intersection.width,intersection.height)); } return true; }else { return false; } }
void cWindow::paintClouds() { brushRect.setScale(vec2f(1.00f, 1.00f)); brushRect.setFillColor(color(0, 0, 0)); //brushRect.setTexture(&visual.gameTex[database.texture[TEX_CLOUD]].handle); sf::FloatRect camRect(camera.pos.x, camera.pos.y, camera.res.x, camera.res.y); mutex.renderClouds.lock(); visual.cloudsPainted = 0; for (int i = 0; i < (int)weather.cloud.size(); i++) { //brushRect.setTexture(&weather.cloudTexture[i], true); brushRect.setTexture(&visual.gameTex[weather.cloud[i].tex].handle, true); brushRect.setPosition(weather.cloud[i].pos); brushRect.setSize(weather.cloud[i].size); brushRect.setOrigin(weather.cloud[i].size / 2.00f); if (camRect.intersects(brushRect.getGlobalBounds())) { window.texHandleShadow.draw(brushRect, window.matrixHandle); visual.cloudsPainted += 1; } } mutex.renderClouds.unlock(); }
void Map::detectCollisionFC(sf::RectangleShape charShape) { for (int i = 0; i < foVector.size(); i++) { if (!foVector[i]->intersected && intersects(foVector[i], charShape.getGlobalBounds(), 0)) { foVector[i]->intersected = true; lives--; if (lives <= 0) { sound.setBuffer(dBuffer); sound.play(); gameOverB = true; } else { sound.setBuffer(oBuffer); sound.play(); } resetPosition(foVector[i]); curNumFO--; if (curNumFO == 0) { nextLevel(); } } } }
float getBoundingBoxTop() const { return _rect.getGlobalBounds().top; }
bool PongGame::doRectanglesIntersect(const sf::RectangleShape &rectangle1, sf::RectangleShape &rectangle2) { sf::FloatRect globalBoundsOfRectangle1 = rectangle1.getGlobalBounds(); sf::FloatRect globalBoundsOfRectangle2 = rectangle2.getGlobalBounds(); return globalBoundsOfRectangle1.intersects(globalBoundsOfRectangle2); }
sf::FloatRect Map::getFoodPos() { return food.getGlobalBounds(); }
sf::FloatRect GetBounds() const { return m_Paddle.getGlobalBounds(); };
Player () { PlayerNumber = totalPlayers; Sprite = loadSpriteData("Player " + intToString(PlayerNumber + 1)); //Get Global width, seperate to rotation CharacterWidth = Sprite.getGlobalBounds().width; nameSpacing = 30; healthBarSpacing = nameSpacing + 15; Sprite.setOrigin(Sprite.getGlobalBounds().width/2, Sprite.getGlobalBounds().height/2); Sprite.setPosition((App.getSize().x/2) - (Sprite.getGlobalBounds().width/2), (App.getSize().y/2) - (Sprite.getGlobalBounds().height/2)); //PlayerName.setFont(sf::Font::getDefaultFont()); PlayerName.setCharacterSize(15); PlayerName.setString("Player: " + intToString(PlayerNumber + 1)); PlayerName.setOrigin(PlayerName.getGlobalBounds().width/2, PlayerName.getGlobalBounds().height/2); PlayerName.setPosition(Sprite.getPosition().x, Sprite.getPosition().y + nameSpacing); NameOutline.setSize(sf::Vector2f(PlayerName.getGlobalBounds().width, PlayerName.getGlobalBounds().height)); NameOutline.setOutlineThickness(2); NameOutline.setFillColor(sf::Color(0, 0, 0, 120)); NameOutline.setOutlineColor(sf::Color::Black); NameOutline.setOrigin(NameOutline.getGlobalBounds().width/2, NameOutline.getGlobalBounds().height/2); NameOutline.setPosition(Sprite.getPosition().x + 1.5, Sprite.getPosition().y + nameSpacing + 4); MaxHealth = Health = 100; HealthBarSize = 50; HealthBarRed.setSize(sf::Vector2f(HealthBarSize, 5)); HealthBarRed.setOutlineThickness(1); HealthBarRed.setFillColor(sf::Color::Red); HealthBarRed.setOutlineColor(sf::Color::Black); HealthBarRed.setPosition(Sprite.getPosition().x - (CharacterWidth/2) - 5, Sprite.getPosition().y + healthBarSpacing); HealthBarGreen.setSize(sf::Vector2f(HealthBarSize, 5)); HealthBarGreen.setOutlineThickness(1); HealthBarGreen.setFillColor(sf::Color::Green); HealthBarGreen.setOutlineColor(sf::Color::Black); HealthBarGreen.setPosition(Sprite.getPosition().x - (CharacterWidth/2) - 5, Sprite.getPosition().y + healthBarSpacing); if (controllerConnected) { ControlScheme = ControlSchemes::Joystick; Key k; k.InputType = Input::JoystickMovedNeg; k.KeyCode = sf::Joystick::Axis::Y; Bindings["Up"] = k; k.InputType = Input::JoystickMovedPos; k.KeyCode = sf::Joystick::Axis::Y; Bindings["Down"] = k; k.InputType = Input::JoystickMovedNeg; k.KeyCode = sf::Joystick::Axis::X; Bindings["Left"] = k; k.InputType = Input::JoystickMovedPos; k.KeyCode = sf::Joystick::Axis::X; Bindings["Right"] = k; } else { ControlScheme = ControlSchemes::KeyboardMouse; Key k; k.InputType = Input::KeyboardInput; k.KeyCode = sf::Keyboard::W; Bindings["Up"] = k; k.InputType = Input::KeyboardInput; k.KeyCode = sf::Keyboard::S; Bindings["Down"] = k; k.InputType = Input::KeyboardInput; k.KeyCode = sf::Keyboard::A; Bindings["Left"] = k; k.InputType = Input::KeyboardInput; k.KeyCode = sf::Keyboard::D; Bindings["Right"] = k; } totalPlayers++; }
int PlayerConnection::Run(sf::RenderWindow &mainWin, int screen) { if (!this->font.loadFromFile("resources/fonts/" + DEFAULT_FONT)) { std::cerr << "Error loading the font " + DEFAULT_FONT << std::endl; system("pause"); return (-1); } this->initializeItems(); sf::Event Event; //Boucler tant que la fenêtre n'est pas fermée while (true) { bool usernameIsValid = username.size() > 3; //Vérifier les événements while (mainWin.pollEvent(Event)) { if (Event.type == sf::Event::Closed) { return (-1); } if (Event.type == sf::Event::TextEntered) //On veut seulement les caracteres ASCII { bool isANumber = (Event.text.unicode >= 48 && Event.text.unicode <= 57); bool isALetter = (Event.text.unicode >= 65 && Event.text.unicode <= 90) || (Event.text.unicode >= 97 && Event.text.unicode <= 122); if (username.size() < 13 && (isANumber || isALetter)) { username += static_cast<char>(Event.text.unicode); } } if (Event.type == sf::Event::KeyReleased && Event.key.code == sf::Keyboard::BackSpace) { username = ""; } if (Event.type == sf::Event::MouseButtonReleased) { sf::Vector2f mouse_coords = sf::Vector2f(Event.mouseButton.x, Event.mouseButton.y); if (intersects(mouse_coords, start.getGlobalBounds()) && usernameIsValid) { return (MENU); } else if (intersects(mouse_coords, nameContainer.getGlobalBounds())) { username = ""; } } if (Event.type == sf::Event::MouseMoved) { sf::Vector2f mouse_coords = sf::Vector2f(Event.mouseMove.x, Event.mouseMove.y); if (intersects(mouse_coords, start.getGlobalBounds())) { start.setColor(sf::Color::Red); start.setScale(1.05, 1.05); } else { start.setColor(sf::Color::White); start.setScale(1,1); } } nameDisplay.setString(username); } mainWin.clear(); mainWin.draw(spriteLogo); mainWin.draw(text); if (!usernameIsValid) { nameContainer.setOutlineColor(sf::Color::Red); } else { nameContainer.setOutlineColor(sf::Color::White); } mainWin.draw(nameContainer); mainWin.draw(nameDisplay); mainWin.draw(start); mainWin.display(); } //On ne devrait jamais se rendre jusqu'à ce point, mais juste au cas, on ferme l'application. return (-1); }
sf::FloatRect getBoundingBox() const { return _rect.getGlobalBounds(); }
// This version of the function is for rectangleShapes (rectangles with no texture attatched) // Otherwise the function is the same as above void cScreen::buttonHighlightDetect(sf::Vector2i &mousePos, sf::RectangleShape &button) { if (button.getGlobalBounds().contains(mousePos.x, mousePos.y)) { button.setFillColor(sf::Color(200, 200, 200)); } else button.setFillColor(sf::Color::White); }
float getBoundingBoxLeft() const { return _rect.getGlobalBounds().left; }
bool Paddle::isColliding(sf::RectangleShape RECTANGLE) { if(paddleObject.getGlobalBounds().intersects(RECTANGLE.getGlobalBounds())) return true; return false; }
float getBoundingBoxHeight() const { return _rect.getGlobalBounds().height; }