1
votes

I'm modelling Newtons Cradle and I am having trouble with wires/strings rotation as they currently rotate around their centre instead of where they are attached to the frame. There seem to be very little info online apart from " Translate to rotation point, rotate, translate back" which does not seem to work.

std::stack<glm::mat4>model; 
model.push(glm::mat4(1.0f));
model.top() = glm::translate(model.top(), glm::vec3(x, y, z));

model.top() = glm::rotate(model.top(), vx, glm::vec3(1, 0, 0)); //rotating in clockwise direction around x-axis
model.top() = glm::rotate(model.top(), vy, glm::vec3(0, 1, 0)); //rotating in clockwise direction around y-axis
model.top() = glm::rotate(model.top(), vz, glm::vec3(0, 0, 1)); //rotating in clockwise direction around z-axis
model.top() = glm::scale(model.top(), glm::vec3(scale/40, scale*5, scale/40));//scale equally in all axis
model.top() = glm::translate(model.top(), glm::vec3(-x, -y, -z));
glUniformMatrix4fv(modelID, 1, GL_FALSE, &model.top()[0][0]);

glm::mat3 normalmatrix = glm::transpose(glm::inverse(glm::mat3(View * model.top())));
glUniformMatrix3fv(normalmatrixID, 1, GL_FALSE, &normalmatrix[0][0])

This is the bit that translates and rotates or its current iteration, stack really does nothing at the moment. If my problem is unclear imagine analogue clock arm, it will rotate around the clock centre at its end and not by arms itself centre. I know there is no native implementation to rotate object around specific point in OpenGl

1

1 Answers

1
votes

As a side note, there is no longer any "native" implementation of any matrix stuff in OpenGL (outside of GLSL).

Your problem here is that you're only setting the top() matrix to a value instead of accumulating the transformations by multiplying them together.

You will probably also later notice a problem where it doesn't move back into the same spot (after making it multiply the matrices together). This is because you're scaling the object down a lot before translating back to the original spot. The order of your transformations matter.

A tutorial like this one that provides an in-depth explanation of all of this stuff will likely be very useful to you:

http://www.opengl-tutorial.org/beginners-tutorials/tutorial-3-matrices/