Jump to content
  • Advertisement
Sign in to follow this  
TheKrane

OpenGL Rotate an object around a point

This topic is 979 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm currently writing a game using OpenGL and C++.

Now I want to rotate a child object around it's parent object. This is what I've got so far:

glm::vec3 direction = originPosition - m_position;

m_position += direction;
m_rotation *= glm::quat(glm::radians(delta));
m_position -= direction * glm::inverse(glm::quat(glm::radians(delta)));

I noticed that the child object is rotating on global axes. How can I rotate the child object on the local axes of the parent object?

 

Thanks in advance,

TheKrane

Edited by TheKrane

Share this post


Link to post
Share on other sites
Advertisement

I explicitly set the origin position to the parent objects position.

I just need to find a way to rotate the object around the local axis of the parent instead of the global axis.

Any suggestions?

Share this post


Link to post
Share on other sites

Can you tell us what originPosition is? Is it the actual origin (0, 0, 0) or where this object is on the parent?

 

Rotations always occur around 0,0,0, if you are doing what I think you are doing then you won't get what you expect. This also largely depends how you are doing your rendering and your actual object hierarchy. Ideally each object should be defined so that it's own rotation/anchor point is at 0, 0, 0. when rendering you then rotate each object and move it to it's world position. If you have multiply layers of hierarchy (which it sounds like you do) then you also need to apply the parents transforms. I think it will save you a bit of work later on if you set it up like that.

 

With what you have now you first need to transform into local space, you need to take into account not only the object's position relative to the parent but also the parent's position relative to the world. If the parent can also rotate then you are in for some pain :P. Is this going to be something you will use a lot? Moving objects with children? If so you should think about approaching it differently. If this is a one off you might be ok to do it as you are trying now.

 

-offsetOnParent

-parentPosition

rotate

+parentPosition

+offsetOnParent

Share this post


Link to post
Share on other sites

OriginPosition is the position of the parent object, which is not at (0,0,0), in world space. I'm using this as my origin since I want to rotate the child object around the origin of the parent object.

If I set originPosition to (0,0,0) the object rotates around the origin of the world.

 

I already got the child object to rotate around the parent object, the only problem is, that it's rotating around the global axis.

For example: If I rotate the parent object 90 degrees on the y axis (0,1,0), the child object is facing the right direction, but it is also rotating on the global y axis (0,1,0) and not on the parent objects y axis, which differs depending on the parent objects rotation.

 

 

 


If the parent can also rotate then you are in for some pain .

Now that's exactly my problem. I bascially want the child object to stick to the parent object. So all I'm doing in my logic code is rotating the parent. Now I want to rotate and position the child object accordingly.

Edited by TheKrane

Share this post


Link to post
Share on other sites

for local rotation i think you'll want to use the formula for "rotation about an arbitrary axis" where the axis of rotation is the local forward, up, or, right vector of the parent.

Share this post


Link to post
Share on other sites

Thanks a lot Norman!
I finally figured out a way to do it using glm::angleAxis and the parents forward, up and right vectors.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!