30 #include "BulletCollision/CollisionShapes/btBoxShape.h"
31 #include "BulletCollision/CollisionShapes/btTriangleIndexVertexArray.h"
32 #include "BulletCollision/CollisionShapes/btBvhTriangleMeshShape.h"
33 #include "BulletDynamics/Dynamics/btRigidBody.h"
34 #include "LinearMath/btDefaultMotionState.h"
35 #include "LinearMath/btTransform.h"
40 tgHillyGround::Config::Config(btVector3 eulerAngles,
51 m_eulerAngles(eulerAngles),
53 m_restitution(restitution),
59 m_triangleSize(triangleSize),
60 m_waveHeight(waveHeight),
63 assert((m_friction >= 0.0) && (m_friction <= 1.0));
64 assert((m_restitution >= 0.0) && (m_restitution <= 1.0));
65 assert((m_size[0] >= 0.0) && (m_size[1] >= 0.0) && (m_size[2] >= 0.0));
68 assert(m_margin >= 0.0);
69 assert(m_triangleSize >= 0.0);
70 assert(m_waveHeight >= 0.0);
71 assert(m_offset >= 0.0);
89 const btScalar mass = 0.0;
91 btTransform groundTransform;
92 groundTransform.setIdentity();
93 groundTransform.setOrigin(m_config.
m_origin);
95 btQuaternion orientation;
99 groundTransform.setRotation(orientation);
103 btDefaultMotionState*
const pMotionState =
104 new btDefaultMotionState(groundTransform);
106 const btVector3 localInertia(0, 0, 0);
108 btRigidBody::btRigidBodyConstructionInfo
const rbInfo(mass, pMotionState, pGroundShape, localInertia);
110 btRigidBody*
const pGroundBody =
new btRigidBody(rbInfo);
117 btCollisionShape * pShape = 0;
120 const size_t vertexCount = m_config.
m_nx * m_config.
m_ny;
122 if (vertexCount > 0) {
124 const size_t triangleCount = 2 * (m_config.
m_nx - 1) * (m_config.
m_ny - 1);
127 btVector3 *
const vertices =
new btVector3[vertexCount];
130 setVertices(vertices);
132 int *indices =
new int[triangleCount * 3];
138 btTriangleIndexVertexArray*
const pMesh =
139 createMesh(triangleCount, indices, vertexCount, vertices);
142 pShape = createShape(pMesh);
145 pShape->setMargin(m_config.
m_margin);
153 btTriangleIndexVertexArray *tgHillyGround::createMesh(
size_t triangleCount,
int indices[],
size_t vertexCount, btVector3 vertices[]) {
154 const int vertexStride =
sizeof(btVector3);
155 const int indexStride = 3 *
sizeof(int);
157 btTriangleIndexVertexArray*
const pMesh =
158 new btTriangleIndexVertexArray(triangleCount,
162 (btScalar*) &vertices[0].x(),
167 btCollisionShape *tgHillyGround::createShape(btTriangleIndexVertexArray *pMesh) {
168 const bool useQuantizedAabbCompression =
true;
169 btCollisionShape *
const pShape =
170 new btBvhTriangleMeshShape(pMesh, useQuantizedAabbCompression);
174 void tgHillyGround::setVertices(btVector3 vertices[]) {
175 for (
size_t i = 0; i < m_config.
m_nx; i++)
177 for (
size_t j = 0; j < m_config.
m_ny; j++)
180 const btScalar y = (m_config.
m_waveHeight * sinf((
float)i) * cosf((
float)j) +
183 vertices[i + (j * m_config.
m_nx)].setValue(x, y, z);
188 void tgHillyGround::setIndices(
int indices[]) {
190 for (std::size_t i = 0; i < m_config.
m_nx - 1; i++)
192 for (std::size_t j = 0; j < m_config.
m_ny - 1; j++)
194 indices[index++] = (j * m_config.
m_nx) + i;
195 indices[index++] = (j * m_config.
m_nx) + i + 1;
196 indices[index++] = ((j + 1) * m_config.
m_nx) + i + 1;
198 indices[index++] = (j * m_config.
m_nx) + i;
199 indices[index++] = ((j + 1) * m_config.
m_nx) + i + 1;
200 indices[index++] = ((j + 1) * m_config.
m_nx) + i;
Contains the definition of class tgHillyGround.
virtual btRigidBody * getGroundRigidBody() const
btCollisionShape * hillyCollisionShape()