Skip to content

Commit

Permalink
Blast toolset work
Browse files Browse the repository at this point in the history
  • Loading branch information
EldarMuradov committed Jul 30, 2024
1 parent 5078a71 commit 76e2ed6
Show file tree
Hide file tree
Showing 10 changed files with 149 additions and 81 deletions.
6 changes: 3 additions & 3 deletions modules/core/src/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,15 +332,15 @@ namespace era_engine
{
model_asset ass = load3DModelFromFile(getAssetPath("/resources/assets/obj/untitled.obj"));

auto& px_sphere_entt1 = scene.createEntity("BlastPXTest")
auto& px_blast_entt1 = scene.createEntity("BlastPXTest")
.addComponent<transform_component>(vec3(0.0f, 5.0f, 0.0f), quat::identity, vec3(1.0f))
.addComponent<mesh_component>(mesh);

physics::fracture fracture;
auto ref = make_ref<submesh_asset>(ass.meshes[0].submeshes[0]);
unsigned int seed = 7249U;
fracture.fractureGameObject(ref, px_sphere_entt1, physics::anchor::anchor_none, seed, 15, defaultmat, defaultmat, 1.0f, 3.0f);
scene.deleteEntity(px_sphere_entt1.handle);
fracture.fractureGameObject(ref, px_blast_entt1, physics::anchor::anchor_none, seed, 15, defaultmat, defaultmat, 100.0f, 3.0f);
scene.deleteEntity(px_blast_entt1.handle);
}
}

Expand Down
25 changes: 13 additions & 12 deletions modules/core/src/px/blast/px_blast_destructions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,12 @@ namespace era_engine::physics
{
setupRigidbody();

//if(!isKinematic)
// freeze();
if(!isKinematic)
freeze();

jointToChunk.clear();
chunkToJoint.clear();

if (!manager->joints.contains(handle))
return;

auto enttScene = globalApp.getCurrentScene();

for (auto joint : manager->joints[handle])
Expand Down Expand Up @@ -210,8 +207,10 @@ namespace era_engine::physics
auto enttScene = globalApp.getCurrentScene();

eentity renderEntity{ handle, &enttScene->registry };

renderEntity.getComponent<physics::px_dynamic_body_component>().setConstraints(0);
auto& rb = renderEntity.getComponent<physics::px_dynamic_body_component>();
rb.setConstraints(0);
rb.setGravity(true);
rb.setFilterMask(-1, -1);
}

void chunk_graph_manager::chunk_node::freeze()
Expand All @@ -221,20 +220,22 @@ namespace era_engine::physics
auto enttScene = globalApp.getCurrentScene();

eentity renderEntity{ handle, &enttScene->registry };
auto& transform = renderEntity.getComponent<transform_component>();

frozenPos = renderEntity.getComponent<transform_component>().position;
frozenRot = renderEntity.getComponent<transform_component>().rotation;
frozenPos = transform.position;
frozenRot = transform.rotation;

renderEntity.getComponent<physics::px_dynamic_body_component>().setConstraints(PX_FREEZE_ALL);
auto& rb = renderEntity.getComponent<physics::px_dynamic_body_component>();
rb.setGravity(false);
rb.setConstraints(PX_FREEZE_ALL);
rb.setFilterMask(16, 16);
}

void chunk_graph_manager::setup(std::vector<entity_handle> bodies, uint32 generation)
{
nbNodes = bodies.size();
nodes.reserve(nbNodes);

physics_holder::physicsRef->unfreezeBlastQueue.reserve(nbNodes * 5);

auto enttScene = globalApp.getCurrentScene();

for (size_t i = 0; i < bodies.size(); i++)
Expand Down
49 changes: 20 additions & 29 deletions modules/core/src/px/blast/px_blast_fracture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "px/blast/px_nvmesh.h"
#include "px/blast/px_blast_destructions.h"
#include "px/core/px_extensions.h"
#include "px/physics/px_joint_component.h"

#include <NvBlastExtAuthoring.h>

Expand Down Expand Up @@ -195,18 +196,18 @@ namespace era_engine::physics
graphManager.setup(chunks);

// Connect blocks that are touching with fixed joints
//for (size_t i = 0; i < chunks.size(); i++)
//{
// connectTouchingChunks(graphManager, meshes[i].first, chunks[i], jointBreakForce);
//}
for (size_t i = 0; i < chunks.size(); i++)
{
connectTouchingChunks(graphManager, meshes[i].first, chunks[i], jointBreakForce);
}

for (auto chunk : chunks)
{
eentity renderEntity{ chunk, &enttScene->registry };
renderEntity.setParent(fractureGameObject);
}

//anchorChunks(gameObject.handle, anchor);
anchorChunks(gameObject.handle, anchor);

return fractureGameObject.handle;
}
Expand Down Expand Up @@ -375,33 +376,23 @@ namespace era_engine::physics
{
if (overlap != chunk)
{
{
eentity body{ overlap, &enttScene->registry };

auto& rbOverlap = body.getComponent<physics::px_dynamic_body_component>();

std::vector<PxFilterData> fd1 = getFilterData(rb.getRigidActor());
std::vector<PxFilterData> fd2 = getFilterData(rbOverlap.getRigidActor());
eentity body{ overlap, &enttScene->registry };

px_fixed_joint* joint = new px_fixed_joint(px_fixed_joint_desc{ 0.1f, 0.1f, 200.0f, 100.0f }, rb.getRigidActor(), rbOverlap.getRigidActor());
auto& rbOverlap = body.getComponent<physics::px_dynamic_body_component>();

joint->joint->setInvInertiaScale0(0.0f);
joint->joint->setInvInertiaScale1(0.0f);
joint->joint->setConstraintFlag(PxConstraintFlag::eCOLLISION_ENABLED, false);
body.addComponent<px_fixed_joint_component>(jointBreakForce);
auto& joint = body.getComponent<px_fixed_joint_component>();
joint.setPair(rbOverlap.getRigidActor(), rb.getRigidActor());

setFilterData(rb.getRigidActor(), fd1);
setFilterData(rbOverlap.getRigidActor(), fd2);

if (manager.joints.contains(chunk))
{
manager.joints[chunk].push_back(joint);
}
else
{
std::vector<px_fixed_joint*> jointVec;
jointVec.push_back(joint);
manager.joints.emplace(chunk, jointVec);
}
if (manager.joints.contains(chunk))
{
manager.joints[chunk].push_back(joint.getJoint());
}
else
{
std::vector<px_fixed_joint*> jointVec;
jointVec.push_back(joint.getJoint());
manager.joints.emplace(chunk, jointVec);
}
}
}
Expand Down
53 changes: 27 additions & 26 deletions modules/core/src/px/core/px_physics_engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,11 @@ namespace era_engine
pairFlags |= physx::PxPairFlag::eNOTIFY_CONTACT_POINTS;
pairFlags |= physx::PxPairFlag::eSOLVE_CONTACT;
pairFlags |= physx::PxPairFlag::eDETECT_DISCRETE_CONTACT;

return physx::PxFilterFlag::eDEFAULT;
}
return physx::PxFilterFlag::eDEFAULT;

return physx::PxFilterFlag::eSUPPRESS;
}

static void clearColliderFromCollection(const physics::px_body_component* collider,
Expand Down Expand Up @@ -789,6 +791,8 @@ namespace era_engine

newTriggerPairs.clear();
lostTriggerPairs.clear();

brokenJoints.clear();
}

void physics::px_simulation_event_callback::sendCollisionEvents()
Expand Down Expand Up @@ -823,40 +827,37 @@ namespace era_engine
{
}

void physics::px_simulation_event_callback::sendJointEvents()
{
for (auto* joint : brokenJoints)
{
if (joint->onJointBreakCallback)
joint->onJointBreakCallback(joint);
}
}

void physics::px_simulation_event_callback::onColliderRemoved(px_body_component* collider)
{
clearColliderFromCollection(collider, newTriggerPairs);
clearColliderFromCollection(collider, lostTriggerPairs);
}

void physics::px_simulation_event_callback::onConstraintBreak(PxConstraintInfo* constraints, PxU32 count)
void physics::px_simulation_event_callback::onJointRemoved(px_joint* joint)
{
#if !_DEBUG
PxRigidActor* act1;
PxRigidActor* act2;
constraints->constraint->getActors(act1, act2);

auto rb1 = physics::physics_holder::physicsRef->actorsMap[act1];
auto rb2 = physics::physics_holder::physicsRef->actorsMap[act1];

if (!rb1 || !rb2)
return;

auto enttScene = globalApp.getCurrentScene();

eentity entt1{ (entity_handle)rb1->entityHandle, &enttScene->registry };
eentity entt2{ (entity_handle)rb2->entityHandle, &enttScene->registry };

if (auto node1 = entt1.getComponentIfExists<physics::chunk_graph_manager::chunk_node>())
{
node1->onJointBreak();
}
brokenJoints.findAndReplaceWithLast(joint);
brokenJoints.popBack();
}

if (auto node2 = entt2.getComponentIfExists<physics::chunk_graph_manager::chunk_node>())
void physics::px_simulation_event_callback::onConstraintBreak(PxConstraintInfo* constraints, PxU32 count)
{
for (uint32 i = 0; i < count; i++)
{
node2->onJointBreak();
PxJoint* joint = reinterpret_cast<PxJoint*>(constraints[i].externalReference);
if (joint->userData)
{
brokenJoints.pushBack(static_cast<px_joint*>(joint->userData));
}
}
#endif
}

void physics::px_simulation_event_callback::onContact(const PxContactPairHeader& pairHeader, const PxContactPair* pairs, PxU32 nbPairs)
Expand Down
11 changes: 10 additions & 1 deletion modules/core/src/px/core/px_physics_engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ namespace era_engine::physics
struct px_body_component;
struct px_collider_component_base;
struct px_soft_body;
struct px_joint;

struct px_simulation_event_callback;

Expand Down Expand Up @@ -479,8 +480,12 @@ filterData.data.word2 = hitTriggers ? 1 : 0

void sendTriggerEvents();

void sendJointEvents();

void onColliderRemoved(px_body_component* collider);

void onJointRemoved(px_joint* joint);

void onConstraintBreak(PxConstraintInfo* constraints, PxU32 count) override;
void onWake(PxActor** actors, PxU32 count) override { std::cout << "onWake\n"; }
void onSleep(PxActor** actors, PxU32 count) override { std::cout << "onSleep\n"; }
Expand All @@ -492,6 +497,8 @@ filterData.data.word2 = hitTriggers ? 1 : 0

PxArray<px_body_component*> kinematicsToRemoveFlag;

PxArray<px_joint*> brokenJoints;

PxArray<px_collision> removedCollisions;

PxArray<colliders_pair> newTriggerPairs;
Expand Down Expand Up @@ -686,4 +693,6 @@ filterData.data.word2 = hitTriggers ? 1 : 0
#include "px/physics/px_character_controller_component.h"
#include "px/features/px_particles.h"
#include "px/features/cloth/px_clothing_factory.h"
#include "px/core/px_extensions.h"
#include "px/core/px_extensions.h"
#include "px/physics/px_joint.h"
#include "px/physics/px_joint_component.h"
21 changes: 13 additions & 8 deletions modules/core/src/px/physics/px_joint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,22 +67,25 @@ namespace era_engine::physics
px_revolute_joint::px_revolute_joint(px_revolute_joint_desc desc, PxRigidActor* f, PxRigidActor* s) : px_joint(f, s)
{
joint = createRevoluteJoint(f, s);
joint->userData = this;
type = px_joint_type::joint_type_revolute;
PxJointAngularLimitPair limitPair(desc.angularPair.lower,
desc.angularPair.upper);
limitPair.stiffness = desc.angularPair.stiffness;
limitPair.damping = desc.angularPair.damping;

auto jInstance = joint->is<PxRevoluteJoint>();
jInstance->setConstraintFlag(PxConstraintFlag::eVISUALIZATION, true);

if (desc.drive.anabledDrive)
{
jInstance->setDriveVelocity(desc.drive.driveVelocity);
jInstance->setDriveGearRatio(desc.drive.driveGearRatio);
jInstance->setDriveForceLimit(desc.drive.driveForceLimit);
jInstance->setRevoluteJointFlag(PxRevoluteJointFlag::eDRIVE_ENABLED, true);
}
#if PX_GPU_BROAD_PHASE
jInstance->setConstraintFlag(PxConstraintFlag::eGPU_COMPATIBLE, true);
#endif
jInstance->setLimit(limitPair);
jInstance->setRevoluteJointFlag(PxRevoluteJointFlag::eLIMIT_ENABLED, true);
}
Expand All @@ -95,16 +98,17 @@ namespace era_engine::physics
{
joint = createSphericalJoint(f, s);
type = px_joint_type::joint_type_spherical;

joint->userData = this;
PxJointLimitCone limitCone(desc.limitCone.yAngle,
desc.limitCone.zAngle);

limitCone.stiffness = desc.limitCone.stiffness;
limitCone.damping = desc.limitCone.damping;

auto jInstance = joint->is<PxSphericalJoint>();
jInstance->setConstraintFlag(PxConstraintFlag::eVISUALIZATION, true);
#if PX_GPU_BROAD_PHASE
jInstance->setConstraintFlag(PxConstraintFlag::eGPU_COMPATIBLE, true);
#endif
jInstance->setLimitCone(limitCone);
jInstance->setSphericalJointFlag(PxSphericalJointFlag::eLIMIT_ENABLED, true);
}
Expand All @@ -116,6 +120,7 @@ namespace era_engine::physics
px_prismatic_joint::px_prismatic_joint(px_prismatic_joint_desc desc, PxRigidActor* f, PxRigidActor* s) : px_joint(f, s)
{
joint = createPrismaticJoint(f, s);
joint->userData = this;
type = px_joint_type::joint_type_prismatic;
PxTolerancesScale ts;
ts.length = 1.0;
Expand All @@ -129,8 +134,9 @@ namespace era_engine::physics
limitPair.damping = desc.linearPair.damping;

auto jInstance = joint->is<PxPrismaticJoint>();
jInstance->setConstraintFlag(PxConstraintFlag::eVISUALIZATION, true);
#if PX_GPU_BROAD_PHASE
jInstance->setConstraintFlag(PxConstraintFlag::eGPU_COMPATIBLE, true);
#endif
jInstance->setLimit(limitPair);
jInstance->setPrismaticJointFlag(PxPrismaticJointFlag::eLIMIT_ENABLED, true);
}
Expand All @@ -142,9 +148,9 @@ namespace era_engine::physics
px_distance_joint::px_distance_joint(px_distance_joint_desc desc, PxRigidActor* f, PxRigidActor* s) : px_joint(f, s)
{
joint = createDistanceJoint(f, s);
joint->userData = this;
type = px_joint_type::joint_type_distance;
auto jInstance = joint->is<PxDistanceJoint>();
jInstance->setConstraintFlag(PxConstraintFlag::eVISUALIZATION, true);
jInstance->setDamping(desc.damping);
jInstance->setStiffness(desc.stiffness);
jInstance->setDistanceJointFlag(PxDistanceJointFlag::eSPRING_ENABLED, true);
Expand All @@ -161,10 +167,9 @@ namespace era_engine::physics
px_fixed_joint::px_fixed_joint(px_fixed_joint_desc desc, PxRigidActor* f, PxRigidActor* s) : px_joint(f, s)
{
joint = createFixedJoint(f, s);
joint->userData = this;
type = px_joint_type::joint_type_fixed;
auto jInstance = joint->is<PxFixedJoint>();
jInstance->setConstraintFlag(PxConstraintFlag::eVISUALIZATION, true);
jInstance->setConstraintFlag(PxConstraintFlag::eVISUALIZATION, false);
#if PX_GPU_BROAD_PHASE
jInstance->setConstraintFlag(PxConstraintFlag::eGPU_COMPATIBLE, true);
#endif
Expand All @@ -179,9 +184,9 @@ namespace era_engine::physics
px_d6_joint::px_d6_joint(px_d6_joint_desc desc, PxRigidActor* f, PxRigidActor* s) : px_joint(f, s)
{
joint = createD6Joint(f, s);
joint->userData = this;
type = px_joint_type::joint_type_d6;
auto jInstance = joint->is<PxD6Joint>();
jInstance->setConstraintFlag(PxConstraintFlag::eVISUALIZATION, true);
if (desc.drive.anabledDrive)
{
auto jointDrive = PxD6JointDrive();
Expand Down
Loading

0 comments on commit 76e2ed6

Please sign in to comment.