I added physx-jni-natives-windows-cuda-2.5.1.jar to the project, but I still get the error "internal error: Failed to load PhysXGpu_64.dll!".
<dependencies>
<!-- Java bindings -->
<dependency>
<groupId>de.fabmax</groupId>
<artifactId>physx-jni</artifactId>
<version>2.5.1</version>
</dependency>
<!-- Native libraries -->
<dependency>
<groupId>de.fabmax</groupId>
<artifactId>physx-jni</artifactId>
<version>2.5.1</version>
<classifier>natives-windows</classifier>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.lwjgl</groupId>
<artifactId>lwjgl</artifactId>
<version>3.3.6</version>
</dependency>
<dependency>
<groupId>org.lwjgl</groupId>
<artifactId>lwjgl</artifactId>
<classifier>natives-windows</classifier>
<version>3.3.6</version>
</dependency>
</dependencies>
import physx.PxTopLevelFunctions;
import physx.common.*;
import org.lwjgl.system.MemoryStack;
import physx.geometry.PxBoxGeometry;
import physx.physics.*;
import physx.support.PxPvd;
import physx.support.PxPvdInstrumentationFlagEnum;
import physx.support.PxPvdInstrumentationFlags;
import physx.support.PxPvdTransport;
public class Main {
public static void main(String[] args) {
int version = PxTopLevelFunctions.getPHYSICS_VERSION();
int versionMajor = version >> 24;
int versionMinor = (version >> 16) & 0xff;
int versionMicro = (version >> 8) & 0xff;
System.out.printf("PhysX loaded, version: %d.%d.%d\n", versionMajor, versionMinor, versionMicro);
// create PhysX foundation object
PxDefaultAllocator allocator = new PxDefaultAllocator();
PxDefaultErrorCallback errorCb = new PxDefaultErrorCallback();
PxFoundation foundation = PxTopLevelFunctions.CreateFoundation(version, allocator, errorCb);
// create PhysX main physics object
PxTolerancesScale tolerances = new PxTolerancesScale();
PxPhysics physics = PxTopLevelFunctions.CreatePhysics(version, foundation, tolerances);
int numThreads = 4;
PxDefaultCpuDispatcher cpuDispatcher = PxTopLevelFunctions.DefaultCpuDispatcherCreate(numThreads);
//初始化cuda
PxCudaContextManager cudaMgr;
try (MemoryStack mem = MemoryStack.stackPush()) {
PxCudaContextManagerDesc desc = PxCudaContextManagerDesc.createAt(mem, MemoryStack::nmalloc);
cudaMgr = PxCudaTopLevelFunctions.CreateCudaContextManager(foundation, desc);
if (cudaMgr == null || !cudaMgr.contextIsValid()) {
System.err.println("Failed creating CUDA context, no CUDA capable GPU?");
cudaMgr = null;
}
}
PxPvd pvd = PxTopLevelFunctions.CreatePvd(foundation);
PxPvdTransport pvdTransport = PxTopLevelFunctions.DefaultPvdSocketTransportCreate("127.0.0.1", 5425, 10);
PxPvdInstrumentationFlags pxPvdInstrumentationFlags = new PxPvdInstrumentationFlags((byte) PxPvdInstrumentationFlagEnum.eALL.value);
//success is true
boolean success = pvd.connect(pvdTransport, pxPvdInstrumentationFlags);
PxVec3 tmpVec = new PxVec3(0f, -9.81f, 0f);
//初始化场景
PxSceneDesc sceneDesc;
PxScene scene;
try (MemoryStack mem = MemoryStack.stackPush()) {
sceneDesc = PxSceneDesc.createAt(mem, MemoryStack::nmalloc, physics.getTolerancesScale());
sceneDesc.setGravity(tmpVec);
sceneDesc.setCpuDispatcher(cpuDispatcher);
sceneDesc.setFilterShader(PxTopLevelFunctions.DefaultFilterShader());
sceneDesc.setCudaContextManager(cudaMgr);
sceneDesc.setStaticStructure(PxPruningStructureTypeEnum.eDYNAMIC_AABB_TREE);
sceneDesc.getFlags().raise(PxSceneFlagEnum.eENABLE_PCM);
sceneDesc.getFlags().raise(PxSceneFlagEnum.eENABLE_GPU_DYNAMICS);
sceneDesc.setBroadPhaseType(PxBroadPhaseTypeEnum.eGPU);
sceneDesc.setSolverType(PxSolverTypeEnum.eTGS);
scene=physics.createScene(sceneDesc);
}
PxMaterial material = physics.createMaterial(0.5f, 0.5f, 0.5f);
PxShapeFlags shapeFlags = new PxShapeFlags((byte) (PxShapeFlagEnum.eSCENE_QUERY_SHAPE.value | PxShapeFlagEnum.eSIMULATION_SHAPE.value));
// create a few temporary objects used during setup
PxTransform tmpPose = new PxTransform(PxIDENTITYEnum.PxIdentity);
PxFilterData tmpFilterData = new PxFilterData(1, 1, 0, 0);
// create a large static box with size 20x1x20 as ground
PxBoxGeometry groundGeometry = new PxBoxGeometry(10f, 0.5f, 10f); // PxBoxGeometry uses half-sizes
PxShape groundShape = physics.createShape(groundGeometry, material, true, shapeFlags);
PxRigidStatic ground = physics.createRigidStatic(tmpPose);
groundShape.setSimulationFilterData(tmpFilterData);
ground.attachShape(groundShape);
scene.addActor(ground);
// create a small dynamic box with size 1x1x1, which will fall on the ground
tmpVec.setX(0f); tmpVec.setY(5f); tmpVec.setZ(0f);
tmpPose.setP(tmpVec);
PxBoxGeometry boxGeometry = new PxBoxGeometry(0.5f, 0.5f, 0.5f); // PxBoxGeometry uses half-sizes
PxShape boxShape = physics.createShape(boxGeometry, material, true, shapeFlags);
PxRigidDynamic box = physics.createRigidDynamic(tmpPose);
boxShape.setSimulationFilterData(tmpFilterData);
box.attachShape(boxShape);
scene.addActor(box);
// clean up temp objects
groundGeometry.destroy();
boxGeometry.destroy();
tmpFilterData.destroy();
tmpPose.destroy();
tmpVec.destroy();
shapeFlags.destroy();
sceneDesc.destroy();
tolerances.destroy();
float boxHeight = box.getGlobalPose().getP().getY();
for (int i = 0; i <= 500; i++) {
scene.simulate(1f/60f);
scene.fetchResults(true);
boxHeight = box.getGlobalPose().getP().getY();
if (i % 10 == 0) {
System.out.println("Step " + i + ": h = " + boxHeight);
}
}
// cleanup stuff
scene.removeActor(ground);
ground.release();
groundShape.release();
scene.removeActor(box);
box.release();
boxShape.release();
pvdTransport.release();
pvd.release();
scene.release();
material.release();
physics.release();
foundation.release();
errorCb.destroy();
allocator.destroy();
}
}
I added physx-jni-natives-windows-cuda-2.5.1.jar to the project, but I still get the error "internal error: Failed to load PhysXGpu_64.dll!".