Skip to content

Commit

Permalink
Added softbodies
Browse files Browse the repository at this point in the history
  • Loading branch information
denis authored and denis committed Nov 14, 2024
1 parent a9cf53b commit f748f81
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 34 deletions.
1 change: 0 additions & 1 deletion Engine/include/Components/PhysicsMeshComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ namespace Prisma

private:
ComponentList m_status;
ComponentList m_statusBend;
std::function<void()> m_apply;
std::function<void()> m_applySoft;
Physics::CollisionData m_collisionData{};
Expand Down
5 changes: 2 additions & 3 deletions Engine/include/Physics/Physics.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,10 @@ namespace Prisma

struct SoftBodySettings
{
float gravity = 1.0f;
int numIteration = 1.0f;
bool sleep = false;
bool updatePosition = false;
JPH::SoftBodySharedSettings::VertexAttributes vertexAttributes = {1, 1, 1};
JPH::SoftBodySharedSettings::EBendType bendType = JPH::SoftBodySharedSettings::EBendType::None;
JPH::SoftBodySharedSettings::VertexAttributes vertexAttributes = {1.0e-5f, 1.0e-5f, 1.0e-5f};
};

struct LandscapeData
Expand Down
37 changes: 17 additions & 20 deletions Engine/src/Components/PhysicsMeshComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,10 @@ void Prisma::PhysicsMeshComponent::ui()
m_status.items.push_back("LANDSCAPE COLLIDER");
m_status.items.push_back("CONVEX COLLIDER");
componentType = std::make_tuple(TYPES::STRINGLIST, "Collider", &m_status);

ComponentType componentMass;
componentMass = std::make_tuple(TYPES::FLOAT, "Mass", &m_collisionData.mass);

ComponentType componentGravity;
componentGravity = std::make_tuple(TYPES::FLOAT, "Gravity", &m_settingsSoft.gravity);

m_statusBend.currentitem = static_cast<int>(m_settingsSoft.bendType);
m_statusBend.items.push_back("NONE");
m_statusBend.items.push_back("DISTANCE");
m_statusBend.items.push_back("DIHEDRAL");

ComponentType componentBend;
componentBend = std::make_tuple(TYPES::STRINGLIST, "Bend", &m_statusBend);

ComponentType componentIteration;
componentIteration = std::make_tuple(TYPES::INT, "Iteration", &m_settingsSoft.numIteration);
ComponentType componentDynamic;
componentDynamic = std::make_tuple(TYPES::BOOL, "Dynamic", &m_collisionData.dynamic);

Expand Down Expand Up @@ -60,9 +49,7 @@ void Prisma::PhysicsMeshComponent::ui()

addGlobal(componentMass);

addGlobal(componentGravity);

addGlobal(componentBend);
addGlobal(componentIteration);

addGlobal(componentDynamic);

Expand Down Expand Up @@ -238,9 +225,19 @@ BodyCreationSettings Prisma::PhysicsMeshComponent::getBodySettings()
case Physics::Collider::BOX_COLLIDER:
{
auto length = glm::abs((aabbData.max - aabbData.min) * 0.5f);
if (length.x < m_minScale || length.y < m_minScale || length.z < m_minScale)
if (length.x < m_minScale)
{
length.x = 1;
}

if (length.y < m_minScale)
{
length.y = 1;
}

if (length.z < m_minScale)
{
length = glm::vec3(1, 1, 1);
length.z = 1;
}
auto boxShape = new BoxShape(JtoVec3(length));
auto result = boxShape->ScaleShape(JtoVec3(scale));
Expand Down Expand Up @@ -339,7 +336,7 @@ void Prisma::PhysicsMeshComponent::addSoftBody()
mesh->verticesData().indices[i + 2]));
}

m_softBodySharedSettings->CreateConstraints(&m_settingsSoft.vertexAttributes, 1, m_settingsSoft.bendType);
m_softBodySharedSettings->CreateConstraints(&m_settingsSoft.vertexAttributes, 1);

m_softBodySharedSettings->Optimize();

Expand All @@ -348,9 +345,9 @@ void Prisma::PhysicsMeshComponent::addSoftBody()
m_collisionData.dynamic
? Layers::MOVING
: Layers::NON_MOVING);
sb_settings.mGravityFactor = m_settingsSoft.gravity;
sb_settings.mAllowSleeping = m_settingsSoft.sleep;
sb_settings.mUpdatePosition = m_settingsSoft.updatePosition;
sb_settings.mNumIterations = m_settingsSoft.numIteration;

m_physicsSoftId = Physics::getInstance().bodyInterface().CreateSoftBody(sb_settings);
Physics::getInstance().bodyInterface().AddBody(m_physicsSoftId->GetID(),
Expand Down
10 changes: 0 additions & 10 deletions UserEngine/src/PlayerController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,6 @@ PlayerController::PlayerController(std::shared_ptr<Prisma::Scene> scene) : m_sce
terrain->addComponent(terrainComponent);
m_scene->root->addChild(terrain);*/


auto plane = std::dynamic_pointer_cast<Prisma::Mesh>(nodeHelper.find(m_scene->root, "Cube.004"));

if (plane)
{
auto physicsComponent = std::dynamic_pointer_cast<Prisma::PhysicsMeshComponent>(plane->components()["Physics"]);
physicsComponent->settingsSoftBody({0, false, false, {1, 1, 1}, SoftBodySharedSettings::EBendType::Dihedral});
physicsComponent->collisionData({Prisma::Physics::Collider::BOX_COLLIDER, 0.0, true, true});
}

auto contact = [&](const Body& body)
{
m_isColliding = true;
Expand Down

0 comments on commit f748f81

Please sign in to comment.