Skip to content

Commit

Permalink
Core: adding some extensions getter method of box2D (#647)
Browse files Browse the repository at this point in the history
Co-authored-by: Vlad Svoka <vlad@solar2d.com>
  • Loading branch information
kan6868 and Shchvova committed Nov 20, 2023
1 parent 82d836f commit 996f85d
Show file tree
Hide file tree
Showing 2 changed files with 168 additions and 22 deletions.
186 changes: 164 additions & 22 deletions librtt/Rtt_DisplayObjectExtensions.cpp
Expand Up @@ -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


Expand All @@ -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 );
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions librtt/Rtt_DisplayObjectExtensions.h
Expand Up @@ -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

Expand Down

0 comments on commit 996f85d

Please sign in to comment.