diff options
Diffstat (limited to 'src/ode/joint.c')
-rw-r--r-- | src/ode/joint.c | 84 |
1 files changed, 52 insertions, 32 deletions
diff --git a/src/ode/joint.c b/src/ode/joint.c index 6fa79b4..7ff4c22 100644 --- a/src/ode/joint.c +++ b/src/ode/joint.c @@ -56,7 +56,7 @@ static int get_field(lua_State *L, int tbl, const char *key, int type) { lua_getfield(L, tbl, key); int actual_type = lua_type(L, -1); - if (actual_type != type) { + if (actual_type != type && actual_type != LUA_TNIL) { luaL_error(L, "field \"%s\" must have type %s but is %s instead", key, lua_typename(L, type), lua_typename(L, actual_type) @@ -67,49 +67,49 @@ static int get_field(lua_State *L, int tbl, const char *key, int type) static int parse_surface_params(lua_State *L, struct dSurfaceParameters *surface, int tbl) { get_field(L, tbl, "mode", LUA_TNUMBER); - surface->mode = lua_tointeger(L, -1); + if (!lua_isnil(L, -1)) surface->mode = lua_tointeger(L, -1); get_field(L, tbl, "mu", LUA_TNUMBER); - surface->mu = lua_tointeger(L, -1); + if (!lua_isnil(L, -1)) surface->mu = lua_tointeger(L, -1); get_field(L, tbl, "mu2", LUA_TNUMBER); - surface->mu2 = lua_tointeger(L, -1); + if (!lua_isnil(L, -1)) surface->mu2 = lua_tointeger(L, -1); get_field(L, tbl, "rho", LUA_TNUMBER); - surface->rho = lua_tointeger(L, -1); + if (!lua_isnil(L, -1)) surface->rho = lua_tointeger(L, -1); get_field(L, tbl, "rho2", LUA_TNUMBER); - surface->rho2 = lua_tointeger(L, -1); + if (!lua_isnil(L, -1)) surface->rho2 = lua_tointeger(L, -1); get_field(L, tbl, "rhoN", LUA_TNUMBER); - surface->rhoN = lua_tointeger(L, -1); + if (!lua_isnil(L, -1)) surface->rhoN = lua_tointeger(L, -1); get_field(L, tbl, "bounce", LUA_TNUMBER); - surface->bounce = lua_tointeger(L, -1); + if (!lua_isnil(L, -1)) surface->bounce = lua_tointeger(L, -1); get_field(L, tbl, "bounce_vel", LUA_TNUMBER); - surface->bounce_vel = lua_tointeger(L, -1); + if (!lua_isnil(L, -1)) surface->bounce_vel = lua_tointeger(L, -1); get_field(L, tbl, "soft_erp", LUA_TNUMBER); - surface->soft_erp = lua_tointeger(L, -1); + if (!lua_isnil(L, -1)) surface->soft_erp = lua_tointeger(L, -1); get_field(L, tbl, "soft_cfm", LUA_TNUMBER); - surface->soft_cfm = lua_tointeger(L, -1); + if (!lua_isnil(L, -1)) surface->soft_cfm = lua_tointeger(L, -1); get_field(L, tbl, "motion1", LUA_TNUMBER); - surface->motion1 = lua_tointeger(L, -1); + if (!lua_isnil(L, -1)) surface->motion1 = lua_tointeger(L, -1); get_field(L, tbl, "motion2", LUA_TNUMBER); - surface->motion2 = lua_tointeger(L, -1); + if (!lua_isnil(L, -1)) surface->motion2 = lua_tointeger(L, -1); get_field(L, tbl, "motionN", LUA_TNUMBER); - surface->motionN = lua_tointeger(L, -1); + if (!lua_isnil(L, -1)) surface->motionN = lua_tointeger(L, -1); get_field(L, tbl, "slip1", LUA_TNUMBER); - surface->slip1 = lua_tointeger(L, -1); + if (!lua_isnil(L, -1)) surface->slip1 = lua_tointeger(L, -1); get_field(L, tbl, "slip2", LUA_TNUMBER); - surface->slip2 = lua_tointeger(L, -1); + if (!lua_isnil(L, -1)) surface->slip2 = lua_tointeger(L, -1); lua_pop(L, 15); } @@ -121,30 +121,50 @@ static int parse_contact_tbl(lua_State *L, dContact *contact, int tbl) parse_surface_params(L, &(contact->surface), surface_tbl); lua_pop(L, 1); - get_field(L, tbl, "geom", LUA_TUSERDATA); - dContactGeom *g = lua_touserdata(L, -1); - contact->geom = *g; - lua_pop(L, 1); + //get_field(L, tbl, "geom", LUA_TUSERDATA); + //dContactGeom *g = lua_touserdata(L, -1); + //contact->geom = *g; + //lua_pop(L, 1); get_field(L, tbl, "fdir1", LUA_TTABLE); - int fdir_tbl = lua_gettop(L); - lua_rawgeti(L, fdir_tbl, 1); - contact->fdir1[0] = lua_tonumber(L, -1); - lua_rawgeti(L, fdir_tbl, 1); - contact->fdir1[1] = lua_tonumber(L, -1); - lua_rawgeti(L, fdir_tbl, 1); - contact->fdir1[2] = lua_tonumber(L, -1); - lua_pop(L, 4); + if (!lua_isnil(L, -1)) { + int fdir_tbl = lua_gettop(L); + lua_rawgeti(L, fdir_tbl, 1); + contact->fdir1[0] = lua_tonumber(L, -1); + lua_rawgeti(L, fdir_tbl, 1); + contact->fdir1[1] = lua_tonumber(L, -1); + lua_rawgeti(L, fdir_tbl, 1); + contact->fdir1[2] = lua_tonumber(L, -1); + lua_pop(L, 3); + } + lua_pop(L, 1); +} + + +int CreateContact_bind(lua_State *L) +{ + luaL_checktype(L, 1, LUA_TTABLE); + dContact *contact = lua_newuserdata(L, sizeof(dContact)); + parse_contact_tbl(L, contact, 1); + return 1; } + +int ContactSetGeom_bind(lua_State *L) +{ + dContact *contact = lua_touserdata(L, 1); + dContactGeom *geom = lua_touserdata(L, 2); + contact->geom = *geom; + return 0; +} + + int dJointCreateContact_bind(lua_State *L) { dWorldID *w = luaL_checkudata(L, 1, ode_world_tname); dJointGroupID *g = luaL_checkudata(L, 2, ode_jointgroup_tname); - luaL_checktype(L, 3, LUA_TTABLE); - dContact contact; - parse_contact_tbl(L, &contact, 3); - dJointID bind_result = dJointCreateContact(*w, *g, &contact); + dContact *contact = lua_touserdata(L, 3); + dJointID bind_result = dJointCreateContact(*w, *g, contact); ode_push_joint(L, bind_result); return 1; } |