Skip to content

Commit

Permalink
Added more Lua bindings and Box2D shape types
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardodoria committed Sep 12, 2023
1 parent edbdb7d commit 4fdb5f3
Show file tree
Hide file tree
Showing 4 changed files with 215 additions and 1 deletion.
4 changes: 3 additions & 1 deletion engine/core/script/binding/CoreClassesLua.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "Manifold2D.h"
#include "ContactImpulse2D.h"
#include "AudioSystem.h"
#include "PhysicsSystem.h"

using namespace Supernova;

Expand Down Expand Up @@ -330,7 +331,8 @@ void LuaBinding::registerCoreClasses(lua_State *L){
.addFunction("moveChildUp", &Scene::moveChildUp)
.addFunction("moveChildDown", &Scene::moveChildDown)
.addFunction("moveChildToLast", &Scene::moveChildToLast)
.addFunction("getAudioSystem", [] (Scene* self, lua_State* L) -> AudioSystem* { return self->getSystem<AudioSystem>().get(); })
.addFunction("getAudioSystem", [] (Scene* self, lua_State* L) { return self->getSystem<AudioSystem>().get(); })
.addFunction("getPhysicsSystem", [] (Scene* self, lua_State* L) { return self->getSystem<PhysicsSystem>().get(); })
.endClass();

luabridge::getGlobalNamespace(L)
Expand Down
26 changes: 26 additions & 0 deletions engine/core/script/binding/ECSClassesLua.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,32 @@ void LuaBinding::registerECSClasses(lua_State *L){
.addProperty("preSolve2D", [] (PhysicsSystem* self, lua_State* L) { return &self->preSolve2D; }, [] (PhysicsSystem* self, lua_State* L) { self->preSolve2D = 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; })
.addFunction("createBody2D", &PhysicsSystem::createBody2D)
.addFunction("removeBody2D", &PhysicsSystem::removeBody2D)
.addFunction("addRectShape2D", &PhysicsSystem::addRectShape2D)
.addFunction("addPolygonShape2D", &PhysicsSystem::addPolygonShape2D)
.addFunction("addCircleShape2D", &PhysicsSystem::addCircleShape2D)
.addFunction("addTwoSidedEdgeShape2D", &PhysicsSystem::addTwoSidedEdgeShape2D)
.addFunction("addOneSidedEdgeShape2D", &PhysicsSystem::addOneSidedEdgeShape2D)
.addFunction("addLoopChainShape2D", &PhysicsSystem::addLoopChainShape2D)
.addFunction("addChainShape2D", &PhysicsSystem::addChainShape2D)
.addFunction("removeAllShapes", &PhysicsSystem::removeAllShapes)
.addFunction("loadBody2D", &PhysicsSystem::loadBody2D)
.addFunction("destroyBody2D", &PhysicsSystem::destroyBody2D)
.addFunction("loadShape2D", &PhysicsSystem::loadShape2D)
.addFunction("destroyShape2D", &PhysicsSystem::destroyShape2D)
.addFunction("loadDistanceJoint2D", &PhysicsSystem::loadDistanceJoint2D)
.addFunction("loadRevoluteJoint2D", &PhysicsSystem::loadRevoluteJoint2D)
.addFunction("loadPrismaticJoint2D", &PhysicsSystem::loadPrismaticJoint2D)
.addFunction("loadPulleyJoint2D", &PhysicsSystem::loadPulleyJoint2D)
.addFunction("loadGearJoint2D", &PhysicsSystem::loadGearJoint2D)
.addFunction("loadMouseJoint2D", &PhysicsSystem::loadMouseJoint2D)
.addFunction("loadWheelJoint2D", &PhysicsSystem::loadWheelJoint2D)
.addFunction("loadWeldJoint2D", &PhysicsSystem::loadWeldJoint2D)
.addFunction("loadFrictionJoint2D", &PhysicsSystem::loadFrictionJoint2D)
.addFunction("loadMotorJoint2D", &PhysicsSystem::loadMotorJoint2D)
.addFunction("loadRopeJoint2D", &PhysicsSystem::loadRopeJoint2D)
.addFunction("destroyJoint2D", &PhysicsSystem::destroyJoint2D)
.endClass();

luabridge::getGlobalNamespace(L)
Expand Down
177 changes: 177 additions & 0 deletions engine/core/subsystem/PhysicsSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,182 @@ int PhysicsSystem::addRectShape2D(Entity entity, float width, float height){
return -1;
}

int PhysicsSystem::addPolygonShape2D(Entity entity, std::vector<Vector2> vertices){
Body2DComponent* body = scene->findComponent<Body2DComponent>(entity);

if (body){
if (body->numShapes < MAX_SHAPES){

body->shapes[body->numShapes].type = CollisionShape2DType::POLYGON;

b2PolygonShape polygon;
b2Vec2 b2vertices[vertices.size()];
for (int i = 0; i < vertices.size(); i++){
b2vertices[i].Set(vertices[i].x / pointsToMeterScale, vertices[i].y / pointsToMeterScale);
}
polygon.Set(b2vertices, vertices.size());

loadShape2D(*body, &polygon, body->numShapes);

body->numShapes++;

return (body->numShapes - 1);
}else{
Log::error("Cannot add more shapes in this body, please increase value MAX_SHAPES");
}
}

return -1;
}

int PhysicsSystem::addCircleShape2D(Entity entity, Vector2 center, float radius){
Body2DComponent* body = scene->findComponent<Body2DComponent>(entity);

if (body){
if (body->numShapes < MAX_SHAPES){

body->shapes[body->numShapes].type = CollisionShape2DType::POLYGON;

b2CircleShape circle;
circle.m_p.Set(center.x / pointsToMeterScale, center.y / pointsToMeterScale);
circle.m_radius = radius / pointsToMeterScale;

loadShape2D(*body, &circle, body->numShapes);

body->numShapes++;

return (body->numShapes - 1);
}else{
Log::error("Cannot add more shapes in this body, please increase value MAX_SHAPES");
}
}

return -1;
}

int PhysicsSystem::addTwoSidedEdgeShape2D(Entity entity, Vector2 vertice1, Vector2 vertice2){
Body2DComponent* body = scene->findComponent<Body2DComponent>(entity);

if (body){
if (body->numShapes < MAX_SHAPES){

body->shapes[body->numShapes].type = CollisionShape2DType::POLYGON;

b2EdgeShape edge;
b2Vec2 v1(vertice1.x / pointsToMeterScale, vertice1.y / pointsToMeterScale);
b2Vec2 v2(vertice2.x / pointsToMeterScale, vertice2.y / pointsToMeterScale);
edge.SetTwoSided(v1, v2);

loadShape2D(*body, &edge, body->numShapes);

body->numShapes++;

return (body->numShapes - 1);
}else{
Log::error("Cannot add more shapes in this body, please increase value MAX_SHAPES");
}
}

return -1;
}

int PhysicsSystem::addOneSidedEdgeShape2D(Entity entity, Vector2 vertice0, Vector2 vertice1, Vector2 vertice2, Vector2 vertice3){
Body2DComponent* body = scene->findComponent<Body2DComponent>(entity);

if (body){
if (body->numShapes < MAX_SHAPES){

body->shapes[body->numShapes].type = CollisionShape2DType::POLYGON;

b2EdgeShape edge;
b2Vec2 v0(vertice0.x / pointsToMeterScale, vertice0.y / pointsToMeterScale);
b2Vec2 v1(vertice1.x / pointsToMeterScale, vertice1.y / pointsToMeterScale);
b2Vec2 v2(vertice2.x / pointsToMeterScale, vertice2.y / pointsToMeterScale);
b2Vec2 v3(vertice3.x / pointsToMeterScale, vertice3.y / pointsToMeterScale);
edge.SetOneSided(v0, v1, v2, v3);

loadShape2D(*body, &edge, body->numShapes);

body->numShapes++;

return (body->numShapes - 1);
}else{
Log::error("Cannot add more shapes in this body, please increase value MAX_SHAPES");
}
}

return -1;
}

int PhysicsSystem::addLoopChainShape2D(Entity entity, std::vector<Vector2> vertices){
Body2DComponent* body = scene->findComponent<Body2DComponent>(entity);

if (body){
if (body->numShapes < MAX_SHAPES){

body->shapes[body->numShapes].type = CollisionShape2DType::POLYGON;

b2ChainShape chain;
b2Vec2 b2vertices[vertices.size()];
for (int i = 0; i < vertices.size(); i++){
b2vertices[i].Set(vertices[i].x / pointsToMeterScale, vertices[i].y / pointsToMeterScale);
}
chain.CreateLoop(b2vertices, vertices.size());

loadShape2D(*body, &chain, body->numShapes);

body->numShapes++;

return (body->numShapes - 1);
}else{
Log::error("Cannot add more shapes in this body, please increase value MAX_SHAPES");
}
}

return -1;
}

int PhysicsSystem::addChainShape2D(Entity entity, std::vector<Vector2> vertices, Vector2 prevVertex, Vector2 nextVertex){
Body2DComponent* body = scene->findComponent<Body2DComponent>(entity);

if (body){
if (body->numShapes < MAX_SHAPES){

body->shapes[body->numShapes].type = CollisionShape2DType::POLYGON;

b2ChainShape chain;
b2Vec2 b2vertices[vertices.size()];
for (int i = 0; i < vertices.size(); i++){
b2vertices[i].Set(vertices[i].x / pointsToMeterScale, vertices[i].y / pointsToMeterScale);
}
b2Vec2 pv(prevVertex.x / pointsToMeterScale, prevVertex.y / pointsToMeterScale);
b2Vec2 nv(nextVertex.x / pointsToMeterScale, nextVertex.y / pointsToMeterScale);
chain.CreateChain(b2vertices, vertices.size(), pv, nv);

loadShape2D(*body, &chain, body->numShapes);

body->numShapes++;

return (body->numShapes - 1);
}else{
Log::error("Cannot add more shapes in this body, please increase value MAX_SHAPES");
}
}

return -1;
}

void PhysicsSystem::removeAllShapes(Entity entity){
Body2DComponent* body = scene->findComponent<Body2DComponent>(entity);

if (body){
for (int i = 0; i < body->numShapes; i++){
destroyShape2D(*body, i);
}
body->numShapes = 0;
}
}

b2Body* PhysicsSystem::getBody(Entity entity){
Body2DComponent* body = scene->findComponent<Body2DComponent>(entity);

Expand Down Expand Up @@ -139,6 +315,7 @@ void PhysicsSystem::destroyBody2D(Body2DComponent& body){
for (int i = 0; i < body.numShapes; i++){
destroyShape2D(body, i);
}
body.numShapes = 0;

world2D->DestroyBody(body.body);

Expand Down
9 changes: 9 additions & 0 deletions engine/core/subsystem/PhysicsSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,16 @@ namespace Supernova{

void createBody2D(Entity entity);
void removeBody2D(Entity entity);

int addRectShape2D(Entity entity, float width, float height);
int addPolygonShape2D(Entity entity, std::vector<Vector2> vertices);
int addCircleShape2D(Entity entity, Vector2 center, float radius);
int addTwoSidedEdgeShape2D(Entity entity, Vector2 vertice1, Vector2 vertice2);
int addOneSidedEdgeShape2D(Entity entity, Vector2 vertice0, Vector2 vertice1, Vector2 vertice2, Vector2 vertice3);
int addLoopChainShape2D(Entity entity, std::vector<Vector2> vertices);
int addChainShape2D(Entity entity, std::vector<Vector2> vertices, Vector2 prevVertex, Vector2 nextVertex);

void removeAllShapes(Entity entity);

b2Body* getBody(Entity entity);

Expand Down

0 comments on commit 4fdb5f3

Please sign in to comment.