diff --git a/engine/core/object/Object.cpp b/engine/core/object/Object.cpp index 09b98713..46cdd03a 100644 --- a/engine/core/object/Object.cpp +++ b/engine/core/object/Object.cpp @@ -203,6 +203,7 @@ void Object::updateTransform(){ Body2D Object::getBody2D(){ scene->getSystem()->createBody2D(entity); + scene->getSystem()->loadBody2D(getComponent()); return Body2D(scene, entity); } diff --git a/engine/core/object/physics/Body2D.cpp b/engine/core/object/physics/Body2D.cpp index 51236a8f..a6b5fcb0 100644 --- a/engine/core/object/physics/Body2D.cpp +++ b/engine/core/object/physics/Body2D.cpp @@ -27,7 +27,13 @@ Body2D& Body2D::operator=(const Body2D& rhs){ } int Body2D::createRectShape2D(float width, float height){ - return scene->getSystem()->addRectShape2D(entity, width, height); + int index = scene->getSystem()->addRectShape2D(entity, width, height); + + if (index >= 0){ + scene->getSystem()->loadShape2D(getComponent(), index); + } + + return index; } void Body2D::setShape2DDensity(float density){ diff --git a/engine/core/object/physics/Joint2D.cpp b/engine/core/object/physics/Joint2D.cpp index f242ffcd..5379c609 100644 --- a/engine/core/object/physics/Joint2D.cpp +++ b/engine/core/object/physics/Joint2D.cpp @@ -32,5 +32,5 @@ Joint2D& Joint2D::operator=(const Joint2D& rhs){ } void Joint2D::initDistanceJoint(Entity bodyA, Entity bodyB, Vector2 worldAnchorOnBodyA, Vector2 worldAnchorOnBodyB){ - scene->getSystem()->getBody(1); + scene->getSystem()->initDistanceJoint(entity, bodyA, bodyB, worldAnchorOnBodyA, worldAnchorOnBodyB); } \ No newline at end of file diff --git a/engine/core/subsystem/PhysicsSystem.cpp b/engine/core/subsystem/PhysicsSystem.cpp index 1b85f20d..8f1ea8b4 100644 --- a/engine/core/subsystem/PhysicsSystem.cpp +++ b/engine/core/subsystem/PhysicsSystem.cpp @@ -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; } @@ -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; } @@ -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); @@ -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; @@ -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"); @@ -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(); diff --git a/engine/core/subsystem/PhysicsSystem.h b/engine/core/subsystem/PhysicsSystem.h index 23ce8566..b8753ba6 100644 --- a/engine/core/subsystem/PhysicsSystem.h +++ b/engine/core/subsystem/PhysicsSystem.h @@ -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);