Jump to content
  • Advertisement
Sign in to follow this  
BlueSpud

Camera Interpolation

This topic is 2121 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello,

I've been working with my own game engine for awhile now and I've been trying to get the input and controls to flow much more like a AAA FPS game, or at least a decent indie one. I've posted several topics in the past on this issue about making a smooth camera, and at the time of posting them, I had been satisfied with the results. Now however, I feel that it is not quite smooth enough, and I've switched the whole engine to SDL so I have control over the loop. (previously I was using GLUT). After changing everything to SDL, the mouse is smooth as butter, but the camera movement (walking) is still stutters and looks bad. I've implemented the last of Dwitter's game loop, the one with interpolation, and here is the relevant code:

int main (int argc,  char** argv)
{
    arg1 = argc;
    arg2 = argv;
    engineInit();
    //the loop has to stay here
    //kill all extra threads so they don't cause problems after we quit
    //gameloop
    SDL_Event event;
    bool running = true;
    
    const int TPS = 20;
    const int SKIP_TICKS = 1000 / TPS;
    const int MAX_FRAMESKIP = 5;
    int loops;
    long lastSec = 0;
    long nextGameTick = SDL_GetTicks();
    
    while (running)
    {
        while (SDL_PollEvent(&event)) {
            //do crap with events
            switch (event.type)
            {
                int x,y,button;
                case SDL_QUIT:
                SDL_GL_DeleteContext(glContext);
                SDL_DestroyWindow(window);
                SDL_Quit();
                cout << "The window has been closed.\n";
                running = false;
                    break;
                case SDL_MOUSEMOTION:
                    SDL_GetMouseState(&x, &y);
                    passiveMouse(x,y);
                    break;
                case SDL_MOUSEBUTTONDOWN :
                   button =  SDL_GetMouseState(&x, &y);
                    mouseFunc(button,1,x,y);
                    break;
                 case SDL_KEYDOWN:
                    keyboardDownFunc(event.key.keysym.sym);
                    break;
                case SDL_KEYUP:
                    keyboardUpFunc(event.key.keysym.sym);
                    break;
                default:
            
                    break;
            }
            
        }
        loops = 0;
        while (SDL_GetTicks()> nextGameTick && loops < MAX_FRAMESKIP) {
            nextGameTick+=SKIP_TICKS;
            loops++;
            TickHandler.tps++;
            TickHandler.onTick();
            
            
            int tickTime = int(SDL_GetTicks()/1000);
            if (tickTime > lastSec+1)
            {
                TickHandler.tps = 0;
                lastSec = tickTime;
            }
        }
        TickHandler.interpolation = double(SDL_GetTicks() + SKIP_TICKS - nextGameTick )
        / double( SKIP_TICKS );
        TickHandler.onRender();
        render();
    }
    Console.consoleActivated = false;
    SDL_GL_DeleteContext(glContext);
    SDL_DestroyWindow(window);
    SDL_Quit();
    
    return 0;
}

TickHandler.onRender() calls a few interpolated functions and here is the one that controls movement of the camera:

void renderTick(float intp)
{
    if (cameraPlayer == true)
    {
        Physics.pos3 = -camy;
        Physics.collisions();
        Input.applyGravity();
        if (Input.walking == true)
            Input.moveCameraFirstPerson(1*intp);
        else
        {
            roll = 0;
            Input.change = false;
        }
    }

}

And here is the move camera first person:

void inputs::moveCameraFirstPerson(float speed)
{
    speed = speed*walkspeed;
    float radx = ((yaw+addedAngle)*MPI/180);
    camx -= (sinf(radx)/10)*speed;
    camz += (cosf(radx)/10)*speed;
    
    Physics.pos1 = -camx;
    Physics.pos2 = -camz;

    if (Physics.collided == true)
    {float radx = ((yaw+Input.addedAngle)*3.1415926535/180);
        camx += (sinf(radx)/20)*speed;
        camz -= (cosf(radx)/20)*speed;
        Physics.collided = false;
        
    }
    Client.x = camx;
    Client.z = camz;
    Client.y = camy;
    
    Projectile.x = camx;
    Projectile.z = camz;
}

I'd love if I could get this all sorted out, any help or reference would be appreciated.

Thanks.

 

 

Share this post


Link to post
Share on other sites
Advertisement
 Why you
void inputs::moveCameraFirstPerson(float speed)
{
speed = speed*walkspeed;
you pass almost const float 

at least change speed = speed*walkspeed; to float tmp; tmp = speed*walkspeed; and why you use walkspeed?]

 

 

 

 

 

remember that every mouse movement the game forces to draw new eye look at point so mouse movement as is is not dependant on time ( you don't use timefactor there)

to smooth camera movement you should use better timer (i use for win32 QueryPerformanceTimer), that done the trick for me

You should also separate drawing routine from mouse movements (that drawing is made independent)

you can also

 

make character to turn from one eyepoint to another in fixed time vaule lets say 22 ms and then blend two scenes or more

 

it wont be much help but you can go here http://www.sulaco.co.za/opengl_project_Basic_3D_engine.htm and downlaod source that will show you how to move mouse (with average of two frame times) almost the same as your code (OpenGLApp.dpr - winmain func) if you use queryperformancetimer there instead of average two frames from gettickcount) i could tell you more precise but i can't now. 

Edited by ___

Share this post


Link to post
Share on other sites

 

 Why you
void inputs::moveCameraFirstPerson(float speed)
{
speed = speed*walkspeed;
you pass almost const float 

at least change speed = speed*walkspeed; to float tmp; tmp = speed*walkspeed; and why you use walkspeed?]

 

 

 

 

 

remember that every mouse movement the game forces to draw new eye look at point so mouse movement as is is not dependant on time ( you don't use timefactor there)

to smooth camera movement you should use better timer (i use for win32 QueryPerformanceTimer), that done the trick for me

You should also separate drawing routine from mouse movements (that drawing is made independent)

you can also

 

make character to turn from one eyepoint to another in fixed time vaule lets say 22 ms and then blend two scenes or more

 

it wont be much help but you can go here http://www.sulaco.co.za/opengl_project_Basic_3D_engine.htm and downlaod source that will show you how to move mouse (with average of two frame times) almost the same as your code (OpenGLApp.dpr - winmain func) if you use queryperformancetimer there instead of average two frames from gettickcount) i could tell you more precise but i can't now. 

 

walkspeed is another variable for things like sprinting and crouching. The mouse movement is very smooth, it is the camera 'walking' that is jittery. The update function isn't being called faster than a millisecond, so I really don't need the precision of a performance counter.

Share this post


Link to post
Share on other sites

ok my bad about this mosue movement it was about camera lol, anyway you should do better timing believe me.

 

another thing is that if you divide a float by in example 180 you will get integer result

 

ex. 3.1415926535/180 should be 3.14159265350f / 180.0f same for void inputs::moveCameraFirstPerson(float speed) you cannot pass integer to float unless you cast it to a 

Input.moveCameraFirstPerson(1.0f*float(intp));

or if you have something more complex then you use (float)var 

 

 

the average timer ticks can also smooth your movement but i prefer queryperformancetimer

 

however i do not see here time independant variable

 

since you move character, by telling what direction it is going, multiplying that direction by speed value and than you have to multiply it by time (ofc in seconds for SI)

Share this post


Link to post
Share on other sites

ok my bad about this mosue movement it was about camera lol, anyway you should do better timing believe me.

 

another thing is that if you divide a float by in example 180 you will get integer result

 

ex. 3.1415926535/180 should be 3.14159265350f / 180.0f same for void inputs::moveCameraFirstPerson(float speed) you cannot pass integer to float unless you cast it to a 

Input.moveCameraFirstPerson(1.0f*float(intp));

or if you have something more complex then you use (float)var 

 

 

the average timer ticks can also smooth your movement but i prefer queryperformancetimer

 

however i do not see here time independant variable

 

since you move character, by telling what direction it is going, multiplying that direction by speed value and than you have to multiply it by time (ofc in seconds for SI)

I didn't know that whole float thing, and I changed the code to do that. Unfortunately, query performance counter seems to fluctuate really badly. It could be a problem in my code, but I don't really think so. Speed when passed to the function is time, the independent variable.

Share this post


Link to post
Share on other sites


another thing is that if you divide a float by in example 180 you will get integer result

There is nothing wrong with dividing a float with an integer, it will result as a float. But dividing an integer with an integer will not give you a float, 1 / 3 = 0.

Share this post


Link to post
Share on other sites

i wonder if you use it like this:

 

CStopWatch * timin;

 

timin = new CStopWatch();

 

timin->startTimer();

 

 

get time









     double l;
timin->stopTimer();

l = timin->getElapsedTime();
FRAME_TIME = float(l);
timin->startTimer();

cpp







//---------------------------------------------------------------------------


#pragma hdrstop

#include "Timer.h"

//---------------------------------------------------------------------------

#pragma package(smart_init)



 
 double CStopWatch::LIToSecs( LARGE_INTEGER & L) {
     return ((double)L.QuadPart /(double)frequency.QuadPart) ;
 }
 
 CStopWatch::CStopWatch(){
     timer.start.QuadPart=0;
     timer.stop.QuadPart=0; 
     QueryPerformanceFrequency( &frequency ) ;
 }
 
 void CStopWatch::startTimer( ) {
     QueryPerformanceCounter(&timer.start) ;
 }
 
 void CStopWatch::stopTimer( ) {
     QueryPerformanceCounter(&timer.stop) ;
 }
 
 double CStopWatch::getElapsedTime() {
     LARGE_INTEGER time;
     time.QuadPart = timer.stop.QuadPart - timer.start.QuadPart;
     return LIToSecs( time) ;
 }

h







//---------------------------------------------------------------------------

#ifndef TimerH
#define TimerH
#include "windows.h"
//---------------------------------------------------------------------------

typedef struct {
     LARGE_INTEGER start;
     LARGE_INTEGER stop;
 } stopWatch;
 
 class CStopWatch {
 
 private:
     stopWatch timer;
     LARGE_INTEGER frequency;
     double LIToSecs( LARGE_INTEGER & L) ;
 public:
     CStopWatch() ;
     void startTimer( ) ;
     void stopTimer( ) ;
     double getElapsedTime() ;
 };
#endif
//         LARGE_INTEGER liFrequency;
//		::QueryPerformanceFrequency(&liFrequency);
//		return ((double)GetElapsed() / (double)liFrequency.QuadPart);

 

There is nothing wrong with dividing a float with an integer, it will result as a float. But dividing an integer with an integer will not give you a float, 1 / 3 = 0.

 

It depends on the compiler, mine won't pass (you multiply int by float you get int)

 

 

however reply to this topic if you still have a problem, we will search for another solution

 

 

maybe you should post a video with that camera problem, i found jittering in my cam but i used better timer

Edited by ___

Share this post


Link to post
Share on other sites


It depends on the compiler, mine won't pass (you multiply int by float you get int)

And what compiler are you using, if I may ask?

Share this post


Link to post
Share on other sites

i wonder if you use it like this:
 
CStopWatch * timin;
 
timin = new CStopWatch();
 
timin->startTimer();
 
 
get time








     double l;
timin->stopTimer();

l = timin->getElapsedTime();
FRAME_TIME = float(l);
timin->startTimer();
cpp





//---------------------------------------------------------------------------


#pragma hdrstop

#include "Timer.h"

//---------------------------------------------------------------------------

#pragma package(smart_init)



 
 double CStopWatch::LIToSecs( LARGE_INTEGER & L) {
     return ((double)L.QuadPart /(double)frequency.QuadPart) ;
 }
 
 CStopWatch::CStopWatch(){
     timer.start.QuadPart=0;
     timer.stop.QuadPart=0; 
     QueryPerformanceFrequency( &frequency ) ;
 }
 
 void CStopWatch::startTimer( ) {
     QueryPerformanceCounter(&timer.start) ;
 }
 
 void CStopWatch::stopTimer( ) {
     QueryPerformanceCounter(&timer.stop) ;
 }
 
 double CStopWatch::getElapsedTime() {
     LARGE_INTEGER time;
     time.QuadPart = timer.stop.QuadPart - timer.start.QuadPart;
     return LIToSecs( time) ;
 }

h





//---------------------------------------------------------------------------

#ifndef TimerH
#define TimerH
#include "windows.h"
//---------------------------------------------------------------------------

typedef struct {
     LARGE_INTEGER start;
     LARGE_INTEGER stop;
 } stopWatch;
 
 class CStopWatch {
 
 private:
     stopWatch timer;
     LARGE_INTEGER frequency;
     double LIToSecs( LARGE_INTEGER & L) ;
 public:
     CStopWatch() ;
     void startTimer( ) ;
     void stopTimer( ) ;
     double getElapsedTime() ;
 };
#endif
//         LARGE_INTEGER liFrequency;
//		::QueryPerformanceFrequency(&liFrequency);
//		return ((double)GetElapsed() / (double)liFrequency.QuadPart);

 
There is nothing wrong with dividing a float with an integer, it will result as a float. But dividing an integer with an integer will not give you a float, 1 / 3 = 0.

 
It depends on the compiler, mine won't pass (you multiply int by float you get int)
 
 
however reply to this topic if you still have a problem, we will search for another solution
 
 
maybe you should post a video with that camera problem, i found jittering in my cam but i used better timer

Thanks for all the code, but I do want to make this cross platform, that why I chose opengl. I don't actually have a windows machine, do I've been using wine to emulate a windows compiler, but I'm not really coding on it. I don't have the time right now but later I will post a video of the camera trying different solutions.

And something for you: I didn't read through all your code but I noticed you used quey preformence frequency which I read can fluctuate on the CPU.

Share this post


Link to post
Share on other sites

i use RAD STUDIO 2007,

 

if its only for win and linux you can use queryperf if not i don't know.

 

i can only say to you that you are doing all wrong :P

like:

 

while (SDL_GetTicks()> nextGameTick && loops < MAX_FRAMESKIP) { - that means you skipframes but do not count time properly

TickHandler.interpolation = double(SDL_GetTicks() + SKIP_TICKS - nextGameTick ) - whats the purpose of this anyway.

 

if you get time between frames you should count every frame even skipped frame. Really its hard for me to explain, i bet your camera is jumping from one point to another

because when you skip in example 5 frames and the time will be 5 seconds then you will jump to unknown destination.

 

 

 

, or it just doesn't process frames that are processed faster than 1 milisecond)

 

 

anyway i can't help you unless you show more code and a video of your camera movement

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!