Page 1 of 1

character rotation

Posted: Sat Dec 24, 2005 9:43 pm
by green_algae
Hi
i'm trying to build an RPG game. Till now i've managed the RPG camera view. when i click on the screen my character runs to that location. i've kept my character as the parent of camera so as to follow my character

my problem is how to rotate my character to run towards its destination without affecting my camera. i've understood that by what angle my character will rotate its negative my camera will rotate so as not to disturb the camera. but i've no idea how to calculate this angle n how to implement to rotate my character no matter from where camera is looking from

thanx

Posted: Sun Dec 25, 2005 1:12 pm
by AshmenGlue
I'm not sure I understand your problem.
i've understood that by what angle my character will rotate its negative my camera will rotate so as not to disturb the camera.
This is the line I don't understand. Rephrase?

In general though, I wouldn't set your character as the parent of the camera. I take it that your camera is movable/rotatable by the player. In that case, just set your camera's Target to the player?

Posted: Sun Dec 25, 2005 1:15 pm
by xterminhate
I should not have linked camera and player nbode for your application.

Just translate the camera position and the camera target in order to follow player node...

Posted: Mon Dec 26, 2005 2:39 am
by green_algae
my problem is with the character. when i click somewhere on the screen my character must rotate and face that direction and then translate to that position. but as my character rotates so does my camera.
only my character should rotate. for that by what angle my character is rotated so should my camera rotate in the opposite direction. thus making the user believe that only the character has rotated. i've no idea how to calculate this angle n how to implement it.
can anyone plz help?

thanx

Posted: Mon Dec 26, 2005 7:00 pm
by panoramix
like xterminhate said, just don't make your camera a child of the character node... however, this approach also has its own issues (flickering camera).

Posted: Fri Dec 30, 2005 2:29 pm
by mepcotterell
dont need to make your camera a child of the camera node... do something like this in your render loop

Code: Select all

			int cOffset = 0; // change this to rotate the camera around the character
			int cZoom = 200; // radius.. decrease to zoom in, increase to zoom out

			core::vector3df r = playernode->getRotation();
			core::vector3df p = playernode->getPosition();

			camera->setTarget( core::vector3df( playernode->getPosition() ) );
			camera->setPosition( 
				core::vector3df(
					( -1 * cZoom * cos( -1 * ( r.Y + cOffset ) * 3.14159265/180 ) ) + p.X,
					p.Y+50,
					( -1 * cZoom * sin( -1 * ( r.Y + cOffset ) * 3.14159265/180 ) ) + p.Z
				)
			); 
basically it uses trigonometry to position the camera a certain distance behind the playernode at all times while also making the playernode the target

to handle keyboard input use bool keys method and use something like this for forward:

Code: Select all

		float speed  = 4.0;
		core::vector3df r = playernode->getRotation();
		core::vector3df v = playernode->getPosition();
		v.X = v.X + ( cos(r.Y * 3.14159265/180)*speed );
		v.Z = v.Z - ( sin(r.Y * 3.14159265/180)*speed );
		playernode->setPosition(v);
reverse:

Code: Select all

		float speed  = 4.0;
		core::vector3df r = playernode->getRotation();
		core::vector3df v = playernode->getPosition();
		v.X = v.X - ( cos(r.Y * 3.14159265/180)*speed );
		v.Z = v.Z + ( sin(r.Y * 3.14159265/180)*speed );
		playernode->setPosition(v);
strafe left:

Code: Select all

		float speed  = 4.0;
		core::vector3df r = playernode->getRotation();
		core::vector3df v = playernode->getPosition();
		v.X = v.X + ( cos((r.Y-90) * 3.14159265/180)*speed );
		v.Z = v.Z - ( sin((r.Y-90) * 3.14159265/180)*speed );
		playernode->setPosition(v);
strafe right:

Code: Select all

		float speed  = 4.0;
		core::vector3df r = playernode->getRotation();
		core::vector3df v = playernode->getPosition();
		v.X = v.X + ( cos((r.Y+90) * 3.14159265/180)*speed );
		v.Z = v.Z - ( sin((r.Y+90) * 3.14159265/180)*speed );
		playernode->setPosition(v);
turn left:

Code: Select all

		core::vector3df r = playernode->getRotation();
		r.Y = r.Y - 4.0f;
		playernode->setRotation(r);
turn right:

Code: Select all

		core::vector3df r = playernode->getRotation();
		r.Y = r.Y + 4.0f;
		playernode->setRotation(r);
of course, you wouldn't put the rotation and postion declaration in each case but i did to show the readers where each value is coming from