Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Implementing Interpolation


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 Danicco   Members   -  Reputation: 449

Like
0Likes
Like

Posted 14 September 2013 - 10:53 AM

I'm implementing interpolation for my orientation class, but I'm not getting good results.

 

I have an Orientation class which contains 2 vectors for positions, the previous and the next one.

My code for checking for updates and getting the interpolation is:

 

void Game::Update()
{
    _currentTime = GetRealTimeInMicroSeconds();
    if(_currentTime - _previousTime > _timeToUpdate) //also has a max frameskip here, omitted
    {
        _elapsedTime += _timeToUpdate;
        _previousTime += _timeToUpdate;
    }
}
 
float Time::GetInterpolation()
{
    _interpolation = (GetRealTimeInMicroSeconds() - _previousTime) / _timeToUpdate;
    return _interpolation; //might need to normalize it
}

 

And in my engine's update function, I'm just doing this:

 

myModel->Position->x += 0.002f;
 
for(unsigned int i = 0; i < _allObjects.size(); i++)
{
    _allObjects[i]->_Update();
    //This swaps the addresses for the current/previous positions for all objects, and call their regular update function
}

 

Debugging, I can check the values are being calculated perfectly:

 

Matrix4x4 Orientation::GetTransformationMatrix(float interpolation)
{
    if(interpolationStatus == true) //if true, A is next
    {
        positionX = positionB.x - ((positionA.x - positionB.x) * interpolation);
    }
    else //else, B is next
    {
        positionX = positionA.x - ((positionB.x - positionA.x) * interpolation);
    }
 
    //use these values, get the matrix done, return it
}

 

So at the first update tick, I can see the values being calculated:

 

positionX = 0 - ((0.002 - 0) * 0.207);
positionX = 0.000414;

 

But on screen, the effect is noticeably lagging, almost as if it didn't had interpolation at all.

Without interpolation, is bad as well, with this interpolation code it's slightly better, but it's still far worse than having this:

 

void Game::Render()
{
    myModel->Position.x += 0.00001f; //way lower value since this is being called more often
}

 

The speed is nearly identical from these two values, but the movement is much smoother in the Render() one.

 

Am I doing something wrong with this interpolation? Is there anything I can do to get it better, smoother movements?



Sponsor:

#2 Kaptein   Prime Members   -  Reputation: 2178

Like
0Likes
Like

Posted 14 September 2013 - 12:53 PM

Well, I\m not completely sure what you\re doing here

The interpolation aspect is for variable framerate, which you should be using for a renderer ONLY

People generally still have 60fps today, but personally having a 144hz monitor, I really really really really really dont like people teaching to use fixed timestep in renderer

That means, I will want to calculate a factor i can just multiply everything in the renderer with as a sort of timestep

 

So an overview:

RENDERER:

void render(double timeElapsed, double dtfactor)

{

    // quickly move towards new_something (0.15 is fast

    something = interpolate(something, new_something, 0.15 * dtfactor);

    // animate FPS weapon or camera bobbing (in this case, modulating camera.y value)

    camera.y = player.y + formula(timeElapsed);

}

 

time elapsed is as double precision timer in seconds since game started

makes things really simple, use it with complex cos() sin() expressions

 

dtfactor is just a multiplier that you can interpolate with when you don't need linear speed

one example where this is absolutely necessary is when you interpolate on the pitch/yaw rotation of the player

i realize interpolating the mouse rotation is complicated, but it's definitely worth it smile.png

 

PHYSICS:

simple timestep, simpler == better

try to avoid integrator unless you really have to :)

 

So, in closing:

Avoid timesteps in renderer, disconnect renderer from physics, use timesteps in physics to avoid having to use additional integration


Edited by Kaptein, 14 September 2013 - 12:55 PM.


#3 Danicco   Members   -  Reputation: 449

Like
0Likes
Like

Posted 14 September 2013 - 03:21 PM

My Render function isn't set at a fixed rate, it's that I get the interpolation inside it instead of sending it as a param:

void Game::Render()
{
    float interpolation = _time.GetInterpolation(); //This is so I can get the interpolation right before the draw begins, and to be able to multithread this function (although I'm not using multithreads for now, they're being called sequentially)
    _sceneManager.Draw(interpolation); //This will loop through all objects and draw using this value
}

When I set the position being modified inside the Render function, it means it's being called/updated as fast as possible, and then the movement gets really really smooth.

I thought by interpolating the draw and leaving the position update in the fixed rate Update() function, I'd get the same result, but I'm not, and with just a simple cube + grid on screen it's clearly noticeable, unless I use really low values.

 

Edit of the Edit: I thought I had figured out why this is happening, but no, still stuck... or not, I'm not sure.

 

I wrote what is (supposedly) happening in the code when I change the position in the Render() function, that is being called as fast as possible and running at something like 6000 fps:

 

FRAME   CHANGE   POSITION   POSITION DISPLAYED
0       0.1      1.1        1.1
1       0.1      1.2        1.2
2       0.1      1.3        1.3
3       0.1      1.4        1.4

 

The intervals are exactly even, and this might give the feeling of smooth movement.

With interpolation, the values might not be so even, and there's the issue of when the Update() catches up.

 

FRAME   CHANGE   PREV.POSITION    CURR.POSITION   POSITION DISPLAYED
0*      0.1      1.0              1.1             (1.1 - 1.0) * INTERPOLATION = 1.02 (i = 0.2)
1       0.0      1.0              1.1             1.05 (i = 0.5)
2*      0.1      1.1              1.2             (1.2 - 1.1) * INTERPOLATION = 1.12 (i = 0.2)
3       0.0      1.1              1.2             1.15 (i = 0.5)
4*      0.1      1.2              1.3             (1.3 - 1.3) * INTERPOLATION = 1.22 (i = 0.2)
5       0.0      1.3              1.3             1.25 (i = 0.5)

 

So the first frame has a 1.02 position, the second a 1.05 happening at exactly halfway to the next update, then an Update() happens, and updates the position to 1.1, so the next position goes to 1.12 (with interpolation).

I think this gap between 1.05 ~ 1.12 is what is giving the choppy movement, but I'm not sure. It's just that it is noticeable and it's bugging me...

 

I'm still trying out many things to figure out and improve it, any help/tips are appreciated!


Edited by Danicco, 14 September 2013 - 03:49 PM.


#4 Kaptein   Prime Members   -  Reputation: 2178

Like
0Likes
Like

Posted 14 September 2013 - 03:56 PM

Edit: I think getting interpolation right in this case is going to be very hard

hmm

I think you'll need to know the speed and trajectory of the objects to make this one perfect

It may not be worth it then..

 

You have FPS fps, averaged over time:

FPS = FPS * 0.9 + (1.0 / (t1 - t0)) * 0.1;

 

and over 1/fps seconds you must move exactly speed / fps deltas

so, obj.xyz += objspeed.xyz / fps;

 

This boils down to:

//float steps = 1.0 / (t1 - t0);  // average me

float invSteps = t1 - t0;

obj.xyz += speed.xyz * invSteps;

 

Where speed is (obj.xyz - obj.lastxyz), so:

float invSteps = t1 - t0;

obj.xyz += (obj.xyz - obj.lastxyz) * invSteps;


Edited by Kaptein, 14 September 2013 - 04:17 PM.


#5 Danicco   Members   -  Reputation: 449

Like
0Likes
Like

Posted 14 September 2013 - 05:13 PM

I figured it out, it was something really really stupid...

 

I don't know why or where I read this, but I was using two positions to store an object's orientation, and every update I was not assigning the previousPosition to the same value as currentPosition.

 

So, I was getting:

 

Frame            1    2    3    4    5
previousPosition 0.0  0.1  0.1  0.2  0.2
currentPosition  0.1  0.1  0.2  0.2  0.3

 

Argh, so the choppy frames were the ones that previousPositioncurrentPosition... I don't even know why I had something like that in the code, but now the movements are nicely smooth!



#6 L. Spiro   Crossbones+   -  Reputation: 14197

Like
0Likes
Like

Posted 14 September 2013 - 07:54 PM

I have an Orientation class which contains 2 vectors for positions, the previous and the next one.

This is already wrong as far as design goes. Your class is doing more than it should be doing.
An Orientation class has only a single orientation.
If you want to introduce interpolation, make an InterpolatedOrientation class which then has 2 instances of the Orientation class, one for the last and one for the current, and then its own values for the interpolated position.


And in my engine's update function, I'm just doing this:
 
 

myModel->Position->x += 0.002f;

I see a hard-coded number. This could already be your problem.


But on screen, the effect is noticeably lagging, almost as if it didn't had interpolation at all.
Without interpolation, is bad as well, with this interpolation code it's slightly better, but it's still far worse than having this:

I am skipping a lot of quotes because I can’t follow your logic because your method of interpolation is already extremely non-standard and the whole thing needs to be gutted and rewritten.

Going with my above design in which you have a class that has 2 orientations, each responsible for only one set of positions (and scales and rotations), it would as follows:

Each time you perform a logical update, you copy the current position into the old position and update the new current position:
void InterpolatedOrientation::Update() {
    m_oLastOrientation = m_oCurOrientation;
}
void InterpolatedOrientation::SetPos( Vector _vPos ) {
    m_oCurOrientation.Pos() = _vPos;
}
Vector InterpolatedOrientation::GetPos( float _fInterpAmount ) {
    return m_oLastOrientation.Pos() + (m_oCurOrientation.Pos() - m_oLastOrientation.Pos()) * _fInterpAmount;
}
They are called in this order. Update() is called at the start of each logical update, SetPos() is called on each logical update after Update(), and GetPos() is called on each render with a value from 0.0f to 1.0f which indicates how far between logical updates the render takes place.


L. Spiro
It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS