[SOLVED] distance camera - intersection from ray
-
emmanuel1359
- Kobold
- Posts: 32
- Joined: Mon Feb 02, 2015 11:05 pm
[SOLVED] distance camera - intersection from ray
hello,
i would like to do an action if i am close from the object in front of me:
so the code would be:
Ray ray = OgreFramework::getSingletonPtr()->m_pCamera->getCameraToViewportRay(0.5, 0.5);
Vector3 intersection;
Real scale = mVolumeRoot->getChunkParameters()->scale;
bool intersects = mVolumeRoot->getChunkParameters()->src->getFirstRayIntersection(ray, intersection, scale);
if (intersects and intersection.distance(OgreFramework::getSingletonPtr()->m_pCamera->getDerivedPosition()) < 10)
but this never raises
i'm supposed to be at the same position that the camera, and the point of intersection is "intersection"
must I substract the vectors ?
i would like to do an action if i am close from the object in front of me:
so the code would be:
Ray ray = OgreFramework::getSingletonPtr()->m_pCamera->getCameraToViewportRay(0.5, 0.5);
Vector3 intersection;
Real scale = mVolumeRoot->getChunkParameters()->scale;
bool intersects = mVolumeRoot->getChunkParameters()->src->getFirstRayIntersection(ray, intersection, scale);
if (intersects and intersection.distance(OgreFramework::getSingletonPtr()->m_pCamera->getDerivedPosition()) < 10)
but this never raises
i'm supposed to be at the same position that the camera, and the point of intersection is "intersection"
must I substract the vectors ?
Last edited by emmanuel1359 on Sat Mar 07, 2015 5:53 pm, edited 1 time in total.
- areay
- Bugbear
- Posts: 819
- Joined: Wed May 05, 2010 4:59 am
- Location: Auckland, NZ
- x 69
Re: distance camera - intersection from ray
What has your debugging turned up?
Is 'bool intersects' being raised at all?
If so, what is the value of the distance?
Is 'bool intersects' being raised at all?
If so, what is the value of the distance?
-
emmanuel1359
- Kobold
- Posts: 32
- Joined: Mon Feb 02, 2015 11:05 pm
Re: distance camera - intersection from ray
there the code added to log values:
And there the log:
thanks !
Code: Select all
**************************************************
Vector3 intersection;
Real scale = mVolumeRoot->getChunkParameters()->scale;
bool intersects = mVolumeRoot->getChunkParameters()->src->getFirstRayIntersection(ray, intersection, scale);
OgreFramework::getSingletonPtr()->m_pLog->logMessage(String("m_pCamera->getDerivedPosition = ")+Ogre::StringConverter::toString(OgreFramework::getSingletonPtr()->m_pCamera->getDerivedPosition()));
OgreFramework::getSingletonPtr()->m_pLog->logMessage(String("intersection ")+Ogre::StringConverter::toString(intersection ));
OgreFramework::getSingletonPtr()->m_pLog->logMessage(String("diff ")+Ogre::StringConverter::toString(intersection - OgreFramework::getSingletonPtr()->m_pCamera->getDerivedPosition()));
OgreFramework::getSingletonPtr()->m_pLog->logMessage(String("distance diff ")+Ogre::StringConverter::toString(intersection.distance(OgreFramework::getSingletonPtr()->m_pCamera->getDerivedPosition())));
OgreFramework::getSingletonPtr()->m_pLog->logMessage(String("intersects ")+Ogre::StringConverter::toString(intersects));
**************************************************Code: Select all
**************************************************
20:43:13: m_pCamera->getDerivedPosition = 2808.64 2488.75 3383.8
20:43:13: intersection 280.514 217.737 292.698
20:43:13: diff -2528.13 -2271.01 -3091.1
20:43:13: distance diff 4593.89
20:43:13: intersects true
20:43:15: m_pCamera->getDerivedPosition = 2809.94 2336.77 3160.65
20:43:15: intersection 282.222 218.113 292.525
20:43:15: diff -2527.72 -2118.66 -2868.12
20:43:15: distance diff 4370.83
20:43:15: intersects true
20:43:16: m_pCamera->getDerivedPosition = 2818.75 2238.82 3011.08
20:43:16: intersection 282.407 218.226 292.419
20:43:16: diff -2536.35 -2020.6 -2718.66
20:43:16: distance diff 4231.67
20:43:16: intersects true
20:43:21: m_pCamera->getDerivedPosition = 2823.99 2185.82 2928.82
20:43:21: intersection 282.433 218.266 292.385
20:43:21: diff -2541.56 -1967.56 -2636.43
20:43:21: distance diff 4157.11
20:43:21: intersects true
20:43:23: Main loop quit
20:43:23: Shutdown OGRE...
**************************************************
-
emmanuel1359
- Kobold
- Posts: 32
- Joined: Mon Feb 02, 2015 11:05 pm
Re: distance camera - intersection from ray
i tried to change
getDerivedPosition
to
getRealPosition
what is the difference ?
because I wonder if there is a problem with local and world coordinate
getDerivedPosition
to
getRealPosition
what is the difference ?
because I wonder if there is a problem with local and world coordinate
-
emmanuel1359
- Kobold
- Posts: 32
- Joined: Mon Feb 02, 2015 11:05 pm
Re: distance camera - intersection from ray
well, that's better, dividing the camera by the scale factor:
Code: Select all
OgreFramework::getSingletonPtr()->m_pLog->logMessage(String("m_pCamera->getPosition = ")+Ogre::StringConverter::toString(OgreFramework::getSingletonPtr()->m_pCamera->getPosition() / scale));
OgreFramework::getSingletonPtr()->m_pLog->logMessage(String("intersection ")+Ogre::StringConverter::toString(intersection ));
OgreFramework::getSingletonPtr()->m_pLog->logMessage(String("diff ")+Ogre::StringConverter::toString(intersection - OgreFramework::getSingletonPtr()->m_pCamera->getPosition() / scale));
OgreFramework::getSingletonPtr()->m_pLog->logMessage(String("distance diff ")+Ogre::StringConverter::toString(intersection.distance(OgreFramework::getSingletonPtr()->m_pCamera->getPosition() / scale)));
OgreFramework::getSingletonPtr()->m_pLog->logMessage(String("intersects ")+Ogre::StringConverter::toString(intersects));Code: Select all
1:24:49: m_pCamera->getPosition = 326.4 270 326.4
21:24:49: intersection 281.27 204.298 158.838
21:24:49: diff -45.1301 -65.7016 -167.562
21:24:49: distance diff 185.554
21:24:49: intersects true
21:24:50: m_pCamera->getPosition = 326.4 270 326.4
21:24:50: intersection 299.523 193.809 182.411
21:24:50: diff -26.8768 -76.1915 -143.989
21:24:50: distance diff 165.107
21:24:50: intersects true
21:24:51: m_pCamera->getPosition = 326.4 270 326.4
21:24:51: intersection 308.408 193.623 203.585
21:24:51: diff -17.992 -76.3771 -122.815
21:24:51: distance diff 145.742
21:24:51: intersects true
21:24:51: m_pCamera->getPosition = 326.4 270 326.4
21:24:51: intersection 312.502 194.412 216.329
21:24:51: diff -13.8981 -75.5878 -110.071
21:24:51: distance diff 134.247
21:24:51: intersects true
21:24:53: m_pCamera->getPosition = 312.516 284.698 346.429
21:24:53: intersection 294.24 221.458 292.887
21:24:53: diff -18.2758 -63.2403 -53.5429
21:24:53: distance diff 84.8539
21:24:53: intersects true
21:24:55: m_pCamera->getPosition = 304.817 264.647 330.405
21:24:55: intersection 285.141 218.285 292.559
21:24:55: diff -19.6761 -46.362 -37.8459
21:24:55: distance diff 62.9992
21:24:55: intersects true
21:24:55: m_pCamera->getPosition = 296.422 246.286 315.504
21:24:55: intersection 281.83 218.192 292.44
21:24:55: diff -14.5921 -28.0942 -23.0644
21:24:55: distance diff 39.1686
21:24:55: intersects true
21:24:57: m_pCamera->getPosition = 280.143 221.736 293.224
21:24:57: intersection 278.369 219.548 290.939
21:24:57: diff -1.77374 -2.18793 -2.28525
21:24:58: distance diff 3.62706
21:24:58: intersects true
21:25:01: m_pCamera->getPosition = 275.251 219.672 290.652
21:25:01: intersection 275.209 219.654 290.625
21:25:01: diff -0.0411682 -0.0187225 -0.0271912
21:25:01: distance diff 0.0527704
21:25:01: intersects true-
scrawl
- OGRE Expert User

- Posts: 1119
- Joined: Sat Jan 01, 2011 7:57 pm
- x 220
Re: distance camera - intersection from ray
See the API docs:i tried to change
getDerivedPosition
to
getRealPosition
what is the difference ?
So the only difference is when a reflection matrix is concerned, e.g. using the camera for a plane reflection, which I assume you're not doing,const Vector3& Ogre::Camera::getRealPosition ( void ) const
Gets the real world position of the camera, including any translation inherited from a node attachment.
const Vector3& Ogre::Camera::getDerivedPosition ( void ) const
Gets the derived position of the camera, including any translation inherited from a node attachment and reflection matrix.
- spacegaier
- OGRE Team Member

- Posts: 4308
- Joined: Mon Feb 04, 2008 2:02 pm
- Location: Germany
- x 137
- Contact:
Re: distance camera - intersection from ray
@emmanuel1359: Please use the "
Code: Select all
" tags for actual coding or long log output lists. Thank you.Ogre Admin [Admin, Dev, PR, Finance, Wiki, etc.] | BasicOgreFramework | AdvancedOgreFramework
Don't know what to do in your spare time? Help the Ogre wiki grow! Or squash a bug...
Don't know what to do in your spare time? Help the Ogre wiki grow! Or squash a bug...
-
emmanuel1359
- Kobold
- Posts: 32
- Joined: Mon Feb 02, 2015 11:05 pm
Re: distance camera - intersection from ray
ok, i'll use it !
thanks spacegaier
EDIT: the better way is:
thanks spacegaier
EDIT: the better way is:
Code: Select all
Vector3 intersection;
Real scale = mVolumeRoot->getChunkParameters()->scale;
bool intersects = mVolumeRoot->getChunkParameters()->src->getFirstRayIntersection(ray, intersection, scale);
if (intersects and OgreFramework::getSingletonPtr()->m_pCamera->getPosition().distance(intersection * scale) < 30)
{
}
- areay
- Bugbear
- Posts: 819
- Joined: Wed May 05, 2010 4:59 am
- Location: Auckland, NZ
- x 69
Re: distance camera - intersection from ray
So you've solved it?
-
emmanuel1359
- Kobold
- Posts: 32
- Joined: Mon Feb 02, 2015 11:05 pm
Re: distance camera - intersection from ray
yes, how to add [solved] in the main title ?
- spacegaier
- OGRE Team Member

- Posts: 4308
- Joined: Mon Feb 04, 2008 2:02 pm
- Location: Germany
- x 137
- Contact:
Re: distance camera - intersection from ray
By editing your first post in the thread. This gives you the option to change the title for the whole thread.emmanuel1359 wrote:yes, how to add [solved] in the main title ?
Ogre Admin [Admin, Dev, PR, Finance, Wiki, etc.] | BasicOgreFramework | AdvancedOgreFramework
Don't know what to do in your spare time? Help the Ogre wiki grow! Or squash a bug...
Don't know what to do in your spare time? Help the Ogre wiki grow! Or squash a bug...