物理引擎ODE

来源:互联网 发布:人工智能计算器在线 编辑:程序博客网 时间:2024/05/21 00:55

出处:http://blog.csdn.net/liyanguestc/article/details/1710924

Russell Smith 的 Open Dynamics Engine(ODE)是一个开源的物理引擎,使用它可以仿真铰接式固形体运动。这样不管我们使用哪种图形库(可能使用 OpenGL),都可以对真实对象的物理特性进行仿真。我们可以使用 ODE 来对合成环境中的各种对象进行建模,例如三维游戏环境中的人物或虚拟驾驶中的交通工具。除了速度快之外, ODE 还可以支持实时仿真的碰撞检测。

ODE 目前可以支持球窝、铰链、滑块、定轴、角电机和 hinge-2 (用于交通工具的连接)连接类型,以及其他一些连接类型。它还可以支持各种碰撞原语(例如球面碰撞和平面碰撞)和几个碰撞空间。

ODE 主要是使用 C++ 语言编写的,但是它也提供了 C 和 C++ 的清晰接口来帮助我们与应用程序进行集成。有关 ODE 更好的一点是它发行所采用的许可证:LGPL(GNU Lesser General Public License)和 BSD License。在这两个许可证中,我们都可以在商业产品中使用 ODE 的源代码,而不需任何费用。其结果是我们在很多商业游戏、飞行模拟器和虚拟现实仿真中都可以找到 ODE。

清单 1 中的源代码给出了一个简单的例子,这个环境中中存在火星的地心引力,里面有一个球具有向上的速度。由于这个环境中具有地心引力,因此向上的速度不会坚持很久;最后这个球会上升到顶点并开始下降。在初始化完成之后(即在这个环境中创建对象并设置好属性之后),我们就可以通过 dWorldStep 对这个世界中的物理特性进行仿真了。要理解发生的现象,可以经常调用 dBodyGetPosition 并给它传入球的标识符来获得它的当前位置。

清单 1. 简单的 ODE 实验:地心引力环境中的球
#include <iostream>
#include <ode/ode.h>

#define time_step (float)0.1

int main()
{
   dWorldID myWorld_id;
   dBodyID mySphere_id;
   dMass sphereMass;
   const dReal *pos;
   float time = 0.0;

   /* Create a new world */
   myWorld_id = dWorldCreate();

   /* Create a sphere in the world */
   mySphere_id   = dBodyCreate( myWorld_id );

   /* Set the world's global gravity vector (Mars) -- x,y,z */
  dWorldSetGravity( myWorld_id, 0, 0, -3.77 );

   /* Set the Sphere's position in the world -- x,y,z */
  dBodySetPosition( mySphere_id, 0, 0, 100 );

   /* Set the Sphere's mass (density, radius) */
  dMassSetSphere( &sphereMass, 1, 2 );
  dBodySetMass( mySphere_id, &sphereMass );

   /* Give the sphere a small amount of upward (z) velocity */
  dBodySetLinearVel( mySphere_id, 0.0, 0.0, 5.0 );

   /* Run the simulation */
   while (time < 5.0) {

     /* Simulate the world for the defined time-step */
    dWorldStep( myWorld_id, time_step );

     /* Get the current position of the sphere */
     pos = dBodyGetPosition( mySphere_id );

     std::cout << "position (" << pos[0] << ", "
              << pos[1] << ", " << pos[2] << ")/n";

     /* Next time step */
     time += time_step;

   }

   /* Destroy the objects */
  dBodyDestroy( mySphere_id );
  dWorldDestroy( myWorld_id );

   return 0;
}


0 0
原创粉丝点击