Jump to content
  • Advertisement
Sign in to follow this  

move objects with camera

This topic is 4946 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

Hi guys, This is a simple problem but for some reason, I seem to be having difficulties getting it to work right. I select an object in my scene and I want to move that object with the camera. Movement is fine, but when I rotate the camera, I can't seem to get the object to rotate properly. For example, if the selected object is in the center of my viewport, any camera movement or rotation should also transform the object such that it remains in the center of the viewport. (doesn't have to be the center, just an example). In other words, the object I've selected is attached to the camera. I can't seem to get the proper transformations working. Any help would be appreciated.

Share this post

Link to post
Share on other sites
How do you apply the transformation matrices? What order? Because I think that the order of the transformations has to be done in the right order. Perhaps you have to check when you apply the camera rotation to the selected object...

Share this post

Link to post
Share on other sites
Well the object needs to move with the camera. If I can get it working, its a nice and fairly intuitive way to place objects within your scene. (This is a level editor application).

If you want, you can see the current state of the level editor here.

Share this post

Link to post
Share on other sites
I'm assuming you have a representation for "orientation" and one for "position" in your engine. Let's assume "orientation" is a quaternion, and "position" is a vector. ("orientation" could also be a 3x3 matrix, or euler angles, doens't matter)

Let's also assume you can extract the basis vectors of your orientation; typically, in a right-handed coordinate system, these will be right, up and backwards; in a left-handed coordinate system, these will be right, up and forwards. You can also pluck these out of your rotation matrix, as they will be your basis vectors -- although exactly which elements they are depends on whether you're row-major or column-major, and whether you're row-vector-on-left or column-vector-on-right.

Now, the orientation of the object should be the same as the orientation of the camera. The position of the object should be the position of the camera, plus some scale factor times the front vector of the camera (if you only have the back vector, make the scale factor be negative).

When you render an object, you take the position of the object, and you subtract the position of the camera. Then you take the orientation of the object, and you subtract the orientation of the camera. Then you bake the orientation you get that way into a rotation matrix, and then you translate by the position you got from the subtraction. Render object; done! Repeat with next object.

Share this post

Link to post
Share on other sites
Well, I have tried methods similar to that with out success. Perhaps you would not mind posting some pseudocode?

Here is basically what I do now.

Vec3 oldViewDir,newViewDir,oldPos;
Vec3 transDelta;
Quat rotQuat;

oldViewDir = getCamera()->getViewDir();
oldPos = getCamera()->getPosition();


newViewDir = getCamera()->getViewDir();

//Build a quaternion that rotates from the
//old view direction to the new view direction.
transDelta = getCamera()->getPosition() - oldCamPos;

//For each object in the selection...
Vec3 currTrans = currObject->getTranslation();

//Translate the object's position so that
//it is rotated about the camera's position.
currTrans -= getCamera()->getPosition();

//Orbit the object about the camera.
currTrans = rotQuat * currTrans;

//Put the object back where it was plus the
currTrans += getCamera()->getPosition();

So that works fine in that it causes the object selection to orbit the camera. (Although it does some kinda weird things). But the problem is that the object's rotation is not correct, just the position.

Any more advice would be great.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!