Skip to content

Commit

Permalink
Testing joints
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardodoria committed Aug 23, 2023
1 parent c0de374 commit 32fec9b
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 24 deletions.
1 change: 1 addition & 0 deletions engine/core/object/Object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ 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: 7 additions & 1 deletion engine/core/object/physics/Body2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,13 @@ Body2D& Body2D::operator=(const Body2D& rhs){
}

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

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

return index;
}

void Body2D::setShape2DDensity(float density){
Expand Down
2 changes: 1 addition & 1 deletion engine/core/object/physics/Joint2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ Joint2D& Joint2D::operator=(const Joint2D& rhs){
}

void Joint2D::initDistanceJoint(Entity bodyA, Entity bodyB, Vector2 worldAnchorOnBodyA, Vector2 worldAnchorOnBodyB){
scene->getSystem<PhysicsSystem>()->getBody(1);
scene->getSystem<PhysicsSystem>()->initDistanceJoint(entity, bodyA, bodyB, worldAnchorOnBodyA, worldAnchorOnBodyB);
}
61 changes: 39 additions & 22 deletions engine/core/subsystem/PhysicsSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ PhysicsSystem::PhysicsSystem(Scene* scene): SubSystem(scene){

this->scene = scene;

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

Expand Down Expand Up @@ -119,19 +121,6 @@ bool PhysicsSystem::loadBody2D(Body2DComponent& body){

body.body = world2D->CreateBody(&bodyDef);

for (int i = 0; i < body.numShapes; i++){
b2FixtureDef fixtureDef;

fixtureDef.density = body.shapes[i].density;
fixtureDef.friction = body.shapes[i].friction;
fixtureDef.restitution = body.shapes[i].restitution;
fixtureDef.isSensor = body.shapes[i].sensor;

body.shapes[i].needUpdate = false;

body.shapes[i].fixture = body.body->CreateFixture(body.shapes[i].shape, 1.0f);
}

return true;
}

Expand All @@ -143,10 +132,7 @@ void PhysicsSystem::destroyBody2D(Body2DComponent& body){
// all the memory reserved for bodies, fixtures, and joints is freed
if (world2D && body.body){
for (int i = 0; i < body.numShapes; i++){
body.body->DestroyFixture(body.shapes[i].fixture);

body.shapes[i].shape = NULL;
body.shapes[i].fixture = NULL;
destroyShape2D(body, i);
}

world2D->DestroyBody(body.body);
Expand All @@ -155,15 +141,43 @@ void PhysicsSystem::destroyBody2D(Body2DComponent& body){
}
}

bool PhysicsSystem::loadShape2D(Body2DComponent& body, size_t index){
if (world2D && !body.shapes[index].fixture){
b2FixtureDef fixtureDef;

fixtureDef.density = body.shapes[index].density;
fixtureDef.friction = body.shapes[index].friction;
fixtureDef.restitution = body.shapes[index].restitution;
fixtureDef.isSensor = body.shapes[index].sensor;

body.shapes[index].needUpdate = false;

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

return true;
}

return false;
}

void PhysicsSystem::destroyShape2D(Body2DComponent& body, size_t index){
if (world2D && body.shapes[index].fixture){
body.body->DestroyFixture(body.shapes[index].fixture);

body.shapes[index].shape = NULL;
body.shapes[index].fixture = NULL;
}
}

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);
//joint.jointDef->bodyA = getBody(joint.bodyA);
//joint.jointDef->bodyB = getBody(joint.bodyB);
//joint.jointDef->collideConnected = joint.collideConnected;
//joint.jointDef->type = getJointType(joint.type);

joint.needUpdate = false;

Expand Down Expand Up @@ -208,6 +222,7 @@ void PhysicsSystem::initDistanceJoint(Entity entity, Entity bodyA, Entity 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");
Expand Down Expand Up @@ -258,6 +273,8 @@ void PhysicsSystem::update(double dt){
}

for (int i = 0; i < body.numShapes; i++){
loadShape2D(body, i);

if (body.shapes[i].needUpdate){
float oldDensity = body.shapes[i].fixture->GetDensity();

Expand Down
3 changes: 3 additions & 0 deletions engine/core/subsystem/PhysicsSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ namespace Supernova{
bool loadBody2D(Body2DComponent& body);
void destroyBody2D(Body2DComponent& body);

bool loadShape2D(Body2DComponent& body, size_t index);
void destroyShape2D(Body2DComponent& body, size_t index);

bool loadJoint2D(Joint2DComponent& joint);
void destroyJoint2D(Joint2DComponent& joint);

Expand Down

0 comments on commit 32fec9b

Please sign in to comment.