OpenSceneGraph Forum Forum Index OpenSceneGraph Forum
Official forum which mirrors the existent OSG mailing lists. Messages posted here are forwarded to the mailing list and vice versa.
   FAQFAQ    SearchSearch    MemberlistMemberlist    RulesRules    UsergroupsUsergroups    RegisterRegister 
 Mail2Forum SettingsMail2Forum Settings  ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
   AlbumAlbum  OpenSceneGraph IRC ChatOpenSceneGraph IRC Chat   SmartFeedSmartFeed 

Make an object linked to camera moovement

Post new topic   Reply to topic    OpenSceneGraph Forum Forum Index -> General
View previous topic :: View next topic  
Author Message

Joined: 20 Mar 2019
Posts: 2
Location: Italy

PostPosted: Wed Mar 20, 2019 9:45 am    Post subject:
Make an object linked to camera moovement
Reply with quote

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:
osg::Matrix objectMatrix;
osg::Matrix cameraInv=myCamera->getInverseViewMatrix();
osg::Matrix newObjectMatrix=objectMatrix*cameraInv;

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

Second approach:

//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;

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!

Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OpenSceneGraph Forum Forum Index -> General All times are GMT
Page 1 of 1

Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum

Similar Topics
Topic Author Forum Replies Posted
No new posts Wrapping Text around object as texture johansson_gbg General 0 Tue Oct 15, 2019 7:45 am View latest post
No new posts Projection matrix with camera looking... AlexSavo General 0 Thu Jul 25, 2019 2:34 am View latest post
No new posts osgVIEWER problem camera skehal General 1 Tue Jul 23, 2019 9:17 am View latest post
No new posts How can we make 'osg' thread and 'ui'... mirro General 1 Mon Jul 01, 2019 2:36 am View latest post
No new posts Keeping camera fixed alasca General 3 Fri May 31, 2019 10:08 am View latest post

Board Security Anti Bot Question MOD - phpBB MOD against Spam Bots
Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP