Strech Model from Point A to Point B

Started by
9 comments, last by Th0ughtCr1me 17 years, 7 months ago
I want to strech a model (now loaded into a disp list from point A(tx,ty,tz) to point b(the distance to which is represented by "templength") the angle is represented between the points is "tempangle" for x rotation, and "tempyangle" for y rotation. My code is as follows

      glPushMatrix();

         glTranslatef(tx,ty,tz);

         glRotatef(tempyangle,0,1,0);
         glRotatef(tempangle,1,0,0);
      
         float tempscale=1;

         glScalef(tempscale,templength,tempscale);   

         glCallList(treemodels);	
   
      glPopMatrix();


It starts the model at the right place(translation works), but the rotation of the model is simply wrong, if I move the X rotation ( glRotatef(tempangle,1,0,0); ) above the y I get no y rotation at all. Any ideas? Edit: I meant to say, the rotation isn't off by a set amount (like all off by 90 or something) it's off by something random, so all the models are off by different amounts.
_______________ Play my Game at neonswarm.com
Advertisement
I don't fully understand what you're trying to do, but I'll summarize what you are doing in your code:

glTranslatef(tx,ty,tz)             //move the object to point aglRotatef(tempyrotation, 0, 1, 0)  //rotate your object around a in the xz-plane by angle tempyrotation //note: if you want to rotate the object around it's origin, rotate first then translateglRotatef(tempxrotation, 1, 0, 0)  //rotate around x which results in rotating your already rotated object around the global x-axis//try to first do x rotation, then y or use quaternionsglScalef(1,templength, 1) //temscale = 1, scales the the rotated and translated object on the global y-axis (up)


If you want a properly scaled obejct at position a which is rotated about it's own origin use the following order:

Scale
RotateZ
RotateX
RotateY
//or use quaternions for rotation
Translate
If I was helpful, feel free to rate me up ;)If I wasn't and you feel to rate me down, please let me know why!
Thanks a lot for your reply, I tried that and I get all the models acting as if they are scaled before they are translated, and so forth. As if all the matrix alterations are in revers, do you know how that could happen?
_______________ Play my Game at neonswarm.com
This is the general way of arbitrarily centered and aligned scaling:

Scaling is done along the current local axes, where the origin isn't affected by the scale. So you have to guarantee that the object to be scaled is located and oriented properly before scaling. However, you don't want to achieve the object in those co-ordinates, so you also have to undo the effort spend to locate/orientate the object properly for scaling. In mathematical terms, you have to build the overall transformation (in OpenGL complient order)

L * A * S * A-1 * L-1

where L denotes the location (a translation) and A the alignment (a rotation) specifying the center and orientation of scaling, and S the scaling itself. In other words: If L describes the center of scaling, then right most transformation moves that point to (0,0,0) making it the origin. Next, if A describes the axes along which to scale, then the A-1 rotates the object so that those axes become aligned with the principal axes. Now the scaling S could be done. After scaling, the effects of A-1 * L-1 are undone by their inverse.

Going furthur from this point, you have to fill in your parameters and perhaps the one or other term can be dropped due to it is identity.


Now I have a problem of what role your parameters play herein. It seems that there is a point called A (before scaling) that should be located at another given point called B (after scaling). Is this right? So an axis of scaling goes throught point A and B as the ray
R(t) := A + t * ( B - A )
describes. Are these assumptions correct?

And should the axis AB be one of the principal axes? Or else should the principal axes still be object or world aligned? From your code it seems that the former case should be valid, but the code doesn't work and hence isn't verbose at this time.
Thanks alot for your attention :) My goal is to get a cylinder to stretch from point A to B. Before to code present I find the angles for X axis rotation and Y axis rotation. So my idea was to Scale along y to the distance between A and B, then Rotate around X and Y, and finally translate. The problem is, as I said in my first post, that it translates correctly (the model is moved to the correct possision), the scale appears to be correct, but the rotation around Point A is not right. So Having read your reply I'm sorry to say that I'm mostly just confused :b
_______________ Play my Game at neonswarm.com
Well, in the code you posted you're doing it the other way round: translate, rotate, scale. You said you want to rotate around A, but that's wrong.

You have to rotate your cylinder around it's local origin, then translate it to A. Translating first, then rotating will rotate your point A around the origin.
If I was helpful, feel free to rate me up ;)If I wasn't and you feel to rate me down, please let me know why!
I understand that my code should result in my end point A being rotated around the origin. My question is, why would it display point A where it should be and only the rotation of the object placed at A wrong?
_______________ Play my Game at neonswarm.com
You display A by something like drawMarkerAtPosition(A)? (Something like a cube or cross to mark that position.)

Then I guess you do that without calling glRotatef(...).

So internally the marker is placed on A but after rotation A isn't there anymore.

Try that:

Apply your transformation (without scaling for this one)
draw your object
draw your marker

That way you should see that point A has been moved to where your object is drawn.
If I was helpful, feel free to rate me up ;)If I wasn't and you feel to rate me down, please let me know why!
That's the thing, the object itself is drawn at the right place (Point A) and the object is rotated around Point A, it's just that the rotation is wrong :(
_______________ Play my Game at neonswarm.com

Just for making that one clear: if you want the cylinder's "up" axis to go from A to B, you HAVE to rotate the object around the origin first, NOT around A.

Some example:

You want your cylinder to start at A(10,0,0) and end at B(10, 0, 10), that's lying in the xz-plane with the cylinder's up perpendicular to the x-axis but parallel to the z-axis.

Angle to rotate: -90 degrees (left rotation) around y-axis

Cylinder is lying on the x-axis with length of 10, local origin is an the left side.

Rotation first then translation:

start: your object starts at O(0,0,0) and ends at E(10,0,0)
after rotation: your object starts at O(0,0,0) and ends at R(0,0,10)
after translation: your object starts at A(10,0,0) and ends at B(10,0,10)


Translation first then rotation:

start: your object starts at O(0,0,0) and ends at E(10,0,0)
after translation: your object starts at A(10,0,0) and ends at C(20,0,0)
after rotation: your object starts at A'(0,0,10) and ends at C'(0,0,20).
If I was helpful, feel free to rate me up ;)If I wasn't and you feel to rate me down, please let me know why!

This topic is closed to new replies.

Advertisement