Hello, I am in need of assistance… I would like help to figure out how to have an object move forward or backward in the direction that it is pointed(which varies according to where the user directs the mouse toward) I am using glTranslatef for my movement of the object. Any help would be appreciated… Thx
Rotate the translation so that it is aligned with the orientation of the object you’re trying to translate and then apply that transformed translation to the object.
Rotate the object to some canonical orientation (say aligned along the X axis), then translate the object in that orientation and then rotate the translated object back to the original orientation.
Thank you very much for you reply and suggestions… I wouod like to know how to do the first thing taht you mentioned(because it seems easier and more along the lines of what i would think would be handy ) Thx agian…
Well, you should have some sequence of rotations that you have applied to the object you want to translate. Apply that same sequence of rotations to your translation and then add that rotated translation to the location of your object.
If you have scaled your object, you may also need to scale your translation factor as well. Do this by postmultiplying the rotation matrices by the scaling matrix.
I hope this helps.
I am sorry about the stupid questions, but I am still pretty new to OpenGL, and I do not know how to apply rotation to translation. Is it very simple? Could you please show me some code, if that wouldn’t trouble you. Thank you very much…
I am wondering if in your original post that if those were just ideas or if there is known code for doind such… Thnx (sorry about the double post)
Okay, I thought you were looking for the underying math (this being the math & algorithm forum) but I can see you’re more interested in the detailed implementation in OpenGL. I don’t think I can help you as much as you would like with the latter, since it’s been a while since I used OpenGL and I don’t have a reference manual handy. I should be able to give you enough detail here so that you can fill in the rest with a few well crafted Google searches.
You’re along the right trail using glTranslate to do this. You simply apply the translation to the object before you apply any of the other transformations. Thus, if you currently have a 4x4 scaling matrix S and a collection of 4x4 rotation matrices R1, R2, … Rn, you simply post-multiply your transformations by the 4x4 translation matrix T which mathematically looks like:
V' = Vn * ... * V2 * V1 * S * T * V
where V is the collection of vertices of your object in their canonical form and V’ is their transformed position.
In OpenGL, simply do each of your transformations (glScale, glRotate, glTranslate) in the desired order. I don’t recall if OpenGL pre or post multiplies, so if doing this in one order doesn’t work, just reverse the order.
I hope this helps.
Thanks very much… I very much appreciate your help…