How to fix rigid "bodies" using PhysX

7

How can I fix two hard or dynamic "bodies" in a way that where one goes the other goes as close as possible to PhysX

.

I'm using the PxFixedJoint class but it did not please me.

Asyoucanseeintheprintabove,thereisa"belly" curve in the formation of the cubes.  I already researched but found nothing concrete. I wanted a solution to fix the objects to each other but with a minimum of curve (so to speak).

Below is a snippet of the code.

// C++
// Função responsável por criar o joint
void HEntity::createFixedJoint(HEntity* target)
{
    physx::PxPhysics* physics = HEngine::getInstance()->getPhysicsContext()->getPhysics();
    physx::PxRigidActor* _target = target->getPhysicsBody();

    physx::PxVec3 pos_a = m_pPhysicsBody->getGlobalPose().p;
    physx::PxVec3 pos_b = _target->getGlobalPose().p;
    physx::PxVec3 offset = (pos_a - pos_b).abs() / 2;

    physx::PxFixedJoint* joint = physx::PxFixedJointCreate(*physics, m_pPhysicsBody, physx::PxTransform(-offset), _target, physx::PxTransform(offset));

    joint->setConstraintFlag(physx::PxConstraintFlag::eVISUALIZATION, true);
    joint->setConstraintFlag(physx::PxConstraintFlag::eCOLLISION_ENABLED, false);

    m_lpJoints.push_back(joint);
}

As I'm using Lua Script to extend / extend my application, below is an excerpt from the script in Lua that creates the cubes.

-- Lua
-- Função responsável por criar os cubos em serie
function createJointCubes()
    local parent = scene:getObjectByName("Object.2") -- Cubo já presente na cena
    local last_obj = nil;

    for i = 1, 12 do
        local obj = HGameObject:new()
        obj:setName("Object_Phy." .. i)
        obj:setMesh(HEngine.importWavefront(imports[2]))

        if last_obj == nil then
            obj:setWorldPosition(parent:getWorldPosition()+HVec3(2, 0, 0))
        else
            obj:setWorldPosition(last_obj:getWorldPosition()+HVec3(2, 0, 0))
        end

        obj:createPhysicsBody()

        if last_obj == nil then
            obj:createFixedJoint(parent)
        else
            obj:createFixedJoint(last_obj)
        end

        scene:addObject(obj)
        last_obj = obj
    end
end


    
asked by anonymous 31.07.2014 / 19:59

1 answer

4

RESOLVED

Well, after a lot of searching, I ended up finding a solution in the PhysX documentation itself.

What I did was "parental" (make "son") the object I wanted (which in the case is static) to a rigidbody object by multiplying the transformation of the "parent" object by the a "son."

Then I used the attachShape method to add the physical shape of the "child" to the list of physical shapes of the "parent", and I updated the mass and inertia .

// C++
// Acada frame ("Parentando")
m_FilhoWorldTransform =  m_pPai->getWorldTransform() * m_FilhoWorldTransform;
// ...
// Adicionando shape "filho" ao rigidbody "Pai"
m_pPai->getRigidBody()->attachShape(m_Filho->getShape());
// ...
// Atualizando massa e inércia
physx::PxU32 mass_count = 2; // Numero de shapes/massas
physx::PxReal* masses = new physx::PxReal[2];
masses[0] = pai_mass;
masses[1] = filho_mass;

physx::PxRigidBodyExt::setMassAndUpdateInertia(*parent->getRigidDynamic(), masses, mass_count);
delete masses;

And see the result. No more "belly". Physics works correctly, it's as if the cubes were part of the "Father" object.

    
18.08.2014 / 13:58