From f907e82a2dc71808eaffc53b5663f324a4337dc3 Mon Sep 17 00:00:00 2001 From: eduardodoria Date: Thu, 17 Aug 2023 22:16:56 -0300 Subject: [PATCH] Added more functions to Body2D --- engine/core/component/Body2DComponent.h | 10 +- engine/core/object/Body2D.cpp | 169 +++++++++++++++++++++++- engine/core/object/Body2D.h | 31 ++++- engine/core/subsystem/PhysicsSystem.cpp | 55 +++----- engine/core/subsystem/PhysicsSystem.h | 4 - 5 files changed, 223 insertions(+), 46 deletions(-) diff --git a/engine/core/component/Body2DComponent.h b/engine/core/component/Body2DComponent.h index 63e72438..6b862e20 100644 --- a/engine/core/component/Body2DComponent.h +++ b/engine/core/component/Body2DComponent.h @@ -25,10 +25,16 @@ namespace Supernova{ }; struct CollisionShape2D{ - CollisionShape2DType type = CollisionShape2DType::POLYGON; - b2Shape* shape = NULL; b2Fixture* fixture = NULL; + + CollisionShape2DType type = CollisionShape2DType::POLYGON; + float friction = 0.2f; + float restitution = 0.0f; + float density = 0.0f; + float sensor = false; + + bool needUpdate = true; }; struct Body2DComponent{ diff --git a/engine/core/object/Body2D.cpp b/engine/core/object/Body2D.cpp index 7345aedb..eb053340 100644 --- a/engine/core/object/Body2D.cpp +++ b/engine/core/object/Body2D.cpp @@ -30,18 +30,110 @@ int Body2D::createRectShape2D(float width, float height){ return scene->getSystem()->addRectShape2D(entity, width, height); } +void Body2D::setShape2DDensity(float density){ + setShape2DDensity(0, density); +} + +void Body2D::setShape2DFriction(float friction){ + setShape2DFriction(0, friction); +} + +void Body2D::setShape2DRestitution(float restitution){ + setShape2DRestitution(0, restitution); +} + void Body2D::setShape2DDensity(size_t index, float density){ - scene->getSystem()->setShape2DDensity(entity, index, density); + Body2DComponent& body = getComponent(); + + if (index >=0 && index < MAX_SHAPES){ + if (body.shapes[index].density != density){ + body.shapes[index].density = density; + + body.shapes[index].needUpdate = true; + } + }else{ + Log::error("Cannot find shape %i of body", index); + } } void Body2D::setShape2DFriction(size_t index, float friction){ - scene->getSystem()->setShape2DFriction(entity, index, friction); + Body2DComponent& body = getComponent(); + + if (index >=0 && index < MAX_SHAPES){ + if (body.shapes[index].friction != friction){ + body.shapes[index].friction = friction; + + body.shapes[index].needUpdate = true; + } + }else{ + Log::error("Cannot find shape %i of body", index); + } } void Body2D::setShape2DRestitution(size_t index, float restitution){ - scene->getSystem()->setShape2DRestitution(entity, index, restitution); + Body2DComponent& body = getComponent(); + + if (index >=0 && index < MAX_SHAPES){ + if (body.shapes[index].restitution != restitution){ + body.shapes[index].restitution = restitution; + + body.shapes[index].needUpdate = true; + } + }else{ + Log::error("Cannot find shape %i of body", index); + } } +float Body2D::getShape2DDensity() const{ + return getShape2DDensity(0); +} + +float Body2D::getShape2DFriction() const{ + return getShape2DFriction(0); +} + +float Body2D::getShape2DRestitution() const{ + return getShape2DRestitution(0); +} + + +float Body2D::getShape2DDensity(size_t index) const{ + Body2DComponent& body = getComponent(); + + if (index >=0 && index < MAX_SHAPES){ + return body.shapes[index].density; + }else{ + Log::error("Cannot find shape %i of body", index); + } + + return -1; +} + +float Body2D::getShape2DFriction(size_t index) const{ + Body2DComponent& body = getComponent(); + + if (index >=0 && index < MAX_SHAPES){ + return body.shapes[index].friction; + }else{ + Log::error("Cannot find shape %i of body", index); + } + + return -1; +} + +float Body2D::getShape2DRestitution(size_t index) const{ + Body2DComponent& body = getComponent(); + + if (index >=0 && index < MAX_SHAPES){ + return body.shapes[index].restitution; + }else{ + Log::error("Cannot find shape %i of body", index); + } + + return -1; +} + + void Body2D::setLinearVelocity(Vector2 linearVelocity){ Body2DComponent& body = getComponent(); @@ -62,7 +154,7 @@ void Body2D::setAngularVelocity(float angularVelocity){ } } -void Body2D::setLinearDuamping(float linearDamping){ +void Body2D::setLinearDamping(float linearDamping){ Body2DComponent& body = getComponent(); if (body.linearDamping != linearDamping){ @@ -132,7 +224,7 @@ void Body2D::setType(Body2DType type){ } } -void Body2D::setEnable(bool enable){ +void Body2D::setEnabled(bool enable){ Body2DComponent& body = getComponent(); if (body.enable != enable){ @@ -151,3 +243,70 @@ void Body2D::setGravityScale(float gravityScale){ body.needUpdate = true; } } + +Vector2 Body2D::getLinearVelocity() const{ + Body2DComponent& body = getComponent(); + + return body.linearVelocity; +} + +float Body2D::getAngularVelocity() const{ + Body2DComponent& body = getComponent(); + + return body.angularVelocity; +} + +float Body2D::getLinearDamping() const{ + Body2DComponent& body = getComponent(); + + return body.linearDamping; +} + +float Body2D::getAngularDamping() const{ + Body2DComponent& body = getComponent(); + + return body.angularDamping; +} + +bool Body2D::isAllowSleep() const{ + Body2DComponent& body = getComponent(); + + return body.allowSleep; +} + +bool Body2D::isAwake() const{ + Body2DComponent& body = getComponent(); + + return body.awake; +} + +bool Body2D::isFixedRotation() const{ + Body2DComponent& body = getComponent(); + + return body.fixedRotation; +} + +bool Body2D::isBullet() const{ + Body2DComponent& body = getComponent(); + + return body.bullet; +} + +Body2DType Body2D::getType() const{ + Body2DComponent& body = getComponent(); + + return body.type; +} + +bool Body2D::isEnabled() const{ + Body2DComponent& body = getComponent(); + + return body.enable; +} + +float Body2D::getGravityScale() const{ + Body2DComponent& body = getComponent(); + + return body.gravityScale; +} + diff --git a/engine/core/object/Body2D.h b/engine/core/object/Body2D.h index 37ec1b60..855be1af 100644 --- a/engine/core/object/Body2D.h +++ b/engine/core/object/Body2D.h @@ -18,21 +18,48 @@ namespace Supernova{ Body2D& operator=(const Body2D& rhs); int createRectShape2D(float width, float height); + + + void setShape2DDensity(float density); + void setShape2DFriction(float friction); + void setShape2DRestitution(float restitution); + void setShape2DDensity(size_t index, float density); void setShape2DFriction(size_t index, float friction); void setShape2DRestitution(size_t index, float restitution); + float getShape2DDensity() const; + float getShape2DFriction() const; + float getShape2DRestitution() const; + + float getShape2DDensity(size_t index) const; + float getShape2DFriction(size_t index) const; + float getShape2DRestitution(size_t index) const; + + void setLinearVelocity(Vector2 linearVelocity); void setAngularVelocity(float angularVelocity); - void setLinearDuamping(float linearDamping); + void setLinearDamping(float linearDamping); void setAngularDamping(float angularDamping); void setAllowSleep(bool allowSleep); void setAwake(bool awake); void setFixedRotation(bool fixedRotation); void setBullet(bool bullet); void setType(Body2DType type); - void setEnable(bool enable); + void setEnabled(bool enable); void setGravityScale(float gravityScale); + + Vector2 getLinearVelocity() const; + float getAngularVelocity() const; + float getLinearDamping() const; + float getAngularDamping() const; + bool isAllowSleep() const; + bool isAwake() const; + bool isFixedRotation() const; + bool isBullet() const; + Body2DType getType() const; + bool isEnabled() const; + float getGravityScale() const; }; } diff --git a/engine/core/subsystem/PhysicsSystem.cpp b/engine/core/subsystem/PhysicsSystem.cpp index 72b0ce5a..b9870ac3 100644 --- a/engine/core/subsystem/PhysicsSystem.cpp +++ b/engine/core/subsystem/PhysicsSystem.cpp @@ -66,39 +66,6 @@ int PhysicsSystem::addRectShape2D(Entity entity, float width, float height){ return -1; } -void PhysicsSystem::setShape2DDensity(Entity entity, size_t index, float density){ - Body2DComponent* body = scene->findComponent(entity); - - if (body && body->shapes[body->numShapes].fixture){ - body->shapes[body->numShapes].fixture->SetDensity(density); - if (body->body){ - body->body->ResetMassData(); - } - }else{ - Log::error("Cannot set density of non existent body"); - } -} - -void PhysicsSystem::setShape2DFriction(Entity entity, size_t index, float friction){ - Body2DComponent* body = scene->findComponent(entity); - - if (body && body->shapes[body->numShapes].fixture){ - body->shapes[body->numShapes].fixture->SetFriction(friction); - }else{ - Log::error("Cannot set friction of non existent body"); - } -} - -void PhysicsSystem::setShape2DRestitution(Entity entity, size_t index, float restitution){ - Body2DComponent* body = scene->findComponent(entity); - - if (body && body->shapes[body->numShapes].fixture){ - body->shapes[body->numShapes].fixture->SetRestitution(restitution); - }else{ - Log::error("Cannot set restitution of non existent body"); - } -} - bool PhysicsSystem::loadBody2D(Body2DComponent& body){ if (world2D && !body.body){ b2BodyDef bodyDef; @@ -181,6 +148,23 @@ void PhysicsSystem::update(double dt){ body.needUpdate = false; } + for (int i = 0; i < body.numShapes; i++){ + if (body.shapes[i].needUpdate){ + float oldDensity = body.shapes[i].fixture->GetDensity(); + + body.shapes[i].fixture->SetDensity(body.shapes[i].density); + body.shapes[i].fixture->SetFriction(body.shapes[i].friction); + body.shapes[i].fixture->SetRestitution(body.shapes[i].restitution); + body.shapes[i].fixture->SetSensor(body.shapes[i].sensor); + + if (oldDensity != body.shapes[i].density){ + body.body->ResetMassData(); + } + + body.shapes[i].needUpdate = false; + } + } + if (signature.test(scene->getComponentType())){ Transform& transform = scene->getComponent(entity); @@ -223,6 +207,11 @@ void PhysicsSystem::update(double dt){ } } + + body.linearVelocity = Vector2(body.body->GetLinearVelocity().x, body.body->GetLinearVelocity().y); + body.angularVelocity = body.body->GetAngularVelocity(); + body.awake = body.body->IsAwake(); + body.enable = body.body->IsEnabled(); } } diff --git a/engine/core/subsystem/PhysicsSystem.h b/engine/core/subsystem/PhysicsSystem.h index 72ce07f9..1313b2a6 100644 --- a/engine/core/subsystem/PhysicsSystem.h +++ b/engine/core/subsystem/PhysicsSystem.h @@ -26,10 +26,6 @@ namespace Supernova{ void removeBody2D(Entity entity); int addRectShape2D(Entity entity, float width, float height); - void setShape2DDensity(Entity entity, size_t index, float density); - void setShape2DFriction(Entity entity, size_t index, float friction); - void setShape2DRestitution(Entity entity, size_t index, float restitution); - bool loadBody2D(Body2DComponent& body); void destroyBody2D(Body2DComponent& body);