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"
41 tgHillyGround::Config::Config(btVector3 eulerAngles,
52 m_eulerAngles(eulerAngles),
54 m_restitution(restitution),
60 m_triangleSize(triangleSize),
61 m_waveHeight(waveHeight),
64 assert((m_friction >= 0.0) && (m_friction <= 1.0));
65 assert((m_restitution >= 0.0) && (m_restitution <= 1.0));
66 assert((m_size[0] >= 0.0) && (m_size[1] >= 0.0) && (m_size[2] >= 0.0));
69 assert(m_margin >= 0.0);
70 assert(m_triangleSize >= 0.0);
71 assert(m_waveHeight >= 0.0);
72 assert(m_offset >= 0.0);
97 std::cout <<
"Hilly ground " << std::endl;
98 const btScalar mass = 0.0;
100 btTransform groundTransform;
101 groundTransform.setIdentity();
102 groundTransform.setOrigin(m_config.
m_origin);
104 btQuaternion orientation;
108 groundTransform.setRotation(orientation);
112 btDefaultMotionState*
const pMotionState =
113 new btDefaultMotionState(groundTransform);
115 const btVector3 localInertia(0, 0, 0);
117 btRigidBody::btRigidBodyConstructionInfo
const rbInfo(mass, pMotionState, pGroundShape, localInertia);
119 btRigidBody*
const pGroundBody =
new btRigidBody(rbInfo);
126 btCollisionShape * pShape = 0;
129 const std::size_t vertexCount = m_config.
m_nx * m_config.
m_ny;
131 if (vertexCount > 0) {
133 const std::size_t triangleCount = 2 * (m_config.
m_nx - 1) * (m_config.
m_ny - 1);
136 m_vertices =
new btVector3[vertexCount];
139 setVertices(m_vertices);
141 m_pIndices =
new int[triangleCount * 3];
144 setIndices(m_pIndices);
147 m_pMesh = createMesh(triangleCount, m_pIndices, vertexCount, m_vertices);
150 pShape = createShape(m_pMesh);
153 pShape->setMargin(m_config.
m_margin);
162 btTriangleIndexVertexArray *tgHillyGround::createMesh(std::size_t triangleCount,
int indices[], std::size_t vertexCount, btVector3 vertices[]) {
163 const int vertexStride =
sizeof(btVector3);
164 const int indexStride = 3 *
sizeof(int);
166 btTriangleIndexVertexArray*
const pMesh =
167 new btTriangleIndexVertexArray(triangleCount,
171 (btScalar*) &vertices[0].x(),
176 btCollisionShape *tgHillyGround::createShape(btTriangleIndexVertexArray *pMesh) {
177 const bool useQuantizedAabbCompression =
true;
178 btCollisionShape *
const pShape =
179 new btBvhTriangleMeshShape(pMesh, useQuantizedAabbCompression);
183 void tgHillyGround::setVertices(btVector3 vertices[]) {
184 for (std::size_t i = 0; i < m_config.
m_nx; i++)
186 for (std::size_t j = 0; j < m_config.
m_ny; j++)
189 const btScalar y = (m_config.
m_waveHeight * sin((
double)i) * cos((
double)j) +
192 vertices[i + (j * m_config.
m_nx)].setValue(x, y, z);
197 void tgHillyGround::setIndices(
int indices[]) {
199 for (std::size_t i = 0; i < m_config.
m_nx - 1; i++)
201 for (std::size_t j = 0; j < m_config.
m_ny - 1; j++)
203 indices[index++] = (j * m_config.
m_nx) + i;
204 indices[index++] = (j * m_config.
m_nx) + i + 1;
205 indices[index++] = ((j + 1) * m_config.
m_nx) + i + 1;
207 indices[index++] = (j * m_config.
m_nx) + i;
208 indices[index++] = ((j + 1) * m_config.
m_nx) + i + 1;
209 indices[index++] = ((j + 1) * m_config.
m_nx) + i;
Contains the definition of class tgHillyGround.
virtual btRigidBody * getGroundRigidBody() const
btCollisionShape * hillyCollisionShape()