NVIDIAGameWorks / PhysX

NVIDIA PhysX SDK

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Bare-bones example to make cube freefall appears to not work

LeeYiyuan opened this issue · comments

I'm trying to create a bare-bones example of dropping a cube. I've setup everything according to the guides, and have spawned a cube in the world. After applying a force and steping the simulation, it seems that the cube's position does not move at all. I am sure that I'm missing something dumb at this point, but am unable to figure it out.

  // Create default error handlers and allocators.
  static PxDefaultErrorCallback gDefaultErrorCallback;
  static PxDefaultAllocator gDefaultAllocatorCallback;

  // Create foundation.
  PxFoundation* foundation = PxCreateFoundation(
      PX_PHYSICS_VERSION, gDefaultAllocatorCallback, gDefaultErrorCallback);

  // Create physics.
  PxPhysics* physics = PxCreatePhysics(PX_PHYSICS_VERSION, *foundation,
                                       PxTolerancesScale(), false, nullptr);

  // Create scene.
  PxSceneDesc desc((PxTolerancesScale()));
  PxScene* scene = physics->createScene(desc);

  // Create material.
  PxMaterial* material = physics->createMaterial(1.0, 1.0, 1.0);

  // Create actor.
  PxRigidDynamic* actor = physics->createRigidDynamic(PxTransform(0, 0, 1));
  actor->setRigidBodyFlag(PxRigidBodyFlag::eKINEMATIC, false);
  actor->setRigidBodyFlag(
      PxRigidBodyFlag::eUSE_KINEMATIC_TARGET_FOR_SCENE_QUERIES, false);
  PxShape* shape = PxRigidActorExt::createExclusiveShape(
      *actor, PxBoxGeometry(0.5, 0.5, 0.5), *material);
  PxRigidBodyExt::setMassAndUpdateInertia(*actor, 1.0);
  scene->addActor(*actor);

  // Apply downward force.
  actor->addForce(PxVec3(0, 0, -9.81));

  // Simulate.
  for (size_t i = 0; i < 1000; i++) {
    scene->simulate(1.0 / 60);
    scene->fetchResults();

    // This always prints 0, 0, 1.
    auto p = actor->getGlobalPose().p;
    std::cout << p.x << ", " << p.y << ", " << p.z << std::endl;
  }

Fixed with desc.cpuDispatcher = PxDefaultCpuDispatcherCreate(1);.