1
General / Re: Float precision and pixel accuracy
« on: March 05, 2017, 11:46:12 pm »
In my engine entitiy's have these two member variables:
all of the physics calculations happen using the position variable, like so:
A couple notes: 1, this assumes that the velocity is in units of [pixels moved per update], otherwise we would need a time variable. 2, this is called symplectic euler integration and it really only works deterministically if acceleration only changes instantaneously.
Then, right before drawing, I round the position of the entity like this:
Then the drawing uses m_coord instead of m_position.
All the physics happens smoothly and the sprites never stray from the pixels.
sf::Vector2f m_position;
sf::Vector2i m_coord;
sf::Vector2i m_coord;
all of the physics calculations happen using the position variable, like so:
m_velocity += m_acceleration;
m_position += m_velocity;
m_position += m_velocity;
A couple notes: 1, this assumes that the velocity is in units of [pixels moved per update], otherwise we would need a time variable. 2, this is called symplectic euler integration and it really only works deterministically if acceleration only changes instantaneously.
Then, right before drawing, I round the position of the entity like this:
m_coord = sf::Vector2i(std::round(m_position.x), std::round(m_position.y));
Then the drawing uses m_coord instead of m_position.
All the physics happens smoothly and the sprites never stray from the pixels.