Hi,
I just started to use the polytope and polytopeintersect and have some results
I did not expect.
In the code there are 3 cases that creates an osg::Box which is the cube I want
to intersect with the plane. The 3 cases have each a different solution, where
I expect them all to have the same solution which is 4 intersections. There is
a comment next to each osg::Box being created.. can someone explain to me why I
don't get 4 intersections in all cases? the only difference in each case is
that I create the box in a different location.
here is the code:
Code:
#include "stdafx.h"
#include <osg/group>
#include <osg/geometry>
#include <osgViewer/Viewer>
#include <osg/Polytope>
#include <osg/ShapeDrawable>
#include <osgUtil/PolytopeIntersector>
#include <osg/shape>
#include <iostream>
osg::Group* createScene()
{
osg::Group* root = new osg::Group;
// create group for objects we want to collide against
osg::Group* pGroup = new osg::Group;
root->addChild( pGroup );
osg::Geometry* pTest = osg::createTexturedQuadGeometry(
osg::Vec3(-2.0f,-2.0f,0.0),
osg::Vec3(4.0f,0.0f,0.0),
osg::Vec3(0.0f,4.0f,0.0f) );
osg::Geode* pGeodeTest = new osg::Geode;
pGeodeTest->addDrawable( pTest );
pGroup->addChild( pGeodeTest );
// create a cube
osg::Geode* pGeode = new osg::Geode;
//osg::ShapeDrawable* pShape = new osg::ShapeDrawable( new
osg::Box(osg::Vec3(0.0f,0.0f,0.0f),0.2f) ); // 4 intersections, but 2 of them
in the same place (not expected)
//osg::ShapeDrawable* pShape = new osg::ShapeDrawable( new
osg::Box(osg::Vec3(0.2f,0.0f,0.0f),0.2f) ); // only two intersections???? (not
expected)
osg::ShapeDrawable* pShape = new osg::ShapeDrawable( new
osg::Box(osg::Vec3(0.5f,0.0f,0.0f),0.2f) ); // 4 intersections (expected)
pShape->setColor( osg::Vec4( 0,1,0,1 ) );
pGeode->addDrawable( pShape );
root->addChild( pGeode );
osg::Polytope* p = new osg::Polytope;
p->setToBoundingBox( pGeode->getBoundingBox() );
osgUtil::PolytopeIntersector* intersector = new
osgUtil::PolytopeIntersector( osgUtil::Intersector::MODEL, *p );
osgUtil::IntersectionVisitor* iv = new osgUtil::IntersectionVisitor;
iv->setIntersector( intersector );
pGroup->accept( *iv );
if ( iv->getIntersector()->containsIntersections() )
{
std::cout << "COUNT: " <<
intersector->getIntersections().size() << std::endl;
std::cout << "FIRST COUNT: " <<
intersector->getFirstIntersection().numIntersectionPoints << std::endl;
osgUtil::PolytopeIntersector::Intersection firstIntersection =
intersector->getFirstIntersection();
for ( unsigned int i=0;
i<firstIntersection.numIntersectionPoints; i++ )
{
osg::Vec3 pos = firstIntersection.intersectionPoints[i];
std::cout << "POS " << i << ": " << pos.x() << ", " <<
pos.y() << ", " << pos.z() << std::endl;
std::cout << "max dist " <<
firstIntersection.maxDistance << std::endl;
std::cout << "distance " << firstIntersection.distance
<< std::endl;
osg::Geode* pGeode = new osg::Geode;
osg::ShapeDrawable* pTestSphere = new
osg::ShapeDrawable( new osg::Sphere(pos,0.02f) );
pTestSphere->setColor( osg::Vec4(1,0,0,1) );
pGeode->addDrawable( pTestSphere );
root->addChild( pGeode );
}
}
return root;
}
int _tmain(int argc, _TCHAR* argv[])
{
// construct the viewer
osg::ref_ptr<osgViewer::Viewer> rViewer = new osgViewer::Viewer;
rViewer->setUpViewInWindow( 32, 32, 800, 600 );
rViewer->setSceneData( createScene() );
return rViewer->run();
}
Thank you!
Cheers,
Peter
------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=43093#43093
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org