C和x2B之间的交互作用+;和具有三维向量参数的Lua函数 我有C++函数(它在我的游戏引擎里面运行一些物理模拟),看起来像: void doSomePhysics( int nIters, Vec3d pos, Vec3d vel ){ /*... don't care ...*/ }

C和x2B之间的交互作用+;和具有三维向量参数的Lua函数 我有C++函数(它在我的游戏引擎里面运行一些物理模拟),看起来像: void doSomePhysics( int nIters, Vec3d pos, Vec3d vel ){ /*... don't care ...*/ },c++,interface,lua,linear-algebra,C++,Interface,Lua,Linear Algebra,我想从lua脚本调用此函数,如下所示: doSomePhysics( 100, {1.0,-2.0,3.0}, {-8.0,7.0,-6.0} ) 我试图弄清楚如何使lua接口与这个函数连接 我想做几个传递2D、3D和4D向量和矩阵的通用辅助函数,因为我会大量使用它们 下面是我目前尝试做的草图(但我知道这是不正确的): 我读了一些简短的教程,但它看起来非常复杂,混乱和容易出错。。。我完全迷失在堆栈索引中(我在堆栈中的当前位置是什么?正确的相对索引是什么?等等)。我不愿意相信最著名的游戏脚本

我想从lua脚本调用此函数,如下所示:

doSomePhysics( 100, {1.0,-2.0,3.0}, {-8.0,7.0,-6.0} ) 
我试图弄清楚如何使lua接口与这个函数连接

我想做几个传递2D、3D和4D向量和矩阵的通用辅助函数,因为我会大量使用它们

下面是我目前尝试做的草图(但我知道这是不正确的):


我读了一些简短的教程,但它看起来非常复杂,混乱和容易出错。。。我完全迷失在堆栈索引中(我在堆栈中的当前位置是什么?正确的相对索引是什么?等等)。我不愿意相信最著名的游戏脚本语言需要如此多的锅炉板代码和如此多的接口每一个小功能的痛苦


编辑:在Vlad的帮助下,我能够对3D向量和矩阵进行编辑

void lua_getVec3(lua_State *L, int idx, Vec3d& vec){
    // universal helper function to get Vec3 function argument from Lua to C++ function
    luaL_checktype(L, idx, LUA_TTABLE);
    lua_rawgeti(L, idx, 1); vec.x = lua_tonumber(L, -1); lua_pop(L, 1);
    lua_rawgeti(L, idx, 2); vec.y = lua_tonumber(L, -1); lua_pop(L, 1);
    lua_rawgeti(L, idx, 3); vec.z = lua_tonumber(L, -1); lua_pop(L, 1);
}

void lua_getMat3(lua_State *L, int idx, Mat3d& mat){
    // universal helper function to get Mat3 function argument from Lua to C++ function
    luaL_checktype(L, idx, LUA_TTABLE);
    lua_pushinteger(L, 1); lua_gettable(L, idx); lua_getVec3(L, -1, mat.a ); lua_pop(L, 1);
    lua_pushinteger(L, 2); lua_gettable(L, idx); lua_getVec3(L, -1, mat.b ); lua_pop(L, 1);
    lua_pushinteger(L, 3); lua_gettable(L, idx); lua_getVec3(L, -1, mat.c ); lua_pop(L, 1);
}

extern "C"
int l_doSomePhysics2(lua_State* L){
    // lua interface for doSomePhysics
    Vec3d pos;
    Mat3d mat;
    int n = lua_tointeger(L, 1);
    lua_getVec3(L, 2, pos );
    lua_getMat3(L, 3, mat );
    doSomePhysics2(n,pos,mat);
    //lua_pushnumber(L, 123);
    return 3;
}
适用于此lua功能:

mat = {{1.1,-0.1,0.1},{-0.2,2.2,0.2},{-0.3,0.3,3.3}}
doSomePhysics2( 100, {-7.7,8.8,9.9}, mat )

您的
void lua\u getVec3(lua\u State*L,Vec3d&vec)
缺少重要信息-lua堆栈上参数表的位置。将其改写为如下内容:

void lua_getVec3(lua_State *L, int idx, Vec3d& vec){
    // universal helper function to get Vec3 function argument from Lua to C++ function
    luaL_checktype(L, idx, LUA_TTABLE);
    lua_rawgeti(L, idx, 1); vec.x = lua_tonumber(L, -1);
    lua_rawgeti(L, idx, 2); vec.y = lua_tonumber(L, -1);
    lua_rawgeti(L, idx, 3); vec.z = lua_tonumber(L, -1);
    lua_pop(L, 3);
}
把它叫做:

int l_doSomePhysics(lua_State* L){
    // lua interface for doSomePhysics
    Vec3d pos,vel;
    int n = lua_tointeger(L, 1);
    lua_getVec3(L, 2, pos );
    lua_getVec3(L, 3, vel);
    doSomePhysics(n,pos,vel);
    lua_pushnumber(state, 123);
    return 1;
} 

一个lib技巧是sol2,单头解决方案,它以一种更简单的方式实现了您想要的功能。但使用某些库总是会带来一些问题(依赖地狱、C++14的要求、模板导致的编译时间更长、分发更复杂……)。所以我宁愿没有它的生活。太好了,它能工作。它也应该是
返回3对吗?安全等方面有什么遗漏吗?嗯,对不起,我没学到多少。。。我不知道如何将它推广到矩阵
{{{1.0,-0.1,0.1},{-0.2,2.0,0.2},{-0.3,0.3,3.0}
如果从函数返回3个值,则需要返回3
。到目前为止,它只是一个数字,因此
返回1
。矩阵只是一张表格
lua\u rawget()
从外部表中获取各个向量,使用
lua\u gettop()
获取检索到的向量的表位置,然后像以前一样调用
lua\u getVec3()
。谢谢。矩阵我自己已经计算出来了,并相应地编辑了我的问题。我忘了在这里写评论了。
int l_doSomePhysics(lua_State* L){
    // lua interface for doSomePhysics
    Vec3d pos,vel;
    int n = lua_tointeger(L, 1);
    lua_getVec3(L, 2, pos );
    lua_getVec3(L, 3, vel);
    doSomePhysics(n,pos,vel);
    lua_pushnumber(state, 123);
    return 1;
}