diff --git a/cocos/physics/CCPhysicsShape.h b/cocos/physics/CCPhysicsShape.h index 49398fbbe2..de3c924b2e 100644 --- a/cocos/physics/CCPhysicsShape.h +++ b/cocos/physics/CCPhysicsShape.h @@ -212,6 +212,7 @@ public: virtual float calculateDefaultMoment() override; void getPoints(Point* outPoints) const; + int getPointsCount() const { return 4; } Size getSize() const; virtual Point getOffset() override { return _offset; } diff --git a/cocos/scripting/lua/bindings/LuaBasicConversions.cpp b/cocos/scripting/lua/bindings/LuaBasicConversions.cpp index f5629705e6..4912c7541a 100644 --- a/cocos/scripting/lua/bindings/LuaBasicConversions.cpp +++ b/cocos/scripting/lua/bindings/LuaBasicConversions.cpp @@ -1539,6 +1539,19 @@ bool luaval_to_std_vector_int(lua_State* L, int lo, std::vector* ret) return ok; } +void points_to_luaval(lua_State* L,const Point* pt, int count) +{ + if (NULL == L) + return; + lua_newtable(L); + for (int i = 1; i <= count; ++i) + { + lua_pushnumber(L, i); + point_to_luaval(L, pt[i-1]); + lua_rawset(L, -3); + } +} + void point_to_luaval(lua_State* L,const Point& pt) { if (NULL == L) @@ -1568,6 +1581,56 @@ void physics_material_to_luaval(lua_State* L,const PhysicsMaterial& pm) lua_rawset(L, -3); /* table[key] = value, L: table */ } +void physics_raycastinfo_to_luaval(lua_State* L, const PhysicsRayCastInfo& info) +{ + if (NULL == L) + return; + + lua_newtable(L); /* L: table */ + + lua_pushstring(L, "shape"); /* L: table key */ + PhysicsShape* shape = info.shape; + if (shape == nullptr) + { + lua_pushnil(L); + }else + { + std::string hashName = typeid(*shape).name(); + auto iter = g_luaType.find(hashName); + std::string className = ""; + if(iter != g_luaType.end()){ + className = iter->second.c_str(); + } else { + className = "PhysicsShape"; + } + + int ID = (int)(shape->_ID); + int* luaID = &(shape->_luaID); + toluafix_pushusertype_ccobject(L, ID, luaID, (void*)shape,className.c_str()); + } + lua_rawset(L, -3); /* table[key] = value, L: table */ + + lua_pushstring(L, "start"); /* L: table key */ + point_to_luaval(L, info.start); + lua_rawset(L, -3); /* table[key] = value, L: table */ + + lua_pushstring(L, "end"); /* L: table key */ + point_to_luaval(L, info.end); + lua_rawset(L, -3); /* table[key] = value, L: table */ + + lua_pushstring(L, "contact"); /* L: table key */ + point_to_luaval(L, info.contact); + lua_rawset(L, -3); /* table[key] = value, L: table */ + + lua_pushstring(L, "normal"); /* L: table key */ + point_to_luaval(L, info.normal); + lua_rawset(L, -3); /* table[key] = value, L: table */ + + lua_pushstring(L, "fraction"); /* L: table key */ + lua_pushnumber(L, (lua_Number) info.fraction); /* L: table key value*/ + lua_rawset(L, -3); /* table[key] = value, L: table */ +} + void size_to_luaval(lua_State* L,const Size& sz) { if (NULL == L) diff --git a/cocos/scripting/lua/bindings/LuaBasicConversions.h b/cocos/scripting/lua/bindings/LuaBasicConversions.h index 5eb00223fd..9636e5ff06 100644 --- a/cocos/scripting/lua/bindings/LuaBasicConversions.h +++ b/cocos/scripting/lua/bindings/LuaBasicConversions.h @@ -173,12 +173,14 @@ extern bool luaval_to_ccvaluevector(lua_State* L, int lo, cocos2d::ValueVector* // from native extern void point_to_luaval(lua_State* L,const Point& pt); +extern void points_to_luaval(lua_State* L,const Point* pt, int count); extern void size_to_luaval(lua_State* L,const Size& sz); extern void rect_to_luaval(lua_State* L,const Rect& rt); extern void color3b_to_luaval(lua_State* L,const Color3B& cc); extern void color4b_to_luaval(lua_State* L,const Color4B& cc); extern void color4f_to_luaval(lua_State* L,const Color4F& cc); extern void physics_material_to_luaval(lua_State* L,const PhysicsMaterial& pm); +extern void physics_raycastinfo_to_luaval(lua_State* L, const PhysicsRayCastInfo& info); extern void affinetransform_to_luaval(lua_State* L,const AffineTransform& inValue); extern void fontdefinition_to_luaval(lua_State* L,const FontDefinition& inValue); extern void array_to_luaval(lua_State* L,Array* inValue); diff --git a/cocos/scripting/lua/bindings/lua_cocos2dx_physics_manual.cpp b/cocos/scripting/lua/bindings/lua_cocos2dx_physics_manual.cpp index 492bd1d5ef..0d11f0f9b4 100644 --- a/cocos/scripting/lua/bindings/lua_cocos2dx_physics_manual.cpp +++ b/cocos/scripting/lua/bindings/lua_cocos2dx_physics_manual.cpp @@ -14,6 +14,10 @@ extern "C" { #include "CCLuaValue.h" #include "CCLuaEngine.h" +#ifndef CC_SAFE_FREE +#define CC_SAFE_FREE(p) { if(p) free(p); p = nullptr; } +#endif + int lua_cocos2dx_physics_PhysicsBody_getJoints(lua_State* tolua_S) { int argc = 0; @@ -172,15 +176,15 @@ int lua_cocos2dx_physics_PhysicsWorld_rayCast(lua_State* tolua_S) #endif argc = lua_gettop(tolua_S)-1; - if (argc == 4) + if (argc == 3) { std::function arg0; cocos2d::Point arg1; cocos2d::Point arg2; + LUA_FUNCTION handler = toluafix_ref_function(tolua_S, 2, 0); do { - arg0 = [tolua_S](cocos2d::PhysicsWorld &world, const cocos2d::PhysicsRayCastInfo &info, void * data) -> bool + arg0 = [handler, tolua_S](cocos2d::PhysicsWorld &world, const cocos2d::PhysicsRayCastInfo &info, void * data) -> bool { - LuaStack* stack = LuaStack::create(); std::string hashName = typeid(&world).name(); auto iter = g_luaType.find(hashName); std::string className = ""; @@ -191,19 +195,17 @@ int lua_cocos2dx_physics_PhysicsWorld_rayCast(lua_State* tolua_S) } tolua_pushusertype(tolua_S, (void*)(&world), className.c_str()); - tolua_pushusertype(tolua_S, (void*)(&info), "PhysicsRayCastInfo"); - bool ret = stack->executeFunction(2); - stack->clean(); - return ret; + physics_raycastinfo_to_luaval(tolua_S, info); + return LuaEngine::getInstance()->getLuaStack()->executeFunctionByHandler(handler, 2); }; - } while(0) - ; + } while(0); + ok &= luaval_to_point(tolua_S, 3, &arg1); ok &= luaval_to_point(tolua_S, 4, &arg2); -#pragma warning NO CONVERSION TO NATIVE FOR void*; if(!ok) return 0; cobj->rayCast(arg0, arg1, arg2, nullptr); + toluafix_remove_function_by_refid(tolua_S, handler); return 0; } CCLOG("%s has wrong number of arguments: %d, was expecting %d \n", "rayCast",argc, 4); @@ -241,14 +243,14 @@ int lua_cocos2dx_physics_PhysicsWorld_queryRect(lua_State* tolua_S) #endif argc = lua_gettop(tolua_S)-1; - if (argc == 3) + if (argc == 2) { std::function arg0; cocos2d::Rect arg1; + LUA_FUNCTION handler = toluafix_ref_function(tolua_S, 2, 0); do { - arg0 = [tolua_S](cocos2d::PhysicsWorld &world, cocos2d::PhysicsShape &shape, void * data) -> bool + arg0 = [handler, tolua_S](cocos2d::PhysicsWorld &world, cocos2d::PhysicsShape &shape, void * data) -> bool { - LuaStack* stack = LuaStack::create(); std::string hashName = typeid(&world).name(); auto iter = g_luaType.find(hashName); std::string className = ""; @@ -259,18 +261,25 @@ int lua_cocos2dx_physics_PhysicsWorld_queryRect(lua_State* tolua_S) } tolua_pushusertype(tolua_S, (void*)(&world), className.c_str()); - stack->pushObject(&shape, "PhysicsShape"); - bool ret = stack->executeFunction(2); - stack->clean(); - return ret; + + hashName = typeid(&shape).name(); + iter = g_luaType.find(hashName); + className = ""; + if(iter != g_luaType.end()){ + className = iter->second.c_str(); + } else { + className = "PhysicsShape"; + } + toluafix_pushusertype_ccobject(tolua_S, shape._ID, &shape._luaID, (void*)(&shape), className.c_str()); + return LuaEngine::getInstance()->getLuaStack()->executeFunctionByHandler(handler, 2); }; } while(0); ok &= luaval_to_rect(tolua_S, 3, &arg1); -#pragma warning NO CONVERSION TO NATIVE FOR void*; if(!ok) return 0; cobj->queryRect(arg0, arg1, nullptr); + toluafix_remove_function_by_refid(tolua_S, handler); return 0; } CCLOG("%s has wrong number of arguments: %d, was expecting %d \n", "queryRect",argc, 3); @@ -310,14 +319,14 @@ int lua_cocos2dx_physics_PhysicsWorld_queryPoint(lua_State* tolua_S) #endif argc = lua_gettop(tolua_S)-1; - if (argc == 3) + if (argc == 2) { std::function arg0; cocos2d::Point arg1; + LUA_FUNCTION handler = toluafix_ref_function(tolua_S, 2, 0); do { - arg0 = [tolua_S](cocos2d::PhysicsWorld &world, cocos2d::PhysicsShape &shape, void * data) -> bool + arg0 = [handler, tolua_S](cocos2d::PhysicsWorld &world, cocos2d::PhysicsShape &shape, void * data) -> bool { - LuaStack* stack = LuaStack::create(); std::string hashName = typeid(&world).name(); auto iter = g_luaType.find(hashName); std::string className = ""; @@ -328,19 +337,26 @@ int lua_cocos2dx_physics_PhysicsWorld_queryPoint(lua_State* tolua_S) } tolua_pushusertype(tolua_S, (void*)(&world), className.c_str()); - stack->pushObject(&shape, "PhysicsShape"); - bool ret = stack->executeFunction(2); - stack->clean(); - return ret; + + hashName = typeid(&shape).name(); + iter = g_luaType.find(hashName); + className = ""; + if(iter != g_luaType.end()){ + className = iter->second.c_str(); + } else { + className = "PhysicsShape"; + } + toluafix_pushusertype_ccobject(tolua_S, shape._ID, &shape._luaID, (void*)(&shape), className.c_str()); + return LuaEngine::getInstance()->getLuaStack()->executeFunctionByHandler(handler, 2); }; assert(false); } while(0) ; ok &= luaval_to_point(tolua_S, 3, &arg1); -#pragma warning NO CONVERSION TO NATIVE FOR void*; if(!ok) return 0; cobj->queryPoint(arg0, arg1, nullptr); + toluafix_remove_function_by_refid(tolua_S, handler); return 0; } CCLOG("%s has wrong number of arguments: %d, was expecting %d \n", "queryPoint",argc, 3); @@ -354,6 +370,779 @@ tolua_lerror: return 0; } +int lua_cocos2dx_physics_PhysicsBody_createPolygon(lua_State* tolua_S) +{ + int argc = 0; + bool ok = true; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; +#endif + +#if COCOS2D_DEBUG >= 1 + if (!tolua_isusertable(tolua_S,1,"PhysicsBody",0,&tolua_err)) goto tolua_lerror; +#endif + + argc = lua_gettop(tolua_S) - 1; + + if (argc == 1) + { + cocos2d::Point* arg0 = nullptr; + int arg1 = 0; + do { + ok = luaval_to_array_of_Point(tolua_S, 2, &arg0, &arg1); + if (nullptr == arg0){ + LUA_PRECONDITION( arg0, "Invalid Native Object"); + }} while (0); + if(!ok) + { + CC_SAFE_FREE(arg0); + return 0; + } + cocos2d::PhysicsBody* ret = cocos2d::PhysicsBody::createPolygon(arg0, arg1); + CC_SAFE_FREE(arg0); + do { + if (NULL != ret){ + std::string hashName = typeid(*ret).name(); + auto iter = g_luaType.find(hashName); + std::string className = ""; + if(iter != g_luaType.end()){ + className = iter->second.c_str(); + } else { + className = "PhysicsBody"; + } + cocos2d::Object *dynObject = dynamic_cast((cocos2d::PhysicsBody*)ret); + if (NULL != dynObject) { + int ID = ret ? (int)(dynObject->_ID) : -1; + int* luaID = ret ? &(dynObject->_luaID) : NULL; + toluafix_pushusertype_ccobject(tolua_S,ID, luaID, (void*)ret,className.c_str()); + } else { + tolua_pushusertype(tolua_S,(void*)ret,className.c_str()); + }} else { + lua_pushnil(tolua_S); + } + } while (0); + return 1; + } + if (argc == 2) + { + cocos2d::Point* arg0; + int arg1 = 0; + cocos2d::PhysicsMaterial arg2; + do { + ok = luaval_to_array_of_Point(tolua_S, 2, &arg0, &arg1); + if (nullptr == arg0){ + LUA_PRECONDITION( arg0, "Invalid Native Object"); + }} while (0); + ok &= luaval_to_physics_material(tolua_S, 3, &arg2); + if(!ok) + { + CC_SAFE_FREE(arg0); + return 0; + } + cocos2d::PhysicsBody* ret = cocos2d::PhysicsBody::createPolygon(arg0, arg1, arg2); + CC_SAFE_FREE(arg0); + do { + if (NULL != ret){ + std::string hashName = typeid(*ret).name(); + auto iter = g_luaType.find(hashName); + std::string className = ""; + if(iter != g_luaType.end()){ + className = iter->second.c_str(); + } else { + className = "PhysicsBody"; + } + cocos2d::Object *dynObject = dynamic_cast((cocos2d::PhysicsBody*)ret); + if (NULL != dynObject) { + int ID = ret ? (int)(dynObject->_ID) : -1; + int* luaID = ret ? &(dynObject->_luaID) : NULL; + toluafix_pushusertype_ccobject(tolua_S,ID, luaID, (void*)ret,className.c_str()); + } else { + tolua_pushusertype(tolua_S,(void*)ret,className.c_str()); + }} else { + lua_pushnil(tolua_S); + } + } while (0); + return 1; + } + if (argc == 3) + { + cocos2d::Point* arg0; + int arg1 = 0; + cocos2d::PhysicsMaterial arg2; + cocos2d::Point arg3; + do { + ok = luaval_to_array_of_Point(tolua_S, 2, &arg0, &arg1); + if (nullptr == arg0){ + LUA_PRECONDITION( arg0, "Invalid Native Object"); + }} while (0); + ok &= luaval_to_physics_material(tolua_S, 3, &arg2); + ok &= luaval_to_point(tolua_S, 4, &arg3); + if(!ok) + { + CC_SAFE_FREE(arg0); + return 0; + } + cocos2d::PhysicsBody* ret = cocos2d::PhysicsBody::createPolygon(arg0, arg1, arg2, arg3); + CC_SAFE_FREE(arg0); + do { + if (NULL != ret){ + std::string hashName = typeid(*ret).name(); + auto iter = g_luaType.find(hashName); + std::string className = ""; + if(iter != g_luaType.end()){ + className = iter->second.c_str(); + } else { + className = "PhysicsBody"; + } + cocos2d::Object *dynObject = dynamic_cast((cocos2d::PhysicsBody*)ret); + if (NULL != dynObject) { + int ID = ret ? (int)(dynObject->_ID) : -1; + int* luaID = ret ? &(dynObject->_luaID) : NULL; + toluafix_pushusertype_ccobject(tolua_S,ID, luaID, (void*)ret,className.c_str()); + } else { + tolua_pushusertype(tolua_S,(void*)ret,className.c_str()); + }} else { + lua_pushnil(tolua_S); + } + } while (0); + return 1; + } + CCLOG("%s has wrong number of arguments: %d, was expecting %d\n ", "createPolygon",argc, 2); + return 0; +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_physics_PhysicsBody_createPolygon'.",&tolua_err); +#endif + return 0; +} + +int lua_cocos2dx_physics_PhysicsBody_createEdgePolygon(lua_State* tolua_S) +{ + int argc = 0; + bool ok = true; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; +#endif + +#if COCOS2D_DEBUG >= 1 + if (!tolua_isusertable(tolua_S,1,"PhysicsBody",0,&tolua_err)) goto tolua_lerror; +#endif + + argc = lua_gettop(tolua_S) - 1; + + if (argc == 1) + { + cocos2d::Point* arg0; + int arg1; + do { + ok = luaval_to_array_of_Point(tolua_S, 2, &arg0, &arg1); + if (nullptr == arg0){ + LUA_PRECONDITION( arg0, "Invalid Native Object"); + }} while (0); + if(!ok) + { + CC_SAFE_FREE(arg0); + return 0; + } + cocos2d::PhysicsBody* ret = cocos2d::PhysicsBody::createEdgePolygon(arg0, arg1); + CC_SAFE_FREE(arg0); + do { + if (NULL != ret){ + std::string hashName = typeid(*ret).name(); + auto iter = g_luaType.find(hashName); + std::string className = ""; + if(iter != g_luaType.end()){ + className = iter->second.c_str(); + } else { + className = "PhysicsBody"; + } + cocos2d::Object *dynObject = dynamic_cast((cocos2d::PhysicsBody*)ret); + if (NULL != dynObject) { + int ID = ret ? (int)(dynObject->_ID) : -1; + int* luaID = ret ? &(dynObject->_luaID) : NULL; + toluafix_pushusertype_ccobject(tolua_S,ID, luaID, (void*)ret,className.c_str()); + } else { + tolua_pushusertype(tolua_S,(void*)ret,className.c_str()); + }} else { + lua_pushnil(tolua_S); + } + } while (0); + return 1; + } + if (argc == 2) + { + cocos2d::Point* arg0; + int arg1; + cocos2d::PhysicsMaterial arg2; + do { + ok = luaval_to_array_of_Point(tolua_S, 2, &arg0, &arg1); + if (nullptr == arg0){ + LUA_PRECONDITION( arg0, "Invalid Native Object"); + }} while (0); + ok &= luaval_to_physics_material(tolua_S, 3, &arg2); + if(!ok) + { + CC_SAFE_FREE(arg0); + return 0; + } + cocos2d::PhysicsBody* ret = cocos2d::PhysicsBody::createEdgePolygon(arg0, arg1, arg2); + CC_SAFE_FREE(arg0); + do { + if (NULL != ret){ + std::string hashName = typeid(*ret).name(); + auto iter = g_luaType.find(hashName); + std::string className = ""; + if(iter != g_luaType.end()){ + className = iter->second.c_str(); + } else { + className = "PhysicsBody"; + } + cocos2d::Object *dynObject = dynamic_cast((cocos2d::PhysicsBody*)ret); + if (NULL != dynObject) { + int ID = ret ? (int)(dynObject->_ID) : -1; + int* luaID = ret ? &(dynObject->_luaID) : NULL; + toluafix_pushusertype_ccobject(tolua_S,ID, luaID, (void*)ret,className.c_str()); + } else { + tolua_pushusertype(tolua_S,(void*)ret,className.c_str()); + }} else { + lua_pushnil(tolua_S); + } + } while (0); + return 1; + } + if (argc == 3) + { + cocos2d::Point* arg0; + int arg1; + cocos2d::PhysicsMaterial arg2; + double arg3; + do { + ok = luaval_to_array_of_Point(tolua_S, 2, &arg0, &arg1); + if (nullptr == arg0){ + LUA_PRECONDITION( arg0, "Invalid Native Object"); + }} while (0); + ok &= luaval_to_physics_material(tolua_S, 3, &arg2); + ok &= luaval_to_number(tolua_S, 4,&arg3); + if(!ok) + { + CC_SAFE_FREE(arg0); + return 0; + } + cocos2d::PhysicsBody* ret = cocos2d::PhysicsBody::createEdgePolygon(arg0, arg1, arg2, arg3); + CC_SAFE_FREE(arg0); + do { + if (NULL != ret){ + std::string hashName = typeid(*ret).name(); + auto iter = g_luaType.find(hashName); + std::string className = ""; + if(iter != g_luaType.end()){ + className = iter->second.c_str(); + } else { + className = "PhysicsBody"; + } + cocos2d::Object *dynObject = dynamic_cast((cocos2d::PhysicsBody*)ret); + if (NULL != dynObject) { + int ID = ret ? (int)(dynObject->_ID) : -1; + int* luaID = ret ? &(dynObject->_luaID) : NULL; + toluafix_pushusertype_ccobject(tolua_S,ID, luaID, (void*)ret,className.c_str()); + } else { + tolua_pushusertype(tolua_S,(void*)ret,className.c_str()); + }} else { + lua_pushnil(tolua_S); + } + } while (0); + return 1; + } + CCLOG("%s has wrong number of arguments: %d, was expecting %d\n ", "createEdgePolygon",argc, 2); + return 0; +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_physics_PhysicsBody_createEdgePolygon'.",&tolua_err); +#endif + return 0; +} + +int lua_cocos2dx_physics_PhysicsBody_createEdgeChain(lua_State* tolua_S) +{ + int argc = 0; + bool ok = true; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; +#endif + +#if COCOS2D_DEBUG >= 1 + if (!tolua_isusertable(tolua_S,1,"PhysicsBody",0,&tolua_err)) goto tolua_lerror; +#endif + + argc = lua_gettop(tolua_S) - 1; + + if (argc == 1) + { + cocos2d::Point* arg0; + int arg1; + do { + ok = luaval_to_array_of_Point(tolua_S, 2, &arg0, &arg1); + if (nullptr == arg0){ + LUA_PRECONDITION( arg0, "Invalid Native Object"); + }} while (0); + if(!ok) + { + CC_SAFE_FREE(arg0); + return 0; + } + cocos2d::PhysicsBody* ret = cocos2d::PhysicsBody::createEdgeChain(arg0, arg1); + CC_SAFE_FREE(arg0); + do { + if (NULL != ret){ + std::string hashName = typeid(*ret).name(); + auto iter = g_luaType.find(hashName); + std::string className = ""; + if(iter != g_luaType.end()){ + className = iter->second.c_str(); + } else { + className = "PhysicsBody"; + } + cocos2d::Object *dynObject = dynamic_cast((cocos2d::PhysicsBody*)ret); + if (NULL != dynObject) { + int ID = ret ? (int)(dynObject->_ID) : -1; + int* luaID = ret ? &(dynObject->_luaID) : NULL; + toluafix_pushusertype_ccobject(tolua_S,ID, luaID, (void*)ret,className.c_str()); + } else { + tolua_pushusertype(tolua_S,(void*)ret,className.c_str()); + }} else { + lua_pushnil(tolua_S); + } + } while (0); + return 1; + } + if (argc == 2) + { + cocos2d::Point* arg0; + int arg1; + cocos2d::PhysicsMaterial arg2; + do { + ok = luaval_to_array_of_Point(tolua_S, 2, &arg0, &arg1); + if (nullptr == arg0){ + LUA_PRECONDITION( arg0, "Invalid Native Object"); + }} while (0); + ok &= luaval_to_physics_material(tolua_S, 3, &arg2); + if(!ok) + { + CC_SAFE_FREE(arg0); + return 0; + } + cocos2d::PhysicsBody* ret = cocos2d::PhysicsBody::createEdgeChain(arg0, arg1, arg2); + CC_SAFE_FREE(arg0); + do { + if (NULL != ret){ + std::string hashName = typeid(*ret).name(); + auto iter = g_luaType.find(hashName); + std::string className = ""; + if(iter != g_luaType.end()){ + className = iter->second.c_str(); + } else { + className = "PhysicsBody"; + } + cocos2d::Object *dynObject = dynamic_cast((cocos2d::PhysicsBody*)ret); + if (NULL != dynObject) { + int ID = ret ? (int)(dynObject->_ID) : -1; + int* luaID = ret ? &(dynObject->_luaID) : NULL; + toluafix_pushusertype_ccobject(tolua_S,ID, luaID, (void*)ret,className.c_str()); + } else { + tolua_pushusertype(tolua_S,(void*)ret,className.c_str()); + }} else { + lua_pushnil(tolua_S); + } + } while (0); + return 1; + } + if (argc == 3) + { + cocos2d::Point* arg0; + int arg1; + cocos2d::PhysicsMaterial arg2; + double arg3; + do { + ok = luaval_to_array_of_Point(tolua_S, 2, &arg0, &arg1); + if (nullptr == arg0){ + LUA_PRECONDITION( arg0, "Invalid Native Object"); + }} while (0); + ok &= luaval_to_physics_material(tolua_S, 3, &arg2); + ok &= luaval_to_number(tolua_S, 4,&arg3); + if(!ok) + { + CC_SAFE_FREE(arg0); + return 0; + } + cocos2d::PhysicsBody* ret = cocos2d::PhysicsBody::createEdgeChain(arg0, arg1, arg2, arg3); + CC_SAFE_FREE(arg0); + do { + if (NULL != ret){ + std::string hashName = typeid(*ret).name(); + auto iter = g_luaType.find(hashName); + std::string className = ""; + if(iter != g_luaType.end()){ + className = iter->second.c_str(); + } else { + className = "PhysicsBody"; + } + cocos2d::Object *dynObject = dynamic_cast((cocos2d::PhysicsBody*)ret); + if (NULL != dynObject) { + int ID = ret ? (int)(dynObject->_ID) : -1; + int* luaID = ret ? &(dynObject->_luaID) : NULL; + toluafix_pushusertype_ccobject(tolua_S,ID, luaID, (void*)ret,className.c_str()); + } else { + tolua_pushusertype(tolua_S,(void*)ret,className.c_str()); + }} else { + lua_pushnil(tolua_S); + } + } while (0); + return 1; + } + CCLOG("%s has wrong number of arguments: %d, was expecting %d\n ", "createEdgeChain",argc, 2); + return 0; +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_physics_PhysicsBody_createEdgeChain'.",&tolua_err); +#endif + return 0; +} + +int lua_cocos2dx_physics_PhysicsShape_recenterPoints(lua_State* tolua_S) +{ + int argc = 0; + bool ok = true; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; +#endif + +#if COCOS2D_DEBUG >= 1 + if (!tolua_isusertable(tolua_S,1,"PhysicsShape",0,&tolua_err)) goto tolua_lerror; +#endif + + argc = lua_gettop(tolua_S) - 1; + + if (argc == 1) + { + cocos2d::Point* arg0; + int arg1 = 0; + do { + ok = luaval_to_array_of_Point(tolua_S, 2, &arg0, &arg1); + if (nullptr == arg0){ + LUA_PRECONDITION( arg0, "Invalid Native Object"); + }} while (0); + if(!ok) + { + CC_SAFE_FREE(arg0); + return 0; + } + cocos2d::PhysicsShape::recenterPoints(arg0, arg1); + points_to_luaval(tolua_S, arg0, arg1); + CC_SAFE_FREE(arg0); + + return 0; + } + if (argc == 2) + { + cocos2d::Point* arg0; + int arg1 = 0; + cocos2d::Point arg2; + do { + ok = luaval_to_array_of_Point(tolua_S, 2, &arg0, &arg1); + if (nullptr == arg0){ + LUA_PRECONDITION( arg0, "Invalid Native Object"); + }} while (0); + ok &= luaval_to_point(tolua_S, 3, &arg2); + if(!ok) + { + CC_SAFE_FREE(arg0); + return 0; + } + cocos2d::PhysicsShape::recenterPoints(arg0, arg1, arg2); + points_to_luaval(tolua_S, arg0, arg1); + CC_SAFE_FREE(arg0); + return 0; + } + CCLOG("%s has wrong number of arguments: %d, was expecting %d\n ", "recenterPoints",argc, 2); + return 0; +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_physics_PhysicsShape_recenterPoints'.",&tolua_err); +#endif + return 0; +} + +int lua_cocos2dx_physics_PhysicsShape_getPolyonCenter(lua_State* tolua_S) +{ + int argc = 0; + bool ok = true; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; +#endif + +#if COCOS2D_DEBUG >= 1 + if (!tolua_isusertable(tolua_S,1,"PhysicsShape",0,&tolua_err)) goto tolua_lerror; +#endif + + argc = lua_gettop(tolua_S) - 1; + + if (argc == 1) + { + cocos2d::Point* arg0; + int arg1 = 0; + do { + ok = luaval_to_array_of_Point(tolua_S, 2, &arg0, &arg1); + if (nullptr == arg0){ + LUA_PRECONDITION( arg0, "Invalid Native Object"); + }} while (0); + ok &= luaval_to_int32(tolua_S, 3,(int *)&arg1); + if(!ok) + { + CC_SAFE_FREE(arg0); + return 0; + } + cocos2d::Point ret = cocos2d::PhysicsShape::getPolyonCenter(arg0, arg1); + CC_SAFE_FREE(arg0); + point_to_luaval(tolua_S, ret); + return 1; + } + CCLOG("%s has wrong number of arguments: %d, was expecting %d\n ", "getPolyonCenter",argc, 2); + return 0; +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_physics_PhysicsShape_getPolyonCenter'.",&tolua_err); +#endif + return 0; +} + +int lua_cocos2dx_physics_PhysicsShapeBox_getPoints(lua_State* tolua_S) +{ + int argc = 0; + cocos2d::PhysicsShapeBox* cobj = nullptr; + bool ok = true; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; +#endif + +#if COCOS2D_DEBUG >= 1 + if (!tolua_isusertype(tolua_S,1,"PhysicsShapeBox",0,&tolua_err)) goto tolua_lerror; +#endif + + cobj = (cocos2d::PhysicsShapeBox*)tolua_tousertype(tolua_S,1,0); + +#if COCOS2D_DEBUG >= 1 + if (!cobj) + { + tolua_error(tolua_S,"invalid 'cobj' in function 'lua_cocos2dx_physics_PhysicsShapeBox_getPoints'", NULL); + return 0; + } +#endif + + argc = lua_gettop(tolua_S)-1; + if (argc == 0) + { + cocos2d::Point arg0[4]; + cobj->getPoints(arg0); + points_to_luaval(tolua_S, arg0, 4); + return 0; + } + CCLOG("%s has wrong number of arguments: %d, was expecting %d \n", "getPoints",argc, 1); + return 0; + +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_physics_PhysicsShapeBox_getPoints'.",&tolua_err); +#endif + + return 0; +} + +int lua_cocos2dx_physics_PhysicsShapePolygon_getPoints(lua_State* tolua_S) +{ + int argc = 0; + cocos2d::PhysicsShapePolygon* cobj = nullptr; + bool ok = true; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; +#endif + +#if COCOS2D_DEBUG >= 1 + if (!tolua_isusertype(tolua_S,1,"PhysicsShapePolygon",0,&tolua_err)) goto tolua_lerror; +#endif + + cobj = (cocos2d::PhysicsShapePolygon*)tolua_tousertype(tolua_S,1,0); + +#if COCOS2D_DEBUG >= 1 + if (!cobj) + { + tolua_error(tolua_S,"invalid 'cobj' in function 'lua_cocos2dx_physics_PhysicsShapePolygon_getPoints'", NULL); + return 0; + } +#endif + + argc = lua_gettop(tolua_S)-1; + if (argc == 0) + { + int count = cobj->getPointsCount(); + cocos2d::Point* arg0 = new cocos2d::Point[count]; + cobj->getPoints(arg0); + points_to_luaval(tolua_S, arg0, count); + CC_SAFE_FREE(arg0); + return 0; + } + CCLOG("%s has wrong number of arguments: %d, was expecting %d \n", "getPoints",argc, 1); + return 0; + +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_physics_PhysicsShapePolygon_getPoints'.",&tolua_err); +#endif + + return 0; +} + +int lua_cocos2dx_physics_PhysicsShapeEdgeBox_getPoints(lua_State* tolua_S) +{ + int argc = 0; + cocos2d::PhysicsShapeEdgeBox* cobj = nullptr; + bool ok = true; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; +#endif + +#if COCOS2D_DEBUG >= 1 + if (!tolua_isusertype(tolua_S,1,"PhysicsShapeEdgeBox",0,&tolua_err)) goto tolua_lerror; +#endif + + cobj = (cocos2d::PhysicsShapeEdgeBox*)tolua_tousertype(tolua_S,1,0); + +#if COCOS2D_DEBUG >= 1 + if (!cobj) + { + tolua_error(tolua_S,"invalid 'cobj' in function 'lua_cocos2dx_physics_PhysicsShapeEdgeBox_getPoints'", NULL); + return 0; + } +#endif + + argc = lua_gettop(tolua_S)-1; + if (argc == 0) + { + int count = cobj->getPointsCount(); + cocos2d::Point* arg0 = new cocos2d::Point[count]; + cobj->getPoints(arg0); + points_to_luaval(tolua_S, arg0, count); + CC_SAFE_FREE(arg0); + return 0; + } + CCLOG("%s has wrong number of arguments: %d, was expecting %d \n", "getPoints",argc, 1); + return 0; + +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_physics_PhysicsShapeEdgeBox_getPoints'.",&tolua_err); +#endif + + return 0; +} + +int lua_cocos2dx_physics_PhysicsShapeEdgePolygon_getPoints(lua_State* tolua_S) +{ + int argc = 0; + cocos2d::PhysicsShapeEdgePolygon* cobj = nullptr; + bool ok = true; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; +#endif + +#if COCOS2D_DEBUG >= 1 + if (!tolua_isusertype(tolua_S,1,"PhysicsShapeEdgePolygon",0,&tolua_err)) goto tolua_lerror; +#endif + + cobj = (cocos2d::PhysicsShapeEdgePolygon*)tolua_tousertype(tolua_S,1,0); + +#if COCOS2D_DEBUG >= 1 + if (!cobj) + { + tolua_error(tolua_S,"invalid 'cobj' in function 'lua_cocos2dx_physics_PhysicsShapeEdgePolygon_getPoints'", NULL); + return 0; + } +#endif + + argc = lua_gettop(tolua_S)-1; + if (argc == 0) + { + int count = cobj->getPointsCount(); + cocos2d::Point* arg0 = new cocos2d::Point[count]; + cobj->getPoints(arg0); + points_to_luaval(tolua_S, arg0, count); + CC_SAFE_FREE(arg0); + return 0; + } + CCLOG("%s has wrong number of arguments: %d, was expecting %d \n", "getPoints",argc, 1); + return 0; + +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_physics_PhysicsShapeEdgePolygon_getPoints'.",&tolua_err); +#endif + + return 0; +} + +int lua_cocos2dx_physics_PhysicsShapeEdgeChain_getPoints(lua_State* tolua_S) +{ + int argc = 0; + cocos2d::PhysicsShapeEdgeChain* cobj = nullptr; + bool ok = true; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; +#endif + +#if COCOS2D_DEBUG >= 1 + if (!tolua_isusertype(tolua_S,1,"PhysicsShapeEdgeChain",0,&tolua_err)) goto tolua_lerror; +#endif + + cobj = (cocos2d::PhysicsShapeEdgeChain*)tolua_tousertype(tolua_S,1,0); + +#if COCOS2D_DEBUG >= 1 + if (!cobj) + { + tolua_error(tolua_S,"invalid 'cobj' in function 'lua_cocos2dx_physics_PhysicsShapeEdgeChain_getPoints'", NULL); + return 0; + } +#endif + + argc = lua_gettop(tolua_S)-1; + if (argc == 0) + { + int count = cobj->getPointsCount(); + cocos2d::Point* arg0 = new cocos2d::Point[count]; + cobj->getPoints(arg0); + points_to_luaval(tolua_S, arg0, count); + CC_SAFE_FREE(arg0); + return 0; + } + CCLOG("%s has wrong number of arguments: %d, was expecting %d \n", "getPoints",argc, 1); + return 0; + +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_physics_PhysicsShapeEdgeChain_getPoints'.",&tolua_err); +#endif + + return 0; +} + int register_all_cocos2dx_physics_manual(lua_State* tolua_S) { lua_pushstring(tolua_S, "PhysicsBody"); @@ -363,6 +1152,78 @@ int register_all_cocos2dx_physics_manual(lua_State* tolua_S) lua_pushstring(tolua_S,"getJoints"); lua_pushcfunction(tolua_S,lua_cocos2dx_physics_PhysicsBody_getJoints ); lua_rawset(tolua_S,-3); + lua_pushstring(tolua_S,"createPolygon"); + lua_pushcfunction(tolua_S,lua_cocos2dx_physics_PhysicsBody_createPolygon ); + lua_rawset(tolua_S,-3); + lua_pushstring(tolua_S,"createEdgeChain"); + lua_pushcfunction(tolua_S,lua_cocos2dx_physics_PhysicsBody_createEdgeChain ); + lua_rawset(tolua_S,-3); + lua_pushstring(tolua_S,"createEdgePolygon"); + lua_pushcfunction(tolua_S,lua_cocos2dx_physics_PhysicsBody_createEdgePolygon ); + lua_rawset(tolua_S,-3); + } + lua_pop(tolua_S, 1); + + lua_pushstring(tolua_S, "PhysicsShape"); + lua_rawget(tolua_S, LUA_REGISTRYINDEX); + if (lua_istable(tolua_S,-1)) + { + lua_pushstring(tolua_S,"recenterPoints"); + lua_pushcfunction(tolua_S,lua_cocos2dx_physics_PhysicsShape_recenterPoints ); + lua_rawset(tolua_S,-3); + lua_pushstring(tolua_S,"getPolyonCenter"); + lua_pushcfunction(tolua_S,lua_cocos2dx_physics_PhysicsShape_getPolyonCenter ); + lua_rawset(tolua_S,-3); + } + lua_pop(tolua_S, 1); + + lua_pushstring(tolua_S, "PhysicsShapeBox"); + lua_rawget(tolua_S, LUA_REGISTRYINDEX); + if (lua_istable(tolua_S,-1)) + { + lua_pushstring(tolua_S,"getPoints"); + lua_pushcfunction(tolua_S,lua_cocos2dx_physics_PhysicsShapeBox_getPoints ); + lua_rawset(tolua_S,-3); + } + lua_pop(tolua_S, 1); + + lua_pushstring(tolua_S, "PhysicsShapeEdgeBox"); + lua_rawget(tolua_S, LUA_REGISTRYINDEX); + if (lua_istable(tolua_S,-1)) + { + lua_pushstring(tolua_S,"getPoints"); + lua_pushcfunction(tolua_S,lua_cocos2dx_physics_PhysicsShapeEdgeBox_getPoints ); + lua_rawset(tolua_S,-3); + } + lua_pop(tolua_S, 1); + + lua_pushstring(tolua_S, "PhysicsShapePolygon"); + lua_rawget(tolua_S, LUA_REGISTRYINDEX); + if (lua_istable(tolua_S,-1)) + { + lua_pushstring(tolua_S,"getPoints"); + lua_pushcfunction(tolua_S,lua_cocos2dx_physics_PhysicsShapePolygon_getPoints ); + lua_rawset(tolua_S,-3); + } + lua_pop(tolua_S, 1); + + lua_pushstring(tolua_S, "PhysicsShapeEdgePolygon"); + lua_rawget(tolua_S, LUA_REGISTRYINDEX); + if (lua_istable(tolua_S,-1)) + { + lua_pushstring(tolua_S,"getPoints"); + lua_pushcfunction(tolua_S,lua_cocos2dx_physics_PhysicsShapeEdgePolygon_getPoints ); + lua_rawset(tolua_S,-3); + } + lua_pop(tolua_S, 1); + + lua_pushstring(tolua_S, "PhysicsShapeEdgeChain"); + lua_rawget(tolua_S, LUA_REGISTRYINDEX); + if (lua_istable(tolua_S,-1)) + { + lua_pushstring(tolua_S,"getPoints"); + lua_pushcfunction(tolua_S,lua_cocos2dx_physics_PhysicsShapeEdgeChain_getPoints); + lua_rawset(tolua_S,-3); } lua_pop(tolua_S, 1); @@ -382,8 +1243,27 @@ int register_all_cocos2dx_physics_manual(lua_State* tolua_S) lua_pushstring(tolua_S,"rayCast"); lua_pushcfunction(tolua_S, lua_cocos2dx_physics_PhysicsWorld_rayCast ); lua_rawset(tolua_S,-3); + lua_pushstring(tolua_S, "DEBUGDRAW_NONE"); + lua_pushnumber(tolua_S, PhysicsWorld::DEBUGDRAW_NONE); + lua_rawset(tolua_S,-3); + lua_pushstring(tolua_S, "DEBUGDRAW_SHAPE"); + lua_pushnumber(tolua_S, PhysicsWorld::DEBUGDRAW_SHAPE); + lua_rawset(tolua_S,-3); + lua_pushstring(tolua_S, "DEBUGDRAW_JOINT"); + lua_pushnumber(tolua_S, PhysicsWorld::DEBUGDRAW_JOINT); + lua_rawset(tolua_S,-3); + lua_pushstring(tolua_S, "DEBUGDRAW_CONTACT"); + lua_pushnumber(tolua_S, PhysicsWorld::DEBUGDRAW_CONTACT); + lua_rawset(tolua_S,-3); + lua_pushstring(tolua_S, "DEBUGDRAW_ALL"); + lua_pushnumber(tolua_S, PhysicsWorld::DEBUGDRAW_ALL); + lua_rawset(tolua_S,-3); } + lua_pop(tolua_S, 1); + + tolua_constant(tolua_S, "PHYSICS_INFINITY", PHYSICS_INFINITY); + return 0; } diff --git a/cocos/scripting/lua/script/Cocos2d.lua b/cocos/scripting/lua/script/Cocos2d.lua index 1445a271fe..793fa064ec 100644 --- a/cocos/scripting/lua/script/Cocos2d.lua +++ b/cocos/scripting/lua/script/Cocos2d.lua @@ -360,3 +360,9 @@ end function cc.AnimationFrameData( _texCoords, _delay, _size) return { texCoords = _texCoords, delay = _delay, size = _size } end + +--PhysicsMaterial +function cc.PhysicsMaterial(_density, _restitution, _friction) + return { density = _density, restitution = _restitution, friction = _friction } +end + diff --git a/cocos/scripting/lua/script/Cocos2dConstants.lua b/cocos/scripting/lua/script/Cocos2dConstants.lua index 7534f6c24e..3bed698e26 100644 --- a/cocos/scripting/lua/script/Cocos2dConstants.lua +++ b/cocos/scripting/lua/script/Cocos2dConstants.lua @@ -351,4 +351,7 @@ cc.EVENT_KEYBOARD = 3 cc.EVENT_MOUSE = 4 cc.EVENT_ACCELERATION = 5 cc.EVENT_CUSTOM = 6 - \ No newline at end of file + +cc.PHYSICSSHAPE_MATERIAL_DEFAULT = {0.0, 0.5, 0.5} +cc.PHYSICSBODY_MATERIAL_DEFAULT = {0.1, 0.5, 0.5} + diff --git a/samples/Lua/TestLua/Resources/luaScript/PhysicsTest/PhysicsTest.lua b/samples/Lua/TestLua/Resources/luaScript/PhysicsTest/PhysicsTest.lua new file mode 100644 index 0000000000..0434b0a3c4 --- /dev/null +++ b/samples/Lua/TestLua/Resources/luaScript/PhysicsTest/PhysicsTest.lua @@ -0,0 +1,620 @@ +local size = cc.Director:getInstance():getWinSize() +local MATERIAL_DEFAULT = cc.PhysicsMaterial(0.1, 0.5, 0.5) +local curLayer = nil +local STATIC_COLOR = cc.c4f(1.0, 0.0, 0.0, 1.0) +local DRAG_BODYS_TAG = 0x80 + +local function range(from, to, step) + step = step or 1 + return function(_, lastvalue) + local nextvalue = lastvalue + step + if step > 0 and nextvalue <= to or step < 0 and nextvalue >= to or + step == 0 + then + return nextvalue + end + end, nil, from - step +end + +local function initWithLayer(layer, callback) + curLayer = layer + layer.spriteTexture = cc.SpriteBatchNode:create("Images/grossini_dance_atlas.png", 100):getTexture() + + local debug = false + local function toggleDebugCallback(sender) + debug = not debug + cc.Director:getInstance():getRunningScene():getPhysicsWorld():setDebugDrawMask(debug and cc.PhysicsWorld.DEBUGDRAW_ALL or cc.PhysicsWorld.DEBUGDRAW_NONE) + end + + layer.toggleDebug = toggleDebugCallback; + cc.MenuItemFont:setFontSize(18) + local item = cc.MenuItemFont:create("Toogle debug") + item:registerScriptTapHandler(toggleDebugCallback) + local menu = cc.Menu:create(item) + layer:addChild(menu) + menu:setPosition(size.width - 50, size.height - 10) + Helper.initWithLayer(layer) + + local function onNodeEvent(event) + if "enter" == event then + callback() + end + end + layer:registerScriptHandler(onNodeEvent) +end + +local function addGrossiniAtPosition(layer, p, scale) + scale = scale or 1.0 + + local posx = math.random() * 200.0 + local posy = math.random() * 100.0 + posx = (math.floor(posx) % 4) * 85 + posy = (math.floor(posy) % 3) * 121 + + local sp = cc.Sprite:createWithTexture(layer.spriteTexture, cc.rect(posx, posy, 85, 121)) + sp:setScale(scale) + sp:setPhysicsBody(cc.PhysicsBody:createBox(cc.size(48.0*scale, 108.0*scale))) + layer:addChild(sp) + sp:setPosition(p) + return sp +end + +local function onTouchBegan(touch, event) + local location = touch:getLocation() + local arr = cc.Director:getInstance():getRunningScene():getPhysicsWorld():getShapes(location) + + local body + for _, obj in ipairs(arr) do + if bit.band(obj:getBody():getTag(), DRAG_BODYS_TAG) ~= 0 then + body = obj:getBody(); + break; + end + end + + if body then + local mouse = cc.Node:create(); + mouse:setPhysicsBody(cc.PhysicsBody:create(PHYSICS_INFINITY, PHYSICS_INFINITY)); + mouse:getPhysicsBody():setDynamic(false); + mouse:setPosition(location); + curLayer:addChild(mouse); + local joint = cc.PhysicsJointPin:construct(mouse:getPhysicsBody(), body, location); + joint:setMaxForce(5000.0 * body:getMass()); + cc.Director:getInstance():getRunningScene():getPhysicsWorld():addJoint(joint); + touch.mouse = mouse + + return true; + end + + return false; +end + +local function onTouchMoved(touch, event) + if touch.mouse then + touch.mouse:setPosition(touch:getLocation()); + end +end + +local function onTouchEnded(touch, event) + if touch.mouse then + curLayer:removeChild(touch.mouse) + touch.mouse = nil + end +end + +local function makeBall(layer, point, radius, material) + material = material or MATERIAL_DEFAULT + + local ball + if layer.ball then + ball = cc.Sprite:createWithTexture(layer.ball:getTexture()) + else + ball = cc.Sprite:create("Images/ball.png") + end + + ball:setScale(0.13 * radius) + + local body = cc.PhysicsBody:createCircle(radius, material) + ball:setPhysicsBody(body) + ball:setPosition(point) + + return ball +end + +local function makeBox(point, size, material) + material = material or DEFAULT_MATERIAL + local box = math.random() > 0.5 and cc.Sprite:create("Images/YellowSquare.png") or cc.Sprite:create("Images/CyanSquare.png"); + + box:setScaleX(size.width/100.0); + box:setScaleY(size.height/100.0); + + local body = cc.PhysicsBody:createBox(size); + box:setPhysicsBody(body); + box:setPosition(cc.p(point.x, point.y)); + + return box; +end + +local function makeTriangle(point, size, material) + material = material or DEFAULT_MATERIAL + local triangle = math.random() > 0.5 and cc.Sprite:create("Images/YellowTriangle.png") or cc.Sprite:create("Images/CyanTriangle.png"); + + if size.height == 0 then + triangle:setScale(size.width/100.0); + else + triangle:setScaleX(size.width/50.0) + triangle:setScaleY(size.height/43.5) + end + + vers = { cc.p(0, size.height/2), cc.p(size.width/2, -size.height/2), cc.p(-size.width/2, -size.height/2)}; + + local body = cc.PhysicsBody:createPolygon(vers); + triangle:setPhysicsBody(body); + triangle:setPosition(point); + + return triangle; +end + +local function PhysicsDemoClickAdd() + local layer = cc.Layer:create() + local function onEnter() + local function onTouchEnded(touch, event) + local location = touch:getLocation(); + addGrossiniAtPosition(layer, location) + end + + local touchListener = cc.EventListenerTouchOneByOne:create() + touchListener:registerScriptHandler(function() return true end, cc.Handler.EVENT_TOUCH_BEGAN) + touchListener:registerScriptHandler(onTouchEnded, cc.Handler.EVENT_TOUCH_ENDED) + local eventDispatcher = layer:getEventDispatcher() + eventDispatcher:addEventListenerWithSceneGraphPriority(touchListener, layer) + + addGrossiniAtPosition(layer, VisibleRect:center()) + + local node = cc.Node:create() + node:setPhysicsBody(cc.PhysicsBody:createEdgeBox(cc.size(VisibleRect:getVisibleRect().width, VisibleRect:getVisibleRect().height))) + node:setPosition(VisibleRect:center()) + layer:addChild(node) + end + initWithLayer(layer, onEnter) + Helper.titleLabel:setString("Grossini") + Helper.subtitleLabel:setString("multi touch to add grossini") + + return layer +end + +local function PhysicsDemoLogoSmash() + local layer = cc.Layer:create() + + local function onEnter() + local logo_width = 188.0 + local logo_height = 35.0 + local logo_raw_length = 24.0 + local logo_image = { + 15,-16,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7,-64,15,63,-32,-2,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,31,-64,15,127,-125,-1,-128,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,127,-64,15,127,15,-1,-64,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,-1,-64,15,-2, + 31,-1,-64,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,-1,-64,0,-4,63,-1,-32,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,1,-1,-64,15,-8,127,-1,-32,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 1,-1,-64,0,-8,-15,-1,-32,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,-31,-1,-64,15,-8,-32, + -1,-32,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7,-15,-1,-64,9,-15,-32,-1,-32,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,31,-15,-1,-64,0,-15,-32,-1,-32,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,63,-7,-1,-64,9,-29,-32,127,-61,-16,63,15,-61,-1,-8,31,-16,15,-8,126,7,-31, + -8,31,-65,-7,-1,-64,9,-29,-32,0,7,-8,127,-97,-25,-1,-2,63,-8,31,-4,-1,15,-13, + -4,63,-1,-3,-1,-64,9,-29,-32,0,7,-8,127,-97,-25,-1,-2,63,-8,31,-4,-1,15,-13, + -2,63,-1,-3,-1,-64,9,-29,-32,0,7,-8,127,-97,-25,-1,-1,63,-4,63,-4,-1,15,-13, + -2,63,-33,-1,-1,-32,9,-25,-32,0,7,-8,127,-97,-25,-1,-1,63,-4,63,-4,-1,15,-13, + -1,63,-33,-1,-1,-16,9,-25,-32,0,7,-8,127,-97,-25,-1,-1,63,-4,63,-4,-1,15,-13, + -1,63,-49,-1,-1,-8,9,-57,-32,0,7,-8,127,-97,-25,-8,-1,63,-2,127,-4,-1,15,-13, + -1,-65,-49,-1,-1,-4,9,-57,-32,0,7,-8,127,-97,-25,-8,-1,63,-2,127,-4,-1,15,-13, + -1,-65,-57,-1,-1,-2,9,-57,-32,0,7,-8,127,-97,-25,-8,-1,63,-2,127,-4,-1,15,-13, + -1,-1,-57,-1,-1,-1,9,-57,-32,0,7,-1,-1,-97,-25,-8,-1,63,-1,-1,-4,-1,15,-13,-1, + -1,-61,-1,-1,-1,-119,-57,-32,0,7,-1,-1,-97,-25,-8,-1,63,-1,-1,-4,-1,15,-13,-1, + -1,-61,-1,-1,-1,-55,-49,-32,0,7,-1,-1,-97,-25,-8,-1,63,-1,-1,-4,-1,15,-13,-1, + -1,-63,-1,-1,-1,-23,-49,-32,127,-57,-1,-1,-97,-25,-1,-1,63,-1,-1,-4,-1,15,-13, + -1,-1,-63,-1,-1,-1,-16,-49,-32,-1,-25,-1,-1,-97,-25,-1,-1,63,-33,-5,-4,-1,15, + -13,-1,-1,-64,-1,-9,-1,-7,-49,-32,-1,-25,-8,127,-97,-25,-1,-1,63,-33,-5,-4,-1, + 15,-13,-1,-1,-64,-1,-13,-1,-32,-49,-32,-1,-25,-8,127,-97,-25,-1,-2,63,-49,-13, + -4,-1,15,-13,-1,-1,-64,127,-7,-1,-119,-17,-15,-1,-25,-8,127,-97,-25,-1,-2,63, + -49,-13,-4,-1,15,-13,-3,-1,-64,127,-8,-2,15,-17,-1,-1,-25,-8,127,-97,-25,-1, + -8,63,-49,-13,-4,-1,15,-13,-3,-1,-64,63,-4,120,0,-17,-1,-1,-25,-8,127,-97,-25, + -8,0,63,-57,-29,-4,-1,15,-13,-4,-1,-64,63,-4,0,15,-17,-1,-1,-25,-8,127,-97, + -25,-8,0,63,-57,-29,-4,-1,-1,-13,-4,-1,-64,31,-2,0,0,103,-1,-1,-57,-8,127,-97, + -25,-8,0,63,-57,-29,-4,-1,-1,-13,-4,127,-64,31,-2,0,15,103,-1,-1,-57,-8,127, + -97,-25,-8,0,63,-61,-61,-4,127,-1,-29,-4,127,-64,15,-8,0,0,55,-1,-1,-121,-8, + 127,-97,-25,-8,0,63,-61,-61,-4,127,-1,-29,-4,63,-64,15,-32,0,0,23,-1,-2,3,-16, + 63,15,-61,-16,0,31,-127,-127,-8,31,-1,-127,-8,31,-128,7,-128,0,0 + }; + + local function get_pixel(x, y) + return bit.band(bit.rshift(logo_image[bit.rshift(x, 3) + y*logo_raw_length + 1], bit.band(bit.bnot(x), 0x07)), 1) + end + + cc.Director:getInstance():getRunningScene():getPhysicsWorld():setGravity(cc.p(0, 0)); + cc.Director:getInstance():getRunningScene():getPhysicsWorld():setUpdateRate(5.0); + + layer.ball = cc.SpriteBatchNode:create("Images/ball.png", #logo_image); + layer:addChild(layer.ball); + for y in range(0, logo_height-1) do + for x in range(0, logo_width-1) do + if get_pixel(x, y) == 1 then + local x_jitter = 0.05*math.random(); + local y_jitter = 0.05*math.random(); + + local ball = makeBall(layer, cc.p(2*(x - logo_width/2 + x_jitter) + VisibleRect:getVisibleRect().width/2, + 2*(logo_height-y + y_jitter) + VisibleRect:getVisibleRect().height/2 - logo_height/2), + 0.95, cc.PhysicsMaterial(0.01, 0.0, 0.0)); + + ball:getPhysicsBody():setMass(1.0); + ball:getPhysicsBody():setMoment(PHYSICS_INFINITY); + + layer.ball:addChild(ball); + end + end + end + + local bullet = makeBall(layer, cc.p(400, 0), 10, cc.PhysicsMaterial(PHYSICS_INFINITY, 0, 0)); + bullet:getPhysicsBody():setVelocity(cc.p(200, 0)); + bullet:setPosition(cc.p(-500, VisibleRect:getVisibleRect().height/2)) + layer.ball:addChild(bullet); + end + + initWithLayer(layer, onEnter) + Helper.titleLabel:setString("Logo Smash") + + return layer +end + +local function PhysicsDemoJoints() + local layer = cc.Layer:create() + local function onEnter() + layer:toggleDebug(); + + local touchListener = cc.EventListenerTouchOneByOne:create() + touchListener:registerScriptHandler(onTouchBegan, cc.Handler.EVENT_TOUCH_BEGAN) + touchListener:registerScriptHandler(onTouchMoved, cc.Handler.EVENT_TOUCH_MOVED) + touchListener:registerScriptHandler(onTouchEnded, cc.Handler.EVENT_TOUCH_ENDED) + local eventDispatcher = layer:getEventDispatcher() + eventDispatcher:addEventListenerWithSceneGraphPriority(touchListener, layer) + + local width = (VisibleRect:getVisibleRect().width - 10) / 4; + local height = (VisibleRect:getVisibleRect().height - 50) / 4; + + local node = cc.Node:create(); + local box = cc.PhysicsBody:create(); + node:setPhysicsBody(box); + box:setDynamic(false); + node:setPosition(cc.p(0, 0)); + layer:addChild(node); + + local scene = cc.Director:getInstance():getRunningScene(); + for i in range(0, 3) do + for j in range(0, 3) do + local offset = cc.p(VisibleRect:leftBottom().x + 5 + j * width + width/2, VisibleRect:leftBottom().y + 50 + i * height + height/2); + box:addShape(cc.PhysicsShapeEdgeBox:create(cc.size(width, height), cc.PHYSICSSHAPE_MATERIAL_DEFAULT, 1, offset)); + print("i,j") + print(i) + print(j) + local index = i*4 + j + if index == 0 then + local sp1 = makeBall(layer, cc.p(offset.x - 30, offset.y), 10); + sp1:getPhysicsBody():setTag(DRAG_BODYS_TAG); + local sp2 = makeBall(layer, cc.p(offset.x + 30, offset.y), 10); + sp2:getPhysicsBody():setTag(DRAG_BODYS_TAG); + + local joint = cc.PhysicsJointPin:construct(sp1:getPhysicsBody(), sp2:getPhysicsBody(), offset); + cc.Director:getInstance():getRunningScene():getPhysicsWorld():addJoint(joint); + + layer:addChild(sp1); + layer:addChild(sp2); + elseif index == 1 then + local sp1 = makeBall(layer, cc.p(offset.x - 30, offset.y), 10); + sp1:getPhysicsBody():setTag(DRAG_BODYS_TAG); + local sp2 = makeBox(cc.p(offset.x + 30, offset.y), cc.size(30, 10)); + sp2:getPhysicsBody():setTag(DRAG_BODYS_TAG); + + local joint = cc.PhysicsJointFixed:construct(sp1:getPhysicsBody(), sp2:getPhysicsBody(), offset); + scene:getPhysicsWorld():addJoint(joint); + + layer:addChild(sp1); + layer:addChild(sp2); + elseif index == 2 then + local sp1 = makeBall(layer, cc.p(offset.x - 30, offset.y), 10); + sp1:getPhysicsBody():setTag(DRAG_BODYS_TAG); + local sp2 = makeBox(cc.p(offset.x + 30, offset.y), cc.size(30, 10)); + sp2:getPhysicsBody():setTag(DRAG_BODYS_TAG); + + local joint = cc.PhysicsJointDistance:construct(sp1:getPhysicsBody(), sp2:getPhysicsBody(), cc.p(0, 0), cc.p(0, 0)); + scene:getPhysicsWorld():addJoint(joint); + + layer:addChild(sp1); + layer:addChild(sp2); + elseif index == 3 then + local sp1 = makeBall(layer, cc.p(offset.x - 30, offset.y), 10); + sp1:getPhysicsBody():setTag(DRAG_BODYS_TAG); + local sp2 = makeBox(cc.p(offset.x + 30, offset.y), cc.size(30, 10)); + sp2:getPhysicsBody():setTag(DRAG_BODYS_TAG); + + local joint = cc.PhysicsJointLimit:construct(sp1:getPhysicsBody(), sp2:getPhysicsBody(), cc.p(0, 0), cc.p(0, 0), 30.0, 60.0); + scene:getPhysicsWorld():addJoint(joint); + + layer:addChild(sp1); + layer:addChild(sp2); + elseif index == 4 then + local sp1 = makeBall(layer, cc.p(offset.x - 30, offset.y), 10); + sp1:getPhysicsBody():setTag(DRAG_BODYS_TAG); + local sp2 = makeBox(cc.p(offset.x + 30, offset.y), cc.size(30, 10)); + sp2:getPhysicsBody():setTag(DRAG_BODYS_TAG); + + local joint = cc.PhysicsJointSpring:construct(sp1:getPhysicsBody(), sp2:getPhysicsBody(), cc.p(0, 0), cc.p(0, 0), 500.0, 0.3); + scene:getPhysicsWorld():addJoint(joint); + + layer:addChild(sp1); + layer:addChild(sp2); + elseif index == 5 then + local sp1 = makeBall(layer, cc.p(offset.x - 30, offset.y), 10); + sp1:getPhysicsBody():setTag(DRAG_BODYS_TAG); + local sp2 = makeBox(cc.p(offset.x + 30, offset.y), cc.size(30, 10)); + sp2:getPhysicsBody():setTag(DRAG_BODYS_TAG); + + local joint = cc.PhysicsJointGroove:construct(sp1:getPhysicsBody(), sp2:getPhysicsBody(), cc.p(30, 15), cc.p(30, -15), cc.p(-30, 0)) + scene:getPhysicsWorld():addJoint(joint); + + layer:addChild(sp1); + layer:addChild(sp2); + elseif index == 6 then + local sp1 = makeBox(cc.p(offset.x - 30, offset.y), cc.size(30, 10)); + sp1:getPhysicsBody():setTag(DRAG_BODYS_TAG); + local sp2 = makeBox(cc.p(offset.x + 30, offset.y), cc.size(30, 10)); + sp2:getPhysicsBody():setTag(DRAG_BODYS_TAG); + scene:getPhysicsWorld():addJoint(cc.PhysicsJointPin:construct(sp1:getPhysicsBody(), box, cc.p(sp1:getPosition()))); + scene:getPhysicsWorld():addJoint(cc.PhysicsJointPin:construct(sp2:getPhysicsBody(), box, cc.p(sp2:getPosition()))); + local joint = cc.PhysicsJointRotarySpring:construct(sp1:getPhysicsBody(), sp2:getPhysicsBody(), 3000.0, 60.0); + scene:getPhysicsWorld():addJoint(joint); + + layer:addChild(sp1); + layer:addChild(sp2); + elseif index == 7 then + local sp1 = makeBox(cc.p(offset.x - 30, offset.y), cc.size(30, 10)); + sp1:getPhysicsBody():setTag(DRAG_BODYS_TAG); + local sp2 = makeBox(cc.p(offset.x + 30, offset.y), cc.size(30, 10)); + sp2:getPhysicsBody():setTag(DRAG_BODYS_TAG); + + scene:getPhysicsWorld():addJoint(cc.PhysicsJointPin:construct(sp1:getPhysicsBody(), box, cc.p(sp1:getPosition()))); + scene:getPhysicsWorld():addJoint(cc.PhysicsJointPin:construct(sp2:getPhysicsBody(), box, cc.p(sp2:getPosition()))); + local joint = cc.PhysicsJointRotaryLimit:construct(sp1:getPhysicsBody(), sp2:getPhysicsBody(), 0.0, math.pi/2); + scene:getPhysicsWorld():addJoint(joint); + + layer:addChild(sp1); + layer:addChild(sp2); + elseif index == 8 then + local sp1 = makeBox(cc.p(offset.x - 30, offset.y), cc.size(30, 10)); + sp1:getPhysicsBody():setTag(DRAG_BODYS_TAG); + local sp2 = makeBox(cc.p(offset.x + 30, offset.y), cc.size(30, 10)); + sp2:getPhysicsBody():setTag(DRAG_BODYS_TAG); + + scene:getPhysicsWorld():addJoint(cc.PhysicsJointPin:construct(sp1:getPhysicsBody(), box, cc.p(sp1:getPosition()))); + scene:getPhysicsWorld():addJoint(cc.PhysicsJointPin:construct(sp2:getPhysicsBody(), box, cc.p(sp2:getPosition()))); + local joint = cc.PhysicsJointRatchet:construct(sp1:getPhysicsBody(), sp2:getPhysicsBody(), 0.0, math.pi/2); + scene:getPhysicsWorld():addJoint(joint); + + layer:addChild(sp1); + layer:addChild(sp2); + elseif index == 9 then + local sp1 = makeBox(cc.p(offset.x - 30, offset.y), cc.size(30, 10)); + sp1:getPhysicsBody():setTag(DRAG_BODYS_TAG); + local sp2 = makeBox(cc.p(offset.x + 30, offset.y), cc.size(30, 10)); + sp2:getPhysicsBody():setTag(DRAG_BODYS_TAG); + + scene:getPhysicsWorld():addJoint(cc.PhysicsJointPin:construct(sp1:getPhysicsBody(), box, cc.p(sp1:getPosition()))); + scene:getPhysicsWorld():addJoint(cc.PhysicsJointPin:construct(sp2:getPhysicsBody(), box, cc.p(sp2:getPosition()))); + local joint = cc.PhysicsJointGear:construct(sp1:getPhysicsBody(), sp2:getPhysicsBody(), 0.0, 2.0); + scene:getPhysicsWorld():addJoint(joint); + + layer:addChild(sp1); + layer:addChild(sp2); + elseif index == 10 then + local sp1 = makeBox(cc.p(offset.x - 30, offset.y), cc.size(30, 10)); + sp1:getPhysicsBody():setTag(DRAG_BODYS_TAG); + local sp2 = makeBox(cc.p(offset.x + 30, offset.y), cc.size(30, 10)); + sp2:getPhysicsBody():setTag(DRAG_BODYS_TAG); + + scene:getPhysicsWorld():addJoint(cc.PhysicsJointPin:construct(sp1:getPhysicsBody(), box, cc.p(sp1:getPosition()))); + scene:getPhysicsWorld():addJoint(cc.PhysicsJointPin:construct(sp2:getPhysicsBody(), box, cc.p(sp2:getPosition()))); + local joint = cc.PhysicsJointMotor:construct(sp1:getPhysicsBody(), sp2:getPhysicsBody(), math.pi/2); + scene:getPhysicsWorld():addJoint(joint); + + layer:addChild(sp1); + layer:addChild(sp2); + end + end + end + end + + initWithLayer(layer, onEnter) + Helper.titleLabel:setString("Joints") + return layer +end + +local function PhysicsDemoPyramidStack() + local layer = cc.Layer:create() + + local function onEnter() + local touchListener = cc.EventListenerTouchOneByOne:create(); + touchListener:registerScriptHandler(onTouchBegan, cc.Handler.EVENT_TOUCH_BEGAN); + touchListener:registerScriptHandler(onTouchMoved, cc.Handler.EVENT_TOUCH_MOVED); + touchListener:registerScriptHandler(onTouchEnded, cc.Handler.EVENT_TOUCH_ENDED); + local eventDispatcher = layer:getEventDispatcher() + eventDispatcher:addEventListenerWithSceneGraphPriority(touchListener, layer); + + local node = cc.Node:create(); + node:setPhysicsBody(cc.PhysicsBody:createEdgeSegment(cc.p(VisibleRect:leftBottom().x, VisibleRect:leftBottom().y + 50), cc.p(VisibleRect:rightBottom().x, VisibleRect:rightBottom().y + 50))); + layer:addChild(node); + + local ball = cc.Sprite:create("Images/ball.png"); + ball:setScale(1); + ball:setPhysicsBody(cc.PhysicsBody:createCircle(10)); + ball:getPhysicsBody():setTag(DRAG_BODYS_TAG); + ball:setPosition(cc.p(VisibleRect:bottom().x, VisibleRect:bottom().y + 60)); + layer:addChild(ball); + + for i in range(0, 13) do + for j in range(0, i) do + local x = VisibleRect:bottom().x + (i/2 - j) * 11 + local y = VisibleRect:bottom().y + (14 - i) * 23 + 100 + local sp = addGrossiniAtPosition(layer, cc.p(x, y), 0.2); + sp:getPhysicsBody():setTag(DRAG_BODYS_TAG); + end + end + end + + initWithLayer(layer, onEnter) + Helper.titleLabel:setString("Pyramid Stack") + + return layer +end + +local function PhysicsDemoRayCast() + local layer = cc.Layer:create() + + local function onEnter() + local function onTouchEnded(touch, event) + local location = touch:getLocation(); + + local r = math.random(3); + if r ==1 then + layer:addChild(makeBall(layer, location, 5 + math.random()*10)); + elseif r == 2 then + layer:addChild(makeBox(location, cc.size(10 + math.random()*15, 10 + math.random()*15))); + elseif r == 3 then + layer:addChild(makeTriangle(location, cc.size(10 + math.random()*20, 10 + math.random()*20))); + end + end + + local touchListener = cc.EventListenerTouchOneByOne:create(); + touchListener:registerScriptHandler(function() return true end, cc.Handler.EVENT_TOUCH_BEGAN); + touchListener:registerScriptHandler(onTouchEnded, cc.Handler.EVENT_TOUCH_ENDED); + local eventDispatcher = layer:getEventDispatcher() + eventDispatcher:addEventListenerWithSceneGraphPriority(touchListener, layer); + + cc.Director:getInstance():getRunningScene():getPhysicsWorld():setGravity(cc.p(0,0)); + + local node = cc.DrawNode:create(); + node:setPhysicsBody(cc.PhysicsBody:createEdgeSegment(cc.p(VisibleRect:leftBottom().x, VisibleRect:leftBottom().y + 50), cc.p(VisibleRect:rightBottom().x, VisibleRect:rightBottom().y + 50))) + node:drawSegment(cc.p(VisibleRect:leftBottom().x, VisibleRect:leftBottom().y + 50), cc.p(VisibleRect:rightBottom().x, VisibleRect:rightBottom().y + 50), 1, STATIC_COLOR); + layer:addChild(node); + + local mode = 0 + cc.MenuItemFont:setFontSize(18); + local item = cc.MenuItemFont:create("Toogle debugChange Mode(any)") + local function changeModeCallback(sender) + mode = (mode + 1) % 3; + + if mode == 0 then + item:setString("Change Mode(any)"); + elseif mode == 1 then + item:setString("Change Mode(nearest)"); + elseif mode == 2 then + item:setString("Change Mode(multiple)"); + end + end + + item:registerScriptTapHandler(changeModeCallback) + + local menu = cc.Menu:create(item); + layer:addChild(menu); + menu:setPosition(cc.p(VisibleRect:left().x+100, VisibleRect:top().y-10)); + + local angle = 0 + local drawNode = nil + local function update(delta) + local L = 150.0; + local point1 = VisibleRect:center() + local d = cc.p(L * math.cos(angle), L * math.sin(angle)); + local point2 = cc.p(point1.x + d.x, point1.y + d.y) + + if drawNode then layer:removeChild(drawNode); end + drawNode = cc.DrawNode:create(); + if mode == 0 then + local point3 = cc.p(point2.x, point2.y) + local function func(world, info) + point3 = info.contact + return false + end + + cc.Director:getInstance():getRunningScene():getPhysicsWorld():rayCast(func, point1, point2); + drawNode:drawSegment(point1, point3, 1, STATIC_COLOR); + + if point2.x ~= point3.x or point2.y ~= point3.y then + drawNode:drawDot(point3, 2, cc.c4f(1.0, 1.0, 1.0, 1.0)); + end + layer:addChild(drawNode); + elseif mode == 1 then + local point3 = cc.p(point2.x, point2.y) + local friction = 1.0; + local function func(world, info) + if friction > info.fraction then + point3 = info.contact; + friction = info.fraction; + end + return true; + end + + cc.Director:getInstance():getRunningScene():getPhysicsWorld():rayCast(func, point1, point2); + drawNode:drawSegment(point1, point3, 1, STATIC_COLOR); + + if point2.x ~= point3.x or point2.y ~= point3.y then + drawNode:drawDot(point3, 2, cc.c4f(1.0, 1.0, 1.0, 1.0)); + end + layer:addChild(drawNode); + elseif mode == 2 then + local points = {} + + local function func(world, info) + points[#points + 1] = info.contact; + return true; + end + + cc.Director:getInstance():getRunningScene():getPhysicsWorld():rayCast(func, point1, point2); + drawNode:drawSegment(point1, point2, 1, STATIC_COLOR); + + for _, p in ipairs(points) do + drawNode:drawDot(p, 2, cc.c4f(1.0, 1.0, 1.0, 1.0)); + end + + layer:addChild(drawNode); + end + + angle = angle + 0.25 * math.pi / 180.0; + + end + + layer:scheduleUpdateWithPriorityLua(update, 0); + end + + initWithLayer(layer, onEnter) + Helper.titleLabel:setString("Ray Cast") + + return layer +end + +local function registerOnEnter() + layer:registerOn(PhysicsDemoLogoSmash) +end + +function PhysicsTest() + cclog("PhysicsTest") + local scene = cc.Scene:createWithPhysics() + + Helper.usePhysics = true + Helper.createFunctionTable = { + PhysicsDemoLogoSmash, + PhysicsDemoPyramidStack, + PhysicsDemoClickAdd, + PhysicsDemoRayCast, + PhysicsDemoJoints, + } + + scene:addChild(Helper.createFunctionTable[1]()) + scene:addChild(CreateBackMenuItem()) + return scene +end diff --git a/samples/Lua/TestLua/Resources/luaScript/helper.lua b/samples/Lua/TestLua/Resources/luaScript/helper.lua index 911924f1fe..c2706d02d3 100644 --- a/samples/Lua/TestLua/Resources/luaScript/helper.lua +++ b/samples/Lua/TestLua/Resources/luaScript/helper.lua @@ -31,6 +31,7 @@ end -- back menu callback local function MainMenuCallback() + Helper.usePhysics = false local scene = cc.Scene:create() scene:addChild(CreateTestMenu()) @@ -82,7 +83,12 @@ function Helper.restartAction() end function Helper.newScene() - local scene = cc.Scene:create() + local scene + if Helper.usePhysics then + scene = cc.Scene:createWithPhysics() + else + scene = cc.Scene:create() + end Helper.currentLayer = Helper.createFunctionTable[Helper.index]() scene:addChild(Helper.currentLayer) scene:addChild(CreateBackMenuItem()) diff --git a/samples/Lua/TestLua/Resources/luaScript/mainMenu.lua b/samples/Lua/TestLua/Resources/luaScript/mainMenu.lua index bba1c79a98..1e191f8726 100644 --- a/samples/Lua/TestLua/Resources/luaScript/mainMenu.lua +++ b/samples/Lua/TestLua/Resources/luaScript/mainMenu.lua @@ -51,6 +51,7 @@ require "luaScript/UserDefaultTest/UserDefaultTest" require "luaScript/ZwoptexTest/ZwoptexTest" require "luaScript/LuaBridgeTest/LuaBridgeTest" require "luaScript/XMLHttpRequestTest/XMLHttpRequestTest" +require "luaScript/PhysicsTest/PhysicsTest" local LINE_SPACE = 40 @@ -95,6 +96,7 @@ local _allTests = { { isSupported = true, name = "ParallaxTest" , create_func = ParallaxTestMain }, { isSupported = true, name = "ParticleTest" , create_func = ParticleTest }, { isSupported = true, name = "PerformanceTest" , create_func= PerformanceTestMain }, + { isSupported = true, name = "PhysicsTest" , create_func = PhysicsTest }, { isSupported = true, name = "RenderTextureTest" , create_func = RenderTextureTestMain }, { isSupported = true, name = "RotateWorldTest" , create_func = RotateWorldTest }, { isSupported = true, name = "SceneTest" , create_func = SceneTestMain }, diff --git a/tools/tolua/cocos2dx_physics.ini b/tools/tolua/cocos2dx_physics.ini new file mode 100644 index 0000000000..7c6c3ed78f --- /dev/null +++ b/tools/tolua/cocos2dx_physics.ini @@ -0,0 +1,67 @@ +[cocos2dx_physics] +# the prefix to be added to the generated functions. You might or might not use this in your own +# templates +prefix = cocos2dx_physics + +# create a target namespace (in javascript, this would create some code like the equiv. to `ns = ns || {}`) +# all classes will be embedded in that namespace +target_namespace = cc + +android_headers = -I%(androidndkdir)s/platforms/android-14/arch-arm/usr/include -I%(androidndkdir)s/sources/cxx-stl/gnu-libstdc++/4.7/libs/armeabi-v7a/include -I%(androidndkdir)s/sources/cxx-stl/gnu-libstdc++/4.7/include +android_flags = -D_SIZE_T_DEFINED_ + +clang_headers = -I%(clangllvmdir)s/lib/clang/3.3/include +clang_flags = -nostdinc -x c++ -std=c++11 + +cocos_headers = -I%(cocosdir)s/cocos/2d -I%(cocosdir)s/cocos/base -I%(cocosdir)s/cocos/physics -I%(cocosdir)s/cocos/2d/platform -I%(cocosdir)s/cocos/2d/platform/android -I%(cocosdir)s/cocos/math/kazmath/include -I%(cocosdir)s/cocos/physics +cocos_flags = -DANDROID -DCOCOS2D_JAVASCRIPT -DCC_USE_PHYSICS=1 + +cxxgenerator_headers = + +# extra arguments for clang +extra_arguments = %(android_headers)s %(clang_headers)s %(cxxgenerator_headers)s %(cocos_headers)s %(android_flags)s %(clang_flags)s %(cocos_flags)s %(extra_flags)s + +# what headers to parse +headers = %(cocosdir)s/cocos/2d/cocos2d.h + +# what classes to produce code for. You can use regular expressions here. When testing the regular +# expression, it will be enclosed in "^$", like this: "^Menu*$". +classes = Event(.*(Physics).*) Physics.* + +# what should we skip? in the format ClassName::[function function] +# ClassName is a regular expression, but will be used like this: "^ClassName$" functions are also +# regular expressions, they will not be surrounded by "^$". If you want to skip a whole class, just +# add a single "*" as functions. See bellow for several examples. A special class name is "*", which +# will apply to all class names. This is a convenience wildcard to be able to skip similar named +# functions from all classes. + +skip = PhysicsBody::[getJoints createPolygon createEdgeChain createEdgePolygon], + PhysicsShape::[recenterPoints getPolyonCenter], + PhysicsShapeBox::[getPoints], + PhysicsShapeEdgeBox::[getPoints], + PhysicsShapePolygon::[getPoints], + PhysicsShapeEdgePolygon::[getPoints], + PhysicsShapeEdgeChain::[getPoints], + PhysicsWorld::[getScene queryPoint queryRect rayCast] + + +rename_functions = + +rename_classes = + +# for all class names, should we remove something when registering in the target VM? +remove_prefix = + +# classes for which there will be no "parent" lookup +classes_have_no_parents = PhysicsWorld PhysicsJoint PhysicsContactPreSolve PhysicsContactPostSolve + +# base classes which will be skipped when their sub-classes found them. +base_classes_to_skip = + +# classes that create no constructor +# Set is special and we will use a hand-written constructor +abstract_classes = + +# Determining whether to use script object(js object) to control the lifecycle of native(cpp) object or the other way around. Supported values are 'yes' or 'no'. +script_control_cpp = no +