 # How can you set a pivot point for a 3dmodel?

I got a cool looking 3d model (a plane), and my problem is as it is right now, when I try to roll & move the plane, it doesn’t do it at the center of the model, it does it at the edge.

Is there a easy way to calculate the center of the model and use that as the pivot point?

Or should I just “fake” it and use gltranslate before I do anything and get it centered that way?

In ASCII terms with o being the pivot point, current model is:
o/-------[]------
and I need /-----[o]-----
(err I hope those come out ok…) BTW, it is a 3ds model, I am loading, and I don’t have 3ds max to modify the model. In OpenGL, all rotations are around (0,0,0).
As far as i know, first translating, then rotating and finally translating back is the only way to rotate around an arbitrary point. After all, the transformations all end up in the same transformation matrix.

just do:

glTranslate3f(-x,-y,-z) //(x,y,z) = center object (see below): put the center at (0,0,0)
glRotatef(x, …); //rotate around center
glRotatef(y, …);
glRotatef(z, …);
glTranslate3f(x,y,z) //put it back

I don’t know how to calculate the center of a model if it is not a regular object (such as a sphere, box, …) though.
Since you don’t have 3dsMax, you can’t reposition the object at (0,0,0).
You could calculate the bounding sphere/box of the plane, and take its center. Ofcourse this won’t be the exact center. Just try with some shapes (spheres, boxes, ellipses,…)
I haven’t tried it myself, i just came up with this idea as i was writing a reply.

Hope this helps,

Sven

[This message has been edited by Sven Clauw (edited 01-06-2001).]

Well, its really easy. To find the center of an object you have some options. Two of them are:
1-Add all vertices and divide by the number of vertices (kind of center of mass).
2-Calc the model’s minx, maxx, miny, maxy, minz and maxz coords, and your center would be (maxx-minx)/2,(maxy-miny)/2,(maxz-minz)/2. (center point of the world-oriented bounding box).

Now, to use it as the pivot, all you have to do is glTranslate(-pivotx,-pivoty,-pivotz), do your transforms and then glTranslate(pivotx,pivoty,pivotz). This is the simplest way, but there are much better ways, but depend on how you manage your 3D model positions, rotation, vertices, etc.

This should give you a good starting point.

Je, it seems I wrote the last simutaneouslly with Sven Clauw… never mind.

hehe Just remembered:

at www.acm.org/jgt you will find the c-source code to accurately calculate the center of mass given a number of vertices. search for: “Fast and accurate computation of polyhedral mass properties” by Brian Mirtich.
the site contains lots of interesting algorithms and techniques, check them out!

Kind regards,

Sven

Thanks for the tips/link guys. I will look into them tonight. (just got home from work–YAWN! )

OR you could simply open up your modeler (3D studio MAX or whatever you use) and then YOU make sure that your model is centered around the origin.Simple as that and you avoid all that center of mass calculations It doesnt really matter which corner you start from, or if you put it in the middle even (middle is logically better).

Make sure that the model’s vertexs are drawn the same for every same frame, no matter where it is. Eg, you move the model using gltranslatef.

So, to rotate around any point, you first store the relevant vector of that point to the model’s origin. Then push the matrix, then translate with that vector to the rotation point. Then rotate by as much as you like. THEN translate back by minus the vector. This last step is the one that confuses people. Just try it and it works.
Then draw your model as you like, then pop the matrix.