eratic-magician

03-12-2017, 01:16 PM

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.

// 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.