Havok physics engine 分析

来源:互联网 发布:淘宝店铺直播申请步骤 编辑:程序博客网 时间:2024/04/30 01:57
作者: 刘鹏
日期: 2009-02-22
Havok 是一个被广泛使用的游戏引擎,已经被用在 200 多个游戏中。2008 年 intel 将其收购后开源。本文是作者在阅读 Havok 的物理引擎编程指南时写下的阅读笔记,较详细地介绍了 Havok 的物理引擎。

physical Simulation

introduction
  • Scientist and programmer use computer to simulate complex situations such as rocket trajectories(ballistic motion), liquid flows(fluid dynamics) and so on.
  • Many of these simulation are very expensive in terms of the CPU rescources requied.
  • They are concerned with high levels of accuracy and as a result, calculation need to be performed with a very high level of detail.
  • We now concern a different problem - game physics; fast, believable simulation executed in a very limited number of CPU cycles.
Rigid body dynamics

Rigid body dynamics is the most common form of physical simulation. For performace reasons, a number of assumptions are usually made about the environment.

  1. The shape of each object is fixed. Each object is infinitely hard/rigid.

    In a real life, a car panel colliding with a wall will blend depending on the strength of the impact. In a rigid body simulation, the car panel will remain completely intact. However game objects still needed to show some damage, Havok use signaling events corresponding to the collision between the car and the wall. Information about the impact can be queried (e.g. the position and the force of the impact) and fed back into the game engine. This could be used to dent the graphical representation of the car panel or even replace the physical shape of the panel to a pre-dented one if required.

  2. The landscape does not change dynamically.

    Simulating a completely dynamic environment (e.g. blowing holes in the ground with a grenade) can be very computationally expensive. In most games the base level of environment remains fixed and objects within the landscape (such as traffic cones, pedestrians, bins etc.) can be modeled as separate physical objects rather than as part of the landscape. Sections of the game environment that are required to be dynamic (such as water, collapsibel walls, destructible land etc.) can be layered on top of the base Havok physics.

  3. The shape of objects can be simplified for physical simulation.

    A replacement object can be used within the physical simulation to speed up the various calculations. While graphically a bus in a game may be a complex combination of rubber, glass, chrome and tin, to the physics engine it may just be a moving box. Havok has a simple set of primitive shapes like bounding boxes and spheres, and allows easy creatioon of custom shapes. Such a shape, can be used as replacement for a highly detailed in-game polygon mesh. Havok also contains functions to automatically extract simplified proxy meshes from arbitrary in-game geometry.

  4. Objects in a scene are not all active at the same time.

    Simulating something at rest is very efficient (Zero CPU time). Within a typical game scene, there may be many potentially active environmental objects such as chairs, crates, barrels etc. Most of them remain at rest for most of the time. Havok automatically handles detecting when objects are at rest and remove them from the active simulation process. It also automatically detects when they become active again and adds them back into the simulation. Complete control is given to the developer to allow them to override the default behavior and activate and deactivate physical objects.

What does a Physics Engine do?

  • Cares little about how the objects are displayed graphically.
  • Simulates the motion and interaction of objects based on a physical description.
The structure of a physics simulation system (From Havok User Guider)The structure of a physics simulation system (From Havok User Guider)
  • Has three basic tasks to perform. Having setup the initial conditions for a given scene; begin the main simulation loop which steps through some task; updates the graphics and then repeats.
    1. Determine which objects are overlapping or about to overlap. The stage is divided into three sections: broadphase, midphase and narrowphase.
    2. Solve all forces acting on the objects.
    3. Having gathered up all the forces, the simulation is advanced by the time step size and the new state of the objects(position, orientation, velocity, acceleration etc) is calculated for this time in the future.The info is then used to update the corresponding display representations of the object.

Simulating a cannon ball

Physical description of a cannon ball: postion, speed, accelerration, weight, state of environment (air resistance, wind force, gravity).

The expected ballistic motion of a cannon ball is an arc.

Expected ballistic motion of a cannon ball(From Havok User Guider)Expected ballistic motion of a cannon ball(From Havok User Guider)

Over a period of time the cannon ball's rate of ascent should slow due to gravity, and it will eventually fall to the ground having traversed an arc.

At a given point in time we examine the state of the ball(speed and acceleration) and knowing the external forces acting on it we make a prediction as to its change in position after a period of time has elapsed, as shown in the figure above.

Estimate state of a body in motion over time (From Havok Guider)Estimate state of a body in motion over time (From Havok Guider)

Given an initial state of an object, and knowing the forces acting on it, we can estimate the new state of a body in motion over time.

As the simulation becomes more complex, the guess produced by the math give less accurate results when using larger time steps.

The smaller the time step taken, the more accurate the result at the end of the time step. So it is better to split larger time step into n consecutive steps of a smaller time interal.

Iterations

Assume we need to update the geometry in the game so it can be displayed once every 1/60 of a sceond. For simulation accuracy and stability, we might still need to do more simulation steps every second. To achieve this,Havok engine allows you to specify the number of "mini-steps" to take. In Havok "mini-steps" are called iterations.

An Iteration is an internal physics simulation step that does not interact with the 3D display.

The iteration parameter specifies the number of steps the physics engine takes before updating the 3D display.

Iterations allow simulation frequency to be decoupled from display frequency.

Iterations(From Havok User Guider)Iterations(From Havok User Guider)
Energy Management

Energy managerment is concerned with identifying objectsin a scene that are not doing very much and remove these from the physical simulation until such time as they begin to move again.

The important elements of energy management are

  • when should an object be deactivated?
  • when should an object be reactivated?

The correct answer is dependent on the game context.

Usually objects are deactivated when they have not moved much recently and are reactivated when hit by other moving objects.

Collision Detection

introduction

If collison detection is naively implemented , it can account for over 90 of the CPU time required for game simlation.

There are a number of ways to speed up the collision detection process:

  1. Reduce the number of collisons that require detailed collision results to be generated. (e.g. use a simpler collision test first);
  2. Reduce the complexity of objects tested for collisions.
  3. Reduce the number of simultaneously moving objects.

Obviously always try to achieve 3 first. We now see 1 and 2.

Multiphase collision detection

The Havok employs a serious of collision layers, each progressively more complex and returning more detailed results. After each stage, as many objects as possible are eliminated from the test process.

  1. Broadphase collision detection: Use axes aligned bounding boxes and test if boxes overlap.
  2. Midphase collision detection : Memory Optimized Partial Polytope.
  3. Narrowphase collision detection : perform accurate collision test (point of collision, normal to the object at the point of collision).
Rigid Bodies and Collision Geometries

The shape of objects has a major impact on the speed of collision testing. Make assumptions about object shape or geometry:spheres,planes,boxes,polygons.

  • For simple shapes, Havok has an implicit mathematical representation of the object.
  • For polygonal representations, Havok uses a description of each object in terms of these polygons (ususally triangles). In this case, objects are classified as:convex and polygon soup.
Collision tolerance

A collision tolerance is an artificial buffer layer which tells the system that at some small distance two objects are "nearly" colliding.

Having a non-zero collision tolerance helps with two performance-related issues:

  • Pick up potential collision before they actually happen.
  • Prevent fast-moving objects from interpenetrating as early as possible.

See Also

Havok Physics Animation 600 PC XS User Guide.

原创粉丝点击