Sign in to follow this  
pammatt

move objects with camera

Recommended Posts

pammatt    211
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
cignox1    735
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
pammatt    211
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.
http://www.delta3d.org/forum/viewtopic.php?forum=12&showtopic=538&mode=&onlytopic=&show=10&page=2

Share this post


Link to post
Share on other sites
hplus0603    11347
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
pammatt    211
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();

moveCamera();

newViewDir = getCamera()->getViewDir();

//Build a quaternion that rotates from the
//old view direction to the new view direction.
rotQuat.makeRotate(oldViewDir,newViewDir);
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
//rotation.
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this