PDA

View Full Version : Implement Unity Transform.LookAt function.



Darkpower
09-02-2014, 02:11 PM
Hi!

I'm trying to implement the Transform.LookAt function that can be found here: http://docs.unity3d.com/ScriptReference/Transform.LookAt.html

This function calculates Yaw, Pitch and Roll and updates the rotation of the object.

Example(Update position and rotation of the sun):

sun.localPosition = Vector3(?, ?, ?);
sun.LookAt(targetPos, upVector) //this should be the same as glm::lookAt(sunPos, targetPos, upVector) I think?


I found another guy who tried to implement the whole Transform class: http://gamedev.stackexchange.com/questions/70648/build-unity-like-transform-class

And I found his implementation of LookAt:

void Transform::LookAt(glm::vec3 targetPosition)
{
if((targetPosition - m_Position) == glm::vec3(0,0,0)) return;
glm::vec3 direction = glm::normalize(targetPosition - m_Position);
m_Rotation.x = asinf(-direction.y)* RadToDeg;
m_Rotation.y = -atan2f(-direction.x, -direction.z)*RadToDeg;

NormalizeAngles();
m_NeedUpdate = true;
}

But "m_Rotation" does only contain Pitch and Yaw, not Roll. How did they(Unity) calculate Roll?


Thanks in advance.

Yandersen
09-02-2014, 02:39 PM
Why don't you just simply copy the algorithm used in gluLookAt (http://www.opengl.org/sdk/docs/man2/xhtml/gluLookAt.xml) function?