#include #include"physxApi.hpp" //void (*)(void*, physx::PxTriggerPair const*, physx::PxU32); static void onTrigger(void* userData, physx::PxTriggerPair* pairs, physx::PxU32 count) { //userdataaaa* data = (userdataaaa*)userData; //int a = data->value; int a = 0; } //void (*)(void*, physx::PxContactPairHeader const*, physx::PxContactPair const*, physx::PxU32); static void onContact(void* userData, physx::PxContactPairHeader const * pairHeader, physx::PxContactPair const* pairs, physx::PxU32 nbPairs) { //userdataaaa* data = (userdataaaa*)userData; //int a = data->value; int a = 0; } //onContact gOnContact; void onConstraintBreak(void* userData, physx::PxConstraintInfo* info, physx::PxU32 flag) { int a = 0; } void onWake(void* userData, physx::PxActor** actors, physx::PxU32 count, bool isSleep) { int a = 0; } void onAdvance(void* userData, physx::PxRigidBody** ppRigid, physx::PxTransform* pTrans, unsigned int nb) { int a = 0; } //virtual PxFilterFlags pairFound(PxU64 pairID, // PxFilterObjectAttributes attributes0, PxFilterData filterData0, const PxActor* a0, const PxShape* s0, // PxFilterObjectAttributes attributes1, PxFilterData filterData1, const PxActor* a1, const PxShape* s1, // PxPairFlags& pairFlags) = 0; physx::PxFilterFlags pairFound(physx::PxU32 pairID, physx::PxFilterObjectAttributes attributes0, physx::PxFilterData filterData0, const physx::PxActor* a0, const physx::PxShape* s0, physx::PxFilterObjectAttributes attributes1, physx::PxFilterData filterData1, const physx::PxActor* a1, const physx::PxShape* s1, physx::PxPairFlags* pairFlags) { *pairFlags = physx::PxPairFlag::eCONTACT_DEFAULT; *pairFlags = physx::PxPairFlag::eTRIGGER_DEFAULT; //return physx::PxFilterFlags(); return physx::PxFilterFlag::eCALLBACK; } //typedef physx::PxQueryHitType::Enum(*RaycastHitCallback)(const physx::PxRigidActor* actor, const physx::PxFilterData* filterData, const physx::PxShape* shape, physx::PxU32 hitFlags, const void* userData); // uint32_t(*RaycastHitCallback)(const physx::PxRigidActor* actor, const physx::PxFilterData* filterData, const physx::PxShape* shape, // uint32_t hitFlags, const void* userData); static physx::PxQueryHitType::Enum preFilter(const physx::PxRigidActor* actor, const physx::PxFilterData* filterData, const physx::PxShape* shape, physx::PxU32 flags, const void* userData) { return physx::PxQueryHitType::eBLOCK; } static physx::PxFilterFlags PhysxWorldFilterShader( physx::PxU32 attributes0, physx::PxFilterData filterData0, physx::PxU32 attributes1, physx::PxFilterData filterData1, physx::PxPairFlags& pairFlags, void* constantBlock, physx::PxU32 constantBlockSize) { //var trigger0 = phys_PxFilterObjectIsTrigger(attributes0); //var trigger1 = phys_PxFilterObjectIsTrigger(attributes1); //var type0 = phys_PxGetFilterObjectType(attributes0); //var type1 = phys_PxGetFilterObjectType(attributes1); pairFlags = physx::PxPairFlag::eSOLVE_CONTACT | physx::PxPairFlag::eDETECT_DISCRETE_CONTACT | physx::PxPairFlag::eNOTIFY_TOUCH_FOUND | physx::PxPairFlag::eNOTIFY_TOUCH_LOST | physx::PxPairFlag::eNOTIFY_CONTACT_POINTS; return (physx::PxFilterFlags)0x4; //返回kill也会崩溃 //return PxFilterFlags.Kill; //return PxFilterFlags.Notify; } int main() { #define PX_PHYSICS_VERSION_MAJOR 5 #define PX_PHYSICS_VERSION_MINOR 1 #define PX_PHYSICS_VERSION_BUGFIX 3 #define PX_PHYSICS_VERSION ((PX_PHYSICS_VERSION_MAJOR<<24) + (PX_PHYSICS_VERSION_MINOR<<16) + (PX_PHYSICS_VERSION_BUGFIX<<8) + 0) //创建found physx_PxFoundation_Pod* _Foundation = (physx_PxFoundation_Pod*)physx_create_foundation(); physx_PxPvd_Pod* _Pvd = phys_PxCreatePvd(_Foundation); physx_PxTolerancesScale_Pod tolerancesScale; tolerancesScale.length = 1; tolerancesScale.speed = 10; physx_PxPhysics_Pod * _Physics = phys_PxCreatePhysics(PX_PHYSICS_VERSION, _Foundation, &tolerancesScale, true, _Pvd, nullptr); //physx_PxPhysics_Pod* _Physics = (physx_PxPhysics_Pod*)physx_create_physics(_Foundation); physx_PxDefaultCpuDispatcher_Pod * _Dispatcher = phys_PxDefaultCpuDispatcherCreate(1, nullptr, 0, 0); physx_PxMaterial_Pod * _Material = PxPhysics_createMaterial_mut(_Physics, 0.0f, 0.0f, 0.0f); #define PVD_HOST "192.168.101.35" physx_PxPvdTransport_Pod*transport = phys_PxDefaultPvdSocketTransportCreate(PVD_HOST, 5425, 10); bool bSuccess = PxPvd_connect_mut(_Pvd, transport,0x7); physx_PxSceneDesc_Pod sceneDesc = PxSceneDesc_new(PxPhysics_getTolerancesScale(_Physics)); //sceneDesc.gravity = new Vector3(9.8f, 0.0f, 0.0f); physx_PxVec3_Pod gravity; gravity.x = 9.8f; gravity.y = 0; gravity.z = 0; sceneDesc.gravity = gravity; sceneDesc.cpuDispatcher = (physx_PxCpuDispatcher_Pod*)_Dispatcher; //需要回头写一下 //sceneDesc.filterShader = get_default_simulation_filter_shader(); //这一步实现的有问题 需要重新实现 //enable_custom_filter_shader 需要使用这个api c#版本中已经修改了 //直接使用filterShader c api没有问题 c#中有问题 sceneDesc.filterShader = PhysxWorldFilterShader; // SimuliationFilterCallBackInfo _filter_call_back; _filter_call_back.pairFoundCallback = pairFound; //必须初始化为nullptr _filter_call_back不会对内部成员自动置为 nullptr 否则 physx 会报错 _filter_call_back.statusChangeCallback = nullptr; _filter_call_back.pairLostCallback = nullptr; //创建 filter call back 写那么多代码就为了验证这一行的功能 。。 妖兽啊 。。。啊啊啊啊啊啊啊啊啊啊啊啊 sceneDesc.filterCallback = (physx_PxSimulationFilterCallback_Pod*)create_simulation_filter_callbacks(&_filter_call_back); //sceneDesc.filterCallback = nullptr; SimulationEventCallbackInfo _event_call_back; //需要回头详细写一下 _event_call_back.collisionCallback = onContact; _event_call_back.collisionUserData = nullptr; sceneDesc.simulationEventCallback = (physx_PxSimulationEventCallback_Pod*)create_simulation_event_callbacks(&_event_call_back); //创建场景 physx_PxScene_Pod *px_scene_ = PxPhysics_createScene_mut(_Physics, &sceneDesc); //创建第一个刚体 physx_PxVec3_Pod startPosition ; startPosition.x = 10; startPosition.y = 0; startPosition.z = 1; physx_PxQuat_Pod startOrientation ; startOrientation.x = 0; startOrientation.y = 0; startOrientation.z = 0; startOrientation.w = 1; physx_PxSphereGeometry_Pod sphere = PxSphereGeometry_new(2.0f); physx_PxGeometry_Pod* geometry = (physx_PxGeometry_Pod*)&sphere; PxScene_lockWrite_mut(px_scene_ ,nullptr ,0 ); physx_PxRigidActor_Pod* rigActor = nullptr; physx_PxTransform_Pod identity = PxTransform_new_2(100); physx_PxTransform_Pod pxTran ; pxTran.p = startPosition; pxTran.q = startOrientation; rigActor = (physx_PxRigidActor_Pod*)phys_PxCreateDynamic( _Physics, &pxTran, geometry, _Material, 10.0f, &identity); PxActor_setActorFlag_mut((physx_PxActor_Pod*)rigActor,2, true); //设置质量和惯性 PxRigidBodyExt_updateMassAndInertia_1((physx_PxRigidBody_Pod*)rigActor, 1.0f, nullptr, false); PxRigidBody_setRigidBodyFlag_mut((physx_PxRigidBody_Pod*)rigActor, 4, true); //设置名字 //PxShape* shape; //rigActor->GetShapes(&shape, 1, 0); //fixed(byte * bytePointer = Encoding.UTF8.GetBytes("niubi")) //{ // shape->SetNameMut(bytePointer); //} //设置线速度 physx_PxVec3_Pod velocity ; velocity.x = 1.0f; velocity.y = 0.0f; velocity.z = 0.0f; PxRigidDynamic_setLinearVelocity_mut((physx_PxRigidDynamic_Pod*)rigActor, &velocity, true); //设置线速度 角速度 的阻力为0 如空气阻力 PxRigidBody_setLinearDamping_mut((physx_PxRigidBody_Pod*)rigActor, 0); PxRigidBody_setAngularDamping_mut((physx_PxRigidBody_Pod*)rigActor, 0); PxScene_addActor_mut(px_scene_,(physx_PxActor_Pod*)rigActor,nullptr); //px_scene_->AddActorMut((PxActor*)rigActor, null); PxScene_unlockWrite_mut(px_scene_); PxScene_lockWrite_mut(px_scene_, nullptr, 0); //创建第二个刚体 physx_PxVec3_Pod startPosition2 ; startPosition2.x = 24; startPosition2.y = 0; startPosition2.z = 0; physx_PxQuat_Pod startOrientation2 ; startOrientation2.x = 0; startOrientation2.y = 0; startOrientation2.z = 0; startOrientation2.w = 1; physx_PxSphereGeometry_Pod sphere2 = PxSphereGeometry_new(1.0f); physx_PxGeometry_Pod* geometry2 = (physx_PxGeometry_Pod*)&sphere2; //PxScene_lockWrite_mut(px_scene_, nullptr, 0); physx_PxRigidActor_Pod* rigActor2 = nullptr; physx_PxTransform_Pod identity2 = PxTransform_new_2(100); physx_PxTransform_Pod pxTran2 ; pxTran2.p = startPosition2; pxTran2.q = startOrientation2; //PxShapeFlags shapeFlags = PxShapeFlag::eVISUALIZATION | PxShapeFlag::eSCENE_QUERY_SHAPE | PxShapeFlag::eSIMULATION_SHAPE //创建物体的默认属性 rigActor2 = (physx_PxRigidActor_Pod*)phys_PxCreateDynamic( _Physics, &pxTran2, geometry2, _Material, 10.0f, &identity2); PxActor_setActorFlag_mut((physx_PxActor_Pod*)rigActor2, 2, true); //设置质量和惯性 PxRigidBodyExt_updateMassAndInertia_1((physx_PxRigidBody_Pod*)rigActor2, 3.0f, nullptr, false); PxRigidBody_setRigidBodyFlag_mut((physx_PxRigidBody_Pod*)rigActor2, 4, true); physx_PxShape_Pod* shape2; //rigActor2->GetShapes(&shape2, 1, 0); PxRigidActor_getShapes((physx_PxRigidActor_Pod*)rigActor2, &shape2,1,0); physx_PxFilterData_Pod filterData2 = PxFilterData_new_1();; filterData2.word0 = 5; filterData2.word1 = 0; filterData2.word2 = 0; filterData2.word3 = 0; //shape2->SetSimulationFilterDataMut(&filterData2); PxShape_setSimulationFilterData_mut(shape2, &filterData2); PxShape_setFlag_mut(shape2, physx::PxShapeFlag::Enum::eSIMULATION_SHAPE,false); PxShape_setFlag_mut(shape2, physx::PxShapeFlag::Enum::eTRIGGER_SHAPE, true); PxShape_setFlag_mut(shape2, physx::PxShapeFlag::Enum::eSCENE_QUERY_SHAPE, true); PxShape_setFlag_mut(shape2, physx::PxShapeFlag::Enum::eVISUALIZATION, true); //shape2->SetFlagMut(physx::PxShapeFlag.SimulationShape, false); //不可以同时设置 //shape2->SetFlagMut(PxShapeFlag.TriggerShape, true); //shape2->SetFlagMut(PxShapeFlag.SceneQueryShape, true); //shape2->SetFlagMut(PxShapeFlag.Visualization, true); physx_PxVec3_Pod velocity2 ; velocity2.x = -1.0f; velocity2.y = 0.0f; velocity2.z = 0.0f; PxRigidDynamic_setLinearVelocity_mut((physx_PxRigidDynamic_Pod*)rigActor2, &velocity2, true); PxRigidBody_setLinearDamping_mut((physx_PxRigidBody_Pod*)rigActor2, 0); PxRigidBody_setAngularDamping_mut((physx_PxRigidBody_Pod*)rigActor2, 0); //px_scene_->AddActorMut((PxActor*)rigActor2, null); PxScene_addActor_mut(px_scene_, (physx_PxActor_Pod*)rigActor2, nullptr); PxRigidDynamic_setContactReportThreshold_mut((physx_PxRigidDynamic_Pod*)rigActor2, 0.1f); PxScene_unlockWrite_mut(px_scene_); physx_PxRaycastHit_Pod rayHit; //rayHit.actor = nullptr; //rayHit.shape = nullptr; //rayHit.u = 0; //rayHit.v = 0; //rayHit.flags = 0x10; physx_PxQueryFilterData_Pod filterData = PxQueryFilterData_new(); //filterData.data.word0 = 5; //filterData.data.word1 = 0; //filterData.data.word2 = 0; //filterData.data.word3 = 0; //必须同时设置 ePREFILTER 和 eDYNAMIC filterData.flags = (1 << 2) | (1<<1); //ePREFILTER | eDYNAMIC = (1<<2), physx_PxVec3_Pod normalizedDir ; normalizedDir.x = 1.0f; normalizedDir.y = 0.0f; normalizedDir.z = 0.0f; physx_PxVec3_Pod tmpNormalizedDir = PxVec3_getNormalized(&normalizedDir); //normalizedDir.GetNormalized(); startPosition.x = 20.0f; //startPosition.z = 0; physx_PxQueryFilterCallback_Pod* fliterCallBack = (physx_PxQueryFilterCallback_Pod*)create_pre_and_post_raycast_filter_callback_func(preFilter,nullptr,nullptr); ////根据原意图推测为 bool hasBlock = PxSceneQueryExt_raycastSingle(px_scene_, &startPosition, &tmpNormalizedDir, 30, 0x20, &rayHit, &filterData, fliterCallBack, nullptr); while (true) { unsigned int error = 0; //px_scene_->SimulateMut(0.05f, null, null, 0, true); PxScene_simulate_mut(px_scene_, 0.05f, nullptr,nullptr, 0,true); PxScene_fetchResults_mut(px_scene_, true, &error); } return 0; }