camera is shaking, or maybe the world is?

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
specialtactics
Posts: 17
Joined: Tue Dec 09, 2003 9:03 am
Location: Germany

camera is shaking, or maybe the world is?

Post by specialtactics »

Hi,

i am writing a game where the camera (instance of ICameraSceneNode*) is looking down on the game board. I attached the camera movement to the mouse but instead of the camera's view angle being constant (the camera's direction is!) it shakes and jitters. The effect is more noticable when the camera moves faster. I tried to pin down the problem, but it even occurs in the following code:

Code: Select all

	m_lookAt = vector3df(0.0, sin(0.008*g_gameTime)*10.0, 0.0);
	m_cam->setTarget(m_lookAt);
	m_cam->setPosition(m_lookAt + vector3df(0.0, 0.0, -25.0));
	m_cam->setUpVector(vector3df(0.0, 1.0, 0.0));
Also here it's more noticable when the camera is moving faster. The camera's view angle onto the board is not constant, it's changing pitch. The effect looks if the position of the camera is a bit ahead of the target. Which it isn't. It doesn't look like any of my variables are the cause, and i don't think it's a precision problem (of matrix calculations or whatever).

I changed the HelloWorld example (actually just changed 5 lines or so) to demonstrate the problem. the main.cpp can be found here: main.cpp and a windows exe here: HelloWorld.exe

Ok, i found a way to get rid of the effect, however i still don't know the source of the problem. In the changed helloworld demo i replaced

Code: Select all

	while(device->run())
	{
		float y = sin(device->getTimer()->getTime()*0.008)*10.0;
		cam->setPosition(vector3df(0.0, y, -40.0));
		cam->setTarget(vector3df(0.0, y, 0.0));
with

Code: Select all

	float y = 0.0;
	float prevY = 0.0;
	while(device->run())
	{
		prevY = y;
		y = sin(device->getTimer()->getTime()*0.008)*10.0;
		cam->setPosition(vector3df(0.0, y, -40.0));
		cam->setTarget(vector3df(0.0, prevY, 0.0));
So, i'm setting the camera's target not to the current target, but to the target of the previous frame (or i set the position ahead...). So it looks like it's a bug in the engine - something is updated/set at the wrong time and so the camera's view is messed up by 1 frame. I'll look closer at the irrlicht source now.

btw: to make the effect more visible (i suppose at 200fps or so it's hard to see) you can insert a Sleep(100)/usleep(100) in the main-loop. Which is what i did in the changed helloworld example.
specialtactics
Posts: 17
Joined: Tue Dec 09, 2003 9:03 am
Location: Germany

Post by specialtactics »

OK, source found, too:

insert

Code: Select all

updateAbsolutePosition();
in CCameraSceneNode.cpp at line 218.

The problem is that the call (a few lines lower) View.buildCameraLookAtMatrixLH(pos, Target, up); used last frame's position. Because a call to CameraSceneNode->setPosition() updates the RelativePosition but doesn't update the AbsolutePosition immedietly.
BlackNinjaGames
Posts: 31
Joined: Mon Sep 05, 2005 4:47 pm
Contact:

Post by BlackNinjaGames »

I was thinking it was your use of sine.

Why is sine in there? Am I missing something?
specialtactics
Posts: 17
Joined: Tue Dec 09, 2003 9:03 am
Location: Germany

Post by specialtactics »

That's just in my testing code to make the effect as visible as possible and not to use any of my game variables.. What the sine does is make the camera move along the y axis back and forth. The constant numbers set the speed and the amplitude of the bouncing.
Guest

Post by Guest »

This "updateabsoluteposition()" has been said a lot around here for jerky/non smooth camera updates - but I thought you were supposed to put it in your own code (in the update loop for example) and not need to put it in the source. Are you saying that it works better by putting it in the source - that there is a definate need for it there (and no performance loss?)

if so I willl put it in where you suggested in my next custom irrlicht as I do have jerky cameras at times even with high framerates (even locked frames)
specialtactics
Posts: 17
Joined: Tue Dec 09, 2003 9:03 am
Location: Germany

Post by specialtactics »

Good point, Guest. You're right, that it should behave exactly the same, no matter if you put it in your code after you setPosition(), setTarget() and setUpVector() or if you put it in the engine where I suggested.

But since this is neither documented anywhere (or maybe I just didn't find the doc), nor intuitive, I think it should be done in the engine.

The cost is basically 1 matrix multiplaction which is done once per frame (just for the active camera), so it's probably not noticable.
r3i
Posts: 147
Joined: Wed Jun 29, 2005 10:15 am
Location: Sorrento
Contact:

Post by r3i »

Hi!
I've had the same problem and I've solved placing the camera a bit high than the ground.
The effect is that the collision manager makes the camera fall on the ground but after camera doesn't have any problems! :roll:

Try this trick...
"We work in the dark, we do what we can, we give what we have, Our doubt is our passion and our passion is our task. The rest: is art of Madness" (H.James)
Post Reply