Skip to content

Commit

Permalink
Added more functions to Body2D
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardodoria committed Aug 18, 2023
1 parent 41c5371 commit f907e82
Show file tree
Hide file tree
Showing 5 changed files with 223 additions and 46 deletions.
10 changes: 8 additions & 2 deletions engine/core/component/Body2DComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
169 changes: 164 additions & 5 deletions engine/core/object/Body2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,110 @@ int Body2D::createRectShape2D(float width, float height){
return scene->getSystem<PhysicsSystem>()->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<PhysicsSystem>()->setShape2DDensity(entity, index, density);
Body2DComponent& body = getComponent<Body2DComponent>();

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<PhysicsSystem>()->setShape2DFriction(entity, index, friction);
Body2DComponent& body = getComponent<Body2DComponent>();

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<PhysicsSystem>()->setShape2DRestitution(entity, index, restitution);
Body2DComponent& body = getComponent<Body2DComponent>();

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<Body2DComponent>();

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<Body2DComponent>();

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<Body2DComponent>();

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<Body2DComponent>();

Expand All @@ -62,7 +154,7 @@ void Body2D::setAngularVelocity(float angularVelocity){
}
}

void Body2D::setLinearDuamping(float linearDamping){
void Body2D::setLinearDamping(float linearDamping){
Body2DComponent& body = getComponent<Body2DComponent>();

if (body.linearDamping != linearDamping){
Expand Down Expand Up @@ -132,7 +224,7 @@ void Body2D::setType(Body2DType type){
}
}

void Body2D::setEnable(bool enable){
void Body2D::setEnabled(bool enable){
Body2DComponent& body = getComponent<Body2DComponent>();

if (body.enable != enable){
Expand All @@ -151,3 +243,70 @@ void Body2D::setGravityScale(float gravityScale){
body.needUpdate = true;
}
}

Vector2 Body2D::getLinearVelocity() const{
Body2DComponent& body = getComponent<Body2DComponent>();

return body.linearVelocity;
}

float Body2D::getAngularVelocity() const{
Body2DComponent& body = getComponent<Body2DComponent>();

return body.angularVelocity;
}

float Body2D::getLinearDamping() const{
Body2DComponent& body = getComponent<Body2DComponent>();

return body.linearDamping;
}

float Body2D::getAngularDamping() const{
Body2DComponent& body = getComponent<Body2DComponent>();

return body.angularDamping;
}

bool Body2D::isAllowSleep() const{
Body2DComponent& body = getComponent<Body2DComponent>();

return body.allowSleep;
}

bool Body2D::isAwake() const{
Body2DComponent& body = getComponent<Body2DComponent>();

return body.awake;
}

bool Body2D::isFixedRotation() const{
Body2DComponent& body = getComponent<Body2DComponent>();

return body.fixedRotation;
}

bool Body2D::isBullet() const{
Body2DComponent& body = getComponent<Body2DComponent>();

return body.bullet;
}

Body2DType Body2D::getType() const{
Body2DComponent& body = getComponent<Body2DComponent>();

return body.type;
}

bool Body2D::isEnabled() const{
Body2DComponent& body = getComponent<Body2DComponent>();

return body.enable;
}

float Body2D::getGravityScale() const{
Body2DComponent& body = getComponent<Body2DComponent>();

return body.gravityScale;
}

31 changes: 29 additions & 2 deletions engine/core/object/Body2D.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
}

Expand Down
55 changes: 22 additions & 33 deletions engine/core/subsystem/PhysicsSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Body2DComponent>(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<Body2DComponent>(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<Body2DComponent>(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;
Expand Down Expand Up @@ -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& transform = scene->getComponent<Transform>(entity);

Expand Down Expand Up @@ -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();
}
}

Expand Down
4 changes: 0 additions & 4 deletions engine/core/subsystem/PhysicsSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit f907e82

Please sign in to comment.