将子弹物理库连接到 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 @R_980_4045@ion,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

非常感谢您的帮助

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

解决方法

据我所知,.mex 定义了一个 MATLAB 函数。你指出的库好像是python库,建议你复习一下quickstart guide(88页),在python中使用,再考虑移植到MATLAB。

看来你可以run python libraries directly in MATLAB

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