首页 > 解决方案 > 将 Bullet Physics 库连接到 MATLAB

问题描述

我正在尝试在 MATLAB 中使用 Bullet Physics 库。我需要进行碰撞检测,因此我想使用 GJK 算法。在 MATLAB 中,可以编译所谓的 MEX 函数。使用 MEX 函数,可以从 MATLAB 命令行调用您自己的 C 或 C++ 程序,就好像它们是内置函数一样。我的问题是 Bullet 库非常大,有很多文件、头函数等,我对编程不是很熟悉。我寻找更简单的实现并发现:

https://github.com/ElsevierSoftwareX/SOFTX_2018_38

它是 GJK 用 C 代码编写的,带有一个获得所需 MEX 功能的文件。该文件如下所示:

% CLEAR ALL VARIABLES
clearvars

% SELECT OPTIMISATION FLAG - FASTER BUT NOT SUITABLE FOR DEBUGGING
if 0
    optflug = '-g'; %#ok<*UNRCH>
else
    optflug = '-O';
end
% SELECT SILET COMPILATION MODE.
if 1 
    silflag = '-silent'; 
else
    silflag = '-v';
end

% TRY COMPILING MEX FILE
fprintf('Compiling mex function... ')
try
mex('../lib/src/openGJK.c',...  % Source of openGJK 
    '-largeArrayDims', ...      % Support large arrays
    optflug, ...                % Compiler flag for debug/optimisation
    '-I../lib/include',...      % Folder to header files
    '-outdir', pwd,...          % Ouput directory for writing mex function
    '-output', 'openGJK',...    % Name of ouput mex file
    '-DMATLABDOESMEXSTUFF',...  % Define variable for mex function in source files
    silflag )                   % Silent/verbose flag

    % File compiled without errors. Return path and name of mex file
    fprintf('completed!\n')
    fprintf('The following mex file has been generated:')
    fprintf('\t%s\n',[pwd,filesep,'openGJK.',mexext]) 
catch
    % Build failed, refer to documentation
    fprintf('\n\n ERROR DETECTED! Mex file cannot be compiled.\n')
    fprintf('\tFor more information, see ')
    fprintf('<a href="http://www.mathworks.com/help/matlab/ref/mex.html">this documentation page</a>.\n\n')
    return
end

所以我尝试用 Bullet 库做类似的事情,但我什至不知道该使用哪个源文件。我尝试了“src”文件夹中的“btBulletCollisionAll.cpp”。但是就像我说的那样,我对 C++ 并不是很熟悉,我想知道为什么只有很多 oof 头文件。我还需要告诉 MATLAB 头文件在哪里以及如何链接它们。

所以基本上我只想像这样在 MATLAB 上调用一个函数:

dist = GJK(shapeA,shapeB);

这是子弹存放处: https ://github.com/bulletphysics/bullet3

非常感谢你的帮助

补充:我不使用 SOFTX_2018_38 因为我还需要 EPA 算法,而且我认为 Bullet 库是高效且健壮的实现。

标签: matlabcollision-detectionbulletphysicsbullet

解决方案


据我所知, .mex 定义了一个 MATLAB 函数。您指向的库似乎是一个 python 库,所以我建议您查看 快速入门指南(88 页),在 python 中使用它,然后考虑移植到 MATLAB。

看来您可以直接在 MATLAB 中运行 python 库

关于 python,它是一个覆盖面非常广的主题,在这里不会尝试写任何其他内容,您可以通过快速搜索找到大量教程。


推荐阅读