Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Topics - ChayHawk

Pages: [1]
1
General / Whats wrong with my collision detection?
« on: March 24, 2023, 04:44:29 pm »
So ive been re learning SFML and i really wanted to learn collision, and i understand it far better now, but im still having a few issues, here is my code, what am i doing wrong with my collision? its not working.

//============================================================================
// Name             : SFML Game
// Author           : Chay Hawk
// Version          : 0.0.14
// Version Date     : March 24th 2023 @ 11:27 AM
// Date Created     :
// Lines of Code    : 224
// Description      :
//============================================================================

#include <iostream>
#include <string>
#include <sstream>
#include <SFML/Graphics.hpp>

sf::Text CreateText(const sf::Font& font, int characterSize, const sf::Color& color, float posX, float posY, const std::string& str)
{
    sf::Text text;
    text.setCharacterSize(characterSize);
    text.setFont(font);
    text.setFillColor(color);
    text.setPosition(sf::Vector2f(posX, posY));
    text.setString(str);

    return text;
}

std::unique_ptr<sf::RectangleShape> CreateRectangle(float width, float height, const sf::Color& color, float setXPos, float setYPos)
{
    std::unique_ptr<sf::RectangleShape> rectangle(new sf::RectangleShape);

    rectangle->setSize(sf::Vector2f(width, height));
    rectangle->setFillColor(color);
    rectangle->setPosition(sf::Vector2f(setXPos, setYPos));
    rectangle->setOrigin(sf::Vector2f(rectangle->getSize().x / 2, rectangle->getSize().y / 2));

    return rectangle;
}


int main()
{
    sf::RenderWindow window(sf::VideoMode(1280, 720), "SFML Feature Testing");
    window.setFramerateLimit(60);

    sf::Font Arial;
    Arial.loadFromFile("Resources/Fonts/arial.ttf");

    std::ostringstream oss;
    std::istringstream iss;

    float windowCenterX{ window.getSize().x / 2.0f };
    float windowCenterY{ window.getSize().y / 2.0f };

    sf::Text MouseXText{ CreateText(Arial,  32, sf::Color::White, 0.0f, 0.0f, "Mouse X Position: ") };
    sf::Text MouseXValue{ CreateText(Arial, 32, sf::Color::White, 0.0f, 0.0f, "Mouse X Value: ") };
    sf::Text MouseYText{ CreateText(Arial,  32, sf::Color::White, 0.0f, 0.0f, "Mouse Y Pos: ") };
    sf::Text MouseYValue{ CreateText(Arial, 32, sf::Color::White, 204, 30,    "Mouse Y Value : ") };


    MouseXValue.setPosition(sf::Vector2f(MouseXText.getPosition().x + MouseXText.getGlobalBounds().width, 0.0f));
    MouseYText.setPosition(sf::Vector2f(0, MouseXText.getPosition().y + MouseXText.getGlobalBounds().height + 5));


    //Center Square
    auto square{ CreateRectangle(300, 300, sf::Color::White, windowCenterX, windowCenterY) };

    float squareLeft{ };
    float squareTop{ };
    float squareRight{ };
    float squareBottom{ };

    //Squares
    auto cornerSquare_1 { CreateRectangle(35, 35, sf::Color::Blue,  0, 0) };
    auto cornerSquare_2 { CreateRectangle(35, 35, sf::Color::Cyan,  0, 0) };
    auto cornerSquare_3 { CreateRectangle(35, 35, sf::Color::Green, 0, 0) };
    auto cornerSquare_4 { CreateRectangle(35, 35, sf::Color::Red,   0, 0) };


    int playerX{ 150 };
    int playerY{ 150 };

    //Player
    auto player{ CreateRectangle(32, 32, sf::Color::Yellow, playerX, playerY) };

    float playerLeft{ };
    float playerTop{ };
    float playerRight{ };
    float playerBottom{ };

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
            {
                window.close();
            }

            if (event.type == sf::Event::Resized)
            {
                window.setView(sf::View(sf::FloatRect(0.0f, 0.0f, static_cast<float>(event.size.width), static_cast<float>(event.size.height))));
                square->setPosition(sf::Vector2f(window.getSize().x / 2, window.getSize().y / 2));
            }
        }

        oss << sf::Mouse::getPosition(window).x;

        MouseXValue.setString(oss.str());

        oss.str("");

        oss << sf::Mouse::getPosition(window).y;

        MouseYValue.setString(oss.str());

        oss.str("");


        /*std::cout << "Player X Position: " << player->getPosition().x << &#39;\n&#39;;
        std::cout << "Player Y Position: " << player->getPosition().y << &#39;\n&#39;;*/


        std::cout << "Player X Left Side:  " << player->getPosition().x - player->getOrigin().x << &#39;\n&#39;;
        std::cout << "Player Y Top:        " << player->getPosition().y - player->getOrigin().y << &#39;\n&#39;;
        std::cout << "Player X Right Side: " << player->getPosition().x + player->getOrigin().x << &#39;\n&#39;;
        std::cout << "Player Y Bottom:     " << player->getPosition().y + player->getOrigin().y << &#39;\n&#39;;

        if (   sf::Mouse::getPosition(window).x >= square->getPosition().x - square->getOrigin().x
            && sf::Mouse::getPosition(window).x <= square->getPosition().x + square->getOrigin().x
            && sf::Mouse::getPosition(window).y >= square->getPosition().y - square->getOrigin().y
            && sf::Mouse::getPosition(window).y <= square->getPosition().y + square->getOrigin().y
            )
        {
            std::cout << "Within white square\n";
        }

        playerLeft = player->getPosition().x - player->getOrigin().x;
        playerTop = player->getPosition().y - player->getOrigin().y;
        playerRight = player->getPosition().x + player->getOrigin().x;
        playerBottom = player->getPosition().y + player->getOrigin().y;

        squareLeft = square->getPosition().x - square->getOrigin().x;
        squareTop = square->getPosition().y - square->getOrigin().y;
        squareRight = square->getPosition().x + square->getOrigin().x;
        squareBottom = square->getPosition().y + square->getOrigin().y;

        if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
        {
            if (playerTop >= squareBottom)
            {
                player->move(0, -5);
            }
        }

        if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
        {
            if (playerLeft <= squareRight)
            {
                player->move(-5, 0);
            }
        }

        if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
        {
            if (playerBottom <= squareTop)
            {
                player->move(0, 5);
            }
        }

        if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
        {
            if (playerRight >= squareLeft)
            {
                player->move(5, 0);
            }
        }

        //Set cornerSquare to one corner.
        //Bottom Right (Blue)
        cornerSquare_1->setPosition(sf::Vector2f(square->getPosition().x + square->getOrigin().x + cornerSquare_1->getOrigin().x, square->getPosition().y + square->getOrigin().y + cornerSquare_1->getOrigin().y));

        //Top Left (Cyan)
        cornerSquare_2->setPosition(sf::Vector2f(square->getPosition().x - square->getOrigin().x - cornerSquare_2->getOrigin().x, square->getPosition().y - square->getOrigin().y - cornerSquare_2->getOrigin().y));

        //Top Right (Green)
        cornerSquare_3->setPosition(sf::Vector2f(square->getPosition().x + square->getOrigin().x + cornerSquare_3->getOrigin().y, square->getPosition().y - square->getOrigin().y - cornerSquare_3->getOrigin().y));

        //Bottom Left (Red)
        cornerSquare_4->setPosition(sf::Vector2f(square->getPosition().x - square->getOrigin().x - cornerSquare_4->getOrigin().y, square->getPosition().y + square->getOrigin().y + cornerSquare_4->getOrigin().y));
       
        window.clear();
        window.draw(MouseXText);
        window.draw(MouseXValue);
        window.draw(MouseYText);
        window.draw(MouseYValue);
        window.draw(*player);
        window.draw(*square);
        window.draw(*cornerSquare_1);
        window.draw(*cornerSquare_2);
        window.draw(*cornerSquare_3);
        window.draw(*cornerSquare_4);
        window.display();
    }

    return 0;
}

2
General / Can this collision code be simplified?
« on: May 27, 2022, 01:20:15 pm »
So i have been watching tutorials on SFML and this guy made these tutorials for collision, so I followed along and he said there could be better alternatives out there so im wondering, is there better cleaner/easier to read collision code for everything under the General Collision section or is this pretty much it?

Heres the tutorials i was following:

Part 1:
Part 2:
Part 3:
Part 5:



#include <SFML/Graphics.hpp>
#include <iostream>
#include <vector>

using std::cout;
using std::vector;

int main()
{
    const unsigned WINDOW_WIDTH{ 800 };
    const unsigned WINDOW_HEIGHT{ 600 };

    sf::RenderWindow window(sf::VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT), "SFML works!");
    window.setFramerateLimit(60);

    float dt;
    sf::Clock dtClock;

    const float gridSize = 50.0f;

    //Player
    const float movementSpeed = 100.f;
    sf::Vector2f velocity;
    sf::RectangleShape player;
    player.setFillColor(sf::Color::Green);
    player.setSize(sf::Vector2f(gridSize, gridSize));
    player.setPosition(sf::Vector2f(0, 0));

    //Walls
    std::vector<sf::RectangleShape> walls;

    sf::RectangleShape wall;
    wall.setFillColor(sf::Color::Red);
    wall.setSize(sf::Vector2f(gridSize, gridSize));
    wall.setPosition(gridSize * 5, gridSize * 2);

    walls.push_back(wall);

    //Collision
    sf::FloatRect nextPos;

    while (window.isOpen())
    {
        dt = dtClock.restart().asSeconds();

        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();

            //Prevents contents of window from being stretched
            if (event.type == sf::Event::Resized)
            {
                // update the view to the new size of the window
                sf::FloatRect visibleArea(0, 0, event.size.width, event.size.height);
                window.setView(sf::View(visibleArea));
            }
        }

        //Player Movement

        velocity.x = 0.f;
        velocity.y = 0.f;

        if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
        {
            velocity.y += -movementSpeed * dt;
        }
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
        {
            velocity.x += -movementSpeed * dt;
        }
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
        {
            velocity.y += movementSpeed * dt;
        }
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
        {
            velocity.x += movementSpeed * dt;
        }

        //General Collision
        for (auto& wall : walls)
        {
            sf::FloatRect playerBounds = player.getGlobalBounds();
            sf::FloatRect wallBounds = wall.getGlobalBounds();

            nextPos = playerBounds;
            nextPos.left += velocity.x;
            nextPos.top += velocity.y;

            if (wallBounds.intersects(nextPos))
            {
                //Bottom Collision
                if (playerBounds.top < wallBounds.top
                    && playerBounds.top + playerBounds.height < wallBounds.top + wallBounds.height
                    && playerBounds.left < wallBounds.left + wallBounds.width
                    && playerBounds.left + playerBounds.width > wallBounds.left)
                {
                    velocity.y = 0;
                    player.setPosition(playerBounds.left, wallBounds.top - playerBounds.height);
                }

                //Top Collision
                if (playerBounds.top > wallBounds.top
                    && playerBounds.top + playerBounds.height > wallBounds.top + wallBounds.height
                    && playerBounds.left < wallBounds.left + wallBounds.width
                    && playerBounds.left + playerBounds.width > wallBounds.left)
                {
                    velocity.y = 0;
                    player.setPosition(playerBounds.left, wallBounds.top + wallBounds.height);
                }

                //Right Collision
                if (playerBounds.left < wallBounds.left
                    && playerBounds.left + playerBounds.width < wallBounds.left + wallBounds.width
                    && playerBounds.top < wallBounds.top + wallBounds.height
                    && playerBounds.top + playerBounds.height > wallBounds.top)
                {
                    velocity.x = 0;
                    player.setPosition(wallBounds.left - playerBounds.width, playerBounds.top);
                }

                //Left Collision
                if (playerBounds.left > wallBounds.left
                    && playerBounds.left + playerBounds.width > wallBounds.left + wallBounds.width
                    && playerBounds.top < wallBounds.top + wallBounds.height
                    && playerBounds.top + playerBounds.height > wallBounds.top)
                {
                    velocity.x = 0;
                    player.setPosition(wallBounds.left + wallBounds.width, playerBounds.top);
                }
            }
        }

        player.move(velocity);

        //Screen Collision

        //Left Collision
        if (player.getPosition().x < 0.0)
        {
            player.setPosition(0, player.getPosition().y);
        }

        //Top Collision
        if (player.getPosition().y < 0)
        {
            player.setPosition(player.getPosition().x, 0);
        }

        //Right Collision
        if (player.getPosition().x + player.getGlobalBounds().width > window.getSize().x)
        {
            player.setPosition(window.getSize().x - player.getGlobalBounds().width, player.getPosition().y);
        }

        //Bottom Collision
        //Right Collision
        if (player.getPosition().y + player.getGlobalBounds().height > window.getSize().y)
        {
            player.setPosition(player.getPosition().x, window.getSize().y - player.getGlobalBounds().height);
        }

        window.clear();
        window.draw(player);

        for (auto& i : walls)
        {
            window.draw(i);
        }

        window.display();

    }

    return 0;
}

3
General / How to set SFML up for VS and not per project
« on: May 20, 2022, 07:59:30 pm »
So when i want to create an SFML project, I have to setup all the locations of the files and modules and its a pain because I like creating a lot of different projects to test things out and learn so for example I will make an SFML project where i just play around with time and getting familiar with SFML's time interface, and maybe another where i practice with loading textures and fonts etc, I dont want to have to keep creating projects and setting all this up each time, is there a way I can set it up once and then when i create a project i can just start using SFML right away? I could have sworn that this feature was available in an older VS but maybe im thinking of code blocks. any help would be appreciated. I'm using VS 2022 btw.

4
General / Will SFML work with Visual Studio 2022?
« on: May 13, 2022, 10:19:05 pm »
Its been quite a long time since i played around with SFML but I would like t6o start learning again so I downloaded it and got it working with VS 2022, im just wondering if there are any issues that might arise from this or if i should download 2017 and use it with that? or will 2022 be fine?

5
General discussions / How up to date are the books in the Learn section?
« on: February 12, 2021, 05:27:25 pm »
So it's been years and years and years since I last got into SFML. I think it was like SFML version 1.3 when i last tried it. I just got it working on VS 2019 and would like to buy some books but I see the books in the learn section were written in 2013-2017, are they still relevant to SFML's current release? like will i have a lot of trouble following them with the current version of SFML? if so, what books would you recommend?

Pages: [1]
anything