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 - Tyler Durden

Pages: [1]
1
General / Re: Little problem
« on: July 21, 2021, 12:27:31 pm »
The problem was exaclty what you said.

I easily fixed it by writing t = 0.007 instead of t += 0.007

Thank you for your help!


2
General / Little problem
« on: July 20, 2021, 08:51:52 pm »
Hello everyone!

I just started to learn SFML a few days ago, and I figured I wanted to do a little exercise to test how I'm doing: having a ball that, starting in the middle of a window, bounced around the window, much like the dvd logo on the television.

And everything worked fine except one small issue: The ball started accelarating a lot almost immediately. I really don't understand how.
Could somebody help me, either telling me where the mistake is or giving me any suggestions of how to make the code differently so that this problem disappears?

Thank you very much!

(Also, I know I copied the whole code, which is something one is advised not to do. Yet, as I'm really not sure where the problem is, I decided to copy it all, because it isn't that big)

[/code
#include<SFML/Graphics.hpp>

float x_position(float t, float v) {
        float x = v * t;
        return x;
}

float y_position(float t, float v) {
        float y = v * t;
        return y;
}


int main() {
        float t = 0;
        int vx = 50;
        int vy = 50;
        //window
        sf::RenderWindow window(sf::VideoMode(1000, 600), "SFML");
        //ball
        sf::Texture ball_texture;
        ball_texture.loadFromFile("Basketball.png"); //340x340 pixels
        sf::Sprite ball;
        ball.setTexture(ball_texture);
        ball.setOrigin(170, 170);
        ball.scale(0.1f, 0.1f);
        ball.setPosition(500, 300);
       
        while (window.isOpen()) {
                ball.move(x_position(t,vx), y_position(t,vy));
                t += 0.000007;
                //boundaries
                if (ball.getPosition().x > 983 || ball.getPosition().x < 17) {
                        vx *= -1;
                       
                }
                if (ball.getPosition().y > 583 || ball.getPosition().y < 17) {
                        vy *= -1;      
                }
                sf::Event event;
                while (window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                                window.close();
                }
                window.clear(sf::Color(150,150,150));
                window.draw(ball);
                window.display();
        }
}]


 

(I know this really isn't the most elegant code, but it is what I came up with)

Pages: [1]