3rd Person Camera

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
cosmo
Posts: 17
Joined: Sat May 06, 2017 1:29 pm

3rd Person Camera

Post by cosmo »

Hi,
I'd like to implement a 3rd person camera but I'm stuck.
Briefly, my system is composed as follow: an empty object, parented to the player and used as target; a camera parented to the target, so that it can rotates accordingly.
Moving the mouse along x axis has the effect of rotates the target left and right, and the camera with it around the player; on the other hand, moving the mouse along Y axis, turns the target up and down.

But how I have to build the matrix rotations in the right way?
Is there in Irrlicht a function to perform this task, like ad example "BuildTheMatrix()" I found in other API'S?
Otherwise, could you please point me in the right direction to build a specific one for this purpose?
Thanks for your help, bye!

This is the code I used for but it didn't work as expected...

Code: Select all

void TP_Camera::Update(float dt)
{
	
	CamYaw += -inputHandler->GetMouseDeltaY() * Sensitivity;
	CamPitch += inputHandler->GetMouseDeltaX() * Sensitivity;

	if (CamYaw > 0.60)
		CamYaw = 0.60f;

	if (CamYaw < -0.90)
		CamYaw = -0.90;


	vector3df camRot(CamYaw, CamPitch, 0);
	camRot *= RADTODEG;
	camRot.rotationToDirection(camRot);
	target->setRotation(camRot);


	CamYaw = 0;
	CamPitch = 0;
		

}

CuteAlien
Admin
Posts: 9849
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: 3rd Person Camera

Post by CuteAlien »

Can you give an example of an API which has BuildTheMatrix so I get an idea what exactly it is supposed to do?

This is one of those things where details really matter as there are quite a few options to code 3rd person cameras.

1. I assume with "parented" you mean player is set as parent of empty object and empty object is set as parent of camera?
2. Does that mean you control the player independently? So player can move & rotate in some other way? This really is only about camera control?
3. Do you want the camera by default to look into the same direction where the player front is and use mouse to move it additionally further in those directions?

Note there are different ways to control a camera:
You can call bindTargetAndRotation(true) and then use camera->setRotation to rotate it (it will internally update the target then).
Also other way round - setting Target it will then try to update the rotation.
I don't recommend either much.

Better stay with defaults. Do not call bindTargetAndRotation (or call it with false as parameter). Then you control the camera via setPosition, setTarget and setUpVector. The rotation of the camera is then independent and only matters if the camera has children nodes.

Also I recomment not thinking too much about "target" but thinking instead of a "front" vector. So calling setTarget is really setTarget(camera->getAbsolutePosition() + frontVector); And be careful you'll need an updateAbsolutePosition() before calling getAbsolutePosition() (or you will have a 1 frame delay).

So assuming all those preliminaries...

First figure out what your "front" vector should be by default. Aka where your player is looking at right now. Generally it's one of the vectors of it's matrix - usually first or third (so M[0], M[1], M[2] or M[8], M[9], M[10]). Or you can get it by multiplying the original direction vector (p.E. (1,0,0) or (0,0,1)) by the player matrix. You do get the player matrix like all nodes via getRelativeTransformation() or getAbsoluteTransformation() (if your player is a top-level node in your scene, which is often the case, then you can use the slightlty faster getRelativeTransformation() and you also don't need to call updateAbsolutePosition() first).

Having your base direction you can now add rotations to it. Aka your CamYaw and CamPitch. I wouldn't ever reset them back to 0. But instead keep them around (maybe slowly get them back to 0 if that's what you want to happen). But as long as you want the camera to stay rotated away from the base-direction do keep them at their current values. And add the rotations every frame back to the base "front" vector. With only 2 degrees of freedom (yaw, pitch) as the rotation order doesn't matter. So: front.rotateXZBy(yaw); And depending what your front vector is then it's probably either rotateYZBy(pitch) or rotateXYBy(pitch).

If you search around forum there might be some code snippets already for 3rd person cameras. There are different ways to code this, so they might not look the same.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Post Reply