|
Title: GLM - How to orient one object to look at another? Post by: Orz on November 02, 2013, 09:24:41 AM Maybe I should rephrase my geometry question. I am using GLM in my engine to compose transformation matrices for my entities, and setting their orientation is driving me insane.
I would really like to have a "lookAt" or "setDirection" method that calculates the orientation needed for one entity to point to another. The "lookAt" method built into GLM doesn't work - I suppose it's intended to transform the camera and not arbitrary entities. Has anyone successfully done something like this in GLM? I would love to see some code samples, because I suspect my fuzzy knowledge of OpenGL is to blame. Title: Re: GLM - How to orient one object to look at another? Post by: ThemsAllTook on November 02, 2013, 01:29:44 PM I haven't used GLM myself, but here's some pseudocode that should construct a matrix for what you want:
Code: mat4 lookAt(vec3 position, vec3 target, vec3 up) { vec3 front = normalize(target - position); vec3 right = normalize(cross(up, front)); up = cross(front, right); return mat4(right.x, up.x, front.x, position.x, right.y, up.y, front.y, position.y, right.z, up.z, front.z, position.z, 0, 0, 0, 1); } This assumes the identity orientation of your objects is facing +z. If it's -z, you can probably just set front to normalize(position - target) instead of normalize(target - position). Haven't tested this, so I may have a sign or a cross product order wrong somewhere... Let me know if you run into trouble! Title: Re: GLM - How to orient one object to look at another? Post by: xgalaxy on November 02, 2013, 05:21:46 PM Just use glm::angleAxis, but lookAt should work...
Title: Re: GLM - How to orient one object to look at another? Post by: Orz on November 03, 2013, 12:01:32 AM I haven't used GLM myself, but here's some pseudocode that should construct a matrix for what you want: Thank you so much. I was looking for a something to explain the correspondence of matrix elements to transformations (other than the OpenGL book), without getting into the details of linear algebra.I ended up doing the below due to ignorance. It works 99% of the time but occasionally flips the object around the Y axis. I will try your technique and try to cut out some of the unit conversions. Code: vec3 Node::localXAxis(void){ return cross(getDirection(), UP_VECTOR); } vec3 Node::localYAxis(void){ return cross(getDirection(), localXAxis()); } /** Orients the node to point at the given position. Instead of using the world coordinates, I use a vector from the origin to the direction because it is more numerically stable. The matrix apparently has to be inverted because it's designed for setting the camera's view transformation.*/ void Node::lookAt(vec3 pos){ mat4 m=glm::inverse(glm::lookAt(NULL_VECTOR, pos-*position, localYAxis())); quat q=toQuat(m); vec3 rot =vec3( glm::pitch(q), glm::yaw(q), glm::roll(q) ); setRotation(rot); } Title: Re: GLM - How to orient one object to look at another? Post by: Thomas Hiatt on November 03, 2013, 07:42:35 AM If you just want to set the orientation of the object you could subtract its position from the targets position. That will give you the direction to the target and you can use that as your orientation vector.
|