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