图形赋予游戏一种视觉的吸引力,但是能够让游戏的世界鲜活起来的还应该是内部的物理引擎。物理引擎是游戏引擎中的子模块,是一种软件组件,可仿真物理系统。它根据牛顿力学定律,计算游戏中物体的合理的物理位置,并将计算结果提供给渲染引擎,从而展示出真实的渲染效果。物理引擎的仿真包括柔性体和刚体力学、流体力学以及碰撞检测。以游戏为中心的物理引擎侧重于实时近似,而科学仿真中的物理引擎则更多地侧重于精确计算以获得高准确性。科学物理引擎依赖于超级计算机的处理能力,而游戏物理引擎则可运行于资源受限的平台(比如手持型游戏设备和移动手机)。
图 1. 游戏应用中的物理引擎
Bullet Physics SDK: real-time collision detection and multi-physics simulation for VR, games, visual effects, robotics, machine learning etc.
- Build bullet
下载Bullet的源代码 https://github.com/bulletphysics/bullet3/releases/latest,然后将其解压到合适的路径下。可以选择运行批处理文件生成VisualStudio工程,这里运行build_visual_studio.bat生成VS2010工程。
在Debug模式下生成解决方案,解压后的bullet3-2.86.1目录中会出现bin文件夹,其中包含了生成的静态库文件和可执行文件。
- HelloWorld example
新建一个空的控制台程序,然后在源文件目录中加入HelloWorld.cpp文件:
#include <btBulletDynamicsCommon.h>
#include <stdio.h>
#include <iostream>
#include <fstream>
using namespace std; /// This is a Hello World program for running a basic Bullet physics simulation int main(int argc, char** argv)
{ btBroadphaseInterface* broadphase = new btDbvtBroadphase(); ///collision configuration contains default setup for memory, collision setup. Advanced users can create their own configuration.
btDefaultCollisionConfiguration* collisionConfiguration = new btDefaultCollisionConfiguration(); ///use the default collision dispatcher. For parallel processing you can use a differnt dispatcher
btCollisionDispatcher* dispatcher = new btCollisionDispatcher(collisionConfiguration); ///the default constraint solver. For parallel processing you can use a different solver
btSequentialImpulseConstraintSolver* solver = new btSequentialImpulseConstraintSolver; ///instantiate the dynamics world
btDiscreteDynamicsWorld* dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher, broadphase, solver, collisionConfiguration); ///sets the gravity. We have chosen the Y axis to be "up".
dynamicsWorld->setGravity(btVector3(,-,)); btCollisionShape* groundShape = new btStaticPlaneShape(btVector3(, , ), );
btCollisionShape* fallShape = new btSphereShape(); //The btDefaultMotionState provides a common implementation to synchronize world transforms with offsets.
btDefaultMotionState* groundMotionState = new btDefaultMotionState(btTransform(btQuaternion(, , , ), btVector3(, -, ))); ///instantiate the ground. Its orientation is the identity, Bullet quaternions are specified in x,y,z,w form.
btRigidBody::btRigidBodyConstructionInfo groundRigidBodyCI(, groundMotionState, groundShape, btVector3(, , )); ///Bullet considers passing a mass of zero equivalent to making a body with infinite mass - it is immovable
btRigidBody* groundRigidBody = new btRigidBody(groundRigidBodyCI); ///add the ground to the world
dynamicsWorld->addRigidBody(groundRigidBody); //The btTransform class supports rigid transforms with only translation and rotation
btDefaultMotionState* fallMotionState = new btDefaultMotionState(btTransform(btQuaternion(, , , ), btVector3(, , ))); btScalar mass = ;
btVector3 fallInertia(, , );
fallShape->calculateLocalInertia(mass, fallInertia); ///when bodies are constructed, they are passed certain parameters. This is done through a special structure Bullet provides for this.
///rigid body is dynamic if and only if mass is non zero, otherwise static
btRigidBody::btRigidBodyConstructionInfo fallRigidBodyCI(mass, fallMotionState, fallShape, fallInertia);
btRigidBody* fallRigidBody = new btRigidBody(fallRigidBodyCI);
dynamicsWorld->addRigidBody(fallRigidBody); ofstream outfile("C:\\Users\\KC\\Desktop\\height.csv", ios::out);
for (int i = ; i < ; i++) { /* prototype:
btDynamicsWorld::stepSimulation(btScalar timeStep, int maxSubSteps=1,
btScalar fixedTimeStep=btScalar(1.)/btScalar(60.));
*/ //It's important that timeStep is always less than maxSubSteps*fixedTimeStep
//The first and third parameters to stepSimulation are measured in seconds
//By decreasing the size of fixedTimeStep, you are increasing the "resolution" of the simulation.
//When you pass Bullet maxSubSteps > 1, it will interpolate movement for you
dynamicsWorld->stepSimulation( / .f, ); btTransform trans;
fallRigidBody->getMotionState()->getWorldTransform(trans); std::cout << "sphere height: " << trans.getOrigin().getY() << std::endl;
outfile<<trans.getOrigin().getY()<<endl;
} outfile.close(); delete fallShape;
delete groundShape; delete dynamicsWorld;
delete solver;
delete dispatcher;
delete collisionConfiguration;
delete broadphase; printf("Press a key to exit\n");
getchar();
}
在项目属性——>C/C++ ——>常规——>附加包含目录中添加头文件的路径:D:\bullet3-2.86.1\src
在C/C++ ——>常规——>代码生成——>运行库选项中选择:多线程调试(/MTd)。Make sure the run-time library is the same as in your project. By default it is set to the multi-threaded, static version. 即之前在build bullet时默认生成的是静态库,则在编译debug程序时应选择/MTd。一个程序中若混合了不同的运行时库(静态库和动态库,调试库和非调试库),可能会产生冲突,所以一个程序中应该使用相同的运行时库。
在链接器——>常规——>附加库目录中输入静态库路径:D:\bullet3-2.86.1\bin
然后在链接器——>输入——>附加依赖项中添加必须的静态库(release版的静态库文件名中没有_debug后缀)
设置完成开始运行。上面的代码将创建一个半径为1的球体和一个静态地面,球中心距地面高度为50m,球的质量为1Kg。然后模拟重力(重力加速度沿Y轴负方向,大小为10m/s2)作用下的*落体运动,仿真步长为1/60s,即每秒计算60次,运行300步(5s)。虽然仿真需要大量的设置,但是当定义了仿真环境之后,物理引擎就会在幕后为你完成全部的繁重工作。仿真时每运行一步输出球中心的高度数据:
将数据记录在CSV文件中,画出高度随时间变化的曲线如下图所示:
- Build and install pybullet
pybullet is an easy to use Python module for physics simulation, robotics and machine learning based on the Bullet Physics SDK. With pybullet you can load articulated bodies from URDF, SDF and other file formats. pybullet provides forward dynamics simulation, inverse dynamics computation, forward and inverse kinematics and collision detection and ray intersection queries. Aside from physics simulation, pybullet supports to rendering, with a CPU renderer and OpenGL visualization and support for virtual reality headsets. 安装pybullet有好几种方式,下面在https://pypi.python.org/pypi/pybullet网站上下载源文件pybullet-0.1.6.tar.gz并解压,切换到原文件目录中运行:python setup.py install。不过这时提示:Microsoft Visual C++ 9.0 is required Unable to find vcvarsall.bat,解决办法是可以安装一个Micorsoft Visual C++ Compiler for Python 2.7的包。经过一段时间编译(中途会弹出好多警告),在源文件目录的build\lib.win-amd64-2.7文件夹下可以找到pybullet.pyd,将其复制到C:\Python27\Lib\site-packages中。
下面的Python代码运行时作为客户端与Physics Engine服务端进行通信(pybullet is designed around a command-status driven API, with a client sending commands and a physics server returning the status. pybullet has some build-in physics servers: DIRECT and GUI. The DIRECT connection sends the commands directly to the physics engine, without using any transport layer, and directly returns the status after executing the command. The GUI connection will create a new graphical user interface with OpenGL rendering, within the same process space as pybullet)
import pybullet as p
from time import sleep physicsClient = p.connect(p.GUI)
p.setGravity(0,0,-10) # The loadURDF will send a command to the physics server to load a physics model from a URDF File
planeId = p.loadURDF("D:/bullet3-2.86.1/data/plane.urdf") cubeStartPos = [0,0,2]
cubeStartOrientation = p.getQuaternionFromEuler([0,0,0]) # The pybullet API uses quaternions to represent orientations.
boxId = p.loadURDF("D:/bullet3-2.86.1/data/sphere2.urdf",cubeStartPos, cubeStartOrientation) while 1:
# stepSimulation will perform all the actions in a single forward dynamics simulation step such as collision detection,
# constraint solving and integration. By default, the physics server will not step the simulation, unless you explicitly
# send a 'stepSimulation' command. This way you can maintain control determinism of the simulation.
p.stepSimulation() # reports the current position and orientation of the base of the body in Cartesian world coordinates
# returns the position list of 3 floats and orientation as list of 4 floats in [x,y,z,w] order.
# Use getEulerFromQuaternion to convert the quaternion to Euler if needed.
cubePos, cubeOrn = p.getBasePositionAndOrientation(boxId)
print(cubePos,cubeOrn)
sleep(0.01) # Time in seconds. p.disconnect() # disconnect from a physics server. A 'DIRECT' or 'GUI' physics server will shutdown
运行时服务端将显示对应的图形界面,仿真每进行一步输出球的质心位置和姿态
参考:
Creating a project from scratch