#include "CollisionTest.h"
CollisionTest::CollisionTest(QWidget *parent, Qt::WFlags flags)
: QWidget(parent, flags)
{
ui.setupUi(this);
SoQt::init(ui.widget);
////根节点
pRootSeparator = new SoSeparator;
pRootSeparator->ref();
pRootSeparator->setName("pRootSeparator");
////******构建场景
////pRootSeparator(根节点)
//// 0--pSon1Separator
//// 0--Transform
//// 1--Pendulum
//// 2--Separator (iv文件节点)
//// 1--pRootSphere
//// 0--Transform
//// 1--Material
//// 2--Sphere
//子节点1
SoSeparator *pSon1Separator = new SoSeparator;
pSon1Separator->setName("pSon1Separator");
SoPendulum *mySon1Pend = new SoPendulum;
pRootSeparator->addChild(pSon1Separator);
SoTransform *mySon1Tform=new SoTransform;
pSon1Separator->addChild(mySon1Tform);
mySon1Tform->translation.setValue(0.04f, 0.0, 0.00);
pSon1Separator->addChild(mySon1Pend);
mySon1Pend->rotation0.setValue(SbVec3f(0,0,1),0.0);
mySon1Pend->rotation1.setValue(SbVec3f(0,0,1),-1.0);
mySon1Pend->speed=0.2;
pSon1Separator->addChild(readIVFile("model.iv"));
///添加球节点根节点
SoSeparator *pRootSphere=new SoSeparator;
pRootSphere->setName("pSphereRoot");
pRootSeparator->addChild(pRootSphere);
///添加球节点位移
SoTransform *spheretran=new SoTransform;
spheretran->translation.setValue(0.04f,-0.02f,0.007f);
pRootSphere->addChild(spheretran);
///添加球节点材料
SoMaterial *spherecol = new SoMaterial;
pRootSphere->addChild(spherecol);
spherecol->diffuseColor.setValue(0.82f,0.82f,0.82f);
///添加球节点
SoSphere *mysphere1= new SoSphere;
mysphere1->setName("Sphere");
mysphere1->radius=0.01f;
pRootSphere->addChild(mysphere1);
/////******场景构建完毕
// 创建view窗体显示COIN三维内容
SoQtExaminerViewer *viewer = new SoQtExaminerViewer(ui.widget);
// 设置显示的根节点
viewer->setSceneGraph(pRootSeparator);
// view窗体显示三维内容
viewer->show();
// 显示物体全部查看模式
viewer->viewAll();
IDAction = new SoIntersectionDetectionAction;
//回调函数
IDAction->addIntersectionCallback(onIntersection, NULL);
//过滤函数
IDAction->setFilterCallback(filterCB,NULL);
//应用到根节点
IDAction->apply(pRootSeparator);
timer = new QTimer();
////设置碰撞检测标志信号槽
connect(ui.pushButton_Start,SIGNAL(clicked()),this,SLOT(startDetect()));
connect(ui.pushButton_Stop,SIGNAL(clicked()),this,SLOT(stopDetect()));
connect(timer,SIGNAL(timeout()),this,SLOT(start()));
}
///初始化标志
bool CollisionTest::b_detectEnabled = false;
CollisionTest::~CollisionTest()
{
}
void CollisionTest::stopDetect()
{
timer->stop();
this->b_detectEnabled = false;
}
void CollisionTest::startDetect()
{
b_detectEnabled = true;
timer->start();
}
void CollisionTest::start()
{
IDAction->apply(pRootSeparator);
}
SoSeparator* CollisionTest::readIVFile(QString filename)
{
char *fileName;
QByteArray ba = filename.toLatin1();
fileName = ba.data();
SoInput input;
// 加载iv文件并读取全部内容
if (input.openFile(fileName))
{
return (SoDB::readAll(&input));
}
}
SoIntersectionDetectionAction::Resp CollisionTest::onIntersection(void *userdata,
const SoIntersectingPrimitive *primitive1, const SoIntersectingPrimitive *primitive2)
{
// These two primitive have an intersection.
qDebug()<<"Intersection hit!/n";
SoPath* primitivePath1 = primitive1->path;
for(int i=0;i<primitivePath1->getLength();i++)
{
SoNode* tmpNode = primitivePath1->getNode(i);
QString tmpName = tmpNode->getName().getString();
QString tmpType = tmpNode->getTypeId().getName().getString();
//qDebug()<<i<<" Name:"<<tmpName<<"\tType:"<<tmpType;
}
SoPath* primitivePath2 = primitive2->path;
for(int i=0;i<primitivePath2->getLength();i++)
{
SoNode* tmpNode = primitivePath2->getNode(i);
QString tmpName = tmpNode->getName().getString();
QString tmpType = tmpNode->getTypeId().getName().getString();
//qDebug()<<i<<" Name:"<<tmpName<<"\tType:"<<tmpType;
}
// This callback will be called for the two next colliding shapes
return SoIntersectionDetectionAction::NEXT_SHAPE;
}
SbBool CollisionTest::filterCB(void * userdata, const SoPath * p1, const SoPath * p2)
{
qDebug()<<"filterCB";
return true;
}