init_physx :: () { default_allocator = PhysX.get_default_allocator(); default_error_callback = PhysX.get_default_error_callback(); foundation := PhysX.PxCreateFoundation(PhysX.PX_PHYSICS_VERSION, *default_allocator, *default_error_callback); if foundation == null { log_error("Could not initialize PhysX\n"); return; } pvd := PhysX.PxCreatePvd(foundation); transport := PhysX.PxDefaultPvdSocketTransportCreate("127.0.0.1", 5425, 10); PhysX.PxPvd_connect(pvd, transport, cast(u8)PhysX.PxPvdInstrumentationFlags.ALL); tolerance_scale : PhysX.PxTolerancesScale; tolerance_scale.length = 100; tolerance_scale.speed = 981; physics = PhysX.PxCreatePhysics(PhysX.PX_PHYSICS_VERSION, foundation, *tolerance_scale, true, pvd, null); dispatcher = PhysX.PxDefaultCpuDispatcherCreate(2); material = PhysX.PxPhysics_createMaterial(physics, 0.5, 0.5, 0.6); } tick_physx :: (scene: *PhysX.PxScene, dt: float) { PhysX.PxScene_simulate(scene, dt, null, null, 0, false); PhysX.PxScene_fetchResults(scene, true, null); } create_physx_scene :: () -> *PhysX.PxScene { tolerance_scale : PhysX.PxTolerancesScale; tolerance_scale.length = 100; tolerance_scale.speed = 981; scene_desc := PhysX.PxSceneDesc_new(*tolerance_scale); scene_desc.gravity.y = -9.81; scene_desc.cpuDispatcher = xx dispatcher; PhysX.set_default_filter_shader(*scene_desc); scene := PhysX.PxPhysics_createScene(physics, *scene_desc); // @Incomplete: If debug pvd_client := PhysX.PxScene_getScenePvdClient(scene); if pvd_client { PhysX.PxPvdSceneClient_setScenePvdFlag(pvd_client, xx PhysX.PxPvdSceneFlag.TRANSMIT_CONSTRAINTS, true); PhysX.PxPvdSceneClient_setScenePvdFlag(pvd_client, xx PhysX.PxPvdSceneFlag.TRANSMIT_CONTACTS, true); PhysX.PxPvdSceneClient_setScenePvdFlag(pvd_client, xx PhysX.PxPvdSceneFlag.TRANSMIT_SCENEQUERIES, true); //PhysX.PxScene_setVisualizationParameter(scene, 0, 1.0); //PhysX.PxScene_setVisualizationParameter(scene, 1, 2.0); } { plane := PhysX.PxPlane_new(0.0,-1.0,0.0,50.0); ground_plane := PhysX.PxCreatePlane(physics, *plane, material); PhysX.PxScene_addActor(scene, ground_plane, null); } { plane := PhysX.PxPlane_new(0.0,-1.0,0.0,50.0); ground_plane := PhysX.PxCreatePlane(physics, *plane, material); PhysX.PxScene_addActor(scene, ground_plane, null); } { plane := PhysX.PxPlane_new(0.0,-1.0,0.0,50.0); ground_plane := PhysX.PxCreatePlane(physics, *plane, material); PhysX.PxScene_addActor(scene, ground_plane, null); } { plane := PhysX.PxPlane_new(0.0,-1.0,0.0,50.0); ground_plane := PhysX.PxCreatePlane(physics, *plane, material); PhysX.PxScene_addActor(scene, ground_plane, null); } stack_z := 10.0; for i: 0..4 { stack_pos := Vector3.{0,100,stack_z}; stack_z -= 10.0; create_stack(scene, PhysX.PxTransform_new(*stack_pos), 10, 2.0); } return scene; } create_stack :: (scene: *PhysX.PxScene, t: PhysX.PxTransform, size: u32, half_extent: float) { shape := PhysX.PxPhysics_createShape(physics, PhysX.PxBoxGeometry_new(half_extent, half_extent, half_extent), material, false, 0); for i: 0..size-1 { for j: 0..size-i-1 { pos := Vector3.{cast(float)(j*2) - cast(float)(size-i), cast(float)(i*2+1), 0} * half_extent; local_tm := PhysX.PxTransform_new(*pos); body := PhysX.PxPhysics_createRigidDynamic(physics, *PhysX.PxTransform_transform(*t, *local_tm)); PhysX.PxRigidActor_attachShape(body, shape); PhysX.PxRigidBodyExt_updateMassAndInertia(body, 10., null, false); PhysX.PxScene_addActor(scene, body, null); } } //shape->release(); } create_dynamic :: (scene: *PhysX.PxScene, t: PhysX.PxTransform, geometry: PhysX.PxGeometry, velocity: Vector3 = .{}) -> *PhysX.PxRigidDynamic { p := Vector3.{0,0,0}; dynamic := PhysX.PxCreateDynamic(physics, *t, *geometry, material, 10.0, *PhysX.PxTransform_new(cast(s32)PhysX.PxIDENTITY.PxIdentity)); PhysX.PxRigidBody_setAngularDamping(dynamic, 0.5); PhysX.PxRigidDynamic_setLinearVelocity(dynamic, *velocity, true); PhysX.PxScene_addActor(scene, dynamic, null); return dynamic; } PhysX :: #import "PhysX"; #scope_file physics : *PhysX.PxPhysics; material : *PhysX.PxMaterial; default_allocator : PhysX.PxAllocatorCallback; default_error_callback : PhysX.PxErrorCallback; default_filter_shader : PhysX.SimulationFilterShader; dispatcher : *PhysX.PxDefaultCpuDispatcher;