[Solved] Problem with 3rd Person Camera

Problems building or running the engine, queries about how to use features etc.
Eldarion
Kobold
Posts: 38
Joined: Sat Dec 23, 2006 7:06 pm

[Solved] Problem with 3rd Person Camera

Post by Eldarion »

Hi

i just created a third person camera for my project.

this is my source:


the problem is, if i move the mouse (to rotate the player) the camerarotation is wrong and it warps.

Code: Select all

void Player::Update(CapInput* Input, float Time)
{
	Ogre::Vector3 Direction = MainNode->getOrientation()*Ogre::Vector3::NEGATIVE_UNIT_Z;
	Direction.normalise();
	Ogre::Vector3 Movement;
	Movement = Vector3(0,0,0);
	mAnimationState->setEnabled(false);

	if(Input->Up)
	{
		Movement = Direction;
	}
	if(Input->Down)
	{
		Movement = -Direction;
	}
	if(Input->Right)
	{
		Movement.z = Direction.x * -1;
		Movement.x = Direction.z;
	}
	if(Input->Left)
	{
		Movement.x = Direction.z * -1;
		Movement.z = Direction.x;
	}


	if(Movement.x == 0 && Movement.z == 0)
	{
		mAnimationState = PlayerEntity->getAnimationState( "Idle1" );
	}
	else
	{
		Movement = Movement * Time * mWalkSpeed;
		MainNode->translate(Movement);
		mAnimationState = PlayerEntity->getAnimationState( "Walk" );
	}

	MainNode->yaw(Input->mRotX);

	mAnimationState->setLoop( true );
	mAnimationState->setEnabled( true );
	mAnimationState->addTime(Time);

}

and this happens if i move the mouse:
http://img110.imageshack.us/my.php?imag ... leroe8.jpg

anybody an idea what i have to change, that the camera rotates right with the body? the body rotation is imo right.

Eldarion
User avatar
Kencho
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 4011
Joined: Fri Sep 19, 2003 6:28 pm
Location: Burgos, Spain
x 2
Contact:

Post by Kencho »

How much does mRotX value? I'm afraid you're yawing a lot of radians at a time, which has two problems: Hyper-high mouse sensivity ;) and inaccuracy issues in the rotation, screwing up the camera transformation matrix and thus giving such "skewes". I would instead store the Euler angles and do a clean rotation from zero every frame instead of modifying the previous orientation.
Image
Eldarion
Kobold
Posts: 38
Joined: Sat Dec 23, 2006 7:06 pm

Post by Eldarion »

mRotX is about 4-6 if i move the mouse.
can you give me a little example what you mean?

do you mean that i should set the orientation to zero every frame and save the euler angles every frame to a variable in the class and then move with the hole rotation?
Last edited by Eldarion on Sun Jul 01, 2007 3:28 pm, edited 1 time in total.
alytle
Gnoblar
Posts: 21
Joined: Thu Feb 02, 2006 1:10 am

Post by alytle »

Every time I've had a problem like that, it's related to mistakenly moving my camera object, instead of the node the camera is attached to. Not sure if that's a possibility here or not.
Eldarion
Kobold
Posts: 38
Joined: Sat Dec 23, 2006 7:06 pm

Post by Eldarion »

you mean i have to move the camera and not the node?
alytle
Gnoblar
Posts: 21
Joined: Thu Feb 02, 2006 1:10 am

Post by alytle »

No, you should move the node - but if you move the camera instead of the node, you will get weird rotations because the camera point is not what you expect.
User avatar
Kencho
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 4011
Joined: Fri Sep 19, 2003 6:28 pm
Location: Burgos, Spain
x 2
Contact:

Post by Kencho »

Eldarion wrote:mRotX is about 4-6 if i move the mouse.
can you give me a little example what you mean?
If mRotX is an Ogre::Degree, then it will be somehow slow most of the times. However, a whole circunference is 2*PI radians, so 6 is almost a whole circunference in a single frame if mRotX is an Ogre::Radian variable ;)
Eldarion wrote:do you mean that i should set the orientation to zero every frame and save the euler angles every frame to a variable in the class and then move with the hole rotation?
Exactly. Each rotation adds a small error in the matrix due to the variables' precission used in the calculations. When you perform various transforms based on the previous ones, you're accumulating errors. Instead, doing it theis other way, you only have a very very small error everytimes.
Image
Eldarion
Kobold
Posts: 38
Joined: Sat Dec 23, 2006 7:06 pm

Post by Eldarion »

its an radian. but i now cast it into a degree. but it even doesn't work. this is my source now:

Code: Select all

void Player::Update(CapInput* Input, float Time)
{
	Ogre::Vector3 Direction = MainNode->getOrientation()*Ogre::Vector3::NEGATIVE_UNIT_Z;
	Direction.normalise();
	Ogre::Vector3 Movement;
	Movement = Vector3(0,0,0);
	mAnimationState->setEnabled(false);
	MainNode->setOrientation(0, 0, 0, 1);
	mRotXStart += Input->mRotX;

	if(Input->Up)
	{
		Movement = Direction;
	}
	if(Input->Down)
	{
		Movement = -Direction;
	}
	if(Input->Right)
	{
		Movement.z = Direction.x * -1;
		Movement.x = Direction.z;
	}
	if(Input->Left)
	{
		Movement.x = Direction.z * -1;
		Movement.z = Direction.x;
	}


	if(Movement.x == 0 && Movement.z == 0)
	{
		mAnimationState = PlayerEntity->getAnimationState( "Idle1" );
	}
	else
	{
		Movement = Movement * Time * mWalkSpeed;
		MainNode->translate(Movement);
		mAnimationState = PlayerEntity->getAnimationState( "Walk" );
	}

	MainNode->yaw(Degree(mRotXStart));

	mAnimationState->setLoop( true );
	mAnimationState->setEnabled( true );
	mAnimationState->addTime(Time);
}
can you tell me what i should change? i think the problem is more, that the camera rotates on its position, and not around the player.
User avatar
Kencho
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 4011
Joined: Fri Sep 19, 2003 6:28 pm
Location: Burgos, Spain
x 2
Contact:

Post by Kencho »

It should work now. But think that maybe the rotation is very small now as it's in degrees (360 for a circle) instead of radians (6.28 for a circle).

About the problem, I thought you were implementing a RE4-like camera and you were getting the image skewed... Then the problem is how everything's structured. The pivoting point for the camera is probably at the same position as the camera, but instead it should be at the same position of the character, and the camera shifted relative to that node.
Image
Eldarion
Kobold
Posts: 38
Joined: Sat Dec 23, 2006 7:06 pm

Post by Eldarion »

So, the problem is halfsolved. The rendered image is not warping anymore if i move the mouse.

But i not get what i want. The problem is, the Camera should rotate AROUND the character, but the camera rotates on its position around itself.

How can i realise that?
User avatar
Kencho
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 4011
Joined: Fri Sep 19, 2003 6:28 pm
Location: Burgos, Spain
x 2
Contact:

Post by Kencho »

You can find the basic idea in this wiki article: http://www.ogre3d.org/wiki/index.php/Si ... son_camera
The only problem is that it's not finished yet (remains to add the source code, but it's pretty straightforward)
Image
Horizon
Greenskin
Posts: 122
Joined: Thu Jun 02, 2005 3:24 pm

Post by Horizon »

Eldarion wrote:So, the problem is halfsolved. The rendered image is not warping anymore if i move the mouse.

But i not get what i want. The problem is, the Camera should rotate AROUND the character, but the camera rotates on its position around itself.

How can i realise that?
Exactly as Kencho said above. You probably have a single node, which functions as the camera position, attached to the node with the player object. Instead, you should create an additional node in the same location as the player object and attach the camera node you were using to this node. Now instead of rotating the camera-node, rotate the additional node we inserted. This should give you your desired behaviour.
Eldarion
Kobold
Posts: 38
Joined: Sat Dec 23, 2006 7:06 pm

Post by Eldarion »

and now theres the problem, i also did whats written in kenchos link.

Code: Select all

void Player::Load(Ogre::Entity* ent, Ogre::SceneNode* node, Ogre::Camera* mCam)
{
	MainNode = node;
	PlayerNode = MainNode->createChildSceneNode("PlayerNode");
	CameraNode = MainNode->createChildSceneNode("CameraNode");
	CameraNode->attachObject (mCam);
	PlayerEntity = ent;
	PlayerNode->attachObject(PlayerEntity);
	// Set idle animation
	mAnimationState = PlayerEntity->getAnimationState( "Idle2" );
	mAnimationState->setLoop( true );
	mAnimationState->setEnabled( true );
}
i get a entity and a scene node about the params because i don't want to give the player object a pointer to the ogre::scenemanager.

what could i do?
[/code]
User avatar
Kencho
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 4011
Joined: Fri Sep 19, 2003 6:28 pm
Location: Burgos, Spain
x 2
Contact:

Post by Kencho »

But still, CameraNode and MainNode are at the same position, and you're not shifting the camera either. Walk through all the steps very carefully (don't forget to reset the camera position at first if you're using the once created by the example framework!), as this method actually works (a lot of people, including me, have been using it for many time).
Image
Eldarion
Kobold
Posts: 38
Joined: Sat Dec 23, 2006 7:06 pm

Post by Eldarion »

Code: Select all

void Player::Load(Ogre::Entity* ent, Ogre::SceneNode* node, Ogre::Camera* mCam)
{
	MainNode = node;
	PlayerNode = MainNode->createChildSceneNode("PlayerNode");
	CameraNode = MainNode->createChildSceneNode("CameraNode");
	CameraNode->attachObject (mCam);
	CameraNode->setPosition(0, 150, 150);
	PlayerEntity = ent;
	PlayerNode->attachObject(PlayerEntity);
	PlayerNode->setScale(0.4,0.4,0.4);
	// Set idle animation
	mAnimationState = PlayerEntity->getAnimationState( "Idle2" );
	mAnimationState->setLoop( true );
	mAnimationState->setEnabled( true );
}
so, i set the camera to another point at the creating of her. i use the source of the basic tutorial 0.
sry, i think the problem is real simple but i don't get it yet.

[/code]
User avatar
Kencho
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 4011
Joined: Fri Sep 19, 2003 6:28 pm
Location: Burgos, Spain
x 2
Contact:

Post by Kencho »

Now, rotating (yawing) MainNode should work as you expect, unless you have other wrong parameters for your camera (for instance, non-zero position of the camera). My question is, is the camera now pivoting around the player or around itself?
Image
Eldarion
Kobold
Posts: 38
Joined: Sat Dec 23, 2006 7:06 pm

Post by Eldarion »

it is rotating around itself. what parameters i have to check?
Horizon
Greenskin
Posts: 122
Joined: Thu Jun 02, 2005 3:24 pm

Post by Horizon »

Eldarion wrote:it is rotating around itself. what parameters i have to check?
Correct. If you rotate the CameraNode, the camera attached to it will rotate around that node, and as that node is at the camera's position that's what it will rotate around. If you rotate the PlayerNode, however, the camera will rotate around the character, because the CameraNode is attached to and offset from the PlayerNode, the character will also rotate though.

So, if you want to be able to rotate the camera around the character, without rotating the character, you have to introduce an additional node, let's call it CameraRotationNode, atttached to the PlayerNode and at the same position as the PlayerNode. Then attach your CameraNode to the CameraRotationNode. If you then rotate the CameraRotationNode, your character will not rotate, but your camera will rotate around it.

Somewhat like this (quickly made in paint):
Image
Eldarion
Kobold
Posts: 38
Joined: Sat Dec 23, 2006 7:06 pm

Post by Eldarion »

i want to rotate both at the same time. i'm not rotating the camera node i rotate the main node. but the camera is rotating around itself :(
Horizon
Greenskin
Posts: 122
Joined: Thu Jun 02, 2005 3:24 pm

Post by Horizon »

Eldarion wrote:i want to rotate both at the same time. i'm not rotating the camera node i rotate the main node. but the camera is rotating around itself :(
So this is what you're doing and you're rotating the PlayerNode?
Image
Eldarion
Kobold
Posts: 38
Joined: Sat Dec 23, 2006 7:06 pm

Post by Eldarion »

Yes and i solved the problem. I had coded a few new classes and i also running and old function to rotate the camera. So there was double rotation.
User avatar
Kencho
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 4011
Joined: Fri Sep 19, 2003 6:28 pm
Location: Burgos, Spain
x 2
Contact:

Post by Kencho »

Solved then? If so, it's time to tag this thread :)
Image
Eldarion
Kobold
Posts: 38
Joined: Sat Dec 23, 2006 7:06 pm

Post by Eldarion »

yes its solved. i was real wierd of this problem, because the same code has already worked in another dem. then i changed somithing of the architecture, i think i'll have to be more carefully next time.
User avatar
jacmoe
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 20570
Joined: Thu Jan 22, 2004 10:13 am
Location: Denmark
x 179
Contact:

Post by jacmoe »

Care to share your solution with us? :)
/* Less noise. More signal. */
Ogitor Scenebuilder - powered by Ogre, presented by Qt, fueled by Passion.
OgreAddons - the Ogre code suppository.
Eldarion
Kobold
Posts: 38
Joined: Sat Dec 23, 2006 7:06 pm

Post by Eldarion »

I can give my code to you, or do you mean i should put it into the wiki?
Post Reply