character rotation

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
green_algae
Posts: 7
Joined: Wed Sep 14, 2005 12:20 pm

character rotation

Post 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
AshmenGlue
Posts: 29
Joined: Wed Oct 12, 2005 9:09 am

Post 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?
xterminhate
Posts: 206
Joined: Thu Sep 01, 2005 9:26 pm
Location: France

Post 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...
green_algae
Posts: 7
Joined: Wed Sep 14, 2005 12:20 pm

Post 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
panoramix
Posts: 6
Joined: Thu Oct 06, 2005 8:49 pm

Post 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).
mepcotterell
Posts: 10
Joined: Thu Dec 29, 2005 2:42 pm

Post 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
Post Reply