Use constexpr for compile time constants

This commit is contained in:
Daniel Chappuis 2016-07-11 08:59:39 +02:00
parent 16478722de
commit cfede8f179
12 changed files with 33 additions and 34 deletions

View File

@ -32,7 +32,7 @@
using namespace reactphysics3d;
// Initialization of static variables
const int TreeNode::NULL_TREE_NODE = -1;
constexpr int TreeNode::NULL_TREE_NODE = -1;
// Constructor
DynamicAABBTree::DynamicAABBTree(decimal extraAABBGap) : mExtraAABBGap(extraAABBGap) {

View File

@ -43,10 +43,10 @@ namespace reactphysics3d {
// ---------- Constants ---------- //
/// Maximum number of support points of the polytope
const unsigned int MAX_SUPPORT_POINTS = 100;
constexpr unsigned int MAX_SUPPORT_POINTS = 100;
/// Maximum number of facets of the polytope
const unsigned int MAX_FACETS = 200;
constexpr unsigned int MAX_FACETS = 200;
// Class TriangleComparison

View File

@ -32,7 +32,6 @@
// We want to use the ReactPhysics3D namespace
using namespace reactphysics3d;
// Constructor
EdgeEPA::EdgeEPA() {

View File

@ -36,7 +36,7 @@
namespace reactphysics3d {
// Constants
const unsigned int MAX_TRIANGLES = 200; // Maximum number of triangles
constexpr unsigned int MAX_TRIANGLES = 200; // Maximum number of triangles
// Class TriangleStore
/**

View File

@ -37,9 +37,9 @@
namespace reactphysics3d {
// Constants
const decimal REL_ERROR = decimal(1.0e-3);
const decimal REL_ERROR_SQUARE = REL_ERROR * REL_ERROR;
const int MAX_ITERATIONS_GJK_RAYCAST = 32;
constexpr decimal REL_ERROR = decimal(1.0e-3);
constexpr decimal REL_ERROR_SQUARE = REL_ERROR * REL_ERROR;
constexpr int MAX_ITERATIONS_GJK_RAYCAST = 32;
// Class GJKAlgorithm
/**

View File

@ -55,7 +55,7 @@ Vector3 CylinderShape::getLocalSupportPointWithoutMargin(const Vector3& directio
Vector3 supportPoint(0.0, 0.0, 0.0);
decimal uDotv = direction.y;
Vector3 w(direction.x, 0.0, direction.z);
decimal lengthW = sqrt(direction.x * direction.x + direction.z * direction.z);
decimal lengthW = std::sqrt(direction.x * direction.x + direction.z * direction.z);
if (lengthW > MACHINE_EPSILON) {
if (uDotv < 0.0) supportPoint.y = -mHalfHeight;

View File

@ -83,66 +83,66 @@ const decimal DECIMAL_LARGEST = std::numeric_limits<decimal>::max();
const decimal MACHINE_EPSILON = std::numeric_limits<decimal>::epsilon();
/// Pi constant
const decimal PI = decimal(3.14159265);
constexpr decimal PI = decimal(3.14159265);
/// 2*Pi constant
const decimal PI_TIMES_2 = decimal(6.28318530);
constexpr decimal PI_TIMES_2 = decimal(6.28318530);
/// Default friction coefficient for a rigid body
const decimal DEFAULT_FRICTION_COEFFICIENT = decimal(0.3);
constexpr decimal DEFAULT_FRICTION_COEFFICIENT = decimal(0.3);
/// Default bounciness factor for a rigid body
const decimal DEFAULT_BOUNCINESS = decimal(0.5);
constexpr decimal DEFAULT_BOUNCINESS = decimal(0.5);
/// Default rolling resistance
const decimal DEFAULT_ROLLING_RESISTANCE = decimal(0.0);
constexpr decimal DEFAULT_ROLLING_RESISTANCE = decimal(0.0);
/// True if the spleeping technique is enabled
const bool SPLEEPING_ENABLED = true;
constexpr bool SPLEEPING_ENABLED = true;
/// Object margin for collision detection in meters (for the GJK-EPA Algorithm)
const decimal OBJECT_MARGIN = decimal(0.04);
constexpr decimal OBJECT_MARGIN = decimal(0.04);
/// Distance threshold for two contact points for a valid persistent contact (in meters)
const decimal PERSISTENT_CONTACT_DIST_THRESHOLD = decimal(0.03);
constexpr decimal PERSISTENT_CONTACT_DIST_THRESHOLD = decimal(0.03);
/// Velocity threshold for contact velocity restitution
const decimal RESTITUTION_VELOCITY_THRESHOLD = decimal(1.0);
constexpr decimal RESTITUTION_VELOCITY_THRESHOLD = decimal(1.0);
/// Number of iterations when solving the velocity constraints of the Sequential Impulse technique
const uint DEFAULT_VELOCITY_SOLVER_NB_ITERATIONS = 10;
constexpr uint DEFAULT_VELOCITY_SOLVER_NB_ITERATIONS = 10;
/// Number of iterations when solving the position constraints of the Sequential Impulse technique
const uint DEFAULT_POSITION_SOLVER_NB_ITERATIONS = 5;
constexpr uint DEFAULT_POSITION_SOLVER_NB_ITERATIONS = 5;
/// Time (in seconds) that a body must stay still to be considered sleeping
const float DEFAULT_TIME_BEFORE_SLEEP = 1.0f;
constexpr float DEFAULT_TIME_BEFORE_SLEEP = 1.0f;
/// A body with a linear velocity smaller than the sleep linear velocity (in m/s)
/// might enter sleeping mode.
const decimal DEFAULT_SLEEP_LINEAR_VELOCITY = decimal(0.02);
constexpr decimal DEFAULT_SLEEP_LINEAR_VELOCITY = decimal(0.02);
/// A body with angular velocity smaller than the sleep angular velocity (in rad/s)
/// might enter sleeping mode
const decimal DEFAULT_SLEEP_ANGULAR_VELOCITY = decimal(3.0 * (PI / 180.0));
constexpr decimal DEFAULT_SLEEP_ANGULAR_VELOCITY = decimal(3.0 * (PI / 180.0));
/// In the broad-phase collision detection (dynamic AABB tree), the AABBs are
/// inflated with a constant gap to allow the collision shape to move a little bit
/// without triggering a large modification of the tree which can be costly
const decimal DYNAMIC_TREE_AABB_GAP = decimal(0.1);
constexpr decimal DYNAMIC_TREE_AABB_GAP = decimal(0.1);
/// In the broad-phase collision detection (dynamic AABB tree), the AABBs are
/// also inflated in direction of the linear motion of the body by mutliplying the
/// followin constant with the linear velocity and the elapsed time between two frames.
const decimal DYNAMIC_TREE_AABB_LIN_GAP_MULTIPLIER = decimal(1.7);
constexpr decimal DYNAMIC_TREE_AABB_LIN_GAP_MULTIPLIER = decimal(1.7);
/// Maximum number of contact manifolds in an overlapping pair that involves two
/// convex collision shapes.
const int NB_MAX_CONTACT_MANIFOLDS_CONVEX_SHAPE = 1;
constexpr int NB_MAX_CONTACT_MANIFOLDS_CONVEX_SHAPE = 1;
/// Maximum number of contact manifolds in an overlapping pair that involves at
/// least one concave collision shape.
const int NB_MAX_CONTACT_MANIFOLDS_CONCAVE_SHAPE = 3;
constexpr int NB_MAX_CONTACT_MANIFOLDS_CONCAVE_SHAPE = 3;
}

View File

@ -30,7 +30,7 @@
using namespace reactphysics3d;
// Static variables definition
const decimal BallAndSocketJoint::BETA = decimal(0.2);
constexpr decimal BallAndSocketJoint::BETA = decimal(0.2);
// Constructor
BallAndSocketJoint::BallAndSocketJoint(const BallAndSocketJointInfo& jointInfo)

View File

@ -30,7 +30,7 @@
using namespace reactphysics3d;
// Static variables definition
const decimal FixedJoint::BETA = decimal(0.2);
constexpr decimal FixedJoint::BETA = decimal(0.2);
// Constructor
FixedJoint::FixedJoint(const FixedJointInfo& jointInfo)

View File

@ -31,7 +31,7 @@
using namespace reactphysics3d;
// Static variables definition
const decimal HingeJoint::BETA = decimal(0.2);
constexpr decimal HingeJoint::BETA = decimal(0.2);
// Constructor
HingeJoint::HingeJoint(const HingeJointInfo& jointInfo)

View File

@ -29,7 +29,7 @@
using namespace reactphysics3d;
// Static variables definition
const decimal SliderJoint::BETA = decimal(0.2);
constexpr decimal SliderJoint::BETA = decimal(0.2);
// Constructor
SliderJoint::SliderJoint(const SliderJointInfo& jointInfo)

View File

@ -34,9 +34,9 @@ using namespace reactphysics3d;
using namespace std;
// Constants initialization
const decimal ContactSolver::BETA = decimal(0.2);
const decimal ContactSolver::BETA_SPLIT_IMPULSE = decimal(0.2);
const decimal ContactSolver::SLOP= decimal(0.01);
constexpr decimal ContactSolver::BETA = decimal(0.2);
constexpr decimal ContactSolver::BETA_SPLIT_IMPULSE = decimal(0.2);
constexpr decimal ContactSolver::SLOP= decimal(0.01);
// Constructor
ContactSolver::ContactSolver(const std::map<RigidBody*, uint>& mapBodyToVelocityIndex)