Skip to content

Commit

Permalink
Added hinge 3D joint
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardodoria committed Oct 11, 2023
1 parent 7290f21 commit c8b75f3
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 1 deletion.
3 changes: 2 additions & 1 deletion engine/core/component/Joint3DComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ namespace Supernova{
enum class Joint3DType{
FIXED,
DISTANCE,
POINT
POINT,
HINGE
};

struct Joint3DComponent{
Expand Down
4 changes: 4 additions & 0 deletions engine/core/object/physics/Joint3D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,8 @@ void Joint3D::setDistanceJoint(Entity bodyA, Entity bodyB, Vector3 worldAnchorOn

void Joint3D::setPointJoint(Entity bodyA, Entity bodyB, Vector3 worldAnchor){
scene->getSystem<PhysicsSystem>()->loadPointJoint3D(getComponent<Joint3DComponent>(), bodyA, bodyB, worldAnchor);
}

void Joint3D::setHingeJoint(Entity bodyA, Entity bodyB, Vector3 worldAnchor, Vector3 axis, Vector3 normal){
scene->getSystem<PhysicsSystem>()->loadHingeJoint3D(getComponent<Joint3DComponent>(), bodyA, bodyB, worldAnchor, axis, normal);
}
1 change: 1 addition & 0 deletions engine/core/object/physics/Joint3D.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ namespace Supernova{
void setFixedJoint(Entity bodyA, Entity bodyB);
void setDistanceJoint(Entity bodyA, Entity bodyB, Vector3 worldAnchorOnBodyA, Vector3 worldAnchorOnBodyB);
void setPointJoint(Entity bodyA, Entity bodyB, Vector3 worldAnchor);
void setHingeJoint(Entity bodyA, Entity bodyB, Vector3 worldAnchor, Vector3 axis, Vector3 normal);

Joint3DType getType();
};
Expand Down
33 changes: 33 additions & 0 deletions engine/core/subsystem/PhysicsSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -939,6 +939,39 @@ bool PhysicsSystem::loadPointJoint3D(Joint3DComponent& joint, Entity bodyA, Enti
return true;
}

bool PhysicsSystem::loadHingeJoint3D(Joint3DComponent& joint, Entity bodyA, Entity bodyB, Vector3 anchor, Vector3 axis, Vector3 normal){
Signature signatureA = scene->getSignature(bodyA);
Signature signatureB = scene->getSignature(bodyB);

if (signatureA.test(scene->getComponentType<Body3DComponent>()) && signatureB.test(scene->getComponentType<Body3DComponent>())){

Body3DComponent myBodyA = scene->getComponent<Body3DComponent>(bodyA);
Body3DComponent myBodyB = scene->getComponent<Body3DComponent>(bodyB);

updateBody3DPosition(signatureA, bodyA, myBodyA, true);
updateBody3DPosition(signatureB, bodyB, myBodyB, true);

JPH::HingeConstraintSettings settings;
settings.mPoint1 = JPH::Vec3(anchor.x, anchor.y, anchor.z);
settings.mPoint2 = JPH::Vec3(anchor.x, anchor.y, anchor.z);
settings.mHingeAxis1 = JPH::Vec3(axis.x, axis.y, axis.z);
settings.mHingeAxis2 = JPH::Vec3(axis.x, axis.y, axis.z);
settings.mNormalAxis1 = JPH::Vec3(normal.x, normal.y, normal.z);
settings.mNormalAxis2 = JPH::Vec3(normal.x, normal.y, normal.z);

joint.joint = settings.Create(*myBodyA.body, *myBodyB.body);

world3D->AddConstraint(joint.joint);
joint.type = Joint3DType::HINGE;

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

return true;
}

void PhysicsSystem::load(){
}

Expand Down
1 change: 1 addition & 0 deletions engine/core/subsystem/PhysicsSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ namespace Supernova{
bool loadFixedJoint3D(Joint3DComponent& joint, Entity bodyA, Entity bodyB);
bool loadDistanceJoint3D(Joint3DComponent& joint, Entity bodyA, Entity bodyB, Vector3 anchorA, Vector3 anchorB);
bool loadPointJoint3D(Joint3DComponent& joint, Entity bodyA, Entity bodyB, Vector3 anchor);
bool loadHingeJoint3D(Joint3DComponent& joint, Entity bodyA, Entity bodyB, Vector3 anchor, Vector3 axis, Vector3 normal);

virtual void load();
virtual void destroy();
Expand Down
1 change: 1 addition & 0 deletions engine/core/util/JoltPhysicsAux.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "Jolt/Physics/Constraints/FixedConstraint.h"
#include "Jolt/Physics/Constraints/DistanceConstraint.h"
#include "Jolt/Physics/Constraints/PointConstraint.h"
#include "Jolt/Physics/Constraints/HingeConstraint.h"


// Based on: https://github.com/jrouwe/JoltPhysics/blob/master/HelloWorld/HelloWorld.cpp
Expand Down

0 comments on commit c8b75f3

Please sign in to comment.