Bullet Collision Detection & Physics Library
btHeightfieldTerrainShape Class Reference

btHeightfieldTerrainShape simulates a 2D heightfield terrain More...

#include <btHeightfieldTerrainShape.h>

Inheritance diagram for btHeightfieldTerrainShape:
Collaboration diagram for btHeightfieldTerrainShape:

Public Member Functions

 BT_DECLARE_ALIGNED_ALLOCATOR ()
 
 btHeightfieldTerrainShape (int heightStickWidth, int heightStickLength, const void *heightfieldData, btScalar heightScale, btScalar minHeight, btScalar maxHeight, int upAxis, PHY_ScalarType heightDataType, bool flipQuadEdges)
 preferred constructor
 
 btHeightfieldTerrainShape (int heightStickWidth, int heightStickLength, const void *heightfieldData, btScalar maxHeight, int upAxis, bool useFloatData, bool flipQuadEdges)
 legacy constructor
 
virtual ~btHeightfieldTerrainShape ()
 
void setUseDiamondSubdivision (bool useDiamondSubdivision=true)
 
void setUseZigzagSubdivision (bool useZigzagSubdivision=true)
 could help compatibility with Ogre heightfields. See https://code.google.com/p/bullet/issues/detail?id=625
 
virtual void getAabb (const btTransform &t, btVector3 &aabbMin, btVector3 &aabbMax) const
 getAabb returns the axis aligned bounding box in the coordinate frame of the given transform t.
 
virtual void processAllTriangles (btTriangleCallback *callback, const btVector3 &aabbMin, const btVector3 &aabbMax) const
 process all triangles within the provided axis-aligned bounding box
 
virtual void calculateLocalInertia (btScalar mass, btVector3 &inertia) const
 
virtual void setLocalScaling (const btVector3 &scaling)
 
virtual const btVector3getLocalScaling () const
 
virtual const char * getName () const
 
- Public Member Functions inherited from btConcaveShape
 BT_DECLARE_ALIGNED_ALLOCATOR ()
 
 btConcaveShape ()
 
virtual ~btConcaveShape ()
 
virtual btScalar getMargin () const
 
virtual void setMargin (btScalar collisionMargin)
 
- Public Member Functions inherited from btCollisionShape
 BT_DECLARE_ALIGNED_ALLOCATOR ()
 
 btCollisionShape ()
 
virtual ~btCollisionShape ()
 
virtual void getBoundingSphere (btVector3 &center, btScalar &radius) const
 
virtual btScalar getAngularMotionDisc () const
 getAngularMotionDisc returns the maximum radius needed for Conservative Advancement to handle time-of-impact with rotations.
 
virtual btScalar getContactBreakingThreshold (btScalar defaultContactThresholdFactor) const
 
void calculateTemporalAabb (const btTransform &curTrans, const btVector3 &linvel, const btVector3 &angvel, btScalar timeStep, btVector3 &temporalAabbMin, btVector3 &temporalAabbMax) const
 calculateTemporalAabb calculates the enclosing aabb for the moving object over interval [0..timeStep) result is conservative
 
bool isPolyhedral () const
 
bool isConvex2d () const
 
bool isConvex () const
 
bool isNonMoving () const
 
bool isConcave () const
 
bool isCompound () const
 
bool isSoftBody () const
 
bool isInfinite () const
 isInfinite is used to catch simulation error (aabb check)
 
int getShapeType () const
 
virtual btVector3 getAnisotropicRollingFrictionDirection () const
 the getAnisotropicRollingFrictionDirection can be used in combination with setAnisotropicFriction See Bullet/Demos/RollingFrictionDemo for an example
 
void setUserPointer (void *userPtr)
 optional user data pointer
 
void * getUserPointer () const
 
void setUserIndex (int index)
 
int getUserIndex () const
 
virtual int calculateSerializeBufferSize () const
 
virtual const char * serialize (void *dataBuffer, btSerializer *serializer) const
 fills the dataBuffer and returns the struct name (and 0 on failure)
 
virtual void serializeSingleShape (btSerializer *serializer) const
 

Protected Member Functions

virtual btScalar getRawHeightFieldValue (int x, int y) const
 This returns the "raw" (user's initial) height, not the actual height.
 
void quantizeWithClamp (int *out, const btVector3 &point, int isMax) const
 given input vector, return quantized version
 
void getVertex (int x, int y, btVector3 &vertex) const
 this returns the vertex in bullet-local coordinates
 
void initialize (int heightStickWidth, int heightStickLength, const void *heightfieldData, btScalar heightScale, btScalar minHeight, btScalar maxHeight, int upAxis, PHY_ScalarType heightDataType, bool flipQuadEdges)
 protected initialization
 

Protected Attributes

btVector3 m_localAabbMin
 
btVector3 m_localAabbMax
 
btVector3 m_localOrigin
 
int m_heightStickWidth
 terrain data
 
int m_heightStickLength
 
btScalar m_minHeight
 
btScalar m_maxHeight
 
btScalar m_width
 
btScalar m_length
 
btScalar m_heightScale
 
union { 
 
   const unsigned char *   m_heightfieldDataUnsignedChar 
 
   const short *   m_heightfieldDataShort 
 
   const btScalar *   m_heightfieldDataFloat 
 
   const void *   m_heightfieldDataUnknown 
 
};  
 
PHY_ScalarType m_heightDataType
 
bool m_flipQuadEdges
 
bool m_useDiamondSubdivision
 
bool m_useZigzagSubdivision
 
int m_upAxis
 
btVector3 m_localScaling
 
- Protected Attributes inherited from btConcaveShape
btScalar m_collisionMargin
 
- Protected Attributes inherited from btCollisionShape
int m_shapeType
 
void * m_userPointer
 
int m_userIndex
 

Detailed Description

btHeightfieldTerrainShape simulates a 2D heightfield terrain

The caller is responsible for maintaining the heightfield array; this class does not make a copy.

The heightfield can be dynamic so long as the min/max height values capture the extremes (heights must always be in that range).

The local origin of the heightfield is assumed to be the exact center (as determined by width and length and height, with each axis multiplied by the localScaling).

NOTE: be careful with coordinates. If you have a heightfield with a local min height of -100m, and a max height of +500m, you may be tempted to place it at the origin (0,0) and expect the heights in world coordinates to be -100 to +500 meters. Actually, the heights will be -300 to +300m, because bullet will re-center the heightfield based on its AABB (which is determined by the min/max heights). So keep in mind that once you create a btHeightfieldTerrainShape object, the heights will be adjusted relative to the center of the AABB. This is different to the behavior of many rendering engines, but is useful for physics engines.

Most (but not all) rendering and heightfield libraries assume upAxis = 1 (that is, the y-axis is "up"). This class allows any of the 3 coordinates to be "up". Make sure your choice of axis is consistent with your rendering system.

The heightfield heights are determined from the data type used for the heightfieldData array.

  • PHY_UCHAR: height at a point is the uchar value at the grid point, multipled by heightScale. uchar isn't recommended because of its inability to deal with negative values, and low resolution (8-bit).
  • PHY_SHORT: height at a point is the short int value at that grid point, multipled by heightScale.
  • PHY_FLOAT: height at a point is the float value at that grid point. heightScale is ignored when using the float heightfield data type.

Whatever the caller specifies as minHeight and maxHeight will be honored. The class will not inspect the heightfield to discover the actual minimum or maximum heights. These values are used to determine the heightfield's axis-aligned bounding box, multiplied by localScaling.

For usage and testing see the TerrainDemo.

Definition at line 71 of file btHeightfieldTerrainShape.h.

Constructor & Destructor Documentation

◆ btHeightfieldTerrainShape() [1/2]

btHeightfieldTerrainShape::btHeightfieldTerrainShape ( int heightStickWidth,
int heightStickLength,
const void * heightfieldData,
btScalar heightScale,
btScalar minHeight,
btScalar maxHeight,
int upAxis,
PHY_ScalarType heightDataType,
bool flipQuadEdges )

preferred constructor

This constructor supports a range of heightfield data types, and allows for a non-zero minimum height value. heightScale is needed for any integer-based heightfield data types.

Definition at line 22 of file btHeightfieldTerrainShape.cpp.

◆ btHeightfieldTerrainShape() [2/2]

btHeightfieldTerrainShape::btHeightfieldTerrainShape ( int heightStickWidth,
int heightStickLength,
const void * heightfieldData,
btScalar maxHeight,
int upAxis,
bool useFloatData,
bool flipQuadEdges )

legacy constructor

The legacy constructor assumes the heightfield has a minimum height of zero. Only unsigned char or floats are supported. For legacy compatibility reasons, heightScale is calculated as maxHeight / 65535 (and is only used when useFloatData = false).

Definition at line 36 of file btHeightfieldTerrainShape.cpp.

◆ ~btHeightfieldTerrainShape()

btHeightfieldTerrainShape::~btHeightfieldTerrainShape ( )
virtual

Definition at line 121 of file btHeightfieldTerrainShape.cpp.

Member Function Documentation

◆ BT_DECLARE_ALIGNED_ALLOCATOR()

btHeightfieldTerrainShape::BT_DECLARE_ALIGNED_ALLOCATOR ( )

◆ calculateLocalInertia()

void btHeightfieldTerrainShape::calculateLocalInertia ( btScalar mass,
btVector3 & inertia ) const
virtual

Implements btCollisionShape.

Definition at line 395 of file btHeightfieldTerrainShape.cpp.

◆ getAabb()

void btHeightfieldTerrainShape::getAabb ( const btTransform & t,
btVector3 & aabbMin,
btVector3 & aabbMax ) const
virtual

getAabb returns the axis aligned bounding box in the coordinate frame of the given transform t.

Implements btCollisionShape.

Definition at line 127 of file btHeightfieldTerrainShape.cpp.

◆ getLocalScaling()

const btVector3 & btHeightfieldTerrainShape::getLocalScaling ( ) const
virtual

Implements btCollisionShape.

Definition at line 406 of file btHeightfieldTerrainShape.cpp.

◆ getName()

virtual const char * btHeightfieldTerrainShape::getName ( ) const
inlinevirtual

Implements btCollisionShape.

Definition at line 163 of file btHeightfieldTerrainShape.h.

◆ getRawHeightFieldValue()

btScalar btHeightfieldTerrainShape::getRawHeightFieldValue ( int x,
int y ) const
protectedvirtual

This returns the "raw" (user's initial) height, not the actual height.

The actual height needs to be adjusted to be relative to the center of the heightfield's AABB.

Definition at line 149 of file btHeightfieldTerrainShape.cpp.

◆ getVertex()

void btHeightfieldTerrainShape::getVertex ( int x,
int y,
btVector3 & vertex ) const
protected

this returns the vertex in bullet-local coordinates

Definition at line 187 of file btHeightfieldTerrainShape.cpp.

◆ initialize()

void btHeightfieldTerrainShape::initialize ( int heightStickWidth,
int heightStickLength,
const void * heightfieldData,
btScalar heightScale,
btScalar minHeight,
btScalar maxHeight,
int upAxis,
PHY_ScalarType heightDataType,
bool flipQuadEdges )
protected

protected initialization

Handles the work of constructors so that public constructors can be backwards-compatible without a lot of copy/paste.

Definition at line 54 of file btHeightfieldTerrainShape.cpp.

◆ processAllTriangles()

void btHeightfieldTerrainShape::processAllTriangles ( btTriangleCallback * callback,
const btVector3 & aabbMin,
const btVector3 & aabbMax ) const
virtual

process all triangles within the provided axis-aligned bounding box

basic algorithm:

  • convert input aabb to local coordinates (scale down and shift for local origin)
  • convert input aabb to a range of heightfield grid points (quantize)
  • iterate over all triangles in that subset of the grid

Implements btConcaveShape.

Definition at line 281 of file btHeightfieldTerrainShape.cpp.

◆ quantizeWithClamp()

void btHeightfieldTerrainShape::quantizeWithClamp ( int * out,
const btVector3 & point,
int isMax ) const
protected

given input vector, return quantized version

This routine is basically determining the gridpoint indices for a given input vector, answering the question: "which gridpoint is closest to the provided point?".

"with clamp" means that we restrict the point to be in the heightfield's axis-aligned bounding box.

Definition at line 260 of file btHeightfieldTerrainShape.cpp.

◆ setLocalScaling()

void btHeightfieldTerrainShape::setLocalScaling ( const btVector3 & scaling)
virtual

Implements btCollisionShape.

Definition at line 402 of file btHeightfieldTerrainShape.cpp.

◆ setUseDiamondSubdivision()

void btHeightfieldTerrainShape::setUseDiamondSubdivision ( bool useDiamondSubdivision = true)
inline

Definition at line 147 of file btHeightfieldTerrainShape.h.

◆ setUseZigzagSubdivision()

void btHeightfieldTerrainShape::setUseZigzagSubdivision ( bool useZigzagSubdivision = true)
inline

could help compatibility with Ogre heightfields. See https://code.google.com/p/bullet/issues/detail?id=625

Definition at line 150 of file btHeightfieldTerrainShape.h.

Member Data Documentation

◆ [union]

◆ m_flipQuadEdges

bool btHeightfieldTerrainShape::m_flipQuadEdges
protected

Definition at line 95 of file btHeightfieldTerrainShape.h.

◆ m_heightDataType

PHY_ScalarType btHeightfieldTerrainShape::m_heightDataType
protected

Definition at line 94 of file btHeightfieldTerrainShape.h.

◆ m_heightfieldDataFloat

const btScalar* btHeightfieldTerrainShape::m_heightfieldDataFloat

Definition at line 90 of file btHeightfieldTerrainShape.h.

◆ m_heightfieldDataShort

const short* btHeightfieldTerrainShape::m_heightfieldDataShort

Definition at line 89 of file btHeightfieldTerrainShape.h.

◆ m_heightfieldDataUnknown

const void* btHeightfieldTerrainShape::m_heightfieldDataUnknown

Definition at line 91 of file btHeightfieldTerrainShape.h.

◆ m_heightfieldDataUnsignedChar

const unsigned char* btHeightfieldTerrainShape::m_heightfieldDataUnsignedChar

Definition at line 88 of file btHeightfieldTerrainShape.h.

◆ m_heightScale

btScalar btHeightfieldTerrainShape::m_heightScale
protected

Definition at line 85 of file btHeightfieldTerrainShape.h.

◆ m_heightStickLength

int btHeightfieldTerrainShape::m_heightStickLength
protected

Definition at line 80 of file btHeightfieldTerrainShape.h.

◆ m_heightStickWidth

int btHeightfieldTerrainShape::m_heightStickWidth
protected

terrain data

Definition at line 79 of file btHeightfieldTerrainShape.h.

◆ m_length

btScalar btHeightfieldTerrainShape::m_length
protected

Definition at line 84 of file btHeightfieldTerrainShape.h.

◆ m_localAabbMax

btVector3 btHeightfieldTerrainShape::m_localAabbMax
protected

Definition at line 75 of file btHeightfieldTerrainShape.h.

◆ m_localAabbMin

btVector3 btHeightfieldTerrainShape::m_localAabbMin
protected

Definition at line 74 of file btHeightfieldTerrainShape.h.

◆ m_localOrigin

btVector3 btHeightfieldTerrainShape::m_localOrigin
protected

Definition at line 76 of file btHeightfieldTerrainShape.h.

◆ m_localScaling

btVector3 btHeightfieldTerrainShape::m_localScaling
protected

Definition at line 101 of file btHeightfieldTerrainShape.h.

◆ m_maxHeight

btScalar btHeightfieldTerrainShape::m_maxHeight
protected

Definition at line 82 of file btHeightfieldTerrainShape.h.

◆ m_minHeight

btScalar btHeightfieldTerrainShape::m_minHeight
protected

Definition at line 81 of file btHeightfieldTerrainShape.h.

◆ m_upAxis

int btHeightfieldTerrainShape::m_upAxis
protected

Definition at line 99 of file btHeightfieldTerrainShape.h.

◆ m_useDiamondSubdivision

bool btHeightfieldTerrainShape::m_useDiamondSubdivision
protected

Definition at line 96 of file btHeightfieldTerrainShape.h.

◆ m_useZigzagSubdivision

bool btHeightfieldTerrainShape::m_useZigzagSubdivision
protected

Definition at line 97 of file btHeightfieldTerrainShape.h.

◆ m_width

btScalar btHeightfieldTerrainShape::m_width
protected

Definition at line 83 of file btHeightfieldTerrainShape.h.


The documentation for this class was generated from the following files: