• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Danicco

Implementing Interpolation

5 posts in this topic

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?

0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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!

0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0