Transforming + Rotating an object without going weird?

Hi, i guess im fairly advanced in lwjgl however this is a newbie question.

When i rotate an object and then translate the object (its a model ship in a VBO), the object rotates around the centeral point where it first spawned and not from its own centre.
When i translate the object and then rotate it the ship rotates around the centre but does not translate the right way (so the ship always moves the same direction).

No, i don't unfortunatley. I may be using it or whatever however i do not actually know a lot of the names of the code i am using as i often copy it from elsewhere and change bits around to learn what certain pieces do.
I actually work more with lwjgl rather than opengl, though the calls/code are the same.
I also don't go to school (i am 13), so don't expect me to be able to understand cosigns etc so well.

Even though you're 13, if you're into this stuff, I recommend reading Lengyel's book, "Mathematics for 3D Game Programming and Computer Graphics". It's good learning material and when I look up the most common stuff, this is the book I go to. If you're not solid in 3D math, don't slip anything. Read it, understand it and be amazed at how awesome math is.

i often copy it from elsewhere and change bits around to learn what certain pieces do

That's fine if you're trying to figure out what a specific funtion does. However, if you want to really do 3D programming, there is absolutely no way around the appropriate math portions.

I actually work more with lwjgl rather than opengl, though the calls/code are the same.

The interface doesn't change how the encapsulated GL calls operate. LWJGL is just a wrapper usable with Java (since OpenGL implementations are usually implemented in C and LWJGL needs to interface with the C library through the JNI).

I also don't go to school (i am 13)

At that age, aren't you supposed to go to school? (Don't know where you're coming from.)

Regarding your problem at hand, for now, it should suffice to know that matrix rotation leads to rotation of a position around the origin, i.e. (0, 0, 0, 1), of the current coordinate system. There is nothing you can change about that, but there's a simple trick to rotate around an arbitrary point in space: you translate the center of your object to the origin, then rotate, and then translate it back to whatever position you want it to be located at. If the original and resulting position are the same, you effectively rotated around your objects center.

Now, a translation is nothing more than moving a point from one place to another. To achieve the above, in 3D you'll need a 3D vector to describe the direction of the translation and the distance you want to translate the object. The distance is implicit - it is the length of the translation vector. So, if you have a vertex at position (1, 1, 1) and want to translate it to the origin, you'll need a translation vector (-1, -1, -1). The resulting position will be (0, 0, 0) (because (1,1, 1) + (-1, -1, -1) = (0, 0, 0))

To translate it back to where it came from, you'll need to translate in the inverse direction, i.e. you need to negate the translation vector: (-1, -1, -1) then becomes -1 * (-1, -1, -1) = (1, 1, 1). Apply that to the current position (0, 0, 0) and you'll get (0, 0, 0) + (1, 1, 1) = (1, 1, 1). And where back to our original position.

I just typed a whole reply... and it didnt appear when i posted it. :S

Anyway your answer was very good, very informative and helpful, however could you post the exact code in the exact order i need? Certain sites say i should put the code in different orders or backwards etc, and it confuses me.

In england home "education" is allowed. Unfortunatley a lot of home "educated" people don't actually educate themselves, unlike me.

I will look into the book, thanks. Will probably get it too.

I did have a longer reply however i am too tired to type it all out again. Thanks a lot for the informative reply. I do not get that on many forums.

1. Assuming you know the center of your object to be at point C. For each vertex, a translation T encoded into a translation matrix MT needs to be applied which translates in direction -C, i.e. for each vertex vi, the initial transformation is vi' = MT * vi (with i in [0, #vertices]).

2. Then a rotation R follows, encoded in a rotation matrix MR. For each translated vertex vi', a rotation is applied as follows: vi'' = MR *vi'.

3. Afterwards, the rotated vertices are translated by MT-1. For each rotated vertex vi'', the final position results vi''' from vi''' = MT-1 * vi''.

The reason I seperate the steps like that is simple: you'll probably implement the above exactly the same way using the fixed-function pipeline, i.e. you first call glTranslatef(), then glRotatef() and then glTranslatef() again. However, what you actually end up with is a concatenated transformation matrix, which encodes both translations and the rotation: vi''' = MT-1 *MR * MT * vi = (MT-1 * MR * MT) * vi =Mcomplete * vi

Expressing this in fixed-function OpenGL can be achieved in more than one way, but the usual approach is this (let's assume you want to rotate your object about the y-axis and an angle of 45.0 degrees):

But that works just the same. As long as the direction of the ship doesn't change, you translate along this direction. As soon as you hit a point where the ship is supposed to be rotated, you first translate to the origin, then rotate, translate back and then sail along the new direction.

Translating like that normally involves linear interpolation between points of rotation. Beware that, depending on the length of the line segments, translation speeds will vary, but you'll see that yourself. It's a different topic and should be tackled separately once you got the basic stuff down.

For some path, defined with n control points, the (pseudo) code might look a little like this:

The linearly interpolated position is computed with: position = currentPoint + (nextPoint - currentPoint) * n (where n is in [0, 1]). To calculate the angle between the current direction of the ship and the direction of the ship at the next control point, you can take the angle of the current direction and the x-axis and the angle of the next direction and the x-axis, compute the difference and then rotate accordingly.