[osg-users] Make an object linked to camera moovement

Giacomo Bondi giacomobondizz7 at gmail.com
Wed Mar 20 02:45:38 PDT 2019


Hi,
i'm going to develop an example where a 3D Object follows camera in a rigid way. I mean, starting from 3d object position P0 (i.e. in the  upper left corner of viewport), when  i translate or rotate camera, this 3d Object still appears in the upper left corner, as thought 3d object is rigidically linked to camera with a transparent wire. Caused to some constraints, i would like  to link object to camera without addchild method, but using matrix/vector/quaternion transformation. 

The scene has a group GROUP which has PAT (position attitude Transform) children which has Model3D (as 3d object Node). myCamera is the camera


I have tried some approaches, but no ones gave the wanted results.

First approach:
...

Code:
osg::Matrix objectMatrix;
objectMatrix.setPosition(PAT->getPosition());
objectMatrix.setattitude(PAT->getrotate());
osg::Matrix cameraInv=myCamera->getInverseViewMatrix();
osg::Matrix newObjectMatrix=objectMatrix*cameraInv;
PAT->setPosition(newObjectMatrix.getTranslate());
PAT->setattitude(newObjectMatrix.getRotate());



After translation and/or camera rotation, Object doesn't located in the desired position.

Second approach:



Code:
//collect some initial values
//called once
void Init()
{
osg::Vec3 eye, center, up;
myCamera->getViewMatrixAsLookAt(eye, center, up);
osg::Vec3 objectPosition = PAT->getPosition();
//class attribute, osg::Vec3 
//vector between camera and object
mOffsetFromCamera = objectPosition - eye;
osg::Matrix cameraInv =myCamera->getInverseViewMatrix();
//class attribute
mCamRotOffset = cameraInv.getRotate();
}

// during simulation
// on each frame (update)
void Update()
{
osg::Vec3 eye, center, up;
osg::Matrix cameraInv=myCamera()->getInverseViewMatrix();
myCamera->getViewMatrixAsLookAt(eye, center, up);
osg::Quat deltaCamRot=cameraInv.getRotate()-mCamRotOffset;

//Rotate vector between camera and object
osg::Vec3 newOffsetDirection = deltaCamRot*mOffsetFromCamera;
osg::Vec3 newObjectPosition = eye + newOffsetDirection;
PAT->setPosition(newObjectPosition);
}



In this approach, camera translation keep 3D object in the desired position, but camera rotation doesn't.

Please, can you suggest me other approaches?
Is there an example which shows some useful tips?

Thank you!

Cheers,
Giacomo

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=75724#75724







More information about the osg-users mailing list