Skip to content

Commit 9e15e30

Browse files
committed
Test selected body against all other bodies
1 parent db3499d commit 9e15e30

File tree

1 file changed

+74
-30
lines changed

1 file changed

+74
-30
lines changed

demos/rlCollisionDemo/MainWindow.cpp

+74-30
Original file line numberDiff line numberDiff line change
@@ -392,20 +392,44 @@ MainWindow::parseCommandLine()
392392
void
393393
MainWindow::test()
394394
{
395+
this->distanceCoordinate->point.setNum(0);
396+
this->distanceLineSet->coordIndex.setNum(0);
397+
this->distanceCoordinate->point.setNum(0);
398+
399+
this->depthCoordinate->point.setNum(0);
400+
this->depthLineSet->coordIndex.setNum(0);
401+
this->depthCoordinate->point.setNum(0);
402+
403+
rl::sg::Body* body = this->collisionScene->getModel(this->model)->getBody(this->body);
404+
395405
if (rl::sg::SimpleScene* simpleScene = dynamic_cast<rl::sg::SimpleScene*>(this->collisionScene.get()))
396406
{
397-
bool test = simpleScene->isColliding();
407+
std::size_t collisions = 0;
408+
409+
for (rl::sg::Scene::Iterator i = this->collisionScene->begin(); i != this->collisionScene->end(); ++i)
410+
{
411+
for (rl::sg::Model::Iterator j = (*i)->begin(); j != (*i)->end(); ++j)
412+
{
413+
if (body != *j)
414+
{
415+
if (simpleScene->areColliding(body, *j))
416+
{
417+
++collisions;
418+
}
419+
}
420+
}
421+
}
422+
423+
this->simpleLabel->setText("Collisions: " + QString::number(collisions));
398424

399-
if (test)
425+
if (collisions > 0)
400426
{
401-
this->simpleLabel->setText("Collision: true");
402427
this->viewer->setBackgroundColor(SbColor(0.5, 0, 0));
403428
this->gradientBackground->color0.setValue(0.5f, 0.0f, 0.0f);
404429
this->gradientBackground->color1.setValue(1.0f, 1.0f, 1.0f);
405430
}
406431
else
407432
{
408-
this->simpleLabel->setText("Collision: false");
409433
this->viewer->setBackgroundColor(SbColor(0, 0, 0));
410434
this->gradientBackground->color0.setValue(0.8f, 0.8f, 0.8f);
411435
this->gradientBackground->color1.setValue(1.0f, 1.0f, 1.0f);
@@ -414,23 +438,33 @@ MainWindow::test()
414438

415439
if (rl::sg::DistanceScene* distanceScene = dynamic_cast<rl::sg::DistanceScene*>(this->collisionScene.get()))
416440
{
417-
this->distanceCoordinate->point.setNum(0);
418-
this->distanceLineSet->coordIndex.setNum(0);
419-
this->distanceCoordinate->point.setNum(0);
441+
rl::math::Vector3 point1 = rl::math::Vector3::Zero();
442+
rl::math::Vector3 point2 = rl::math::Vector3::Zero();
443+
rl::math::Real distance = std::numeric_limits<rl::math::Real>::max();
420444

421-
rl::math::Vector3 point1;
422-
rl::math::Vector3 point2;
423-
424-
rl::math::Real distance = distanceScene->distance(
425-
this->collisionScene->getModel(0)->getBody(0),
426-
this->collisionScene->getModel(1)->getBody(0),
427-
point1,
428-
point2
429-
);
445+
for (rl::sg::Scene::Iterator i = this->collisionScene->begin(); i != this->collisionScene->end(); ++i)
446+
{
447+
for (rl::sg::Model::Iterator j = (*i)->begin(); j != (*i)->end(); ++j)
448+
{
449+
if (body != *j)
450+
{
451+
rl::math::Vector3 tmpPoint1;
452+
rl::math::Vector3 tmpPoint2;
453+
rl::math::Real tmpDistance = distanceScene->distance(body, *j, tmpPoint1, tmpPoint2);
454+
455+
if (tmpDistance < distance)
456+
{
457+
distance = tmpDistance;
458+
point1 = tmpPoint1;
459+
point2 = tmpPoint2;
460+
}
461+
}
462+
}
463+
}
430464

431465
this->distanceLabel->setText("Distance: " + QString::number(distance));
432466

433-
if (distance > std::numeric_limits<rl::math::Real>::epsilon())
467+
if (distance > 0 && distance < std::numeric_limits<rl::math::Real>::max())
434468
{
435469
this->distanceCoordinate->point.set1Value(
436470
this->distanceCoordinate->point.getNum(),
@@ -462,23 +496,33 @@ MainWindow::test()
462496

463497
if (rl::sg::DepthScene* depthScene = dynamic_cast<rl::sg::DepthScene*>(this->collisionScene.get()))
464498
{
465-
this->depthCoordinate->point.setNum(0);
466-
this->depthLineSet->coordIndex.setNum(0);
467-
this->depthCoordinate->point.setNum(0);
499+
rl::math::Vector3 point1 = rl::math::Vector3::Zero();
500+
rl::math::Vector3 point2 = rl::math::Vector3::Zero();
501+
rl::math::Real depth = 0;
468502

469-
rl::math::Vector3 point1;
470-
rl::math::Vector3 point2;
471-
472-
rl::math::Real depth = depthScene->depth(
473-
this->collisionScene->getModel(0)->getBody(0),
474-
this->collisionScene->getModel(1)->getBody(0),
475-
point1,
476-
point2
477-
);
503+
for (rl::sg::Scene::Iterator i = this->collisionScene->begin(); i != this->collisionScene->end(); ++i)
504+
{
505+
for (rl::sg::Model::Iterator j = (*i)->begin(); j != (*i)->end(); ++j)
506+
{
507+
if (body != *j)
508+
{
509+
rl::math::Vector3 tmpPoint1;
510+
rl::math::Vector3 tmpPoint2;
511+
rl::math::Real tmpDepth = depthScene->depth(body, *j, tmpPoint1, tmpPoint2);
512+
513+
if (tmpDepth > depth)
514+
{
515+
depth = tmpDepth;
516+
point1 = tmpPoint1;
517+
point2 = tmpPoint2;
518+
}
519+
}
520+
}
521+
}
478522

479523
this->depthLabel->setText("Depth: " + QString::number(depth));
480524

481-
if (depth > std::numeric_limits<rl::math::Real>::epsilon())
525+
if (depth > 0)
482526
{
483527
this->depthCoordinate->point.set1Value(
484528
this->depthCoordinate->point.getNum(),

0 commit comments

Comments
 (0)