NVIDIAGameWorks / PhysX-3.4

NVIDIA PhysX SDK 3.4
https://www.nvidia.com/
2.35k stars 276 forks source link

How can I change a shape's local position #138

Closed MoonHJ98 closed 2 years ago

MoonHJ98 commented 2 years ago

I want to change the local position of a shape in Phsyx.

Even if the setLocalposition() function of the shape is called, the position of the shape does not change.

The setLocalposition() function seems to change the value in the connected actor (rigidbody), but even if the shape's getActor() function is called, NULL is returned.

In PxShape.h, NULL is returned if shape is not exclusive, but I don't know what this "exclusive" means. Can you tell me what to do?

Here is my code for changing local position for shape

float pos[3] = { transform.p.x, transform.p.y, transform.p.z };

ImGui::InputFloat3("Center", pos);
transform.p = PxVec3(pos[0], pos[1], pos[2]);

PxTransform localTm(pos[0], pos[1], pos[2]);
PxTransform _transform(PxVec3(0));

shape->setLocalPose(_transform.transform(localTm));

this is my code for create collider

HRESULT Collider::Initialize(shared_ptr<GameObject> _object, 
PxGeometryType::Enum _geoType)
{
     object = _object;
     componentType = COLLIDER;

     geoType = _geoType;
     shape = AddCollider();

     rigidbody = FindRigidbody();

     if (rigidbody.lock() != nullptr)
         AddColliderToRigidbody();
     else
         AddRigidBodyForCollider();

     colliderRenderer = ColliderRenderer::Create(object.lock(), geoType);

     transform = shape->getLocalPose();

     return S_OK;
}

and this is for attach shape to rigidbody

 void Collider::AddRigidBodyForCollider()
 {
     auto rigid = Rigidbody::Create(object.lock());
     rigidbody = rigid;

     rigidbody.lock()->GetRigidBody().attachShape(*shape);

     object.lock()->GetComponents()[ComponentType::RIGIDBODY] = rigidbody.lock();

 }
kstorey-nvidia commented 2 years ago

Are you getting any errors/warnings when trying to set the local pose on the shape?

This operation should work, but you need to be aware that it is only legal to call if (a) the shape is an exclusive shape or (b) it is a shared shape that is not attached to any actors.