Object Rotation, please Help!!!!

Started by
6 comments, last by lifeless 21 years ago
Ok, here is the thing. I''ve got two points in 3d, i get the vector between them, now I have an object (say a cylinder) on the origin, what i need is to rotate that object and translate it to point #1 and rotate it to fit the position of the vector, in other words rotate and translate the cylinder, so on face will be on one of the point and the other face in the second point. What id did was: glPushMatrix(); glTranslatef(point1.x,point1.y,point1.z); vec2=set(vec.x,vec.y,0); tmp=set(0,1,0); ang=angle(vec2,tmp); glPushMatrix(); glRotatef(ang,0,0,1); vec2=set(0,vec.y,vec.z); tmp=set(0,1,0); ang=angle(vec2,tmp); glPushMatrix(); glRotatef(ang,1,0,0); where vec, is the vector between my two points. I first project my vector in the XY plane, and get the angle, the next step is project it into the YZ plane and do the same, the angle() function gets the angle between the two vectors. Has any of you got an idea on how to make it work?, or if im doing something wrong?
Advertisement
well thats pretty much it

i think in GL first u rotate then u translate (right??)

say vVect = vDest - vPos;
get the angles between the vector and the axys
angX = acos(vVect.x / len(vVect));
angY = acos(vVect.y / len(vVect));
angZ = acos(vVect.z / len(vVect));

rotate by the angles and then translate



angX = acos(vVect.x / len(vVect));

is the same than acos(dotProduct(vec,tmp)/(len(tmp)*len(vec)) given tmp=(1,0,0) which is exactly the same that my angle() function does.

Except that i only make the rotation for only two angles (it seems to me that the third rotation is not necessary but im not sure).

So im doing exactly the same, and it doesnt work :S

I tried doing the rotation before and after and no matter what i do it doesnt work.

Any other idea?
does it seem like your first rotation is correct but the second screws up? in that case its the simple fact that the first rotation already changed the axes.
try building the matrix yourself instead. the up vector is obvious (point2-point1), another can be chosen randomly. but this would require two crossproducts and some normalization. you can delegate that work maybe by abusing glulookat and passing the inverse parameters. but as this seems similar to do billboards i wouldnt start with rotating but placing it right in the first place.
f@dzhttp://festini.device-zero.de

Ok, i dont get it, why would the first rotation change the axes? I mean, basically what im doing is what Ilici says (except that i do only two rotations, which i think those are the only necessary), and then rotate about those axes. I just dont get what you are saying of the cross product etc.
Perhaps you should say how it doesn''t work. In other words, what IS it doing.

As far as the two rotations, because they are around two different axes they should be ok...

Did you try rotating and then translating?
Thats exactly what i do, translate and then rotate.

It shows almost the value, say like this :

      o   o     /  _/    /  /   / _/  /_/ /o


Say the first line is the correct (drawing the line between the two points). The second is the actually been displayed.

[edited by - lifeless on May 6, 2003 4:36:29 PM]
id have to take more time and follow what you''re doing. what i was talking about, is that the first rotation around the z-axis changes the modelview matrix and if you rotate 45deg to the left, then your new x-axis will point along (1,1,0) and not (1,0,0) anymore.. if your second rotation is supposed to be local thats fine, but if you expect it to rotate around the global "right" axis then its not.

and as i said. i would think about not "moving til its right" but simply "place it the right way".. instead of doing translations and rotations it would be easier to just set up the matrix yourself.
f@dzhttp://festini.device-zero.de

This topic is closed to new replies.

Advertisement