Пример #1
0
 float width() const noexcept { return shape.getSize().x; }
Пример #2
0
sf::Vector2f collide(sf::CircleShape ball, sf::RectangleShape block)
{
    sf::Vector2f ball_center = ball.getPosition() + sf::Vector2f(ball.getRadius(), ball.getRadius());
    sf::Vector2f block_center = block.getPosition() + sf::Vector2f(block.getSize().x/2, block.getSize().y/2);

    sf::Vector2f diff = ball_center - block_center;

    // Not intersecting
    if(abs(diff.x) > block.getSize().x/2 + ball.getRadius() || abs(diff.y) > block.getSize().y/2 + ball.getRadius())
    {
        return sf::Vector2f(0,0);
    }
    std::cout << diff.x << ", " << diff.y << std::endl;

    // hit edge of block (top/bottom)
    if(abs(diff.x) <= PADDLE_WIDTH/2)
    {
        if(diff.y <= 0)
        {
            return sf::Vector2f(0,-1);
        }
        else if(diff.y > 0)
        {
            return sf::Vector2f(0,1);
        }
    }
    // hit edge of block (left/right)
    else if(abs(diff.y) <= PADDLE_HEIGHT/2)
    {
        if(diff.x <= 0)
        {
            return sf::Vector2f(-1,0);
        }
        else if(diff.x > 0)
        {
            return sf::Vector2f(1,0);
        }
    }

    float distance = getDistance(ball_center, block.getPosition());
    if(distance <= ball.getRadius())
    {
        return (ball_center - block.getPosition()) / distance;
    }
    sf::Vector2f block_corner = block.getPosition() + sf::Vector2f(block.getSize().x,0);
    distance = getDistance(ball_center, block_corner);
    if(distance <= ball.getRadius())
    {
        return (ball_center - block_corner) / distance;
    }
    block_corner = block.getPosition() + sf::Vector2f(0, block.getSize().y);
    distance = getDistance(ball_center, block_corner);
    if(distance <= ball.getRadius())
    {
        return (ball_center - block_corner) / distance;
    }
    block_corner = block.getPosition() + block.getSize();
    distance = getDistance(ball_center, block_corner);
    if(distance <= ball.getRadius())
    {
        return (ball_center - block_corner) / distance;
    }

    // Not intersecting, even in the corners
    return sf::Vector2f(0,0);
}
Пример #3
0
int main(){

    logger.logNoEndl("Setting up window...");
    window.create(sf::VideoMode(430,430), "GL");
    logger.continueln(" done!");

    logger.logNoEndl("Setting up variables...");

    rect1.setFillColor(sf::Color(200,200,200));
    rect1.setOutlineColor(sf::Color(0,0,0));
    rect1.setOutlineThickness(-2);
    rect1.setPosition(10,10);

    rect2.setFillColor(sf::Color(200,200,200));
    rect2.setOutlineColor(sf::Color(0,0,0));
    rect2.setOutlineThickness(-2);
    rect2.setPosition(220,10);

    rect3.setFillColor(sf::Color(200,200,200));
    rect3.setOutlineColor(sf::Color(0,0,0));
    rect3.setOutlineThickness(-2);
    rect3.setPosition(10,220);

    rect4.setFillColor(sf::Color(200,200,200));
    rect4.setOutlineColor(sf::Color(0,0,0));
    rect4.setOutlineThickness(-2);
    rect4.setPosition(220,220);

    sf::Font font;
    if (!font.loadFromFile("arial.ttf")){
        logger.log(Logger::LogType::Error, "Arial font not found. Cannot continue...");
        return 1;
    }

    sf::Text label1("Solid White", font);
    label1.setColor(sf::Color::Black);
    label1.setPosition(rect1.getPosition().x + rect1.getSize().x / 2 - label1.getLocalBounds().width / 2, rect1.getPosition().y + rect1.getSize().y / 2 - label1.getLocalBounds().height / 2 - 10);

    sf::Text label2("Marquee", font);
    label2.setColor(sf::Color::Black);
    label2.setPosition(rect2.getPosition().x + rect2.getSize().x / 2 - label2.getLocalBounds().width / 2, rect2.getPosition().y + rect2.getSize().y / 2 - label2.getLocalBounds().height / 2 - 10);

    sf::Text label3("Color Cycle", font);
    label3.setColor(sf::Color::Black);
    label3.setPosition(rect3.getPosition().x + rect3.getSize().x / 2 - label3.getLocalBounds().width / 2, rect3.getPosition().y + rect3.getSize().y / 2 - label3.getLocalBounds().height / 2 - 10);

    sf::Text label4("Pew", font);
    label4.setColor(sf::Color::Black);
    label4.setPosition(rect4.getPosition().x + rect4.getSize().x / 2 - label4.getLocalBounds().width / 2, rect4.getPosition().y + rect4.getSize().y / 2 - label4.getLocalBounds().height / 2 - 10);


    unsigned short r = 0,
                   g = 0,
                   b = 0;

    logger.continueln(" done!");

    logger.log("Running until told to stop.");
    bool running = true;
    while(running){
        sf::Event event;
        while (window.pollEvent(event)){
            if (event.type == sf::Event::Closed){
                running = false;
            } else if (event.type == sf::Event::MouseMoved){
                doMouseMove();
            } else if (event.type == sf::Event::MouseButtonPressed || event.type == sf::Event::MouseButtonReleased){
                doMouseButton(event.mouseButton.button);
            }
        }
        window.clear(sf::Color::White);
        window.draw(rect1);
        window.draw(rect2);
        window.draw(rect3);
        window.draw(rect4);
        window.draw(label1);
        window.draw(label2);
        window.draw(label3);
        window.draw(label4);
        window.display();
    }

    logger.log("Closing...");
}
Пример #4
0
 float height() const noexcept { return shape.getSize().y; }
Пример #5
0
	void draw() {
		// update the mouse position and add it to the broadphase
		auto mousePosition = static_cast<sf::Vector2f>(sf::Mouse::getPosition(window));
		const auto mouseSize = mouseObject.getSize();
		mouseObject.setPosition(mousePosition);
		mousePosition -= mouseObject.getOrigin();
		broadphase.addRectangle(
			mousePosition.x,
			mousePosition.y,
			mouseSize.x,
			mouseSize.y,
			&mouseObject);

		// move the rectangles around and add them to the broadphase
		for (const auto& object : objects) {
			object->setPosition(randb(0, window.getSize().x), randb(0,  window.getSize().y));
			const auto objectPosition = object->getPosition() - object->getOrigin();
			const auto objectSize = object->getSize();
			broadphase.addRectangle(
				objectPosition.x,
				objectPosition.y,
				objectSize.x,
				objectSize.y,
				object);

			object->setFillColor(sf::Color(0x00, 0x8B, 0x8B));
			object->setOutlineColor(sf::Color::White);
		}

		// now query the collision pairs and change the color of objects that hit the mouse object to red
		const auto &collisionPairs = broadphase.getCollisionPairs();
		for (const auto &pair : collisionPairs) {
			sf::RectangleShape *other = nullptr;
			if (pair.first == &mouseObject)
				other = (sf::RectangleShape*)pair.second;
			else if (pair.second == &mouseObject)
				other = (sf::RectangleShape*)pair.first;
			else {
				((sf::RectangleShape*)pair.first)->setOutlineColor(sf::Color::Cyan);
				((sf::RectangleShape*)pair.second)->setOutlineColor(sf::Color::Cyan);
			}
			if (other != nullptr)
				other->setFillColor(sf::Color(200, 0, 0));
		}

		// clear out the broadphase for the next run
		broadphase.clear();

		// clear the window with black color
		window.clear(sf::Color::Black);

		for (const auto& object : objects)
			window.draw(*object);

		window.draw(mouseObject);

		// draw a grid to outline the buckets
		const float lineThickness = 5;
		const sf::Color lineColor(150, 150, 150);
		sf::RectangleShape verticalLine(sf::Vector2f(lineThickness, window.getSize().y));
		verticalLine.setFillColor(lineColor);
		for (size_t i = 1; i < (window.getSize().x / broadphase.getCellWidth()); ++i) {
			verticalLine.setPosition(i * broadphase.getCellWidth(), 0);
			window.draw(verticalLine);
		}
		sf::RectangleShape horizontalLine(sf::Vector2f(window.getSize().x, lineThickness));
		horizontalLine.setFillColor(lineColor);
		for (size_t i = 1; i < (window.getSize().y / broadphase.getCellHeight()); ++i) {
			horizontalLine.setPosition(0, i * broadphase.getCellHeight());
			window.draw(horizontalLine);
		}
	}