I tried to write a code in my program that would remember the first position and target of the camera, and if user clicks the button called 'Reset' it resets (duh) the camera to that starting position.
The problem is that after clicking the button for a split second I see the correct result (the change is applied) and then all the objects disappear (it is as if the camera was becoming invalid, off, or what?). Does any of you know what I do wrong and could suggest how to rewrite this part of code?
class MyEventReceiver : public IEventReceiver
{
public:
MyEventReceiver()
{
}
virtual bool OnEvent(SEvent event)
{
if (event.EventType == EET_KEY_INPUT_EVENT)
{
// s key saves camera state
if (event.KeyInput.Key == KEY_KEY_S && event.KeyInput.PressedDown)
{
scene::ICameraSceneNode* theCamera = Device->getSceneManager()->getActiveCamera();
From = theCamera->getPosition();
Target = theCamera->getTarget();
Proj = theCamera->getProjectionMatrix();
return true;
}
// r key restores camera state
else if (event.KeyInput.Key == KEY_KEY_R && event.KeyInput.PressedDown)
{
scene::ICameraSceneNode* theCamera = Device->getSceneManager()->getActiveCamera();
theCamera->setPosition(From);
theCamera->setTarget(Target);
theCamera->setProjectionMatrix(Proj);
return true;
}
}
return false;
}
private:
core::vector3df From; // put the camera where the old camera was
core::vector3df Target; // point the new camera at the same location as the old one
core::matrix4 Proj; // set the projection matrix (fov and friends)
};
Well, my code was pretty similar, but I copy-pasted yours just in case.
And unfortunately the same error persists : when I click the "Reset scene" button, there is a split second blink, when the scene is reset (I can see that the camera and objects come to their initial positions), and then every object disappears.
I do not have the slightest idea why this happens.
I checked all the settings (visible, drop etc), to ensure that the objects are not lost from sight for some other reason, but I haven't found any other cause.
As I said, the above code worked just fine for me. Maybe the camera is being accessed from somewhere else? If you look in the debugger, are the look from and look at values correct?
It may help to reduce your code down to a simple test case. If you are able to reduce it down to something small, then post it. If it starts working all of the sudden, then figure out what you changed...
Did you manage to solve your problem? I have quite a similar problem, everything is ok for first time the scene is drawn, but then everything just disappears. With FPS-camera everything looks right and my camera is never moved, so I'm quite out of solutions in this one.