中的代码无法编译通过,好在ubuntu仓库自带fcl
1.安装fcl
sudo apt-get install libfcl-dev
2.测试
-
cmake_minimum_required(VERSION 2.8)
-
project( fcl_test )
-
-
include(CheckCXXCompilerFlag)
-
CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
-
CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)
-
if(COMPILER_SUPPORTS_CXX11)
-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
-
elseif(COMPILER_SUPPORTS_CXX0X)
-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
-
else()
-
message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.")
-
endif()
-
-
include_directories(/usr/include)
-
link_directories(/usr/lib)
-
-
add_executable( fcl_test test_fcl_collision.cpp)
-
-
target_link_libraries( fcl_test fcl)
main函数框架
-
#include "fcl/collision.h"
-
#include "fcl/math/transform.h"
-
#include "fcl/math/vec_3f.h"
-
#include "fcl/config.h"
-
#include "fcl/BVH/BVH_model.h"
-
#include "fcl/broadphase/broadphase.h"
-
#include "fcl/collision.h"
-
#include "fcl/distance.h"
-
#include "fcl/continuous_collision.h"
-
#include "types.h"
-
#include <iostream>
-
using namespace fcl;
-
-
-
-
//==============================================================================
-
int main(int argc, char* argv[])
-
{
-
-
return 1;
-
}
三角网格模型
通过输入一系列的定点和三角网格,通过BVH模型可以测试
-
// set mesh triangles and vertice indices
-
std::vector<Vec3f> vertices;
-
std::vector<Triangle> triangles;
-
// code to set the vertices and triangles
-
-
// BVHModel is a template class for mesh geometry, for default OBBRSS template is used
-
typedef BVHModel<OBBRSS> Model;
-
Model* model = new Model();
-
// add the mesh data into the BVHModel structure
-
model->beginModel();
-
model->addSubModel(vertices, triangles);
-
model->endModel();
定义shape
-
// // R and T are the rotation matrix and translation vector
-
Matrix3f R;
-
R.setIdentity();
-
Vec3f T1(0,0,0);
-
Vec3f T2(2,0,1);
-
// code for setting R and T
-
-
// transform is configured according to R and T
-
// Transform3f pose(R, T);
-
-
-
std::shared_ptr<Box> box0(new Box(1,1,1));
-
std::shared_ptr<Box> box1(new Box(1,1,1));
-
-
std::shared_ptr<Cylinder> cylinder0(new Cylinder(1,1));
-
std::shared_ptr<Cylinder> cylinder1(new Cylinder(1,1));
-
// Given two objects o1 and o2
-
CollisionObject* o1=new CollisionObject(cylinder0,Transform3f(R,T1));
-
-
//o1->cgeom=box0;
-
CollisionObject* o2=new CollisionObject(cylinder1,Transform3f(R,T2));
测试是否碰撞
-
// set the collision request structure, here we just use the default setting
-
CollisionRequest request;
-
// result will be returned via the collision result structure
-
CollisionResult result;
-
// perform collision test
-
collide(o1, o2, request, result);
-
bool res=result.isCollision()?1:0;
-
if(res) std::cerr<<"y"<<std::endl;
-
else std::cerr<<"n"<<std::endl;
测量距离
-
set the distance request structure, here we just use the default setting
-
DistanceRequest request;
-
// result will be returned via the collision result structure
-
DistanceResult result;
-
// perform distance test
-
distance(o1, o2, request, result);
-
std::cerr<<result.min_distance<<std::endl;
-
std::cerr<<o1->getAABB().center()<<std::endl;
-
std::cerr<<o1->getAABB().min_<<std::endl;
-
std::cerr<<o1->getAABB().max_<<std::endl;
当两个物体按照一定的轨迹运动时,需要提前预测碰撞,可采用连续碰撞
-
// // The goal transforms for o1 and o2
-
Transform3f tf_goal_o1 ;
-
Transform3f tf_goal_o2 ;
-
// // set the continuous collision request structure, here we just use the default setting
-
ContinuousCollisionRequest request;
-
// // result will be returned via the continuous collision result structure
-
ContinuousCollisionResult result;
-
// // perform continuous collision test
-
continuousCollide(o1, tf_goal_o1, o2, tf_goal_o2, request, result);
-
-
std::cerr<<result.time_of_contact<<std::endl;
阅读(2895) | 评论(0) | 转发(0) |