Havok测试
前段时间挖掘了一下以前HalfLife2的泄漏代码,发现了尽然有Havok的全部代码,真是太开心了,可知道Havok是业界著名的软件模拟物理引擎,被普遍用在很多游戏中以提高游戏真实感,而HalfLife2就使用了Havok技术,而其泄漏代码也包含了这些Havok技术。为此特地花了一个下午研究了一下Havok,发现它同我以前使用的PhysX物理引擎差不多,至少概念和使用方法基本类似,所以特别写了一个Demo测试下Havok的性能,Demo很简单就是模拟200个盒子从50米的高度下落的自由落体运动,代码写起来也很简单,核心代码不过以下部分:
IVP_Environment_Manager *env_mgr;
env_mgr=IVP_Environment_Manager::get_environment_manager();
IVP_Application_Environment app_env;
g_HKEnv=env_mgr->create_environment(&app_env,0,0);
if(!g_HKEnv)
return S_FALSE;
//设置环境的重力为垂直向下9.81
IVP_U_Point g(0.0f,-9.81f,0.0f);
g_HKEnv->set_gravity(&g); IVP_U_Point boxVert;
IVP_U_Vector<IVP_U_Point> points;
for (int offset=0,z=-1; z<=1; z+=2) {
for (int y=-1; y<=1; y+=2) {
for (int x=-1; x<=1; x+=2) {
boxVert.set(2*x, 2*y, 2*z);
points.add(&boxVert);
}
}
} IVP_Compact_Surface *boxSurface =
IVP_SurfaceBuilder_Pointsoup::convert_pointsoup_to_compact_surface(&points); IVP_SurfaceManager *boxSM =
new IVP_SurfaceManager_Polygon(boxSurface);
for(int n=0;n<totalBallCount;n++)
{
IVP_Template_Real_Object template_real_object;
template_real_object.mass = 1.0; // the object's mass (unit: kg)
template_real_object.material = new IVP_Material_Simple(0.7, 0.6); IVP_U_Quat direction;
direction.init(); // default orientation
IVP_U_Point position(0.0+sinf(n)*30, 50.0+n*5, 30.0); g_box = g_HKEnv->create_polygon(boxSM,
&template_real_object, &direction, &position); //box需要碰撞检查
g_box->enable_collision_detection();
//box需要参与模拟
g_box->ensure_in_simulation(); // immediately revive object
} IVP_U_Point floorVert;
points.clear();
for (int offset=0,z=-1; z<=1; z+=2) {
for (int y=-1; y<=1; y+=2) {
for (int x=-1; x<=1; x+=2) {
floorVert.set(50*x, 0.5*y-15, 50*z);
points.add(&floorVert);
}
}
} IVP_Compact_Surface *compact_surface =
IVP_SurfaceBuilder_Pointsoup::convert_pointsoup_to_compact_surface(&points); IVP_SurfaceManager *surface_manager =
new IVP_SurfaceManager_Polygon(compact_surface); IVP_Template_Real_Object template_real_object;
template_real_object.mass = 1.0;
template_real_object.material = new IVP_Material_Simple(0.7, 0.6);
// friction, elasticity
template_real_object.set_name("cube"); // optional name
template_real_object.physical_unmoveable = IVP_TRUE;
// object is unmoveable IVP_U_Quat direction;
direction.init(); // default orientation
IVP_U_Point position(0.0, 0.0, 50.0); // the sphere's position // within the physical g_floor = g_HKEnv->create_polygon(surface_manager,
&template_real_object, &direction, &position); g_floor->enable_collision_detection();
g_floor->ensure_in_simulation(); // immediately revive object这样就可以了,剩下的事情havok会自动计算以后所有盒子的状态、位置等,下面是这个demo的截图,可以看到所有的盒子都动起来了: http://www.sineysoft.com/blog/upload/WindowsLiveWriterHavok_14073ht_thumb%5B3%5D.jpg
页:
[1]