0
\$\begingroup\$

I'm trying to rotate an object around a certain point. If this point is the center of my world space I use the following algorithm:

glm::vec3 center is the center of the current object. Each frame it will get updated.

glm::mat4 transformationMatrix is the 4x4 matrix I am "uploading" to my shader later on. It will get multiplied by the vertex position.

t_point is the point I'd like to rotate around

t_deflection is the impact for each axis (in order to draw ellipses instead of circles)

t_speed is in my case the current time since the game started (e.g. glfwGetTime())

void rotateByPointY( const glm::vec3& t_point, const glm::vec3& t_deflection, const float& t_speed )
{
    float radius = glm::length( t_point - center );

    glm::vec3 newPosition( glm::sin( t_speed ) * radius * t_deflection.x, center.y, glm::cos( t_speed ) * radius * t_deflection.z );

    translationMatrix = glm::translate( glm::mat4( 1.0f ), newPosition );
    center = newPosition;
}

How can I take into account a point other than the center of my world in the calculation?

EDIT:

So my rotateByPointY calculates the translation around the world center. Therefore I am adding the new center vector to my new position:

translationMatrix = glm::translate( glm::mat4( 1.0f ), newPosition + t_point );
center = newPosition + t_point;

That is working now!

\$\endgroup\$
  • 1
    \$\begingroup\$ Have you heard the joke about the blue elephant gun? Try reducing the problem to one you already know how to solve. If you know how to rotate around zero, see if you can take the point you want to rotate around and shift it to zero, then shift the result back. \$\endgroup\$ – DMGregory Feb 28 '18 at 16:47
  • \$\begingroup\$ I did not know the joke but it is a good one! \$\endgroup\$ – linux_lover Feb 28 '18 at 17:37
2
\$\begingroup\$

You first need to translate the objects, so that the center point falls on (0,0,0), this basically make everything centered around that. Then you can just rotate the object and then translate everything back.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.