diff --git a/src/collision/shapes/HeightFieldShape.cpp b/src/collision/shapes/HeightFieldShape.cpp index d978b99c..0d3a9265 100644 --- a/src/collision/shapes/HeightFieldShape.cpp +++ b/src/collision/shapes/HeightFieldShape.cpp @@ -57,7 +57,7 @@ HeightFieldShape::HeightFieldShape(int nbWidthGridPoints, int nbLengthGridPoints mHeightFieldData = heightFieldData; decimal halfHeight = (mMaxHeight - mMinHeight) * decimal(0.5); - assert(halfHeight > 0); + assert(halfHeight >= 0); // Compute the local AABB of the height field if (mUpAxis == 0) { diff --git a/src/collision/shapes/HeightFieldShape.h b/src/collision/shapes/HeightFieldShape.h index bfbfec22..1aa1e1c9 100644 --- a/src/collision/shapes/HeightFieldShape.h +++ b/src/collision/shapes/HeightFieldShape.h @@ -198,22 +198,23 @@ inline Vector3 HeightFieldShape::getVertexAt(int x, int y) const { // Get the height value const decimal height = getHeightAt(x, y); - // Get the difference between the center of AABB and zero level of the height field - const decimal originToZeroHeight = (mMaxHeight - mMinHeight) * decimal(0.5); + // Height values origin + const decimal heightOrigin = -(mMaxHeight - mMinHeight) * decimal(0.5) - mMinHeight; Vector3 vertex; switch (mUpAxis) { - case 0: vertex = Vector3(height - originToZeroHeight, -mWidth * decimal(0.5) + x, -mLength * decimal(0.5) + y) * mScaling; + case 0: vertex = Vector3(heightOrigin + height, -mWidth * decimal(0.5) + x, -mLength * decimal(0.5) + y); break; - case 1: vertex = Vector3(-mWidth * decimal(0.5) + x, height - originToZeroHeight, -mLength * decimal(0.5) + y) * mScaling; + case 1: vertex = Vector3(-mWidth * decimal(0.5) + x, heightOrigin + height, -mLength * decimal(0.5) + y); break; - case 2: vertex = Vector3(-mWidth * decimal(0.5) + x, -mLength * decimal(0.5) + y, height - originToZeroHeight) * mScaling; + case 2: vertex = Vector3(-mWidth * decimal(0.5) + x, -mLength * decimal(0.5) + y, heightOrigin + height); break; default: assert(false); } assert(mAABB.contains(vertex)); - return vertex; + + return vertex * mScaling; } // Return the height of a given (x,y) point in the height field diff --git a/testbed/common/HeightField.cpp b/testbed/common/HeightField.cpp index d32f85bc..b30d7768 100644 --- a/testbed/common/HeightField.cpp +++ b/testbed/common/HeightField.cpp @@ -25,6 +25,7 @@ // Libraries #include "HeightField.h" +#include "PerlinNoise.h" // Constructor HeightField::HeightField(const openglframework::Vector3 &position, @@ -47,7 +48,7 @@ HeightField::HeightField(const openglframework::Vector3 &position, // Create the collision shape for the rigid body (convex mesh shape) and // do not forget to delete it at the end - mHeightFieldShape = new rp3d::HeightFieldShape(HEIGHTFIELD_WIDTH, HEIGHTFIELD_LENGTH, 0, 5, + mHeightFieldShape = new rp3d::HeightFieldShape(NB_POINTS_WIDTH, NB_POINTS_LENGTH, mMinHeight, mMaxHeight, mHeightData, rp3d::HeightFieldShape::HEIGHT_FLOAT_TYPE); // Initial position and orientation of the rigid body @@ -90,7 +91,7 @@ HeightField::HeightField(const openglframework::Vector3 &position, float mass, // Create the collision shape for the rigid body (convex mesh shape) and // do not forget to delete it at the end - mHeightFieldShape = new rp3d::HeightFieldShape(HEIGHTFIELD_WIDTH, HEIGHTFIELD_LENGTH, 0, 5, + mHeightFieldShape = new rp3d::HeightFieldShape(NB_POINTS_WIDTH, NB_POINTS_LENGTH, mMinHeight, mMaxHeight, mHeightData, rp3d::HeightFieldShape::HEIGHT_FLOAT_TYPE); // Initial position and orientation of the rigid body @@ -188,12 +189,33 @@ void HeightField::render(openglframework::Shader& shader, // Compute the heights of the height field void HeightField::generateHeightField() { - mMinHeight = 0.0; - mMaxHeight = 5.0; + double persistence = 6; + double frequency = 0.13; + double amplitude = 18; + int octaves = 1; + int randomseed = 779; + PerlinNoise perlinNoise(persistence, frequency, amplitude, octaves, randomseed); - for (int i=0; i mMaxHeight) mMaxHeight = mHeightData[arrayIndex] ; + if (mHeightData[arrayIndex] < mMinHeight) mMinHeight = mHeightData[arrayIndex] ; } } } @@ -204,21 +226,27 @@ void HeightField::generateGraphicsMesh() { std::vector indices; int vertexId = 0; - for (int i=0; iresetTransform(transform); - rp3d::Vector3 spherePos(-3.6, 15, 1.7); + rp3d::Vector3 spherePos(0, 13, 0); rp3d::Transform sphereTransform(spherePos, initOrientation); mBox->resetTransform(sphereTransform); }