Skip to content

Commit

Permalink
Joint2D development
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardodoria committed Aug 25, 2023
1 parent 32fec9b commit aebb4f9
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 61 deletions.
4 changes: 3 additions & 1 deletion engine/core/component/Joint2DComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,16 @@ namespace Supernova{
};

struct Joint2DComponent{
b2JointDef* jointDef = NULL;
b2Joint* joint = NULL;

Entity bodyA;
Entity bodyB;
Joint2DType type = Joint2DType::DISTANCE;
bool collideConnected = false;

Vector2 worldAnchorOnBodyA;
Vector2 worldAnchorOnBodyB;

bool needUpdate = true;
};

Expand Down
1 change: 0 additions & 1 deletion engine/core/object/Object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,6 @@ void Object::updateTransform(){

Body2D Object::getBody2D(){
scene->getSystem<PhysicsSystem>()->createBody2D(entity);
scene->getSystem<PhysicsSystem>()->loadBody2D(getComponent<Body2DComponent>());
return Body2D(scene, entity);
}

Expand Down
8 changes: 1 addition & 7 deletions engine/core/object/physics/Body2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,7 @@ Body2D& Body2D::operator=(const Body2D& rhs){
}

int Body2D::createRectShape2D(float width, float height){
int index = scene->getSystem<PhysicsSystem>()->addRectShape2D(entity, width, height);

if (index >= 0){
scene->getSystem<PhysicsSystem>()->loadShape2D(getComponent<Body2DComponent>(), index);
}

return index;
return scene->getSystem<PhysicsSystem>()->addRectShape2D(entity, width, height);
}

void Body2D::setShape2DDensity(float density){
Expand Down
11 changes: 9 additions & 2 deletions engine/core/object/physics/Joint2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ Joint2D& Joint2D::operator=(const Joint2D& rhs){
return *this;
}

void Joint2D::initDistanceJoint(Entity bodyA, Entity bodyB, Vector2 worldAnchorOnBodyA, Vector2 worldAnchorOnBodyB){
scene->getSystem<PhysicsSystem>()->initDistanceJoint(entity, bodyA, bodyB, worldAnchorOnBodyA, worldAnchorOnBodyB);
void Joint2D::setDistanceJoint(Entity bodyA, Entity bodyB, Vector2 worldAnchorOnBodyA, Vector2 worldAnchorOnBodyB){
Joint2DComponent& joint = getComponent<Joint2DComponent>();

joint.type = Joint2DType::DISTANCE;

joint.bodyA = bodyA;
joint.bodyB = bodyB;
joint.worldAnchorOnBodyA = worldAnchorOnBodyA;
joint.worldAnchorOnBodyB = worldAnchorOnBodyB;
}
2 changes: 1 addition & 1 deletion engine/core/object/physics/Joint2D.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace Supernova{
Joint2D(const Joint2D& rhs);
Joint2D& operator=(const Joint2D& rhs);

void initDistanceJoint(Entity bodyA, Entity bodyB, Vector2 worldAnchorOnBodyA, Vector2 worldAnchorOnBodyB);
void setDistanceJoint(Entity bodyA, Entity bodyB, Vector2 worldAnchorOnBodyA, Vector2 worldAnchorOnBodyB);

};
}
Expand Down
76 changes: 29 additions & 47 deletions engine/core/subsystem/PhysicsSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ PhysicsSystem::PhysicsSystem(Scene* scene): SubSystem(scene){

this->scene = scene;

//this->world2D = NULL;
b2Vec2 gravity(0.0f, 10.0f);
world2D = new b2World(gravity);
this->world2D = NULL;
this->pointsToMeterScale = 64.0;
}

Expand Down Expand Up @@ -149,10 +147,11 @@ bool PhysicsSystem::loadShape2D(Body2DComponent& body, size_t index){
fixtureDef.friction = body.shapes[index].friction;
fixtureDef.restitution = body.shapes[index].restitution;
fixtureDef.isSensor = body.shapes[index].sensor;
fixtureDef.shape = body.shapes[index].shape;

body.shapes[index].needUpdate = false;

body.shapes[index].fixture = body.body->CreateFixture(body.shapes[index].shape, 0.0);
body.shapes[index].fixture = body.body->CreateFixture(&fixtureDef);

return true;
}
Expand All @@ -171,21 +170,36 @@ void PhysicsSystem::destroyShape2D(Body2DComponent& body, size_t index){

bool PhysicsSystem::loadJoint2D(Joint2DComponent& joint){
if (world2D && !joint.joint){
if (!joint.jointDef){
joint.jointDef = new b2JointDef();
}
//joint.jointDef->bodyA = getBody(joint.bodyA);
//joint.jointDef->bodyB = getBody(joint.bodyB);
//joint.jointDef->collideConnected = joint.collideConnected;
//joint.jointDef->type = getJointType(joint.type);
Signature signatureA = scene->getSignature(joint.bodyA);
Signature signatureB = scene->getSignature(joint.bodyB);

joint.needUpdate = false;
if (signatureA.test(scene->getComponentType<Body2DComponent>()) && signatureB.test(scene->getComponentType<Body2DComponent>())){
b2JointDef* jointDef;

joint.joint = world2D->CreateJoint(joint.jointDef);
Body2DComponent myBodyA = scene->getComponent<Body2DComponent>(joint.bodyA);
Body2DComponent myBodyB = scene->getComponent<Body2DComponent>(joint.bodyB);

delete joint.jointDef;
if (joint.type == Joint2DType::DISTANCE){
b2Vec2 anchorA(joint.worldAnchorOnBodyA.x / pointsToMeterScale, joint.worldAnchorOnBodyA.y / pointsToMeterScale);
b2Vec2 anchorb(joint.worldAnchorOnBodyB.x / pointsToMeterScale, joint.worldAnchorOnBodyB.y / pointsToMeterScale);

return true;
jointDef = new b2DistanceJointDef();
((b2DistanceJointDef*)jointDef)->Initialize(myBodyA.body, myBodyB.body, anchorA, anchorb);
}

jointDef->collideConnected = joint.collideConnected;
jointDef->type = getJointType(joint.type);

joint.needUpdate = false;

joint.joint = world2D->CreateJoint(jointDef);

delete jointDef;

return true;
}else{
Log::error("Cannot create joint, error in bodyA or bodyB");
}
}

return false;
Expand All @@ -197,38 +211,6 @@ void PhysicsSystem::destroyJoint2D(Joint2DComponent& joint){

joint.joint = NULL;
}

if (joint.jointDef){
delete joint.jointDef;
}
}

void PhysicsSystem::initDistanceJoint(Entity entity, Entity bodyA, Entity bodyB, Vector2 worldAnchorOnBodyA, Vector2 worldAnchorOnBodyB){
Joint2DComponent joint = scene->getComponent<Joint2DComponent>(entity);

if (joint.jointDef){
delete joint.jointDef;
}

joint.jointDef = new b2DistanceJointDef();

Signature signatureA = scene->getSignature(bodyA);
Signature signatureB = scene->getSignature(bodyB);

if (signatureA.test(scene->getComponentType<Body2DComponent>()) && signatureB.test(scene->getComponentType<Body2DComponent>())){
Body2DComponent myBodyA = scene->getComponent<Body2DComponent>(bodyA);
Body2DComponent myBodyB = scene->getComponent<Body2DComponent>(bodyB);

b2Vec2 anchorA(worldAnchorOnBodyA.x, worldAnchorOnBodyA.y);
b2Vec2 anchorb(worldAnchorOnBodyB.x, worldAnchorOnBodyB.y);


((b2DistanceJointDef*)joint.jointDef)->Initialize(myBodyA.body, myBodyB.body, anchorA, anchorb);
}else{
Log::error("Cannot create joint, error in bodyA or bodyB");
}

//joint.joint
}

void PhysicsSystem::load(){
Expand Down
2 changes: 0 additions & 2 deletions engine/core/subsystem/PhysicsSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ namespace Supernova{
bool loadJoint2D(Joint2DComponent& joint);
void destroyJoint2D(Joint2DComponent& joint);

void initDistanceJoint(Entity entity, Entity bodyA, Entity bodyB, Vector2 worldAnchorOnBodyA, Vector2 worldAnchorOnBodyB);

virtual void load();
virtual void destroy();
virtual void update(double dt);
Expand Down

0 comments on commit aebb4f9

Please sign in to comment.