Skip to content

Commit

Permalink
Joint2D development
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardodoria committed Aug 21, 2023
1 parent 7caa1d7 commit c0de374
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 24 deletions.
23 changes: 12 additions & 11 deletions engine/core/component/Joint2DComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,34 @@

#include "Supernova.h"

class b2JointDef;
class b2Joint;

namespace Supernova{

enum class Joint2DType{
DISTANCE,
REVOLUTE
REVOLUTE,
PRISMATIC,
PULLEY,
GEAR,
MOUSE,
WHEEL,
WELD,
ROPE,
FRICTION,
MOTOR
};

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

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

Vector2 localAnchorA = Vector2(0.0f, 0.0f);
Vector2 localAnchorB = Vector2(0.0f, 0.0f);
float referenceAngle = 0.0f;
float lowerAngle = 0.0f;
float upperAngle = 0.0f;
float maxMotorTorque = 0.0f;
float motorSpeed = 0.0f;
float enableLimit = false;
float enableMotor = false;

bool needUpdate = true;
};

Expand Down
11 changes: 10 additions & 1 deletion engine/core/object/physics/Joint2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@

#include "Joint2D.h"

#include "Scene.h"
#include "subsystem/PhysicsSystem.h"
#include "component/Body2DComponent.h"
#include "component/Joint2DComponent.h"

using namespace Supernova;

Joint2D::Joint2D(Scene* scene): EntityHandle(scene){
addComponent<Joint2DComponent>({});
}

Joint2D::Joint2D(Scene* scene, Entity entity): EntityHandle(scene, entity){

}
Expand All @@ -24,4 +29,8 @@ Joint2D::Joint2D(const Joint2D& rhs): EntityHandle(rhs){
Joint2D& Joint2D::operator=(const Joint2D& rhs){

return *this;
}

void Joint2D::initDistanceJoint(Entity bodyA, Entity bodyB, Vector2 worldAnchorOnBodyA, Vector2 worldAnchorOnBodyB){
scene->getSystem<PhysicsSystem>()->getBody(1);
}
3 changes: 3 additions & 0 deletions engine/core/object/physics/Joint2D.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@ namespace Supernova{

class Joint2D: public EntityHandle{
public:
Joint2D(Scene* scene);
Joint2D(Scene* scene, Entity entity);
virtual ~Joint2D();

Joint2D(const Joint2D& rhs);
Joint2D& operator=(const Joint2D& rhs);

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

};
}

Expand Down
58 changes: 46 additions & 12 deletions engine/core/subsystem/PhysicsSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,15 +157,19 @@ void PhysicsSystem::destroyBody2D(Body2DComponent& body){

bool PhysicsSystem::loadJoint2D(Joint2DComponent& joint){
if (world2D && !joint.joint){
b2JointDef jointDef;
jointDef.bodyA = getBody(joint.bodyA);
jointDef.bodyB = getBody(joint.bodyB);
jointDef.collideConnected = joint.collideConnected;
jointDef.type = getJointType(joint.type);
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.needUpdate = false;

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

delete joint.jointDef;

return true;
}
Expand All @@ -179,8 +183,38 @@ 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(){
if (!world2D){
Expand Down Expand Up @@ -255,16 +289,13 @@ void PhysicsSystem::update(double dt){
Entity entity = joints2d->getEntity(i);
Signature signature = scene->getSignature(entity);

loadJoint2D(joint);

if (joint.needUpdate){
if (joint.type == Joint2DType::DISTANCE){
b2DistanceJoint* distJoint = (b2DistanceJoint*)joint.joint;
//distJoint.se
}
destroyJoint2D(joint);

joint.needUpdate = false;
}

loadJoint2D(joint);
}

if (bodies2d->size() > 0){
Expand Down Expand Up @@ -317,4 +348,7 @@ void PhysicsSystem::entityDestroyed(Entity entity){
if (signature.test(scene->getComponentType<Body2DComponent>())){
destroyBody2D(scene->getComponent<Body2DComponent>(entity));
}
if (signature.test(scene->getComponentType<Joint2DComponent>())){
destroyJoint2D(scene->getComponent<Joint2DComponent>(entity));
}
}
2 changes: 2 additions & 0 deletions engine/core/subsystem/PhysicsSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ 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 c0de374

Please sign in to comment.