Ejemplo n.º 1
0
    //void createStack(const PxTransform& t, PxU32 size, PxReal halfExtent){
    void createStack(PxTransform t, uint size, float halfExtent)
    {
        //PxShape* shape = gPhysics->createShape(PxBoxGeometry(halfExtent, halfExtent, halfExtent), *gMaterial);
        PxShapePtr shape = gPhysics.createShape(new PxBoxGeometry(halfExtent, halfExtent, halfExtent), gMaterial);

        //for(PxU32 i=0; i<size;i++){
        // for(PxU32 j=0;j<size-i;j++){
        for (int i = 0; i < size; i++)
        {
            for (int j = 0; j < size - i; j++)
            {
                //PxTransform localTm(PxVec3(PxReal(j*2) - PxReal(size-i), PxReal(i*2+1), 0) * halfExtent);
                //PxRigidDynamic* body = gPhysics->createRigidDynamic(t.transform(localTm));
                //body->attachShape(*shape);
                //PxRigidBodyExt::updateMassAndInertia(*body, 10.0f);
                //gScene->addActor(*body);
                PxTransform       localTm = new PxTransform(new PxVec3(((j * 2) - (size - i)) * halfExtent, (i * 2 + 1) * halfExtent, 0));
                PxRigidDynamicPtr body    = gPhysics.createRigidDynamic(t.transform(localTm));
                body.attachShape(shape);
                PxRigidBodyExt.updateMassAndInertia(body, 10);
                gScene.addActor(body);
            }
        }
        //shape->release();
        shape.release();
    }
    /**
     * Deserialize shared data and use resulting collection to deserialize and instance actor collections
     */
    public static void deserializeObjects(ref PxInputData sharedData, ref PxInputData actorData)
    {
        PxSerializationRegistry *sr = PxSerialization.createSerializationRegistry(ref *gPhysics);

        PxCollection *sharedCollection = null;
        {
            if (gUseBinarySerialization)
            {
                void *alignedBlock = createAlignedBlock(sharedData.getLength());
                sharedData.read(alignedBlock, sharedData.getLength());
                sharedCollection = PxSerialization.createCollectionFromBinary(alignedBlock, ref *sr);
            }
            else
            {
                sharedCollection = PxSerialization.createCollectionFromXml(ref sharedData, ref *gCooking, ref *sr);
            }
        }

        // Deserialize collection and instantiate objects twice, each time with a different transform
        PxTransform *transforms = stackalloc PxTransform[2] {
            new PxTransform(new PxVec3(-5.0f, 0.0f, 0.0f)), new PxTransform(new PxVec3(5.0f, 0.0f, 0.0f))
        };

        for (uint i = 0; i < 2; i++)
        {
            PxCollection *collection = null;

            // If the PxInputData actorData would refer to a file, it would be better to avoid reading from it twice.
            // This could be achieved by reading the file once to memory, and then working with copies.
            // This is particulary practical when using binary serialization, where the data can be directly
            // converted to physics objects.
            actorData.seek(0);

            if (gUseBinarySerialization)
            {
                void *alignedBlock = createAlignedBlock(actorData.getLength());
                actorData.read(alignedBlock, actorData.getLength());
                collection = PxSerialization.createCollectionFromBinary(alignedBlock, ref *sr, sharedCollection);
            }
            else
            {
                collection = PxSerialization.createCollectionFromXml(ref actorData, ref *gCooking, ref *sr, sharedCollection);
            }

            for (uint o = 0; o < collection->getNbObjects(); o++)
            {
                //BIOQUIRK: is<T> is not translated https://github.com/MochiLibraries/Mochi.PhysX/issues/11
                //PxRigidActor* rigidActor = collection->getObject(o).is<PxRigidActor>();
                PxRigidActor *rigidActor = null;
                {
                    PxBase *obj = collection->getObject(o);
                    //BIOQUIRK: Because PxRigidActor is not concrete, this ends up going down the isKindOf path, which is not publicly accessible.
                    // This is special-cased for this snippet, ideally we should just expose is<T> in a more C#-friendly way.
                    if ((PxConcreteType)obj->getConcreteType() is PxConcreteType.eRIGID_STATIC or PxConcreteType.eARTICULATION_LINK or PxConcreteType.eRIGID_DYNAMIC)
                    {
                        rigidActor = (PxRigidActor *)obj;
                    }
                }

                if (rigidActor != null)
                {
                    PxTransform globalPose = rigidActor->getGlobalPose();
                    globalPose = globalPose.transform(transforms[i]);
                    rigidActor->setGlobalPose(globalPose);
                }
            }

            gScene->addCollection(*collection);
            collection->release();
        }
        sharedCollection->release();

        PxMaterial *material;

        gPhysics->getMaterials(&material, 1);
        PxRigidStatic *groundPlane = PxCreatePlane(ref *gPhysics, new PxPlane(0, 1, 0, 0), ref *material);

        gScene->addActor(ref *groundPlane);
        sr->release();
    }