Skip to content

Commit

Permalink
Added physics 3D Lua callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardodoria committed Dec 4, 2023
1 parent 170e5c4 commit 000e0cd
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 4 deletions.
27 changes: 27 additions & 0 deletions engine/core/script/LuaFunctionBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
#include "object/physics/Contact2D.h"
#include "object/physics/Manifold2D.h"
#include "object/physics/ContactImpulse2D.h"
#include "object/physics/Body3D.h"
#include "object/physics/Contact3D.h"
#include "object/physics/CollideShapeResult3D.h"
#include "LuaBinding.h"

#include "lua.hpp"
Expand Down Expand Up @@ -82,6 +85,10 @@ void LuaFunctionBase::push_value(lua_State *vm, int n){
lua_pushinteger(vm, n);
}

void LuaFunctionBase::push_value(lua_State *vm, unsigned int n){
lua_pushinteger(vm, n);
}

void LuaFunctionBase::push_value(lua_State *vm, float n){
lua_pushnumber(vm, n);
}
Expand All @@ -106,6 +113,11 @@ void LuaFunctionBase::push_value(lua_State *vm, size_t n){
lua_pushnumber(vm, n);
}

void LuaFunctionBase::push_value(lua_State *vm, Vector3 o){
if (!luabridge::push<Vector3>(vm, o))
throw luabridge::makeErrorCode(luabridge::ErrorCode::LuaStackOverflow);
}

void LuaFunctionBase::push_value(lua_State *vm, Body2D o){
if (!luabridge::push<Body2D>(vm, o))
throw luabridge::makeErrorCode(luabridge::ErrorCode::LuaStackOverflow);
Expand All @@ -126,6 +138,21 @@ void LuaFunctionBase::push_value(lua_State *vm, ContactImpulse2D o){
throw luabridge::makeErrorCode(luabridge::ErrorCode::LuaStackOverflow);
}

void LuaFunctionBase::push_value(lua_State *vm, Body3D o){
if (!luabridge::push<Body3D>(vm, o))
throw luabridge::makeErrorCode(luabridge::ErrorCode::LuaStackOverflow);
}

void LuaFunctionBase::push_value(lua_State *vm, Contact3D o){
if (!luabridge::push<Contact3D>(vm, o))
throw luabridge::makeErrorCode(luabridge::ErrorCode::LuaStackOverflow);
}

void LuaFunctionBase::push_value(lua_State *vm, CollideShapeResult3D o){
if (!luabridge::push<CollideShapeResult3D>(vm, o))
throw luabridge::makeErrorCode(luabridge::ErrorCode::LuaStackOverflow);
}

template <>
int LuaFunctionBase::get_value<int>(lua_State* vm){
int val = lua_tointeger(vm, -1);
Expand Down
9 changes: 9 additions & 0 deletions engine/core/script/LuaFunctionBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define LUA_FUNCTIONBASE_H

#include <string>
#include "math/Vector3.h"

typedef struct lua_State lua_State;

Expand All @@ -11,6 +12,9 @@ namespace Supernova{
class Contact2D;
class Manifold2D;
class ContactImpulse2D;
class Body3D;
class Contact3D;
class CollideShapeResult3D;

// the base function wrapper class
class LuaFunctionBase{
Expand All @@ -35,16 +39,21 @@ namespace Supernova{
// are implicitly convertible to one of the types

void push_value(lua_State *vm, int n);
void push_value(lua_State *vm, unsigned int n);
void push_value(lua_State *vm, float n);
void push_value(lua_State *vm, long n);
void push_value(lua_State *vm, bool b);
void push_value(lua_State *vm, const std::string &s);
void push_value(lua_State *vm, wchar_t s);
void push_value(lua_State *vm, size_t n);
void push_value(lua_State *vm, Vector3 n);
void push_value(lua_State *vm, Body2D o);
void push_value(lua_State *vm, Contact2D o);
void push_value(lua_State *vm, Manifold2D o);
void push_value(lua_State *vm, ContactImpulse2D o);
void push_value(lua_State *vm, Body3D o);
void push_value(lua_State *vm, Contact3D o);
void push_value(lua_State *vm, CollideShapeResult3D o);

// other overloads, for stuff like userdata or C functions

Expand Down
39 changes: 35 additions & 4 deletions engine/core/script/binding/CoreClassesLua.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
#include "Contact2D.h"
#include "Manifold2D.h"
#include "ContactImpulse2D.h"
#include "Body3D.h"
#include "Contact3D.h"
#include "CollideShapeResult3D.h"
#include "AudioSystem.h"
#include "PhysicsSystem.h"

Expand Down Expand Up @@ -209,33 +212,61 @@ void LuaBinding::registerCoreClasses(lua_State *L){
.endClass();

luabridge::getGlobalNamespace(L)
.beginClass<FunctionSubscribe<void(Contact2D)>>("FunctionSubscribe_V_C")
.beginClass<FunctionSubscribe<void(Contact2D)>>("FunctionSubscribe_V_C2")
.addFunction("__call", &FunctionSubscribe<void(Contact2D)>::call)
.addFunction("call", &FunctionSubscribe<void(Contact2D)>::call)
.addFunction("add", (bool (FunctionSubscribe<void(Contact2D)>::*)(const std::string&, lua_State*))&FunctionSubscribe<void(Contact2D)>::add)
.endClass();

luabridge::getGlobalNamespace(L)
.beginClass<FunctionSubscribe<void(Contact2D, Manifold2D)>>("FunctionSubscribe_V_CM")
.beginClass<FunctionSubscribe<void(Contact2D, Manifold2D)>>("FunctionSubscribe_V_C2M2")
.addFunction("__call", &FunctionSubscribe<void(Contact2D, Manifold2D)>::call)
.addFunction("call", &FunctionSubscribe<void(Contact2D, Manifold2D)>::call)
.addFunction("add", (bool (FunctionSubscribe<void(Contact2D, Manifold2D)>::*)(const std::string&, lua_State*))&FunctionSubscribe<void(Contact2D, Manifold2D)>::add)
.endClass();

luabridge::getGlobalNamespace(L)
.beginClass<FunctionSubscribe<void(Contact2D, ContactImpulse2D)>>("FunctionSubscribe_V_CC")
.beginClass<FunctionSubscribe<void(Contact2D, ContactImpulse2D)>>("FunctionSubscribe_V_C2CI2")
.addFunction("__call", &FunctionSubscribe<void(Contact2D, ContactImpulse2D)>::call)
.addFunction("call", &FunctionSubscribe<void(Contact2D, ContactImpulse2D)>::call)
.addFunction("add", (bool (FunctionSubscribe<void(Contact2D, ContactImpulse2D)>::*)(const std::string&, lua_State*))&FunctionSubscribe<void(Contact2D, ContactImpulse2D)>::add)
.endClass();

luabridge::getGlobalNamespace(L)
.beginClass<FunctionSubscribe<bool(Body2D, size_t, Body2D, size_t)>>("FunctionSubscribe_B_BSBS")
.beginClass<FunctionSubscribe<bool(Body2D, size_t, Body2D, size_t)>>("FunctionSubscribe_B_B2SB2S")
.addFunction("__call", &FunctionSubscribe<bool(Body2D, size_t, Body2D, size_t)>::call)
.addFunction("call", &FunctionSubscribe<bool(Body2D, size_t, Body2D, size_t)>::call)
.addFunction("add", (bool (FunctionSubscribe<bool(Body2D, size_t, Body2D, size_t)>::*)(const std::string&, lua_State*))&FunctionSubscribe<bool(Body2D, size_t, Body2D, size_t)>::add)
.endClass();

luabridge::getGlobalNamespace(L)
.beginClass<FunctionSubscribe<void(Body3D)>>("FunctionSubscribe_V_B3")
.addFunction("__call", &FunctionSubscribe<void(Body3D)>::call)
.addFunction("call", &FunctionSubscribe<void(Body3D)>::call)
.addFunction("add", (bool (FunctionSubscribe<void(Body3D)>::*)(const std::string&, lua_State*))&FunctionSubscribe<void(Body3D)>::add)
.endClass();

luabridge::getGlobalNamespace(L)
.beginClass<FunctionSubscribe<void(Body3D, Body3D, Contact3D)>>("FunctionSubscribe_V_B3B3C3")
.addFunction("__call", &FunctionSubscribe<void(Body3D, Body3D, Contact3D)>::call)
.addFunction("call", &FunctionSubscribe<void(Body3D, Body3D, Contact3D)>::call)
.addFunction("add", (bool (FunctionSubscribe<void(Body3D, Body3D, Contact3D)>::*)(const std::string&, lua_State*))&FunctionSubscribe<void(Body3D, Body3D, Contact3D)>::add)
.endClass();

luabridge::getGlobalNamespace(L)
.beginClass<FunctionSubscribe<void(unsigned int, unsigned int, int, int)>>("FunctionSubscribe_V_UUII")
.addFunction("__call", &FunctionSubscribe<void(unsigned int, unsigned int, int, int)>::call)
.addFunction("call", &FunctionSubscribe<void(unsigned int, unsigned int, int, int)>::call)
.addFunction("add", (bool (FunctionSubscribe<void(unsigned int, unsigned int, int, int)>::*)(const std::string&, lua_State*))&FunctionSubscribe<void(unsigned int, unsigned int, int, int)>::add)
.endClass();

luabridge::getGlobalNamespace(L)
.beginClass<FunctionSubscribe<void(Body3D, Body3D, Vector3, CollideShapeResult3D)>>("FunctionSubscribe_V_B3B3V3CR3")
.addFunction("__call", &FunctionSubscribe<void(Body3D, Body3D, Vector3, CollideShapeResult3D)>::call)
.addFunction("call", &FunctionSubscribe<void(Body3D, Body3D, Vector3, CollideShapeResult3D)>::call)
.addFunction("add", (bool (FunctionSubscribe<void(Body3D, Body3D, Vector3, CollideShapeResult3D)>::*)(const std::string&, lua_State*))&FunctionSubscribe<void(Body3D, Body3D, Vector3, CollideShapeResult3D)>::add)
.endClass();

luabridge::getGlobalNamespace(L)
.beginClass<Texture>("Texture")
.addConstructor <void (*) (void), void (*) (std::string), void (*) (TextureData, std::string)> ()
Expand Down
7 changes: 7 additions & 0 deletions engine/core/script/binding/ECSClassesLua.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@ void LuaBinding::registerECSClasses(lua_State *L){
.addProperty("postSolve2D", [] (PhysicsSystem* self, lua_State* L) { return &self->postSolve2D; }, [] (PhysicsSystem* self, lua_State* L) { self->postSolve2D = L; })
.addProperty("shouldCollide2D", [] (PhysicsSystem* self, lua_State* L) { return &self->shouldCollide2D; }, [] (PhysicsSystem* self, lua_State* L) { self->shouldCollide2D = L; })

.addProperty("onBodyActivated3D", [] (PhysicsSystem* self, lua_State* L) { return &self->onBodyActivated3D; }, [] (PhysicsSystem* self, lua_State* L) { self->onBodyActivated3D = L; })
.addProperty("onBodyDeactivated3D", [] (PhysicsSystem* self, lua_State* L) { return &self->onBodyDeactivated3D; }, [] (PhysicsSystem* self, lua_State* L) { self->onBodyDeactivated3D = L; })
.addProperty("onContactAdded3D", [] (PhysicsSystem* self, lua_State* L) { return &self->onContactAdded3D; }, [] (PhysicsSystem* self, lua_State* L) { self->onContactAdded3D = L; })
.addProperty("onContactPersisted3D", [] (PhysicsSystem* self, lua_State* L) { return &self->onContactPersisted3D; }, [] (PhysicsSystem* self, lua_State* L) { self->onContactPersisted3D = L; })
.addProperty("onContactRemoved3D", [] (PhysicsSystem* self, lua_State* L) { return &self->onContactRemoved3D; }, [] (PhysicsSystem* self, lua_State* L) { self->onContactRemoved3D = L; })
.addProperty("shouldCollide3D", [] (PhysicsSystem* self, lua_State* L) { return &self->shouldCollide3D; }, [] (PhysicsSystem* self, lua_State* L) { self->shouldCollide3D = L; })

.addFunction("createBody2D", &PhysicsSystem::createBody2D)
.addFunction("removeBody2D", &PhysicsSystem::removeBody2D)

Expand Down

0 comments on commit 000e0cd

Please sign in to comment.