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

Author Topic: BOx2d with SFML  (Read 3984 times)

0 Members and 1 Guest are viewing this topic.

davidweschler

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
BOx2d with SFML
« on: June 15, 2024, 10:39:29 pm »
Hi everyone!
i am building the game 'Geometry Dash' using Box2d (in c++ and SFML of course), and my the movement of the player is very choppy.
I tried everything- erasing the Box2d part in the code, playing around with the framerate, using m_window.setVerticalSyncEnabled(true); , different ways to use the setp() func and different ways to handle time, but nothing seems to help!
In addition, i noticed that the Behavior of the player is different when i run the program on different computers, but its never smooth.
Does anyone have any idea what might be causing it? i'll take any advice at this point.
thanks!
David
« Last Edit: June 15, 2024, 11:03:42 pm by davidweschler »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11029
    • View Profile
    • development blog
    • Email
Re: BOx2d with SFML
« Reply #1 on: June 15, 2024, 11:32:07 pm »
You may need to be a bit more specific than "very choppy".
For example, do you have a screen recording of it?

What's your movement code?

Do you want to use Box2D?
Geometry Dash doesn't exactly use realistic physics, which is what Box2D generally tries to emulate.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

davidweschler

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: BOx2d with SFML
« Reply #2 on: June 16, 2024, 08:44:13 am »
I'm attaching a video that shows part of the problem, i need to use Box2D because its part of a project in University.

Also, here's the movement code:

void Player::move(sf::Time time)
{
        b2Vec2 boxPos = getBox()->GetPosition();

        auto dt = time.asSeconds();

        getBox()->SetTransform(boxPos + b2Vec2(VELOCITY * dt, 0.0f), getBox()->GetAngle());
        setPosition(sf::Vector2f(boxPos.x * 30, boxPos.y * 30));
}

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11029
    • View Profile
    • development blog
    • Email
Re: BOx2d with SFML
« Reply #3 on: June 17, 2024, 09:39:44 am »
I don't necessarily see what you mean with the video.

Based on your movement code I can however see that you won't be getting buttery smooth movements.
The movement is directly tied to the previous frame time (dt), which is "good enough" for some games, but won't provide the smoothest of movements.
Our eyes & brain are really sensitive to even the slightest mismatch in distance over time. So you get the different variables, from inconsistent frame rate, to inconsistently calculated distance and the mix of the two, on which frame, what position is rendered.
What you instead want is to have a fix timestep for your physics calculation, so the distance is calculated accurately and consistent over time. Meaning you have to separate the rendering and physics.

There a great and famous post that explains it in details: https://gafferongames.com/post/fix_your_timestep/

Note also the section about interpolation, that's what gives the best smoothness.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/