Physx范围伤害检测

44 篇文章 1 订阅
16 篇文章 2 订阅

区域判断使用Overlap来实现。 Overlap有两种模式

1.列出所有范围内的Shape

2.判断范围内使用有Shape


#include <stdio.h>

#include "NxPhysics.h"
#include "ErrorStream.h"
#include "Utilities.h"
#include "SamplesVRDSettings.h"
#include "FileLog.h"

#define ROOM_NUM 1
#define SAMPLES_USE_VRD 1

NxSDKCreateError g_ErrorCode = NXCE_NO_ERROR;

NxPhysicsSDK* InitPhysxSDK()
{
	// Initialize PhysicsSDK
	NxPhysicsSDKDesc desc;
	
	NxPhysicsSDK* pPhysicsSDK = NxCreatePhysicsSDK(NX_PHYSICS_SDK_VERSION, NULL, new ErrorStream(), desc, &g_ErrorCode);
	if(pPhysicsSDK == NULL) 
	{
		return NULL;
	}

#if SAMPLES_USE_VRD
	// The settings for the VRD host and port are found in SampleCommonCode/SamplesVRDSettings.h
	if (pPhysicsSDK->getFoundationSDK().getRemoteDebugger())
	{
		pPhysicsSDK->getFoundationSDK().getRemoteDebugger()->connect(SAMPLES_VRD_HOST, SAMPLES_VRD_PORT, SAMPLES_VRD_EVENTMASK);
	}
#endif

	pPhysicsSDK->setParameter(NX_SKIN_WIDTH, 0.05f);
	return pPhysicsSDK;
}

void UninitPhysxSDK(NxPhysicsSDK* pPhysicsSDK)
{
	if(pPhysicsSDK != NULL)
	{
		NxReleasePhysicsSDK(pPhysicsSDK);
	}
}

NxScene* CreateScene(NxPhysicsSDK* pSDK)
{
	// Create a scene
	NxScene* pScene = NULL;
	NxSceneDesc sceneDesc;
	sceneDesc.flags &= ~NX_SF_SIMULATE_SEPARATE_THREAD;
	sceneDesc.gravity				= NxVec3(0.0f, -9.81f, 0.0f);
	pScene = pSDK->createScene(sceneDesc);
	if(pScene == NULL) 
	{
		return pScene;
	}

	// Set default material
	NxMaterial* defaultMaterial = pScene->getMaterialFromIndex(0);
	defaultMaterial->setRestitution(0.0f);
	defaultMaterial->setStaticFriction(0.5f);
	defaultMaterial->setDynamicFriction(0.5f);

	 Create ground plane
	//NxPlaneShapeDesc planeDesc;
	//NxActorDesc actorDesc;
	//actorDesc.shapes.pushBack(&planeDesc);
	//pScene->createActor(actorDesc);

	return pScene;
}

NxActor* CreateCube(NxScene* pSence, const NxVec3& pos, int size=2, const NxVec3* initialVelocity=NULL)
{
	// Create body
	NxBodyDesc bodyDesc;
	bodyDesc.angularDamping	= 0.5f;
	if(initialVelocity) bodyDesc.linearVelocity = *initialVelocity;

	NxBoxShapeDesc boxDesc;
	boxDesc.dimensions = NxVec3((float)size, (float)size, (float)size);

	NxActorDesc actorDesc;
	actorDesc.shapes.pushBack(&boxDesc);
	actorDesc.body			= &bodyDesc;
	actorDesc.density		= 10.0f;
	actorDesc.globalPose.t  = pos;
	return pSence->createActor(actorDesc);
}

NxActor* CreateRoom(NxScene* pSence, const NxVec3& pos, int size=2, const NxVec3* initialVelocity=NULL)
{
	// Create body
	NxBodyDesc bodyDesc;
	bodyDesc.angularDamping	= 0.5f;
	if(initialVelocity) bodyDesc.linearVelocity = *initialVelocity;

	NxActorDesc actorDesc;
	actorDesc.body			= &bodyDesc;
	actorDesc.density		= 10.0f;
	actorDesc.globalPose.t  = pos;

	// 中间box
	NxBoxShapeDesc boxDescMid;
	boxDescMid.dimensions = NxVec3((float)size, (float)size, (float)size);
	boxDescMid.localPose.t = NxVec3(0.0, 0.0, 0.0);
	boxDescMid.userData = (void*)1;

	// 底面box
	NxBoxShapeDesc boxDescUp;
	boxDescUp.dimensions = NxVec3((float)size*3, (float)size*3, (float)size);
	boxDescUp.localPose.t = NxVec3(0.0, 0.0, (float)size*2*2);
	boxDescUp.userData = (void*)2;
	NxBoxShapeDesc boxDescDown;
	boxDescDown.dimensions = NxVec3((float)size*3, (float)size*3, (float)size);
	boxDescDown.localPose.t = NxVec3(0.0, 0.0, (float)-size*2*2);
	boxDescDown.userData = (void*)3;

	// 前后面box
	NxBoxShapeDesc boxDescForward;
	boxDescForward.dimensions = NxVec3((float)size, (float)size*3, (float)size);
	boxDescForward.localPose.t = NxVec3((float)size*2, 0.0, 0.0);
	boxDescForward.userData = (void*)4;
	NxBoxShapeDesc boxDescBack;
	boxDescBack.dimensions = NxVec3((float)size, (float)size*3, (float)size);
	boxDescBack.localPose.t = NxVec3((float)-size*2, 0.0, 0.0);
	boxDescBack.userData = (void*)5;

	// 左右面
	NxBoxShapeDesc boxDescLeft;
	boxDescLeft.dimensions = NxVec3((float)size, (float)size, (float)size);
	boxDescLeft.localPose.t = NxVec3(0.0, (float)size*2, 0.0);
	boxDescLeft.userData = (void*)6;
	NxBoxShapeDesc boxDescRight;
	boxDescRight.dimensions = NxVec3((float)size, (float)size, (float)size);
	boxDescRight.localPose.t = NxVec3(0.0, (float)-size*2, 0.0);
	boxDescRight.userData = (void*)7;

	actorDesc.shapes.pushBack(&boxDescMid);
	actorDesc.shapes.pushBack(&boxDescUp);
	actorDesc.shapes.pushBack(&boxDescDown);
	actorDesc.shapes.pushBack(&boxDescForward);
	actorDesc.shapes.pushBack(&boxDescBack);
	actorDesc.shapes.pushBack(&boxDescLeft);
	actorDesc.shapes.pushBack(&boxDescRight);
	return pSence->createActor(actorDesc);
}

void main()
{
	NxPhysicsSDK* pPhysxSDK = InitPhysxSDK();
	if(!pPhysxSDK)
	{
		printf("\nSDK create error (%d - %s).\nUnable to initialize the PhysX SDK, exiting the sample.\n\n", g_ErrorCode, getNxSDKCreateError(g_ErrorCode));
		return;
	}

	NxScene* pScene = CreateScene(pPhysxSDK);
	if(!pScene)
	{
		printf("\nError: Unable to create a PhysX scene, exiting the sample.\n\n");
		UninitPhysxSDK(pPhysxSDK);
		return;
	}

	NxActor* arActor[ROOM_NUM];
	NxShape* arShape[1024] = {0};
	arActor[0] = CreateRoom(pScene, NxVec3(0.0, 0.0, 0.0), 4);
	arActor[0]->raiseBodyFlag(NX_BF_DISABLE_GRAVITY);

	NxVec3 pos = arActor[0]->getGlobalPosition();
	while (true)
	{
		pScene->simulate(0.16);
		pScene->fetchResults(NX_ALL_FINISHED, true);

		NxU32 iShapeNum = pScene->getNbDynamicShapes();
		memset(arShape, 0, sizeof(arShape));
		NxSphere testSphere(NxVec3(0.0, 0.0, 0.0), 5);
		pScene->overlapSphereShapes(testSphere, NX_DYNAMIC_SHAPES, 1024 < iShapeNum ? 1024 : iShapeNum, arShape, NULL, 1);

		memset(arShape, 0, sizeof(arShape));
		NxSphere testSphere1(NxVec3(4.0, 0.0, 0.0), 3);
		pScene->overlapSphereShapes(testSphere1, NX_DYNAMIC_SHAPES, 1024 < iShapeNum ? 1024 : iShapeNum, arShape, NULL);

		memset(arShape, 0, sizeof(arShape));
		NxSphere testSphere2(NxVec3(0.0, 0.0, 40.0), 80);
		pScene->overlapSphereShapes(testSphere2, NX_DYNAMIC_SHAPES, 1024 < iShapeNum ? 1024 : iShapeNum, arShape, NULL);
	}

	UninitPhysxSDK(pPhysxSDK);

}
virtual NxU32overlapSphereShapes (constNxSphere &worldSphere, NxShapesType shapeType,NxU32 nbShapes, NxShape **shapes,NxUserEntityReport< NxShape * > *callback,NxU32 activeGroups=0xffffffff, const NxGroupsMask *groupsMask=NULL, bool accurateCollision=false)=0
 Returns the set of shapes overlapped by the world-space sphere. 

activeGroups是对0-31个分组使用bit flag来判定对哪些分组做范围判断


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值