From 996f85d0d4d47b8ff5e04853858fdcea553f4917 Mon Sep 17 00:00:00 2001 From: Huynh Quoc Khanh aka Kan <70838508+Kan-Kzeit@users.noreply.github.com> Date: Mon, 20 Nov 2023 08:11:46 +0700 Subject: [PATCH] Core: adding some extensions getter method of box2D (#647) Co-authored-by: Vlad Svoka --- librtt/Rtt_DisplayObjectExtensions.cpp | 186 ++++++++++++++++++++++--- librtt/Rtt_DisplayObjectExtensions.h | 4 + 2 files changed, 168 insertions(+), 22 deletions(-) diff --git a/librtt/Rtt_DisplayObjectExtensions.cpp b/librtt/Rtt_DisplayObjectExtensions.cpp index 9cde99cd3..aa23e3f2e 100644 --- a/librtt/Rtt_DisplayObjectExtensions.cpp +++ b/librtt/Rtt_DisplayObjectExtensions.cpp @@ -268,6 +268,124 @@ DisplayObjectExtensions::resetMassData( lua_State *L ) return 0; } +int +DisplayObjectExtensions::getWorldVector(lua_State* L) +{ + DisplayObject* o = (DisplayObject*)LuaProxy::GetProxyableObject(L, 1); + + Rtt_WARN_SIM_PROXY_TYPE(L, 1, DisplayObject); + + if (o) + { + const PhysicsWorld& physics = LuaContext::GetRuntime(L)->GetPhysicsWorld(); + Real scale = physics.GetPixelsPerMeter(); + + Self* extensions = o->GetExtensions(); + b2Body* fBody = extensions->GetBody(); + + Real lx = Rtt_RealDiv(lua_tonumber(L, 2), scale); + Real ly = Rtt_RealDiv(lua_tonumber(L, 3), scale); + + b2Vec2 localVector = b2Vec2(Rtt_RealToFloat(lx), Rtt_RealToFloat(ly)); + + b2Vec2 worldVector = fBody->GetWorldVector(localVector); + + lua_pushnumber(L, worldVector.x); + lua_pushnumber(L, worldVector.y); + + return 2; + } + + return 0; +} + +int +DisplayObjectExtensions::getInertia(lua_State* L) +{ + DisplayObject* o = (DisplayObject*)LuaProxy::GetProxyableObject(L, 1); + + Rtt_WARN_SIM_PROXY_TYPE(L, 1, DisplayObject); + + if (o) + { + const PhysicsWorld& physics = LuaContext::GetRuntime(L)->GetPhysicsWorld(); + Real scale = physics.GetPixelsPerMeter(); + + Self* extensions = o->GetExtensions(); + b2Body* fBody = extensions->GetBody(); + + float32 inertia = fBody->GetInertia() * scale; + + lua_pushnumber(L, inertia); + + return 1; + } + + return 0; +} + +int +DisplayObjectExtensions::getLinearVelocityFromWorldPoint(lua_State* L) +{ + DisplayObject* o = (DisplayObject*)LuaProxy::GetProxyableObject(L, 1); + + Rtt_WARN_SIM_PROXY_TYPE(L, 1, DisplayObject); + + if (o) + { + const PhysicsWorld& physics = LuaContext::GetRuntime(L)->GetPhysicsWorld(); + Real scale = physics.GetPixelsPerMeter(); + + Self* extensions = o->GetExtensions(); + b2Body* fBody = extensions->GetBody(); + + Real wx = Rtt_RealDiv(lua_tonumber(L, 2), scale); + Real wy = Rtt_RealDiv(lua_tonumber(L, 3), scale); + + b2Vec2 worldPoint = b2Vec2(Rtt_RealToFloat(wx), Rtt_RealToFloat(wy)); + + b2Vec2 velocity = fBody->GetLinearVelocityFromWorldPoint(worldPoint); + + lua_pushnumber(L, velocity.x); + lua_pushnumber(L, velocity.y); + + return 2; + } + + return 0; +} + +int +DisplayObjectExtensions::getLinearVelocityFromLocalPoint(lua_State* L) +{ + DisplayObject* o = (DisplayObject*)LuaProxy::GetProxyableObject(L, 1); + + Rtt_WARN_SIM_PROXY_TYPE(L, 1, DisplayObject); + + if (o) + { + const PhysicsWorld& physics = LuaContext::GetRuntime(L)->GetPhysicsWorld(); + Real scale = physics.GetPixelsPerMeter(); + + Self* extensions = o->GetExtensions(); + b2Body* fBody = extensions->GetBody(); + + Real wx = Rtt_RealDiv(lua_tonumber(L, 2), scale); + Real ly = Rtt_RealDiv(lua_tonumber(L, 3), scale); + + b2Vec2 localPoint = b2Vec2(Rtt_RealToFloat(wx), Rtt_RealToFloat(ly)); + + b2Vec2 velocity = fBody->GetLinearVelocityFromLocalPoint(localPoint); + + lua_pushnumber(L, velocity.x); + lua_pushnumber(L, velocity.y); + + return 2; + } + + return 0; +} + #endif // Rtt_PHYSICS @@ -289,30 +407,34 @@ DisplayObjectExtensions::ValueForKey( lua_State *L, const MLuaProxyable& object, static const char * keys[] = { - "isAwake", // 0 - "isBodyActive", // 1 - "isBullet", // 2 - "isSleepingAllowed", // 3 - "isFixedRotation", // 4 - "angularVelocity", // 5 - "linearDamping", // 6 - "angularDamping", // 7 - "bodyType", // 8 - "setLinearVelocity", // 9 - "getLinearVelocity", // 10 - "applyForce", // 11 - "applyTorque", // 12 - "applyLinearImpulse", // 13 - "applyAngularImpulse", // 14 - "resetMassData", // 15 - "isSensor", // 16 - "mass", // 17 - "gravityScale", // 18 - "getMassWorldCenter", // 19 - "getMassLocalCenter", // 20 + "isAwake", // 0 + "isBodyActive", // 1 + "isBullet", // 2 + "isSleepingAllowed", // 3 + "isFixedRotation", // 4 + "angularVelocity", // 5 + "linearDamping", // 6 + "angularDamping", // 7 + "bodyType", // 8 + "setLinearVelocity", // 9 + "getLinearVelocity", // 10 + "applyForce", // 11 + "applyTorque", // 12 + "applyLinearImpulse", // 13 + "applyAngularImpulse", // 14 + "resetMassData", // 15 + "isSensor", // 16 + "mass", // 17 + "gravityScale", // 18 + "getMassWorldCenter", // 19 + "getMassLocalCenter", // 20 + "getWorldVector", // 21 + "getInertia", // 22 + "getLinearVelocityFromWorldPoint", // 23 + "getLinearVelocityFromLocalPoint", // 24 }; static const int numKeys = sizeof( keys ) / sizeof( const char * ); - static StringHash sHash( *LuaContext::GetAllocator( L ), keys, numKeys, 21, 24, 11, __FILE__, __LINE__ ); + static StringHash sHash( *LuaContext::GetAllocator( L ), keys, numKeys, 25, 19, 14, __FILE__, __LINE__ ); StringHash *hash = &sHash; int index = hash->Lookup( key ); @@ -435,6 +557,26 @@ DisplayObjectExtensions::ValueForKey( lua_State *L, const MLuaProxyable& object, lua_pushcfunction( L, Self::getMassLocalCenter ); } break; + case 21: + { + lua_pushcfunction(L, Self::getWorldVector ); + } + break; + case 22: + { + lua_pushcfunction(L, Self::getInertia); + } + break; + case 23: + { + lua_pushcfunction(L, Self::getLinearVelocityFromWorldPoint); + } + break; + case 24: + { + lua_pushcfunction(L, Self::getLinearVelocityFromLocalPoint); + } + break; default: { result = 0; diff --git a/librtt/Rtt_DisplayObjectExtensions.h b/librtt/Rtt_DisplayObjectExtensions.h index 277168389..14d1a9d55 100644 --- a/librtt/Rtt_DisplayObjectExtensions.h +++ b/librtt/Rtt_DisplayObjectExtensions.h @@ -54,6 +54,10 @@ class DisplayObjectExtensions : public LuaProxyVTable static int resetMassData( lua_State *L ); static int getMassWorldCenter( lua_State *L ); static int getMassLocalCenter( lua_State *L ); + static int getWorldVector( lua_State *L); + static int getInertia( lua_State *L ); + static int getLinearVelocityFromWorldPoint(lua_State *L); + static int getLinearVelocityFromLocalPoint(lua_State* L); #endif // Rtt_PHYSICS