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

Author Topic: AlexxanderX problems...  (Read 25404 times)

0 Members and 1 Guest are viewing this topic.

AlexxanderX

  • Full Member
  • ***
  • Posts: 128
    • View Profile
    • AlexanderX
AlexxanderX problems...
« on: October 08, 2012, 07:23:42 pm »
Another problem. I try to make a moving system but I have a probem: when the rectangle(player) fall down from the floor( big regtangle) and I move to right || left the player go in the floor and I don't know how to ressolve it.
Here is the code:
///////////////
/// Headers ///
///////////////
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/Audio.hpp>

///////////////

////////////
/// Code ///
////////////
int main()
{
    /////////////////////////
    // Declarare variabile //
    /////////////////////////
    sf::Vector2f playersize(50,50); // Marimea jucatorului
    sf::Vector2f floorsize(600,50); // Marimea platformei
    bool isPlaying = false; // Variabila de verificare a desfasurarii jocului
    int wWidth = 800;
    int wHeight = 600;
    /////////////////////////

    //////////////////////
    // Creare fereastra //
    //////////////////////
    sf::RenderWindow window(sf::VideoMode(wWidth,wHeight),"Moving System"); // Fereastra
    window.setVerticalSyncEnabled(true); // OpenGL VerticalSync
    //////////////////////

    /////////////
    // Fonturi //
    /////////////
        // Font general
    sf::Font font;
    if (!font.loadFromFile("resources/AGENTORANGE.ttf")) return -1;
    /////////////

    /////////////////////
    // Muzica & Sunete //
    /////////////////////
        // Muzica
            // Muzica de background
                // Background Music 1
//    sf::Music bcgmsc1;
//    if(!bcgmsc1.openFromFile("resources/backgroundmusic.ogg")) return -1;
//    bcgmsc1.play();

        // Sunete

    /////////////////////

    // Creare jucator
    sf::RectangleShape player;
    player.setSize(playersize);
    player.setOutlineThickness(3);
    player.setOutlineColor(sf::Color::Black);
    player.setFillColor(sf::Color::Blue);
    player.setOrigin(playersize / 2.f);


    ////////////
    // Mesaje //
    ////////////
        // Mesaj general
    sf::Text message;
    message.setCharacterSize(40);
    message.setFont(font);
    message.setPosition(100, 15);
    message.setColor(sf::Color::Red);
    message.setString("Moving System\n Space to try it!");
    ////////////

    //////////////////////////////////////
    // Viteza jucator si alte variabile //
    //////////////////////////////////////
    float playerSpeed = 200.f;
    float playerDownSpeed = 300.f;
    float playerDownSpeedT = 50.f;
    //////////////////////////////////////

    ///////////
    // Level //
    ///////////
    sf::RectangleShape floor1;
    floor1.setSize(floorsize);
    floor1.setOutlineThickness(2);
    floor1.setOutlineColor(sf::Color::Green);
    floor1.setFillColor(sf::Color::Black);
    floor1.setOrigin(floorsize / 1.f);
    ///////////

    /////////////
    // Actiune //
    /////////////
    sf::Clock clock; // Crearea ceasului( clock)
    while (window.isOpen())
    {
        sf::Event event;

        while (window.pollEvent(event))
        {
            // Metoda de inchidere
            if ((event.type == sf::Event::Closed) ||
               ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Escape)))
            {
                window.close();
                break;
            }

            if((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Space))
            {
                if(!isPlaying)
                {
                    isPlaying=true;
                    clock.restart();

                    // Resetare pozitii
                    player.setPosition(400,200);
                    floor1.setPosition(700,600);
                }
            }
        }

        if (isPlaying)
        {
            float deltaTime = clock.restart().asSeconds();

            // Miscarea jucatorului
                // Miscarea in Stanga
            if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left) && (player.getPosition().x > 0.f + 20))
            {

                sf::Vector2f pos = player.getPosition();
                player.move(-playerSpeed * deltaTime,0);
            }
                // Miscarea in Dreapta
            if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right) && (player.getPosition().x < wWidth - 20))
            {
                sf::Vector2f pos = player.getPosition();
                player.move(playerSpeed * deltaTime,0);
            }

            // Coliziunea in JOS si SUS
            if (!(player.getPosition().y > floor1.getPosition().y + floorsize.y / 2 - 105))
            {
                player.move(0, playerDownSpeed * deltaTime);
            }

            if (!(player.getPosition().x < floor1.getPosition().x + 24) || !(player.getPosition().x > wWidth - floor1.getPosition().x - 24) )
            {
                player.move(0, playerDownSpeedT * deltaTime);
            }

        }

        // Sterge fereastra
        window.clear(sf::Color::Yellow);

        // Afisare
        if(isPlaying)
        {
            window.draw(player);
            window.draw(floor1);
        }
        else
        {
            window.draw(message);
        }

        // Afisarea ecranul
        window.display();
    }

    return 0;
}
 

And here a screenshot:
« Last Edit: October 17, 2012, 07:31:23 pm by AlexxanderX »
Here you can find my blog and tutorials about SFML - http://alexanderx.net/ (died...) - http://web.archive.org/web/20160110002847/http://alexanderx.net/

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Resolve code...
« Reply #1 on: October 08, 2012, 08:13:28 pm »
By reading some tutorials on collision detection in your case you'd probably want AABB (Axis-Aligned Bounding-Box).
Also you should take more time to look into your code, make research on your own, read tutorials etc. instead of creating new topics whenever things just don't work right away.
We do gladly help but part of programming is finding mistakes and do research on your own, so you should also skill yourself in those categories. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

AlexxanderX

  • Full Member
  • ***
  • Posts: 128
    • View Profile
    • AlexanderX
Re: Resolve code...
« Reply #2 on: October 09, 2012, 07:49:29 am »
Yes, I want an AABB. I searched on net and foundet Box2D. Is good Box2D? And is work with SFML?
Here you can find my blog and tutorials about SFML - http://alexanderx.net/ (died...) - http://web.archive.org/web/20160110002847/http://alexanderx.net/

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Resolve code...
« Reply #3 on: October 09, 2012, 09:22:13 am »
Yes Box2D is good and it works with SFML, but since I feel that you're not very familiar with C++ and SFML I'd advise you against it for now and you're better of implementing AABB on your own, which is very easy with SFML, in fact you don't really need to implement it, but you can use sf::Rect::contains() or sf::Rect::intersects() functions. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

AlexxanderX

  • Full Member
  • ***
  • Posts: 128
    • View Profile
    • AlexanderX
Re: Resolve code...
« Reply #4 on: October 09, 2012, 02:09:03 pm »
Hmm, seem hard. I know only the base on C++ but I can learn and same with SFML. I will try to use your information and to see if can. Exist some tutorials about creating an AABB?
Here you can find my blog and tutorials about SFML - http://alexanderx.net/ (died...) - http://web.archive.org/web/20160110002847/http://alexanderx.net/

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Resolve code...
« Reply #5 on: October 09, 2012, 03:52:33 pm »
Do you even try to find some information or did you investigate the path I showed you with the sf::Rect? Or do you just wait until someone writes you a step by step explanation? :-\

if(sf::FloatRect(floor1.getPosition(), floor1.getSize()).intersects(sf::FloatRect(player.getPosition(), player.getSize())))
    std::cout << "Collision!!!!!111" << std::endl;
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

AlexxanderX

  • Full Member
  • ***
  • Posts: 128
    • View Profile
    • AlexanderX
Re: Resolve code...
« Reply #6 on: October 09, 2012, 04:12:48 pm »
I didn't wanted you to show me the code and yes, I read the information from sf::Rect and I was trying to implement in my code. I only asked if exist some tutorials...  :-[
This is what I maked:
sf::IntRect playerr(player.getPosition().x, player.getPosition().y, 50, 50);
            sf::IntRect floor1r(floor1.getPosition().x, floor1.getPosition().y, 600, 50);
            sf::IntRect intersectr;
            intersect = playerr.intersects(floor1r, intersectr);

            if (!intersect)
            {
                player.move(0, playerDownSpeed * deltaTime);
            }
  ;D

Modify: How to stop my shape, if don't touch another shape( the floor) he is falling down. I think to do a loop to verify every time if he is touching a shape but when running the code he froze when press space:
while (!sf::FloatRect(player.getPosition(), player.getSize()).intersects(sf::FloatRect(floor1.getPosition(), floor1.getSize())))
            {
                player.move(0, playerDownSpeed * deltaTime);
            }

if (!sf::FloatRect(player.getPosition(), player.getSize()).intersects(sf::FloatRect(floor1.getPosition(), floor1.getSize())))
            {
                do
                {
                    player.move(0, playerDownSpeed * deltaTime);
                } while (!sf::FloatRect(player.getPosition(), player.getSize()).intersects(sf::FloatRect(floor1.getPosition(), floor1.getSize())));
            }

do
            {
                player.move(0, playerDownSpeed * deltaTime);
            } while (!sf::FloatRect(player.getPosition(), player.getSize()).intersects(sf::FloatRect(floor1.getPosition(), floor1.getSize())));

Those(3) were my attempts with the loop. Att all 3 when pressed space the app frozen.
« Last Edit: October 09, 2012, 04:42:24 pm by AlexxanderX »
Here you can find my blog and tutorials about SFML - http://alexanderx.net/ (died...) - http://web.archive.org/web/20160110002847/http://alexanderx.net/

AlexxanderX

  • Full Member
  • ***
  • Posts: 128
    • View Profile
    • AlexanderX
Re: Resolve code...
« Reply #7 on: October 09, 2012, 07:52:30 pm »
Tryed and this but same result:
sf::IntRect playerr(player.getPosition().x, player.getPosition().y, 50, 50);
            sf::IntRect floor1r(floor1.getPosition().x, floor1.getPosition().y, 600, 50);
            bool col;
            col = floor1r.contains(player.getPosition().x, player.getPosition().y);
            // Coliziunea in JOS si SUS
            if ((!sf::FloatRect(player.getPosition(), player.getSize()).intersects(sf::FloatRect(floor1.getPosition(), floor1.getSize()))) && !col)
            {
                player.move(0, playerDownSpeed * deltaTime);

            }
Please help, I really can't resolve this problem :(
Here you can find my blog and tutorials about SFML - http://alexanderx.net/ (died...) - http://web.archive.org/web/20160110002847/http://alexanderx.net/

AlexxanderX

  • Full Member
  • ***
  • Posts: 128
    • View Profile
    • AlexanderX
Re: Resolve code...
« Reply #8 on: October 10, 2012, 03:59:46 pm »
OFF: I see no help, but if noone no that is  ;D

I tryed to do this:
if (sf::FloatRect(player.getPosition(), player.getSize()).intersects(sf::FloatRect(floor1.getPosition(), floor1.getSize())))
            {
                gameover=true;

            }

// Sterge fereastra
        window.clear(sf::Color::Yellow);

        // Afisare
        if(isPlaying)
        {
            window.draw(player);
            window.draw(floor1);
            window.draw(messagev);
            if (gameover)
            {
//                window.clear(sf::Color::Black);
                window.draw(msgover);
            }
        }
        else
        {
            window.draw(message);
        }

But nothing. Why?
Here you can find my blog and tutorials about SFML - http://alexanderx.net/ (died...) - http://web.archive.org/web/20160110002847/http://alexanderx.net/

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Resolve code...
« Reply #9 on: October 10, 2012, 05:40:58 pm »
I tryed to do this:
But nothing. Why?
Sorry I've no idea what the problem is. "But nothing" isn't really a useful problem description... :-\

Modify: How to stop my shape, if don't touch another shape( the floor) he is falling down. I think to do a loop to verify every time if he is touching a shape but when running the code he froze when press space:
Is my assumption right that you've no idea what you're doing?

Just sit down for a moment without the PC in front of you and think about this logically.
If nothing special is happening then the player will fall, thus this is the 'default' behavior and that should happen every frame iteration.
But if he hits the ground he shouldn't move anymore.

This logic then translates into something like this:
bool hit = false;
while(window.isOpen())
{
    // ...
    // Event handling
    // ...
    if(!hit)
    {
        player.move(player.getPosition().x, player.getPosition().y + dt * gravity_speed);
    }
    if(collision(player, floor))
    {
        hit = true;
        // reset players position to align with the floor
    }
   
    // draw stuff
}

This skill on how to translate ideas into code is what a programmer needs. If you're not that good in such translations then maybe you shouldn't use SFML already but learn more on how to program with some simpler applications...
« Last Edit: October 10, 2012, 06:14:06 pm by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

AlexxanderX

  • Full Member
  • ***
  • Posts: 128
    • View Profile
    • AlexanderX
Re: Resolve code...
« Reply #10 on: October 11, 2012, 02:10:10 pm »
This I maked but I have problems stoping the player. So I can't figure out this
// reset players position to align with the floor
because when the player is falling down he can move to the left or right so I tryed with
player.move(player.getPosition());
but nothing happen( the player continue falling down).
if(fly)
            {
                player.move(0,playerDownSpeed * deltaTime);
            }

            if (sf::FloatRect(player.getPosition(), player.getSize()).intersects(sf::FloatRect(floor1.getPosition(), floor1.getSize())))
            {
                fly=false;
                player.move(player.getPosition());
            }

You wroted
player.move(player.getPosition().x, player.getPosition().y + dt * gravity_speed);
and I translated for my code in
player.move(player.getPosition().x, player.getPosition().y + playerDownSpeed * deltaTime);
and when I run the code and press space my shape(player) didn't show...

OFF: I'm yound and I don't have so much time for programming and at school I learn only the base for C++.
Here you can find my blog and tutorials about SFML - http://alexanderx.net/ (died...) - http://web.archive.org/web/20160110002847/http://alexanderx.net/

AlexxanderX

  • Full Member
  • ***
  • Posts: 128
    • View Profile
    • AlexanderX
Re: Resolve code...
« Reply #11 on: October 11, 2012, 06:21:22 pm »
I thought to make this:
if(fly)
            {
                player.move(0,playerDownSpeed * deltaTime);
            }

            if (sf::FloatRect(player.getPosition(), player.getSize()).intersects(sf::FloatRect(floor1.getPosition(), floor1.getSize())))
            {
                fly=false;
                black = true;
            }
window.clear(sf::Color::Yellow);

        // Afisare
        if(isPlaying)
        {
            window.draw(player);
            window.draw(floor1);
            window.draw(messagev);
            if (gameover)
            {
                window.clear(sf::Color::Black);
                window.draw(msgover);
            }

            if (black)
            {
                window.clear(sf::Color::Green);
            }
        }
        else
        {
            window.draw(message);
        }

        // Afisarea ecranul
        window.display();

But the green screen didn't appear. I tryed to put to the if statement where verify if press left and when I pressed left the screen gone green. I think the problem in my code is this:
sf::FloatRect(player.getPosition(), player.getSize()).intersects(sf::FloatRect(floor1.getPosition(), floor1.getSize()))
Here you can find my blog and tutorials about SFML - http://alexanderx.net/ (died...) - http://web.archive.org/web/20160110002847/http://alexanderx.net/

AlexxanderX

  • Full Member
  • ***
  • Posts: 128
    • View Profile
    • AlexanderX
Re: Resolve code...
« Reply #12 on: October 12, 2012, 10:16:24 pm »
Can help because I don't know any onther solutions in mind. In the documentation in the exemple is something like this:
 // Test the intersection between r1 and r2
 sf::IntRect result;
 bool b3 = r1.intersects(r2, result); // true
 // result == (4, 2, 16, 3)
And I tryed something like this:
sf::IntRect playerr(player.getPosition().x, player.getPosition().y, 50,50);
            sf::IntRect floor1r(floor1.getPosition().x, floor1.getPosition().y, 600,50);
            sf::IntRect result;
            bool b3 = playerr.intersects(floor1r, result); // true

            if (b3)
            {
                fly=false;
                black = true;
                std::cout << "collision";
            }
And nothing, on the console no "collision" message. I tryed when press the right arrow on console to show me "Right" and worked. So, I really dont't know where is the problem but I think is the condition.
Here you can find my blog and tutorials about SFML - http://alexanderx.net/ (died...) - http://web.archive.org/web/20160110002847/http://alexanderx.net/

AlexxanderX

  • Full Member
  • ***
  • Posts: 128
    • View Profile
    • AlexanderX
Re: Resolve code...
« Reply #13 on: October 13, 2012, 10:16:55 am »
Succes. I maked this from some tutorials:
bool intersectss (const RectangleShape & rect1, const RectangleShape & rect2)
{
    FloatRect r1 = rect1.getGlobalBounds();
    FloatRect r2 = rect2.getGlobalBounds();

    return r1.intersects(r2);
}
if (intersectss(player, floor1))
            {
                FloatRect a=player.getGlobalBounds();
                FloatRect b=floor1.getGlobalBounds();
                std::cout << "collision";
                player.move(player.getPosition().x,player.getPosition().y);
            }
But I have problems with
player.move(player.getPosition().x,player.getPosition().y);
I don't know how to stop him, move him to align with the floor because when he is fly( not collision) he can move to the left or right and I don't know how to make this. With the above function the player dissapear from window.

EDIT
I modified in this:
if (intersectss(player, floor1))
            {
                fly=false;
                FloatRect a=player.getGlobalBounds();
                FloatRect b=floor1.getGlobalBounds();
                std::cout << "collision";
                player.move(0, 0);
                std::cout << player.getPosition().x << " " << player.getPosition().y << "\n";
            }
and is working. Please tell me if need some modifications? And thanks for helping, more for no helping I thanks  ;D.

EDIT2
Now have the same problem like in the first image.  :-\ I will try with the function sf::Rect::contain to sse what I can. But I maked a good collision system  ;D
« Last Edit: October 13, 2012, 12:52:37 pm by AlexxanderX »
Here you can find my blog and tutorials about SFML - http://alexanderx.net/ (died...) - http://web.archive.org/web/20160110002847/http://alexanderx.net/

AlexxanderX

  • Full Member
  • ***
  • Posts: 128
    • View Profile
    • AlexanderX
Re: Resolve code...
« Reply #14 on: October 14, 2012, 12:44:47 pm »
I resolved in a method my problem  ??? . Now I want to implement a jump system but I don'y know how to do that :( . Please some help...
Here you can find my blog and tutorials about SFML - http://alexanderx.net/ (died...) - http://web.archive.org/web/20160110002847/http://alexanderx.net/

 

anything