[SOLVED] quaternion 3rd person camera setTarget?

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
pandoragami
Posts: 226
Joined: Wed Jan 26, 2011 5:37 pm
Contact:

[SOLVED] quaternion 3rd person camera setTarget?

Post by pandoragami »

Hello all,

So far this is the question... and I'll try to explain this the best I can.

Below I have a pic of the problem , the teal line is the camera. The red and black lines are the camera look at vectors (respectively the camera_vector_initial and camera_vector_final). The obj_final is the black circle where the camera will look at with this vector camera_vector_final; the red circle is obviously the initial location with the camera vector camera_vector_initial. What I want to do is interpolate the camera look at positions between the red and black circle indicated by the teal arrow.

Code: Select all

 
camera_vector_initial = (cam->getTarget() - cam->getPosition()).normalize();
camera_vector_final = (obj_final - cam->getPosition()).normalize();
 
I've tried using the setTarget function for the camera at each new position generated by the quaternion starting at position (0,0,0) for cam->getTarget() but then the final position ends up becoming (0,0,0)! Basically the quaternion cannot rotate to a new look at position (e.g. (2,0,0)). Any ideas.




Image
Last edited by pandoragami on Sun Jan 25, 2015 8:37 pm, edited 1 time in total.
thanhle
Posts: 325
Joined: Wed Jun 12, 2013 8:09 am

Re: quaternion 3rd person camera setTarget?

Post by thanhle »

Ideally you don't need quaternion for that.

Since you have target position vector A and B.

The direction from A to B is C = B - A.

Once you know the direction. All you have to do is normalise(C) = D.

Then moving from A to B is.

A' = A + D*dt*(speed)

Then just do normal camera setTarget(A')

Goodluck
thanh
pandoragami
Posts: 226
Joined: Wed Jan 26, 2011 5:37 pm
Contact:

Re: quaternion 3rd person camera setTarget?

Post by pandoragami »

thanhle wrote:Ideally you don't need quaternion for that.

Since you have target position vector A and B.

The direction from A to B is C = B - A.

Once you know the direction. All you have to do is normalise(C) = D.

Then moving from A to B is.

A' = A + D*dt*(speed)

Then just do normal camera setTarget(A')

Goodluck
thanh

Thanks for your response, the problem though is that I don't know the speed or the dt of the object moving from one location to another, that's why I want to just interpolate the two locations using quaternions. I'm wondering about using setRotation instead for the camera; basically I would try to find the positions between point A and B using a quaternion rotation and at each position I would try to find the angle between the position A camera vector and the camera vector somewhere between AB. From that angle I think that updating the camera would work I'm just not sure if setRotation would be it or is it the entire view matrix. Any suggestions?
thanhle
Posts: 325
Joined: Wed Jun 12, 2013 8:09 am

Re: quaternion 3rd person camera setTarget?

Post by thanhle »

If you want to make the camera to focus from point A until B in 10 seconds. Just do it something like this.

totalTime = 10;
C = (B - A)
TotalDistance = (C).getDistance
D = Normalised(C)
speed = Distance/totalTime

You can assume dt to be delta time from last frame. Irrlicht already giving you that.

Then just do this over the interval.

if(totalTime > 0)
{
A' = A + D*dt*(speed)
camera setTarget(A')
totalTime -= dt;
}

Unless you are doing something more complex. That should works.

Else just using simple vector to horizontal angle and set the camera to the obtained angle. But set target is already doing the inverse for you so you don't need to do the inverse transform to obtain the angle.

Regards
thanh
pandoragami
Posts: 226
Joined: Wed Jan 26, 2011 5:37 pm
Contact:

Re: quaternion 3rd person camera setTarget?

Post by pandoragami »

thanhle wrote:If you want to make the camera to focus from point A until B in 10 seconds. Just do it something like this.

totalTime = 10;
C = (B - A)
TotalDistance = (C).getDistance
D = Normalised(C)
speed = Distance/totalTime

You can assume dt to be delta time from last frame. Irrlicht already giving you that.

Then just do this over the interval.

if(totalTime > 0)
{
A' = A + D*dt*(speed)
camera setTarget(A')
totalTime -= dt;
}

Unless you are doing something more complex. That should works.

Else just using simple vector to horizontal angle and set the camera to the obtained angle. But set target is already doing the inverse for you so you don't need to do the inverse transform to obtain the angle.

Regards
thanh

Your method doesn't work because it doesn't interpolate the positions,don't worry about it though.
thanhle
Posts: 325
Joined: Wed Jun 12, 2013 8:09 am

Re: quaternion 3rd person camera setTarget?

Post by thanhle »

Why do you think it doesn't work?

The trajectory from A to B or vector go straight from A to B is:

A' = A + D*dt*(speed)

which is a very simple problem.

If you want to get the number of points until you reach B. Just divide TotalDistance/numofsegments = segment.

Then just do A' = A + D*segment. Then in the number of segments or frames you will reach that point B?

If it's not a straight line segment then you will need to use a spline interpolation.

You don't need matrix or complex number operation for that. As I mentioned you just need to set cam->setTarget(A') for each frame, then it should do that for you.

Well goodluck
Regards
thanh
pandoragami
Posts: 226
Joined: Wed Jan 26, 2011 5:37 pm
Contact:

Re: quaternion 3rd person camera setTarget?

Post by pandoragami »

thanhle wrote:Why do you think it doesn't work?

The trajectory from A to B or vector go straight from A to B is:

A' = A + D*dt*(speed)

which is a very simple problem.

If you want to get the number of points until you reach B. Just divide TotalDistance/numofsegments = segment.

Then just do A' = A + D*segment. Then in the number of segments or frames you will reach that point B?

If it's not a straight line segment then you will need to use a spline interpolation.

You don't need matrix or complex number operation for that. As I mentioned you just need to set cam->setTarget(A') for each frame, then it should do that for you.

Well goodluck
Regards
thanh
Like I said, don't worry about it, I'll try your way too but the thing I'm concerned about with it is the problem with frames of reference or basically getting the camera to follow the object using your method. I know that I would get the direction vector from the camera position and the target but I'm a little worried that in 3d things would get "lost" along the way. With quaternions I would just have to know the cross product of the direction and up vector of the object and line up the vector of the camera that's the result of the cross product from the camera up and direction vectors. From there it's easy and no need for trying to figure out splines and interpolations. I'll mark this as solved.
Post Reply