NVIDIAGameWorks / PhysX-3.4

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

Why dose the application crash at cloth1->removeCollisionCapsule( 1 ) #73

Closed Mubai64 closed 6 years ago

Mubai64 commented 6 years ago

Hello, I'm using the PhysX-3.3 and the cloth1 is a PxCloth* Why dose the application crash at cloth1->removeCollisionCapsule( 1 ) (T_T)

cloth1->addCollisionSphere( PxClothCollisionSphere( PxVec3(2.0f, 0.0f, 0.0f), 1.0f ) );
cloth1->addCollisionSphere( PxClothCollisionSphere( PxVec3(0.0f, 2.0f, 0.0f), 1.0f ) );
cloth1->addCollisionSphere( PxClothCollisionSphere( PxVec3(-2.0f, 0.0f, 0.0f), 1.0f ) );
cloth1->addCollisionSphere( PxClothCollisionSphere( PxVec3(0.0f, -2.0f, 0.0f), 1.0f ) );

cloth1->addCollisionCapsule( 0, 1 );
cloth1->addCollisionCapsule( 1, 2 );
cloth1->addCollisionCapsule( 2, 3 );
cloth1->addCollisionCapsule( 3, 1 );

cloth1->removeCollisionCapsule( 1 );

default default

sschirm commented 6 years ago

Hello, Thanks for the bug report. We are probably not going to release another patch for 3.3. I tried your code and found a trivial bug in Source/LowLevelCloth/src/ClothImpl.h in the function inline void ClothImpl::setCapsules( Range capsules, uint32_t first, uint32_t last )

Please try to replace the function with the one below. Thanks!

template inline void ClothImpl::setCapsules( Range capsules, uint32_t first, uint32_t last ) { const IndexPair srcIndices = reinterpret_cast<const IndexPair>(capsules.begin()); const uint32_t srcIndicesSize = uint32_t(capsules.size() / 2);

uint32_t oldSize = mCloth.mCapsuleIndices.size();
uint32_t newSize = srcIndicesSize + oldSize - last + first;

PX_ASSERT(newSize <= 32);
PX_ASSERT(first <= oldSize);
PX_ASSERT(last <= oldSize);

if(mCloth.mCapsuleIndices.capacity() < newSize)
{
    ContextLockType contextLock(mCloth.mFactory);
    mCloth.mCapsuleIndices.reserve(newSize);
}

// resize to larger of oldSize and newSize
mCloth.mCapsuleIndices.resize(PxMax(oldSize, newSize));

typename T::MappedIndexVectorType dstIndices = mCloth.mCapsuleIndices;

if(uint32_t delta = newSize-oldSize)
{
    // move past-range elements to new place
    move(dstIndices.begin(), last, oldSize, last + delta);

    // fill new elements from capsules
    for(uint32_t i=last; i<last+delta; ++i)
        dstIndices[i] = srcIndices[i-first];

    dstIndices.resize(newSize);
    mCloth.notifyChanged();
}

// fill existing elements from capsules
for (uint32_t i=0; i < srcIndicesSize; ++i)
    dstIndices[first + i] = srcIndices[i];

mCloth.wakeUp();

}