SFML 2.1 movement help

Pages: 12
Its just like any other component based entity system. An object is made up of smaller components instead of a very large class hierarchy.

//velocity += accel * deltaTime.restart().asSeconds(); As I mentioned
//get user input
//change the x or y velocity accordingly
so..It would really look something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
        //WASD Movement
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::A))
        {
            velocity.x -= accel * deltaTime.restart().asSeconds();
        }
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::D))
        {
            velocity.x += accel * deltaTime.restart().asSeconds();
        }
        else
        {
            velocity.x *= decel * deltaTime.restart().asSeconds();
        }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::W))
        {
            velocity.y -= accel * deltaTime.restart().asSeconds();
        }
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::S))
        {
            velocity.y += accel * deltaTime.restart().asSeconds();
        }
        else
        {
            velocity.y *= decel * deltaTime.restart().asSeconds();
        }


Also, why are you multiplying by decel? Should you be adding the decel and it would be a negative number?

Sorry actually you wouldn't restart the clock each time so it would actually be something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
         float deltaTime = clock.restart().asSeconds();
       //WASD Movement
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::A))
        {
            velocity.x -= accel * deltaTime;
        }
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::D))
        {
            velocity.x += accel * deltaTime;
        }
        else
        {
            velocity.x *= decel * deltaTime;
        }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::W))
        {
            velocity.y -= accel * deltaTime;
        }
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::S))
        {
            velocity.y += accel * deltaTime;
        }
        else
        {
            velocity.y *= decel * deltaTime;
        }
Last edited on
Also, why are you multiplying by decel? Should you be adding the decel and it would be a negative number?


decel is a scalar, not an accumulator. This makes it easier to use because you don't have to bounds check (ie: stop at 0).

1
2
3
4
5
6
7
8
9
// deceleration as a scalar:
velocity.x *= decel;


// deceleration as an accumulator
if(velocity.x > 0)
    velocity.x = min(velocity.x - decel, 0);
else
    velocity.x = max(velocity.x + decel, 0);

Hmm, well my sprite moves like a vehicle with that code, which is not what I want, so i'll stick with what I have. However I still need to figure out the collision problem with the sprite not stopping when hitting the edge of the screen.
Ok so I got this but it keeps moving back to 0,0 because I don't know what to put in the last 2 if statements to make it stop there:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
sf::Vector2u currWSize = window.getSize();

        if(position.x < 0)
        {
            position.x = 0;
        }
        if(position.y < 0)
        {
            position.y = 0;
        }
        if(position.x >= currWSize.x)
        {
            position.x = 0;
        }
        if(position.y >= currWSize.y)
        {
            position.y = 0;
        }
When you say position.x = foo; you are forcing the object to be at that position.

So if you want to the object to be on the right side of the screen.......
I tried this, I was sure this would work but it didn't, it doesnt stop or even do anything:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
if(position.x <= 0)
        {
            position.x = 0;
        }
        if(position.y <= 0)
        {
            position.y = 0;
        }
        if(position.x >= currWSize.x)
        {
            position.x = currWSize.x;
        }
        if(position.y >= currWSize.y)
        {
            position.y = currWSize.y;
        }
It certainly does stop. But you aren't visualizing the problem correctly.

See this graphic for what's happening:

http://imgur.com/n1LkgmT
ahhhh, i see, so the origin is set to 0, 0, so I should make an origin point for the bottom right and then it should stop right? I set up a bounding box could I just use that? I'm not sure if i did the bounding box right though

//Collision
sf::FloatRect boundingBox = sprite.getGlobalBounds();
sf::Vector2f point = sprite.getPosition();
if(boundingBox.contains(point))
{

}
Last edited on
ahhhh, i see, so the origin is set to 0, 0, so I should make an origin point for the bottom right and then it should stop right?
Changing from top right to bottom left will not affect. You will need to subtract the width of the object. currWSize.x - sprite.getGlobalBounds().width
So like this?:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
if(position.x <= 0)
        {
            position.x = 0;
        }
        if(position.y <= 0)
        {
            position.y = 0;
        }
        if(position.x >= currWSize.x)
        {
            position.x = currWSize.x - sprite.getGlobalBounds().width;
        }
        if(position.y >= currWSize.y)
        {
            position.y = currWSize.y - sprite.getGlobalBounds().height;
        }


this sort of works but the sprite goes almost all the way off screen.
Last edited on
Nevermind I did it :D I just stopped and did it step by step of what the square was doing and it works :D
Topic archived. No new replies allowed.
Pages: 12