# Getting a mesh's centre of rotation?

Hi, I’m trying to render a bullet model in the view space, then get its world coordinates for use with the Bullet physics library (Rigid bodies). This is my process so far:

``````// Initially rotate the bullet so that it's facing forward
glm::mat4 BulletRotationMatrix = glm::toMat4(bulletRotation);
// Translate it so that it appears at the end of the gun model
glm::mat4 BulletTranslationMatrix = translate(mat4(), glm::vec3(0.35, 0, -3));
glm::mat4 BulletScaleMatrix = scale(mat4(), glm::vec3(0.1f, 0.1f, 0.1f));
glm::mat4 ModelMatrix2 = BulletTranslationMatrix * BulletRotationMatrix * BulletScaleMatrix;
glm::mat4 MVP2 = ProjectionMatrix * ModelMatrix2;

// Attempt to get world coordinates?
glm::mat4 BulletToWorld = inverse(MVP2 * ViewMatrix);
// Bullet coordinates in world space
glm::vec4 bulletWorldCoords = BulletToWorld * glm::vec4(bulletOrigin.x, bulletOrigin.y, bulletOrigin.z, 1.0);
bulletPositions[i] = glm::vec3(bulletWorldCoords);
std::cout << glm::to_string(bulletPositions[i]) << std::endl;
``````

This draws my bullet in view space, but I need to get it’s world coordinates to then draw it in world space and give it a rigidBody. However, I don’t know how to get my model’s centre of rotation (bulletOrigin in the code). When I rotate my model, it’s rotating around it’s own axis and origin, so how can i find that value for use in getting world coordinates?

Thanks.

This:

doesn’t make sense.

The overall transformation from object space to screen space is projection * view * model.

The transformation from object space to world space is just the model transformation. The model transformation transforms object space to world space, the view transformation transforms world space to eye space. The model-view transformation (= view * model) transforms object space to eye space (rendering doesn’t have any use for world-space coordinates).

[QUOTE=GClements;1286331]This:

doesn’t make sense.

The overall transformation from object space to screen space is projection * view * model.

The transformation from object space to world space is just the model transformation. The model transformation transforms object space to world space, the view transformation transforms world space to eye space. The model-view transformation (= view * model) transforms object space to eye space (rendering doesn’t have any use for world-space coordinates).[/QUOTE]

To be honest, I wasn’t sure about that code in the first place, it was a suggestion from another post. Is there a simple way to get world-coordinates for an object rendered in the view space?

“World” coordinates are up to the programmer. Legacy (fixed-function) OpenGL only has object coordinates (the values passed to glVertex/glVertexPointer and glNormal/glNormalPointer), eye coordinates (after transformation by the model-view matrix), clip space (after transformation by the projection matrix), normalised device coordinates (after division by W) and window coordinates (after the viewport transformation). Modern OpenGL doesn’t even have eye coordinates unless the vertex shader chooses to use them.

If you have separate model and view matrices, then transforming object coordinates by the model matrix will give you world coordinates. If you have coordinates to which additional transformations have been applied, you need to transform them by the inverse of the additional transformations. E.g. world_coords=inverse(projection*view)*clip_coords, world_coords=inverse(view)*eye_coords.

This is what I need, I think. Given the code I’ve posted (The first section at least, where I have a model, view and projection matrix that make up MVP2), could you please show me how’d you’d get the ‘world_coords’ from that? I’m not quite sure on the exact steps, and wouldn’t this still leave me with a mat4 rather than a vec3?

if you’ve got the mat4 “model-to-world” matrix, the last column is the vec3 “position” you’re loking for (if scaling = 1): https://learnopengl.com/#!Getting-started/Transformations

Try this:

glm::mat4 ModelMatrix2 = BulletRotationMatrix * BulletScaleMatrix * BulletTranslationMatrix;

Do you mean my MVP2 matrix? Or would I need to do something like this to get the “model-to-world” matrix?:
glm::mat4 BulletToWorld = MVP2 * ViewMatrix;

Printing that out doesn’t give me the co-ordinates I’d expect, I’ve tried a few variations on it as well like:
glm::mat4 BulletToWorld = MVP2 * ViewMatrix * inverse(BulletScaleMatrix);
or
glm::mat4 BulletToWorld = MVP2 * inverse(ViewMatrix);

But again, none of the numbers in the print out are the what I’d expect (they’re all way too small, my room dimensions are within the 9 to -7 range and I’m getting things like 3 to - 1).

Could I please ask for a code example with my definitions from the original post, I’ve been stuck with this issue for a few days.

Thanks.