FaceTo function that works regardless of Up

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
robmar
Posts: 1125
Joined: Sun Aug 14, 2011 11:30 pm

FaceTo function that works regardless of Up

Post by robmar »

getHorizontalAngle works okay until the camera is looking down, then the object starts to spin as there is no link to the actual frustum horizon... well, that's how it seems.

As objects that need to face to camera move to the far right or left of the screen, they also tend to tilt.

Is there any good solution to this?
thanhle
Posts: 325
Joined: Wed Jun 12, 2013 8:09 am

Re: FaceTo function that works regardless of Up

Post by thanhle »

I think you need to put some code here.
It's easy to recognize what you're trying to achieve and what you are doing wrong.

regards
thanh
robmar
Posts: 1125
Joined: Sun Aug 14, 2011 11:30 pm

Re: FaceTo function that works regardless of Up

Post by robmar »

I don't have any code to fix this problem, just using the standard Irrlicht method to face to object, which uses getHorizontalAngle.

If you use this with a cube you can see the problem, the cube faces the camera, but if the cube is far right top or far left top it tilts over one way or the other.

This is I think, because the X and Y axis are used to face to the camera, and when the X axis is rotated to face the object, the Y axis shifts off world UP, then when the function sets the Y rotation the object is "skewed".

Also when the camera moves towards a position in which it looks right down the world Y axis, the objects tend to spin wildly.

I'd like to find a way to stop that, and keep the object level to the frustum, does that make sense?
thanhle
Posts: 325
Joined: Wed Jun 12, 2013 8:09 am

Re: FaceTo function that works regardless of Up

Post by thanhle »

Maybe code to show the problem.
E.g. how do you set the angle? or combine the angles after you use getHorizontalAngle?
Are you using matrix or quaternion operation to combine the angle?

Regards
thanh
robmar
Posts: 1125
Joined: Sun Aug 14, 2011 11:30 pm

Re: FaceTo function that works regardless of Up

Post by robmar »

Code: Select all

 
vector3df camloc = cam->getPosition(); // Camera location
vector3df cubeloc = cube->getPosition(); // Cube/objectlocation
 
core::vector3df vect = (cubeloc->getAbsolutePosition() - cam->getAbsolutePosition()).normalize();   // Face-to direction vector
core::vector3df rot = vect.getHorizontalAngle();
cube->setRotation(vector3df(rot);
 
getHorizontalAngle just return the required node rotation to face in the direction passed in.

It works fine as long as I keep the camera looking level to the horizon, but not if the camera flies over the zenith.
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: FaceTo function that works regardless of Up

Post by mongoose7 »

Applying the camera rotation to the object will cause it to point in the same direction. But now you need it to look at the camera, so consider the forward direction of the camera and the direction towards the object an build the rotation transformation to rotate the camera/object in that direction. Now combine the two transformations.
robmar
Posts: 1125
Joined: Sun Aug 14, 2011 11:30 pm

Re: FaceTo function that works regardless of Up

Post by robmar »

...but, the code works, the objects always face the camera.

I'm getting a tilt off horizontal (to the frustum) when the objects are left or right of the centre of the screen.

That's what I want to avoid.

I'm going to try setting the direction to be parallel across the screen using the frustum box... see how that looks
thanhle
Posts: 325
Joined: Wed Jun 12, 2013 8:09 am

Re: FaceTo function that works regardless of Up

Post by thanhle »

Maybe there are new math technique without trig that I'm not aware of.
But I know 3 Ways to do that using trigonometry-based method.

1) Atan2 method. This method would be simple to use. I think it would best meet your requirement.
2) matrix multiply
3) quaternion multiply

Lets use method 2. Procedure will be same to method 3.

Code: Select all

//outside loop
 
Assume facing rotation axis is y-axis.
Assume initial facing direction  = z-axis (0,0,1);
 
 
vector3df cubeDirection = vector3df(0,0,1);   //Init object direction
 
 
//Put these below in it's on function call.
vector3df camloc = camera->getPosition(); // Camera location
vector3df cubeloc = cube->getPosition(); // Cube/objectlocation
camloc.Y = 0;    cubeloc.Y = 0;    //We only care about y-rotation. So zero y-pos
 
 
core::vector3df vect = (camloc - cubeloc).normalize();   // Face-to direction vector
vector3df cubeRot = cube->getRotation();
 matrix4 m1,m2;
 m1.buildRotateFromTo(cubeDirection , vect);
 m2.setRotationDegrees(cubeRot);
 m2 = m1*m2;
 
cube->setRotation(m2.getRotationDegrees());
 cubeDirection = vector3df(vect);   //Update current facing position vector.
regards,
thanh
robmar
Posts: 1125
Joined: Sun Aug 14, 2011 11:30 pm

Re: FaceTo function that works regardless of Up

Post by robmar »

Hi, thanks for the other technique.

I changed the way I calculate the direction vector, so that where ever the object is, it looks in the direction of the camera, or its reverse, rather than looking at the camera. That fixed the slant issue so all done, easy one after all!
Post Reply