前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >哇哈哈, 终于天上掉箱子了

哇哈哈, 终于天上掉箱子了

作者头像
逍遥剑客
发布2018-05-23 15:44:29
3770
发布2018-05-23 15:44:29
举报
基本步骤:
  1. 初始化SDK
  2. 创建场景, 设置重力
  3. 设置默认材质, 摩擦力系数之类的
  4. 创建一个地面
  5. 每次按Enter创建一个Box, 让它自己往下掉

主要的代码如下:

代码语言:javascript
复制
//------------------------------------------------------------------------------ 
void 
TestPhysXApp::OnProcessInput()  
{  
 // TODO: add your input operation code here 
 if (this->inputServer->GetDefaultKeyboard()->KeyDown(Input::Key::Return))  
    {  
        NxBodyDesc bodyDesc;  
        bodyDesc.angularDamping = 0.5f;  
        NxBoxShapeDesc boxDesc;  
        boxDesc.dimensions = NxVec3(0.5f, 0.5f, 0.5f);  
        NxActorDesc actorDesc;  
        actorDesc.shapes.pushBack(&boxDesc);  
        actorDesc.body = &bodyDesc;  
        actorDesc.density = 10.0f;  
        actorDesc.globalPose.t = NxVec3(0.0f, 10.0f, 0.0f);  
 this->colors.Append(this->GetRandomColor());  
 this->nxScene->createActor(actorDesc)->userData = (void*)(this->colors.Size() - 1);  
    }  
    ViewerApplication::OnProcessInput();  
}  
//------------------------------------------------------------------------------ 
void 
TestPhysXApp::OnUpdateFrame()  
{  
 // simulation 
 float frameTime = (float)this->GetFrameTime();  
 this->nxScene->simulate(frameTime);  
 float curTime = (float) this->GetTime();  
 // draw ground 
 const float period = 3.0f;  
 float now = float(curTime - int(curTime / period) * period - period * 0.5f);  
    scalar s = n_abs(now / (period * 0.5f));  
    float4 color = float4::lerp(float4(1, 0, 0, 1), float4(0, 1, 0, 1), s);  
    matrix44 transform = matrix44::identity();  
    transform.translate(float4(0, -1, 0, 0));  
    transform.scale(float4(100, 1, 100, 1));  
    DebugShapeRenderer::Instance()->DrawBox(transform, color);  
 // draw boxes 
    NxActor** actors = this->nxScene->getActors();  
    SizeT numActors = this->nxScene->getNbActors();  
 for (IndexT i = 1; i < numActors; ++i)  
    {  
        NxActor* actor = actors[i];  
        actor->getGlobalPose().getColumnMajor44((float*)&transform);  
        IndexT colorIndex = (IndexT)actor->userData;  
        DebugShapeRenderer::Instance()->DrawBox(transform, this->colors[colorIndex]);  
    }  
    ViewerApplication::OnUpdateFrame();  
 // fetch simulation results 
 this->nxScene->flushStream();  
 this->nxScene->fetchResults(NX_RIGID_BODY_FINISHED, true);  
}  
//------------------------------------------------------------------------------ 
void 
TestPhysXApp::InitNx()  
{  
 // intialize sdk 
    NxPhysicsSDKDesc desc;  
 this->nxSDK = NxCreatePhysicsSDK(NX_PHYSICS_SDK_VERSION, NULL, NULL, desc);  
 this->nxSDK->setParameter(NX_SKIN_WIDTH, 0.05f);  
    n_assert(NULL != this->nxSDK);  
 // create scene 
    NxSceneDesc sceneDesc;  
    sceneDesc.gravity = NxVec3(0.0f, -9.8f, 0.0f);  
 this->nxScene = this->nxSDK->createScene(sceneDesc);  
    n_assert(NULL != this->nxScene);  
 // set default material 
    NxMaterial* defaultMaterial = this->nxScene->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);  
 this->nxScene->createActor(actorDesc);  
}  
//------------------------------------------------------------------------------ 
float4  
TestPhysXApp::GetRandomColor() const 
{  
 float r = n_rand(0.0f, 1.0f);  
 float g = n_rand(0.0f, 1.0f);  
 float b = n_rand(0.0f, 1.0f);  
 return float4(r, g, b, 1.0f);  
} 
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2009年08月25日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档