[ad_1]
I am making an attempt to implement mounted timestep with interpolation in my recreation engine, nevertheless the rendering just isn’t easy. I’ve already learn this publish.
That is the perform that interpolates two values:
inline float Interpolate(float present,float earlier){
return present*ALPHA+earlier*(1.0f-ALPHA);
}
That is the primary loop:
whereas(!glfwWindowShouldClose(WINDOW)){
glfwPollEvents();
CURRENT_FRAME=glfwGetTime();
DELTA_TIME=CURRENT_FRAME-LAST_FRAME;
LAST_FRAME=CURRENT_FRAME;
if(DELTA_TIME>0.25f)
DELTA_TIME=0.25f;
m_Accumulator+=DELTA_TIME;
whereas(m_Accumulator>=m_FixedTimeStep){
OnUpdate(m_FixedTimeStep);
m_Accumulator-=m_FixedTimeStep;
}
ALPHA=m_Accumulator/m_FixedTimeStep;
//Rendering
}
That is the physics replace:
Entity *entity;
b2Body *physique;
for(int i=0;i<rigidbodies.dimension();i++){
entity=GetEntity(rigidbodies[i].m_UID);
physique=rigidbodies[i].m_RuntimeBody;
const auto &place=body->GetPosition();
entity->m_PreviousX=entity->m_X;
entity->m_PreviousY=entity->m_Y;
entity->m_X=place.x*(1.0f/m_ScalingFactor);
entity->m_Y=place.y*(1.0f/m_ScalingFactor);
}
I get the interpolated worth immediately when rendering, instance:
RENDERER->DrawTexture(Interpolate(entity->m_X,entity->m_PreviousX),Interpolate(entity->m_Y,entity->m_PreviousY);
I am utilizing a hard and fast timestep of 1/60, nevertheless additionally at 60 fps it is not easy. When the fps improve, the rendering solely will get worse.
EDIT:
I forgot to vary part of the code, now it is actually easy.
[ad_2]