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

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

0 Members and 1 Guest are viewing this topic.

AlexxanderX

  • Full Member
  • ***
  • Posts: 128
    • View Profile
    • AlexanderX
Re: AlexxanderX problems...
« Reply #15 on: October 17, 2012, 07:40:28 pm »
Still did nothing. I don't know how to do that  :'( . This what I tryed:
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up) && PlayerJump)
        {
            float jumpn = 0.f;
            while (jumpn < 30.f)
            {
                player.move(0.f,-moveDistance*updateTime/100.0);
                jumpn++;

            }

            PlayerJump = false;
        }
But when I click on UP the player go up automatly, because, I think, the app make to fast the while function and I need something to pause them some miliseconds...
Here you can find my blog and tutorials about SFML - http://alexanderx.net/ (died...) - http://web.archive.org/web/20160110002847/http://alexanderx.net/

G.

  • Hero Member
  • *****
  • Posts: 1592
    • View Profile
Re: AlexxanderX problems...
« Reply #16 on: October 17, 2012, 09:39:00 pm »
Are you sure you know what's going on with your while? It's basic C++ (or algorithm), you should learn the basics before attempting more advanced things like games. ^^
float jumpn = 0.f;
while (jumpn < 30.f)
{
    player.move(0.f,-moveDistance*updateTime/100.0);
    jumpn++;

}
If you unfold this loop here's what you get :
float jumpn = 0.f;
player.move(0.f,-moveDistance*updateTime/100.0);
jumpn++;
player.move(0.f,-moveDistance*updateTime/100.0);
jumpn++;
[... 30 times total ...]
player.move(0.f,-moveDistance*updateTime/100.0);
jumpn++;
player.move(0.f,-moveDistance*updateTime/100.0);
jumpn++;
Which is also equal to :
float jumpn = 0.f;
player.move(0.f,(-moveDistance*updateTime/100.0) * 30);
jumpn += 30;
I'm sure you'll notice what's going wrong here.


eXpl0it3r, thanks, I haven't even noticed that I was using quotes instead of code. :/
« Last Edit: October 18, 2012, 02:01:31 am by G. »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10829
    • View Profile
    • development blog
    • Email
Re: AlexxanderX problems...
« Reply #17 on: October 17, 2012, 10:31:00 pm »
@G. please don't use quotes for displaying code, but make use of the code=cpp tag... ;)

It's basic C++ (or algorithm), you should learn the basics before attempting more advanced things like games.
Yes that's exactly why I haven't answered here anymore...

You're lacking major basic knowledge in basic concepts, some aren't even related to C++ or SFML directly but are plain logical ones (like the one G. illustrated). That's also why I told you to sit down and really think about what you're trying to do and then implement it and not just write something and hope it would work as you want it to and if it doesn't come here and post the problem... :-\
Learn more about C++ and try to read some code of other people and make sure you understand every little detail.

But since you've been stuck for so long and I kind of see that you're try to, I've written a small example, where you can move around a block, collide with the floor and the box and you can jump. Keep in mind it's an example and not production code, it should give you an example you can read and understand into its last detail.
And I don't want to hear the question at all how one would go about fixing the 'teleporting' problem when bumping into the box, that's for you to figure out. ;)

#include <SFML/Graphics.hpp>

int main()
{
    // Create window
    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML");
    // Limit framerate
    window.setFramerateLimit(60);

    // Keep track of the frametime
    sf::Clock frametime;

    // Big floor
    sf::RectangleShape floor(sf::Vector2f(800.f, 40.f));
    floor.setPosition(0.f, 560.f);
    floor.setFillColor(sf::Color(10, 180, 30));

    // Small box
    sf::RectangleShape box(sf::Vector2f(40.f, 40.f));
    box.setPosition(500.f, 480.f);
    box.setFillColor(sf::Color(10, 180, 30));

    // Moveable player
    sf::RectangleShape player(sf::Vector2f(40.f, 40.f));
    player.setOrigin(20.f, 20.f);
    player.setPosition(400.f, 40.f);
    player.setFillColor(sf::Color(10, 30, 180));

    // Player speed
    sf::Vector2f speed(0.f, 0.f);

    // Gravity value
    const float gravity = 980.f;

    // Check if we're touching any floor/box
    bool touching = false;

    while(window.isOpen())
    {
        // Get delta time for framerate depended movement
        float dt = frametime.restart().asSeconds();

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

        // Slow down horizontal speed
        if(speed.x > 6.f)
            speed.x -= 3.f;
        else if(speed.x < -6.f)
            speed.x += 3.f;
        else
            speed.x = 0.f;

        // Adjust vertical speed
        if(touching)
            speed.y = gravity;
        else if(speed.y < gravity)
            speed.y += 10.f;
        else if(speed.y > gravity)
            speed.y = gravity;

        // Horizontal movement
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left) && sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
            speed.x = 0;
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
            speed.x = -120.f;
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
            speed.x = 120.f;

        // Jumping
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space) && touching)
            speed.y = -300.f;

        // Move the player
        player.setPosition(player.getPosition().x + speed.x * dt, player.getPosition().y + speed.y * dt);

        // Check collision & position adjustment
        if(floor.getGlobalBounds().intersects(player.getGlobalBounds())) // Floor
        {
            player.setPosition(player.getPosition().x, floor.getPosition().y-player.getOrigin().y);
            touching = true;
        }
        else if(box.getGlobalBounds().intersects(player.getGlobalBounds())) // Box
        {
            player.setPosition(player.getPosition().x, box.getPosition().y-player.getOrigin().y);
            touching = true;
        }
        else // We're not colliding
            touching = false;

        // Render
        window.clear();
        window.draw(player);
        window.draw(box);
        window.draw(floor);
        window.display();
    }
}
 
« Last Edit: October 17, 2012, 10:33:59 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: AlexxanderX problems...
« Reply #18 on: December 18, 2012, 04:08:16 pm »
Thanks for long help eXpl0it3r. I learned some more about C++( and now, at school ned to restart with C++ :D ). I revised your code and I think you know, because is only a test for what I needed, your app have sam bugs. I will restart making my 2D app :D
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: 10829
    • View Profile
    • development blog
    • Email
Re: AlexxanderX problems...
« Reply #19 on: December 18, 2012, 04:22:41 pm »
I revised your code and I think you know, because is only a test for what I needed, your app have sam bugs.
I guess you mean 'some bugs' right?

Yeah I know that and I've also mentioned it. ;)
Keep in mind it's an example and not production code, it should give you an example you can read and understand into its last detail.
And I don't want to hear the question at all how one would go about fixing the 'teleporting' problem when bumping into the box, that's for you to figure out. ;)

Good luck!
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: AlexxanderX problems...
« Reply #20 on: December 18, 2012, 09:36:14 pm »
Thanks! Now I resolved about the collision I want to know about principles of SideScrolling( I'm interested in how can move the map( the map is moving?) and I need to create from the start the all map?)
Here you can find my blog and tutorials about SFML - http://alexanderx.net/ (died...) - http://web.archive.org/web/20160110002847/http://alexanderx.net/

G.

  • Hero Member
  • *****
  • Posts: 1592
    • View Profile
Re: AlexxanderX problems...
« Reply #21 on: December 18, 2012, 10:09:27 pm »
You don't move the map, you move the camera (the view).
Check the wiki for more info on sf::View.

AlexxanderX

  • Full Member
  • ***
  • Posts: 128
    • View Profile
    • AlexanderX
Re: AlexxanderX problems...
« Reply #22 on: December 20, 2012, 07:18:52 pm »
Thanks :D .

I created a menu and I want to know if I maked good and what I need to improve:
#include<SFML/Graphics.hpp>
using namespace sf;

int main()
{
    RenderWindow window(VideoMode(800, 600), "MENU");
    window.setMouseCursorVisible(false);

    //  Meniul
    //      Fontul
    Font font;
    if(!font.loadFromFile("resurse/cbb.ttf")) return -1;

    float x = -10;
    //      Textele
    Text txtstartgame;
    short btxtstartgame = 1;
    txtstartgame.setFont(font);
    txtstartgame.setCharacterSize(50);
    txtstartgame.setColor(Color::Red);
    txtstartgame.setOrigin(x, -20);
    txtstartgame.setString("Start Game");

    Text txtoptions;
    short btxtoptions = 1;
    txtoptions.setFont(font);
    txtoptions.setCharacterSize(50);
    txtoptions.setColor(Color::Red);
    txtoptions.setOrigin(x, -60);
    txtoptions.setString("Options");

    Text txtexit;
    short btxtexit = 1;
    txtexit.setFont(font);
    txtexit.setCharacterSize(50);
    txtexit.setColor(Color::Red);
    txtexit.setOrigin(x, -100);
    txtexit.setString("Exit");

    short vstartapp = 1;

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

            if ((event.type == Event::KeyPressed) && (event.key.code == Keyboard::Down))
            {
                if (btxtstartgame == 1)
                {
                    txtstartgame.setColor(Color::Red);
                    txtoptions.setColor(Color::Green);
                    btxtstartgame = 0;
                    btxtoptions = 1;
                }
                else if (btxtoptions == 1)
                {
                    txtexit.setColor(Color::Green);
                    txtoptions.setColor(Color::Red);
                    btxtexit = 1;
                    btxtoptions = 0;
                }
            }

            if ((event.type == Event::KeyPressed) && (event.key.code == Keyboard::Up))
            {
                if (btxtoptions == 1)
                {
                    txtstartgame.setColor(Color::Green);
                    txtoptions.setColor(Color::Red);
                    btxtstartgame = 1;
                    btxtoptions = 0;
                }
                else if (btxtexit == 1)
                {
                    txtexit.setColor(Color::Red);
                    txtoptions.setColor(Color::Green);
                    btxtexit = 0;
                    btxtoptions = 1;
                }
            }
        }

        if (vstartapp == 1)
        {
            txtstartgame.setColor(Color::Green);
            vstartapp = 0;
        }

        window.clear();
        window.draw(txtstartgame);
        window.draw(txtoptions);
        window.draw(txtexit);
        window.display();
    }


}
 
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: AlexxanderX problems...
« Reply #23 on: January 17, 2013, 06:47:47 pm »
After creating my physic system I mitted with this error: every frame my game verify for collision from a dynamic lists of floor( only the floor wich I can touch) and game work properly, but when I start my game( debug mode) the game make collision with a floor. I verified the proprietes of that floor( floor is an sf::Sprite vector) and my console give me those results:
Quote
maps.floor[1] top:4.51608e-039 left:2.51749e-009 width:1.85831e+028 height:1.19468e-038
...and here is the code:
// every frame create the map
void Maps::display(RenderWindow &window, int &level)
{
    int floorn = 0;
    for (int row=0; row<map_y; row++)
    {
        for (int col=0; col<map_x; col++)
        {
                if (mapn[row*map_x+col] != -1)
                {
                    //if (mapn[row*map_y+col] == 0) std::cout << "row(" << row << ")*map_y(" << map_y << ")+col(" << col << ") =>" << row*map_y+col << "\n";
                    floor[floorn] = _mapsprites[mapn[row*map_x+col]];
                    floor[floorn].setPosition(col * 64, row * 64);
                    std::cout << "maps.floor[1] top:" << floor[1].getGlobalBounds().top << " left:" << floor[1].getGlobalBounds().left << " width:" << floor[1].getGlobalBounds().width << " height:" << floor[1].getGlobalBounds().height << "\n"; // here I verify
                    window.draw(floor[floorn]);
                    floorn += 1;
                    floor.resize(floorn+1);
                }
        }
    }
}

//the function where all lead and go
Game::Game(sf::RenderWindow &window)
{
    int level = 1;
    Player player(level);
    Maps maps(level);
    speed.x = 0.f;
    speed.y = 0.f;
    p_s_moving = 0; // player_state_moving
    p_s_jumping = 0;
    p_coll_floor = 0;
    p_pos = player.player.getPosition();


    Clock clock, test;
    while (window.isOpen())
    {
        processEvents(window);
        dt = clock.restart().asSeconds();
        /* FOR FUTURE TEST */ //std::cout << "dt:" << dt << std::endl; /*T*/
        update(maps, player);
        window.clear();
        maps.display(window, level);
        window.draw(player.player);
        window.display();
    }
}

//function whee verify
void Game::update(Maps &maps, Player &player)
{
    p_moving(player);
    int i;
    sf::FloatRect f_player(p_pos.x, p_pos.y, 50, 50);
    int nfloor;
    float PlusY = 0, PlusX = 0;
    //std::cout << "p:" << player.getGlobalBounds().top << "\n";
    if (checkCollisionFloor(player, nfloor, maps))
    {
        if (player.player.getGlobalBounds().top + player.player.getGlobalBounds().height > maps.floor[nfloor].getGlobalBounds().top)
            PlusY = (player.player.getGlobalBounds().top + player.player.getGlobalBounds().height) > (maps.floor[nfloor].getGlobalBounds().top/2) ?
                -(player.player.getGlobalBounds().top + player.player.getGlobalBounds().height - maps.floor[nfloor].getGlobalBounds().top):
                0;
        if (((f_player.left + f_player.width) < maps.floor[nfloor].getGlobalBounds().left || (f_player.left + f_player.width) > maps.floor[nfloor].getGlobalBounds().left) && (player.player.getGlobalBounds().top + player.player.getGlobalBounds().height == maps.floor[nfloor].getGlobalBounds().top + maps.floor[nfloor].getGlobalBounds().height))
            PlusX = (f_player.left + f_player.width) < maps.floor[nfloor].getGlobalBounds().left ?
                (maps.floor[nfloor].getGlobalBounds().left + maps.floor[nfloor].getGlobalBounds().width) - f_player.left : (f_player.left + f_player.width) - maps.floor[nfloor].getGlobalBounds().left;
        speed.y = 0;
    }
    else
    {
        std::cout << "NOINTERSECTED" << i << "\n";
        if (((player.player.getGlobalBounds().top + player.player.getGlobalBounds().height)!=maps.floor[nfloor].getGlobalBounds().top) && !(((player.player.getGlobalBounds().left + player.player.getGlobalBounds().width) > (maps.floor[nfloor].getGlobalBounds().left + maps.floor[nfloor].getGlobalBounds().width)) || (player.player.getGlobalBounds().left < maps.floor[nfloor].getGlobalBounds().left)))
            speed.y = 160;
        //p_pos += sf::Vector2f(speed.x * dt, speed.y * dt);
        //return;
    }
    player.player.setPosition(player.player.getPosition().x + speed.x * dt + PlusX, player.player.getPosition().y + speed.y * dt + PlusY);
}

//checkCollision function
bool Game::checkCollisionFloor(Player &player, int &nfloor, Maps &maps)
{
    nfloor = 0;
    for (int i=0; i<(int)maps.dynamic_floor.size(); i++)
    {
        if (intersects(player.player, maps.floor[maps.dynamic_floor[i]]))
        {
            nfloor = i;
            return true;
        }
    }
    return false;
}
Here you can find my blog and tutorials about SFML - http://alexanderx.net/ (died...) - http://web.archive.org/web/20160110002847/http://alexanderx.net/

masskiller

  • Sr. Member
  • ****
  • Posts: 284
  • Pointers to Functions rock!
    • MSN Messenger - kyogre_jb@hotmail.com
    • View Profile
    • Email
Re: AlexxanderX problems...
« Reply #24 on: January 18, 2013, 05:06:21 am »
Could you describe your problem a bit better. I hardly understood what was wrong.

You could also consider using a tile-map class, there are two (I think) in the wiki.
Programmer, Artist, Composer and Storyline/Script Writer of "Origin of Magic". If all goes well this could turn into a commercial project!

Finally back into the programming world!

AlexxanderX

  • Full Member
  • ***
  • Posts: 128
    • View Profile
    • AlexanderX
Re: AlexxanderX problems...
« Reply #25 on: January 20, 2013, 02:47:55 pm »
I resolved my problem: I was first updating and after drawing :D
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: AlexxanderX problems...
« Reply #26 on: June 27, 2013, 01:52:51 pm »
Hello. I have another problem that I can't resolve it:

The timer("47") is setted to the 0,0 and no origin setted( so default 0,0) and how can see from image the y coord is wrong. Same bug and for the coords text(blue text) which is setted to 1,1 and again the y coord is wrong. Please some help, why the y coords is wrong? I tryed to change the font to Arial and after to Comic Sans MS but same bug.
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: AlexxanderX problems...
« Reply #27 on: July 02, 2013, 09:06:59 am »
Same problem... Please some help.

I created another project with this simple code - only font and text - https://gist.github.com/AlexxanderX/5907274 and I have the same bug: the text's y' coordonate is wrong. For this test I've used the Ubuntu-B font( to download: http://www.mediafire.com/download/4snkhqxly27noow/Ubuntu-B.ttf)  Here is the results of running the above code:
« Last Edit: July 02, 2013, 09:11:17 am 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/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: AlexxanderX problems...
« Reply #28 on: July 02, 2013, 09:14:20 am »
It looks right. What did you expect?
Laurent Gomila - SFML developer

Zeneus

  • Newbie
  • *
  • Posts: 30
    • View Profile
    • Email
Re: AlexxanderX problems...
« Reply #29 on: July 02, 2013, 11:47:47 am »
As Laurent said.. the code is correct and works..
The coordinates are (0,0) which is the correct position of the text..
If you dont CHANGE the y coordinate, it wont move vertically.. Thats why it seems wrong to you..
You have to change it ;)

 

anything