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.


Messages - Ganado

Pages: 1 2 [3]
31
That seems to work fine, thanks a lot for the help.

Before I looked at your latest reply, I was trying to figure it out myself and came up with this, which seems to do the same thing, but your code makes more sense.  :D
            if (freefall == true)
            {
                for (int j = 0; j < i; j++)
                {
                    if (brickImage.getPosition().y >= (WINDOW_HEIGHT - WINDOW_HEIGHT/10.0))
                    {
                        freefall = false;
                        break;
                    }
                    brickImage.move(0, 1);
                }
                i++;
            }

Anyway, it fixed the problem with jumping. The acceleration of the block seems to be the same as original post, so no problem with that either. Thanks again.

32
Thanks for the quick reply, I tested the code you gave me. That does work to get the correct end result, but the problem is that the brick noticeably "jumps" back up to the "ground" position if the previous position was below it.

The problem with my code seems to be with this part
        else if (freefall == true)
        {
            brickImage.move(0, i);
            i++;
        }
The brickImage.move(0, i); allows it to go past the ground line without first testing it... currently I'm trying to do something with a for loop to immediately test each pixel movement, but so far it's not working correctly and still jumps back up.

33
Using SFML 2.1, Code::Blocks 12.11

So far SFML seems really nice. However, I have an issue in this little practice code, it's probably something obvious that I'm not seeing... anyway, here's the code, I'll explain it after:

int main()
{
    const int WINDOW_WIDTH = 960;
    const int WINDOW_HEIGHT = 540;

    // Removed code, Renders "Window", with WIDTH/HEIGHT
    Window.setVerticalSyncEnabled(true); // 60 fps

    sf::Texture brickTexture;
    sf::Sprite brickImage;
    // Removed code, just loads the brick.png, a 60x40 image
    brickImage.setTexture(brickTexture);
    brickImage.setOrigin(brickImage.getLocalBounds().width/2.0, brickImage.getLocalBounds().height/2.0); // Sets sprite origin at (30, 20)
    brickImage.setPosition(WINDOW_WIDTH/2, WINDOW_HEIGHT/2);


        // Important code
    bool freefall = true;
    int i = 1;
    while (Window.isOpen())
    {
        sf::Event event;
        while(Window.pollEvent(event))
        {
            switch(event.type)
            {
            case sf::Event::MouseButtonPressed:
                if (event.mouseButton.button == sf::Mouse::Left)
                {
                    brickImage.setPosition(event.mouseButton.x, event.mouseButton.y);
                    freefall = true;
                    i = 1;
                }
                break;
            }
        }
        if (brickImage.getPosition().y >= (WINDOW_HEIGHT - WINDOW_HEIGHT/10.0))
            freefall = false;   // The position at which it stops falling is not constant...
        else if (freefall == true)
        {
            brickImage.move(0, i);
            i++;
        }

        Window.clear(); // Removed for the screenshot
        Window.draw(brickImage);

        Window.display();
    }

}
If you want compilable code, see: http://pastebin.com/cjhYfAne
Note that you'll need an image named brick.png to go with it (dimensions don't matter)

Problem:
Basically, this code is making a rectangle brick accelerate as it falls. I want the brick to stop falling at the same height on the screen each time (in this case, 9/10-ths of the screen height with respect to the center point of the brick), but the problem is that the end position is slightly different depending on how high you drop the brick from.
Here's an example, with Window.clear(); off:


See how the end position at the bottom of the screen of each brick dropped is different? Why does it not stop falling immediately after it reaches the cut-off point? I ask for some help please.

Also, if I'm placing certain parts of code in the wrong part of the loop in general, please tell me.

Unrelated questions:
In my while (Window.isOpen()) loop, if I have brickImage.move(0, 1), does that make it move 60 pixels per second? (60 if my Vsync/FPS is set to 60)

Also had a question of how things like the standard rand() function work without including <cstdlib> or how using time as a seed in srand can work without <ctime>.

Thank you for reading!

34
Hope this is in the right spot, anyway:

In the 2.0/2.1 tutorial (possibly other versions too, didn't check), the link to the sf::IntRect class is broken.
I'm referring to these two webpages:
http://www.sfml-dev.org/tutorials/2.0/graphics-sprite.php
http://www.sfml-dev.org/tutorials/2.1/graphics-sprite.php

"The sf::IntRect class is a simple utility type that represents a rectangle. Its constructor takes the coordinates of the left-top corner, and the size of the rectangle."

The link, which is the bold part of my quotation, is broken with a 404 error.
The link tries to go to:
http://www.sfml-dev.org/documentation/2.0/classsf_1_1IntRect.php and
http://www.sfml-dev.org/documentation/2.1/classsf_1_1IntRect.php, respectively.

Pages: 1 2 [3]
anything