Skip to content

Commit

Permalink
Lua bindings for physics 2D
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardodoria committed Sep 11, 2023
1 parent 3875ff0 commit f6c3f03
Show file tree
Hide file tree
Showing 13 changed files with 307 additions and 43 deletions.
8 changes: 7 additions & 1 deletion engine/core/component/Body2DComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,16 @@ namespace Supernova{

enum class Body2DType{
STATIC,
kINEMATIC,
KINEMATIC,
DYNAMIC
};

enum class Manifold2DType{
CIRCLES,
FACEA,
FACEB
};

// fixture in Box2D
struct CollisionShape2D{
b2Fixture* fixture = NULL;
Expand Down
1 change: 1 addition & 0 deletions engine/core/component/MeshComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "buffer/InterleavedBuffer.h"
#include "buffer/IndexBuffer.h"
#include "buffer/ExternalBuffer.h"
#include "math/Rect.h"
#include <map>
#include <memory>

Expand Down
42 changes: 21 additions & 21 deletions engine/core/object/physics/Body2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ using namespace Supernova;
b2BodyType getBodyTypeToB2(Body2DType type){
if (type == Body2DType::STATIC){
return b2_staticBody;
}else if (type == Body2DType::kINEMATIC){
}else if (type == Body2DType::KINEMATIC){
return b2_kinematicBody;
}else if (type == Body2DType::DYNAMIC){
return b2_dynamicBody;
Expand All @@ -27,7 +27,7 @@ Body2DType getB2ToBodyType(b2BodyType type){
if (type == b2_staticBody){
return Body2DType::STATIC;
}else if (type == b2_kinematicBody){
return Body2DType::kINEMATIC;
return Body2DType::KINEMATIC;
}else if (type == b2_dynamicBody){
return Body2DType::DYNAMIC;
}
Expand Down Expand Up @@ -310,19 +310,19 @@ float Body2D::getGravityScale() const{
return body.body->GetGravityScale();
}

void Body2D::setFilterCategoryBits(uint16_t categoryBits){
setFilterCategoryBits(0, categoryBits);
void Body2D::setCategoryBitsFilter(uint16_t categoryBits){
setCategoryBitsFilter(0, categoryBits);
}

void Body2D::setFilterMaskBits(uint16_t maskBits){
setFilterMaskBits(0, maskBits);
void Body2D::setMaskBitsFilter(uint16_t maskBits){
setMaskBitsFilter(0, maskBits);
}

void Body2D::setFilterGroupIndex(int16_t groupIndex){
setFilterGroupIndex(0, groupIndex);
void Body2D::setGroupIndexFilter(int16_t groupIndex){
setGroupIndexFilter(0, groupIndex);
}

void Body2D::setFilterCategoryBits(size_t shapeIndex, uint16_t categoryBits){
void Body2D::setCategoryBitsFilter(size_t shapeIndex, uint16_t categoryBits){
Body2DComponent& body = getComponent<Body2DComponent>();

if (shapeIndex >= 0 && shapeIndex < MAX_SHAPES){
Expand All @@ -336,7 +336,7 @@ void Body2D::setFilterCategoryBits(size_t shapeIndex, uint16_t categoryBits){
}
}

void Body2D::setFilterMaskBits(size_t shapeIndex, uint16_t maskBits){
void Body2D::setMaskBitsFilter(size_t shapeIndex, uint16_t maskBits){
Body2DComponent& body = getComponent<Body2DComponent>();

if (shapeIndex >= 0 && shapeIndex < MAX_SHAPES){
Expand All @@ -350,7 +350,7 @@ void Body2D::setFilterMaskBits(size_t shapeIndex, uint16_t maskBits){
}
}

void Body2D::setFilterGroupIndex(size_t shapeIndex, int16_t groupIndex){
void Body2D::setGroupIndexFilter(size_t shapeIndex, int16_t groupIndex){
Body2DComponent& body = getComponent<Body2DComponent>();

if (shapeIndex >= 0 && shapeIndex < MAX_SHAPES){
Expand All @@ -364,19 +364,19 @@ void Body2D::setFilterGroupIndex(size_t shapeIndex, int16_t groupIndex){
}
}

uint16_t Body2D::getFilterCategoryBits() const{
return getFilterCategoryBits(0);
uint16_t Body2D::getCategoryBitsFilter() const{
return getCategoryBitsFilter(0);
}

uint16_t Body2D::getFilterMaskBits() const{
return getFilterMaskBits(0);
uint16_t Body2D::getMaskBitsFilter() const{
return getMaskBitsFilter(0);
}

int16_t Body2D::getFilterGroupIndex() const{
return getFilterGroupIndex(0);
int16_t Body2D::getGroupIndexFilter() const{
return getGroupIndexFilter(0);
}

uint16_t Body2D::getFilterCategoryBits(size_t shapeIndex) const{
uint16_t Body2D::getCategoryBitsFilter(size_t shapeIndex) const{
Body2DComponent& body = getComponent<Body2DComponent>();

if (shapeIndex >= 0 && shapeIndex < MAX_SHAPES){
Expand All @@ -388,7 +388,7 @@ uint16_t Body2D::getFilterCategoryBits(size_t shapeIndex) const{
return 0;
}

uint16_t Body2D::getFilterMaskBits(size_t shapeIndex) const{
uint16_t Body2D::getMaskBitsFilter(size_t shapeIndex) const{
Body2DComponent& body = getComponent<Body2DComponent>();

if (shapeIndex >= 0 && shapeIndex < MAX_SHAPES){
Expand All @@ -400,7 +400,7 @@ uint16_t Body2D::getFilterMaskBits(size_t shapeIndex) const{
return 0;
}

int16_t Body2D::getFilterGroupIndex(size_t shapeIndex) const{
int16_t Body2D::getGroupIndexFilter(size_t shapeIndex) const{
Body2DComponent& body = getComponent<Body2DComponent>();

if (shapeIndex >= 0 && shapeIndex < MAX_SHAPES){
Expand All @@ -424,7 +424,7 @@ float Body2D::getInertia() const{
return body.body->GetInertia();
}

Vector2 Body2D::getLinearVelocity(Vector2 worldPoint) const{
Vector2 Body2D::getLinearVelocityFromWorldPoint(Vector2 worldPoint) const{
Body2DComponent& body = getComponent<Body2DComponent>();
float pointsToMeterScale = scene->getSystem<PhysicsSystem>()->getPointsToMeterScale();

Expand Down
31 changes: 18 additions & 13 deletions engine/core/object/physics/Body2D.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
#define BODY2D_H

#include "EntityHandle.h"
#include "math/Vector2.h"
#include "component/Body2DComponent.h"

class b2Body;
class b2Fixture;

namespace Supernova{

Expand Down Expand Up @@ -68,25 +73,25 @@ namespace Supernova{
bool isEnabled() const;
float getGravityScale() const;

void setFilterCategoryBits(uint16_t categoryBits);
void setFilterMaskBits(uint16_t maskBits);
void setFilterGroupIndex(int16_t groupIndex);
void setCategoryBitsFilter(uint16_t categoryBits);
void setMaskBitsFilter(uint16_t maskBits);
void setGroupIndexFilter(int16_t groupIndex);

void setFilterCategoryBits(size_t shapeIndex, uint16_t categoryBits);
void setFilterMaskBits(size_t shapeIndex, uint16_t maskBits);
void setFilterGroupIndex(size_t shapeIndex, int16_t groupIndex);
void setCategoryBitsFilter(size_t shapeIndex, uint16_t categoryBits);
void setMaskBitsFilter(size_t shapeIndex, uint16_t maskBits);
void setGroupIndexFilter(size_t shapeIndex, int16_t groupIndex);

uint16_t getFilterCategoryBits() const;
uint16_t getFilterMaskBits() const;
int16_t getFilterGroupIndex() const;
uint16_t getCategoryBitsFilter() const;
uint16_t getMaskBitsFilter() const;
int16_t getGroupIndexFilter() const;

uint16_t getFilterCategoryBits(size_t shapeIndex) const;
uint16_t getFilterMaskBits(size_t shapeIndex) const;
int16_t getFilterGroupIndex(size_t shapeIndex) const;
uint16_t getCategoryBitsFilter(size_t shapeIndex) const;
uint16_t getMaskBitsFilter(size_t shapeIndex) const;
int16_t getGroupIndexFilter(size_t shapeIndex) const;

float getMass() const;
float getInertia() const;
Vector2 getLinearVelocity(Vector2 worldPoint) const;
Vector2 getLinearVelocityFromWorldPoint(Vector2 worldPoint) const;

void applyForce(const Vector2& force, const Vector2& point, bool wake);
void applyForceToCenter(const Vector2& force, bool wake);
Expand Down
6 changes: 0 additions & 6 deletions engine/core/object/physics/Manifold2D.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@ class b2Manifold;

namespace Supernova{

enum class Manifold2DType{
CIRCLES,
FACEA,
FACEB
};

class Manifold2D{
private:
Scene* scene;
Expand Down
5 changes: 5 additions & 0 deletions engine/core/script/LuaBridgeAddon.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ namespace luabridge
template<> struct Stack<ContainerType> : EnumWrapper<ContainerType>{};
template<> struct Stack<PivotPreset> : EnumWrapper<PivotPreset>{};

template<> struct Stack<CollisionShape2DType> : EnumWrapper<CollisionShape2DType>{};
template<> struct Stack<Body2DType> : EnumWrapper<Body2DType>{};
template<> struct Stack<Joint2DType> : EnumWrapper<Joint2DType>{};
template<> struct Stack<Manifold2DType> : EnumWrapper<Manifold2DType>{};

template <>
struct Stack <Touch>
{
Expand Down
29 changes: 29 additions & 0 deletions engine/core/script/LuaFunctionBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@
#include "LuaFunction.h"

#include "util/StringUtils.h"
#include "object/physics/Body2D.h"
#include "object/physics/Contact2D.h"
#include "object/physics/Manifold2D.h"
#include "object/physics/ContactImpulse2D.h"
#include "LuaBinding.h"

#include "lua.hpp"
#include "LuaBridge.h"
#include <stdexcept>

using namespace Supernova;
Expand Down Expand Up @@ -97,6 +102,30 @@ void LuaFunctionBase::push_value(lua_State *vm, wchar_t s){
lua_pushstring(vm, StringUtils::toUTF8(s).c_str());
}

void LuaFunctionBase::push_value(lua_State *vm, size_t n){
lua_pushnumber(vm, n);
}

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

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

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

void LuaFunctionBase::push_value(lua_State *vm, ContactImpulse2D o){
if (!luabridge::push<ContactImpulse2D>(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
10 changes: 10 additions & 0 deletions engine/core/script/LuaFunctionBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ typedef struct lua_State lua_State;

namespace Supernova{

class Body2D;
class Contact2D;
class Manifold2D;
class ContactImpulse2D;

// the base function wrapper class
class LuaFunctionBase{

Expand Down Expand Up @@ -35,6 +40,11 @@ namespace Supernova{
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, 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);

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

Expand Down
32 changes: 32 additions & 0 deletions engine/core/script/binding/CoreClassesLua.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
#include "Log.h"
#include "Input.h"
#include "System.h"
#include "Body2D.h"
#include "Contact2D.h"
#include "Manifold2D.h"
#include "ContactImpulse2D.h"

using namespace Supernova;

Expand Down Expand Up @@ -202,6 +206,34 @@ void LuaBinding::registerCoreClasses(lua_State *L){
.addFunction("add", (bool (FunctionSubscribe<float(float)>::*)(const std::string&, lua_State*))&FunctionSubscribe<float(float)>::add)
.endClass();

luabridge::getGlobalNamespace(L)
.beginClass<FunctionSubscribe<void(Contact2D)>>("FunctionSubscribe_V_C")
.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")
.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")
.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")
.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<Texture>("Texture")
.addConstructor <void (*) (void), void (*) (std::string), void (*) (TextureData, std::string)> ()
Expand Down
8 changes: 8 additions & 0 deletions engine/core/script/binding/ECSClassesLua.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "lua.hpp"

#include "LuaBridge.h"
#include "LuaBridgeAddon.h"

#include "ecs/Entity.h"
#include "ecs/Signature.h"
Expand All @@ -15,6 +16,7 @@

//TODO: Add all systems
#include "subsystem/AudioSystem.h"
#include "subsystem/PhysicsSystem.h"

//TODO: Add all components and properties
#include "component/ActionComponent.h"
Expand Down Expand Up @@ -105,6 +107,12 @@ void LuaBinding::registerECSClasses(lua_State *L){
.addStaticProperty("globalVolume", &AudioSystem::getGlobalVolume, &AudioSystem::setGlobalVolume)
.endClass();

luabridge::getGlobalNamespace(L)
.beginClass<PhysicsSystem>("PhysicsSystem")
.addProperty("pointsToMeterScale", &PhysicsSystem::getPointsToMeterScale, &PhysicsSystem::setPointsToMeterScale)
.addProperty("beginContact2D", [] (PhysicsSystem* self, lua_State* L) { return &self->beginContact2D; }, [] (PhysicsSystem* self, lua_State* L) { self->beginContact2D = L; })
.endClass();

luabridge::getGlobalNamespace(L)
.beginClass<EntityManager>("EntityManager")
.addConstructor <void (*) (void)> ()
Expand Down
Loading

0 comments on commit f6c3f03

Please sign in to comment.