Getting camera position at two different times

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
malloc
Posts: 13
Joined: Sat Feb 04, 2012 7:25 pm

Getting camera position at two different times

Post by malloc »

I'm trying to determine which way the camera is moving by grabbing its position twice at two different times and comparing them. I tried to do this like so:

Code: Select all

while(g_device->run() && g_driver)
        if (g_device->isWindowActive())
        {
                g_driver->beginScene(true, true, SColor(0,200,200,200));
                
                g_smgr->drawAll();
                
                g_cam_current_pos = g_cam->getPosition();
                
                g_env->drawAll(); 
 
                g_driver->endScene();
                
                g_cam_last_pos = g_cam_current_pos;
                
        }
However, g_cam_last_pos and g_cam_current_pos always seem to be equal.

Any advice?
Mel
Competition winner
Posts: 2293
Joined: Wed May 07, 2008 11:40 am
Location: Granada, Spain

Re: Getting camera position at two different times

Post by Mel »

get the camera position BEFORE the draw all method, and AFTER. That way you sould have the camera position before and after the animation takes place.
"There is nothing truly useless, it always serves as a bad example". Arthur A. Schmitt
malloc
Posts: 13
Joined: Sat Feb 04, 2012 7:25 pm

Re: Getting camera position at two different times

Post by malloc »

Thanks Mel
malloc
Posts: 13
Joined: Sat Feb 04, 2012 7:25 pm

Re: Getting camera position at two different times

Post by malloc »

Sadly, this doesn't seem to be working. Is there a more reliable/elegant solution?

Edit: I'm going to try creating a thread which reports updates the camera position every 0.25 seconds. Seems like the easiest solution.
Edit2: Fantastic! This works. It's probably crude, but it works so I'm gonna go with it for now.
Mel
Competition winner
Posts: 2293
Joined: Wed May 07, 2008 11:40 am
Location: Granada, Spain

Re: Getting camera position at two different times

Post by Mel »

Other way is to read the camera between 2 frames. 0.25 seconds (250 ms) is a big amount of time compared to the time a frame may take to be rendered (at 60 fps, the render takes 16.7 msec to complete, and if the frame rate goes up, this time goes down). For instance, you can read the camera, store its position into a variable, and in the next frame, store this variable into another variable, read the camera again and compare. It is how its done in many places.
"There is nothing truly useless, it always serves as a bad example". Arthur A. Schmitt
CuteAlien
Admin
Posts: 10012
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Getting camera position at two different times

Post by CuteAlien »

Note that positions are not updated before rendering. You can enforce that with updateAbsolutePosition.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
malloc
Posts: 13
Joined: Sat Feb 04, 2012 7:25 pm

Re: Getting camera position at two different times

Post by malloc »

Thanks everyone. I'll investigate what you suggested Mel, and look into updateAbsolutePosition.

Edit: The frames works perfectly for my needs. Code for anyone to follow in my footsteps:

Code: Select all

int lastFPS = -1;
        int fps;
        // g_cam_current_pos & g_cam_last_pos are vector3df
        while(g_device->run() && g_driver)
        if (g_device->isWindowActive())
        {
                g_driver->beginScene(true, true, SColor(0,200,200,200));
                
                g_smgr->drawAll();
                
                g_cam_current_pos = g_cam->getPosition();
                
                
                g_env->drawAll();
                
                fps = g_driver->getFPS();
                
                if(lastFPS != fps) {
                        g_cam_last_pos = g_cam_current_pos;
                        lastFPS = fps;
                }
 
                g_driver->endScene();
        }
malloc
Posts: 13
Joined: Sat Feb 04, 2012 7:25 pm

Re: Getting camera position at two different times

Post by malloc »

Sorry to bump this again, but I suppose it's important for anyone in the future with this problem. After some time, I noticed problems with my last solution. Turns out the code above doesn't work frame by frame as I once thought. So, I made a new solution. I created a variable and had it check itself like so:

Code: Select all

 
int check = 0;
...
while(g_device->run() && g_driver)
        {
                if (g_device->isWindowActive())
                {
                        check = (check == 10) ? 0 : check + 1;
Basically, this variable increments itself by 1 every frame until 10, at which point it resets itself to zero. Further along in my loop I put:

Code: Select all

                        if(World.running && check == 5) {
                                g_cam->updateAbsolutePosition();
                                g_cam_current_pos = g_cam->getAbsolutePosition();
                                printf("Updating current pos...\n");
                        }
                        
                        if(World.running && check == 10) {
                                g_cam_last_pos = g_cam_current_pos;
                                printf("Updating last pos...\n");
                        }
 
This means that it stores the camera position when check is equal to 5, then it waits 5 frames, stores the last position, waits another 5 frames and stores the current position again. Then in my Event Receiver, I did the following:

Code: Select all

                                case KEY_KEY_W:
                                        if(check == 6)
                                        printf("Old (%.2f, %.2f, %.2f)\nNew (%.2f, %.2f, %.2f)\n",
                                                g_cam_last_pos.X, g_cam_last_pos.Y, g_cam_last_pos.Z,
                                                g_cam_current_pos.X, g_cam_current_pos.Y, g_cam_current_pos.Z);
This successfully got me the right positions 5 frames apart. Hopefully I can now close the chapter on this giant time waster of a problem and get on with my game.
Post Reply