FPS Camera setting position

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
Dog-E
Posts: 28
Joined: Sun Mar 30, 2008 5:52 am

FPS Camera setting position

Post by Dog-E »

Hi everyone.

I was looking at the code of CCameraFPSSceneNode. I want to handle movement on my own so I use my own position component.

Here is parts of my code:

This one is from the update() function of my CameraFPS.

Code: Select all

if( Flag[FLAG_INIRRSCENE] )
	{
		//Update the direction of Position component from Irrlicht
		vector3df camTarget( irrSceneNode_->getTarget().X, 
								 irrSceneNode_->getTarget().Y, 
								 irrSceneNode_->getTarget().Z );
		
		cmpPos_->setTarget( camTarget );
		
		//Update the position of Irrlicht camera from position component
		VEC3D camPosition = cmpPos_->getPosition();
		irrSceneNode_->setPosition( irr::core::vector3df( camPosition.x, camPosition.y, camPosition.z ) );
	}
From receiveEvent(...) of CameraFPS:

Code: Select all


//Setting camera speed
if( key == KEY_UP )
			{
				cmpPos_->setSpeed( 0.5f );
			}
			else if( key == KEY_DOWN )
			{
				cmpPos_->setSpeed( -0.5f );
			}

Setting the target from Position component.

Code: Select all

void Position::setTarget( VEC3D target )
{	
	direction_ = NORMALIZED(target - position_);
}
And update() of Position component:

Code: Select all

...
	position_ += direction_*speed_*timeFactor;
...
I had absolutely no problem with the negative speed, going backwards. The system takes the target info from ICameraSceneNode and uses it to change the direction of the Position component, if down is pressed, sets the speed of position comp. to some negative value and position updates itself every frame to reflect the change in the position, CameraFPS takes that position and sets it using ICameraSceneNode. Works great until I use a positive speed.
Basically if I use a really small value, like 0.5f, it works fine. However once I go beyond that, I think what happens is since I'm setting the position to somewhere beyond the target, the node doesn't allow it. At first I was thinking it was a division by zero while normalizing the direction vector but I shouldn't get a div/0 if I set the speed to something like 100.0f. Sadly, it still doesn't work.

Any ideas?

Thanks a lot.
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

I'm not entirely clear on what the symptom of your problem is, or in which order the calculations are being performed, but sight unseen I'd venture that it could be solved by judicious use of updateAbsolutePosition(), or by offsetting the VEC3D target by the amount of movement before using it.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
Dog-E
Posts: 28
Joined: Sun Mar 30, 2008 5:52 am

Post by Dog-E »

Awesome.
Well the symptom was just junk values for the new position. Here is the fixed code. Someone might have a use for it (mind the VEC3D, it's the engine's own)

Code: Select all

//New position of the camera from EGE_Position
		VEC3D newPosition( cmpPos_->getPosition() );
		
		//Current target from ICameraSceneNode
		VEC3D camTarget( irrSceneNode_->getTarget() );
		
		//Set the target of EGE_Position to the new target with the offset
		cmpPos_->setTarget( camTarget +  newPosition - VEC3D( irrSceneNode_->getAbsolutePosition() ) );
		
		//Update the new position of ICameraSceneNode
		irrSceneNode_->setPosition( vector3df( newPosition.x, newPosition.y, newPosition.z ) );
Post Reply