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
GiacomoB
Newbie


Joined: 20 Mar 2019
Posts: 1
Location: Italy

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

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
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 Keeping camera fixed alasca General 3 Fri May 31, 2019 10:08 am View latest post
No new posts adding object models in osgEarth best... Db80 General 10 Tue Apr 30, 2019 6:12 pm View latest post
No new posts AddSlave & removeSlave camera see... bclay General 2 Wed Apr 10, 2019 6:52 pm View latest post
No new posts FPS camera Ligvest General 0 Mon Apr 01, 2019 1:03 pm View latest post
No new posts First Person Camera disable inertial ... Ligvest General 3 Thu Mar 28, 2019 1:01 pm 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