![]() |
|
Get model to face direction of movement - SOLVED - Printable Version +- iDevGames Forums (http://www.idevgames.com/forums) +-- Forum: Development Zone (/forum-3.html) +--- Forum: iPhone, iPad & iPod Game Development (/forum-11.html) +--- Thread: Get model to face direction of movement - SOLVED (/thread-8073.html) |
Get model to face direction of movement - SOLVED - MikeD - Sep 5, 2010 02:49 PM This is a question that has been asked in MANY forums on the web as I've been reading most of them :s That said, I'm still confused as to how to orient my 3D model to face the direction of travel. I have a normalized 3D vector that is pointing in the direction of travel and I want to orient my model to face that direction. I'm thinking I need to create a 3D matrix from that vector, but all the examples I've been trying cause the model to rotate in all sorts of ways, but never face the direction I expect. At the moment I'm trying to create a matrix from a vector using code that was inside the gluLookAt function. This code looks like this: Code: static inline void SSMatrixFromVector(float* mtx, const SSVector3D v, const float upx, const float upy, const float upz)The model is rotating, but not in a way that keeps the front of the shop oriented toward the direction of travel. The models rolls and banks as the direction changes but not in the correct way. I hope that has made some sense. If anyone has any pointers or suggestions that would be great. Cheers MikeD RE: Get model to face direction of movement - OneSadCookie - Sep 5, 2010 03:16 PM This is what gluLookAt does -- given a forward vector and an up vector: normalize both cross forward and up to get right/left (depending on handedness and order of cross product) cross forward and right/left to get new up use forward, right/left and new up as a rotation matrix Of course, there are two cross products that you can change the order of to get the wrong answer, several stages that may or may not need renormalization, and you'll have to fight the fact that OpenGL's matrices aren't the same way round as your linear algebra text. Your code would be a lot easier to read if you defined some functions like "normalize" and "cross". RE: Get model to face direction of movement - AnotherJake - Sep 5, 2010 03:38 PM If you're having difficulty doing this via gluLookAt, or your own UVN rotation code isn't working, you could also just go brute-force on it and use some trig, as long as you aren't using it for anything particularly performance critical. This is assuming up is toward the zenith, so roll isn't taken into account here. I don't know if this code works or not. I just butchered it together for some ideas of how to approach it brute-force style. I put in two functions, one for y being the zenith and one for z being the zenith. Code: enumYou'd use it, something like this, where myPoint would be your movement vector and mySphereCoords would be filled in with angles you can use to rotate your object with to match the direction vector (theoretically anyway, like I said, I don't know if this works or not ):Code: // init to demo values (not normalized, but not necessary in this situation)It works by imagining you're at the center of the earth and know the cartesian point where something is located on the surface (your direction vector), and then converts that to lat/lon. RE: Get model to face direction of movement - SOLVED - MikeD - Sep 6, 2010 12:37 PM Thanks for the help guys. Based on your info and so more reading I've created a function that will create a matrix that can be used to orientate my model in the direction they are travelling, or any direction I pass in. I've put the function below in case anyone else goes hunting for something similar. Code: static inline void SSMatrixFaceVector(float* mtx, const SSVector3D dir, const SSVector3D up)Thanks again for the quick response and the help ![]() Cheers MikeD |