另一个简单级的范例翻译

     初读hvk的第一个独立运行的例子(StandAloneDemos),确实有些难度,头都看大了,后来在网上偶得一篇教程,把那些个函数内部的运行都给透明化了,枝条砍去,就剩几根大树枝,以此用该例讲解了一下physic模块的建立运行步骤,通过该例入门来窥探havok的部分结构,闲来没事翻译了一部分,另外加入了部分自己的见解,本人翻译粗拙,若有错请E文大大们原谅。原文为http://piotrpluta.opol.pl/?p=106
 Coding – Havok utility class
Some coding at last! If you look on that stand alone demo we launched at at the beginning, you will see that it is written procedurally for simplification. Drawback of that solution is much code in one place (although comments written by demo makers are very good and they help to orientate). I deciced to take adventage of more OO approach. Take a look at declaration of a class which encapsulate Havok data and allow to step the simulation forward.
//

view plaincopy to clipboardprint?
class HavokUtilities  
{   //HavokUtilities类的内部
public:  
 HavokUtilities(void);
 virtual ~HavokUtilities(void);
 //自构造与析构havokutilites
 //sets default timestep and calls private initHavok() method
 //设置默认时间步并调用私有函数inithavok()
 //This way, if HavokUtilities object is destroyed, all resources assigned by it will be freed.
 //同样的,如果havokunilities对象被销毁,那么所有的这些资源也将被释放
 
 void registerVisualDebugger();  
 //注册VisualDebugger,俗称VD或VDB,原来以为在源程序中不用注册VD,循环中也不用逐步推进VD,程序运行时VD就会自动模拟,事实证明,这是痴人说梦
 //sets up Visual Debugger. If you want to use VD, you have to call this after creating HavokUtilities object
 //设置VD,如果你想用VD,你就必须在建立havokutilites物体后注册VD

 void stepSimulation(float dt);  
 //一步一步的进行模拟(物理世界)

 void stepVisualDebugger(float dt);  
 //如果你想不用任何其他图形引擎并查看物理引擎的实时效果,那么VD是个不错的选择,相应的代价就是源程序中一样得注册和与单步模拟一起进行单步VD
 //have to be called inside some loop. They tell the simulation and VD to step forward by delta time dt.
 //这个(模拟与VD)必须调用内部循环,他们将在时间的驱动下向前同时运行

 hkpWorld* getWorld();  
 //returns pointer to hkpWorld (Havok world instance)
 //返回指针给物理世界(havok世界实例)
 hkVisualDebugger* getVisualDebugger();  
 //returns pointer to hkVisualDebugger (VD instance)
 //返回指针给VD(VD实例)

 float getTimestep();  //得到时间流逝
 
private:  
 void initHavok();  
 void deinitHavok();  
 
 bool m_visualDebuggerActive;   //VD是否激活变量
 
 hkpWorld* m_world;   //物理世界 指针初始化
 hkVisualDebugger* m_vdb;   //VBD 指针初始化
 hkpPhysicsContext* m_context;   //context 指针初始化
 
 hkJobThreadPool* m_threadPool;   //线程池 指针初始化
 hkThreadMemory* m_threadMemory;   //线程内存? 指针初始化
 hkJobQueue* m_jobQueue;    //工作队列 指针初始化
 char* m_stackBuffer;   //字符串
}; 
   //In the interface section you can see declaration of physics world, Visual Debugger and context(which processes information from referenced world and presents them to debugger). World is container for simulated objects. We can add simulation elements to it, and during simulation stepping physical interactions of those objects will be resolved.
   //在部分接口里面,你可以看到物理世界的声明,VD和context(就是那些来源于引用世界并把他们发给调试器的处理信息)。世界就是个模拟物体的集装箱,我们可以往里面添加模拟元素,通过一步步的循环运算来实现物理互动
   //Havok memory, thread pool and job queue are declared below. Thread pool and job queue are used in multithread world stepping. If you would like to read more about how multithreading is implemented in Havok, refer to “Multithreading” chapter in User Manual. At the moment keep in mind that after registering physics world with job queue (initHavok() function):
   //下面是物理内存、线程池和工作队列的声明,线程池和工作队列在多线程世界里配合着单步运行着,如果你对在havok里多线程如何实施感兴趣,不妨读读用户指南(在哪里?免费跪求)里的多线程部分。切记要将工作队列注册到物理世界中去(inithavok函数)

   m_world->registerWithJobQueue(m_jobQueue); 

   //we can step the world using current and all the threads in the thread pool (stepSimulation() function):
   //我们可以单步运行世界用当前所有线程池里的线程(stepSimulation()函数,就是那些空着没用的躺在线程池里睡大觉的线程,拉他们去跟工作队列作配对结合)

   m_world->stepMultithreaded(m_jobQueue, m_threadPool, dt); 
   //单步运行多线程


-----------------------------------------------
循环部分:
//Now we have to esablish some kind of game loop in main(). We will duplicate this loop from stand alone Havok demo, but before that we have to create our HavokUtilities object, register VDB:
//现在我们可以很容易的在main()中作游戏循环了,我们将在aloneDemo中不断重复循环,因为我们已经事先建立好了HavokUtilities物体,并注册了VD

int main(int argc, const char** argv)  
{  
    HavokUtilities* havokUtilities = new HavokUtilities();  
    havokUtilities->registerVisualDebugger(); 
    //在main中HavokUtilites被实例化并注册VD

//建立watch(表)物体 - 我们将模拟30秒时间  
hkStopwatch stopWatch;   //秒表实例化
stopWatch.start();       //秒表.开始
hkReal lastTime = stopWatch.getElapsedSeconds();   //得到秒表流逝的时间
 
//initialize fixed time step - one step every 1/60 of a second  
//初始化时间步,每1/60秒运行一步
hkReal timestep = 1.f / 60.f;  

//calculate number of steps for entire loop 
//为整个循环 计算步数
//(30 seconds divided by single time step)  
//在30秒除以单步时间=总步数
int numSteps = int(30.f / timestep);  
 
//application loop, breaks after 15 real time seconds  
//应用循环,在15真实秒后
for ( int i = 0; i < numSteps; ++i )  
{//循环模拟总步数次

    //step the simulation and VDB  
    //单步模拟和VD
    havokUtilities->stepSimulation(timestep);  
    havokUtilities->stepVisualDebugger(timestep);  
    //参数就是某一时间点,得到的结果就是某一时间点模拟出的物理状况及VD

    //pause until the actual time has passed  
    //暂停直到时间流逝完一个单步时间长度
    while (stopWatch.getElapsedSeconds() < lastTime + timestep);  
    {   //只要当前时间<最后模拟时间+单步时间,那么最后模拟时间增加一个单步时间长度
         lastTime += timestep;  
    }  

//We set a watch to simulate 30 seconds of real time - that's our simulation duration. 1/60 time step means that simulation should be stepped 60 times during one second of real time. Time stepping if complicated concept, especially with varying framerate (i.e. in real applications with graphics). For our console demo this fixed step should work well.
//After setting time step, we calculate number of steps needed for entire simulation and run a loop, where simulation and VDB are stepped forward by that fixed time step. To simulate real time pass, we hold loop execution after stepping until the actual time has passed. Don't forget to delete HavokUtilities object after loop, so it can call deinitHavok() and free aquired resources.
//Our demo should confirm connection to Visual Debugger by writing to the console, although scene in VBD will be still empty - we haven't added any objects to the simulation.
//我们设置了一个watch(秒表)来模拟30秒真实时间-那是我们模拟的持续时间。1/60的时间步长意思就是每秒60次来模拟过程,这即使是对一些复杂的概念,尤其是不同的帧率,我们在这个Demo中控制也应该能够被演示得很好了。

------------------------------------------------
5. 建立固定面//creating fixed surface
//Last thing I promised at the beginning of this tutorial - we will create a flat, fixed(static game scene element) surface to preview in VDB. It's not much, but it shows the basics of Havok shapes creation. Write function definition in main.cpp, before main() function:
//在这个tutorial开始时我承诺的最后一件事-我们将建立一块又平又结实的表面来在VDB中进行渲染,那并不太多,但是也足以演示了在havok中一个基本形状的建立方法。


void addFixedSurface(hkpWorld* world, const hkVector4& position,  
                             const hkVector4& dimensions)  
{  
    //在指定位置建立一个指定大小的固体表面
    //creates fixed surface with specified position and dimensions  
   
    //create box shape using given dimensions  
    //用指定大小建立出一个盒子形状
    hkpConvexShape* shape = new hkpBoxShape(dimensions,0);  
   
    //create rigid body information structure
    //建立一个刚体信息结构 
    hkpRigidBodyCinfo rigidBodyInfo;  
 
    //MOTION_FIXED means static element in game scene  
    //MOTION_FIXED 的意思就是在游戏场景中是静态不动的,位置不受任何力影响的
    rigidBodyInfo.m_motionType = hkpMotion::MOTION_FIXED;  
    rigidBodyInfo.m_shape = shape;  
    rigidBodyInfo.m_position = position;  
    //刚体信息的定义为:类型=静态不动类型,形状=先前已经指定的形状,位置=一个三维点
 
    //create new rigid body with supplied info  
    //用支持信息(即刚体信息结构)建立出一个刚体
    hkpRigidBody* rigidBody = new hkpRigidBody(rigidBodyInfo);  
 
    //add rigid body to physics world  
    //把刚体加到物理世界中去
    world->lock();  
    world->addEntity(rigidBody);  
 
    //decerase reference counter for rigid body and shape  
    //从参考计数器中为刚体和形状作减一操作
    rigidBody->removeReference();  
    shape->removeReference();  
 
    world->unlock();  

//Funtion first creates a hkBoxShape, which defines the object's shape and size for collision detection purposes. hkBoxShape constructor takes half extents as a parameter - X by Y by Z box has a X/2 by Y/2 by Z/2 half extents. This shape, among with position and motion type is used to fill rigid body info structure. Any scene object that doesn't change it's size could be rigid body - boxes, surfaces, car chassis etc. MOTION_FIXED motion type means that object will be static in our physics world. It is necessary - fixed motion type will cause it to fall, according to gravitation set during initialization.
//The function has one more thing to do: create a rigid body with rigidBodyInfo passed as a parameter and add it to physics world. Notice the removeReference() calls on rigidBody and shape objects. Havok manages those objects by using a reference system. Therefore physics world (hkpWorld) will keep reference to rigid body once added. If we don't need it anymore here, we could decerase reference counter, giving hkpWorld object full ownership over the object.
//We have to call addFixedSurface() in our main() function, after we create HavokUtilities object:
//函数首先建立出了一个hkboxshape,用于定义支持碰撞的物体形状和大小。hkboxshape作为参数的一半

HavokUtilities* havokUtilities = new HavokUtilities();  
havokUtilities->registerVisualDebugger();  
addFixedSurface(havokUtilities->getWorld(), hkVector4(0,0,0),  
                hkVector4(50.0f,1.0f,50.0f)); 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值