File indexing completed on 2025-06-01 03:50:40

0001 /*
0002 * Copyright (c) 2006-2009 Erin Catto http://www.gphysics.com
0003 *
0004 * This software is provided 'as-is', without any express or implied
0005 * warranty.  In no event will the authors be held liable for any damages
0006 * arising from the use of this software.
0007 * Permission is granted to anyone to use this software for any purpose,
0008 * including commercial applications, and to alter it and redistribute it
0009 * freely, subject to the following restrictions:
0010 * 1. The origin of this software must not be misrepresented; you must not
0011 * claim that you wrote the original software. If you use this software
0012 * in a product, an acknowledgment in the product documentation would be
0013 * appreciated but is not required.
0014 * 2. Altered source versions must be plainly marked as such, and must not be
0015 * misrepresented as being the original software.
0016 * 3. This notice may not be removed or altered from any source distribution.
0017 */
0018 
0019 #ifndef B2_SETTINGS_H
0020 #define B2_SETTINGS_H
0021 
0022 #include <cassert>
0023 #include <cfloat>
0024 #include <cmath>
0025 #include <climits>
0026 #include <stdint.h>
0027 #include <qmath.h>
0028 
0029 #define B2_NOT_USED(x) ((void)(x))
0030 #define b2Assert(A) assert(A)
0031 
0032 typedef signed char int8;
0033 typedef signed short int16;
0034 typedef signed int int32;
0035 typedef unsigned char uint8;
0036 typedef unsigned short uint16;
0037 typedef unsigned int uint32;
0038 
0039 template<size_t fltsize> struct b2_floatValues;
0040 template<> struct b2_floatValues<4> {
0041     static inline float maxFloat() { return FLT_MAX; }
0042     static inline float epsilon() { return FLT_EPSILON; }
0043     static inline float pi() { return 3.14159265359f; }
0044 };
0045 template<> struct b2_floatValues<8> {
0046     static inline double maxFloat() { return DBL_MAX; }
0047     static inline double epsilon() { return DBL_EPSILON; }
0048     static inline double pi() { return M_PI; }
0049 };
0050 
0051 #define b2_maxFloat     b2_floatValues<sizeof(qreal)>::maxFloat()
0052 #define b2_epsilon      b2_floatValues<sizeof(qreal)>::epsilon()
0053 #define b2_pi           b2_floatValues<sizeof(qreal)>::pi()
0054 
0055 /// @file
0056 /// Global tuning constants based on meters-kilograms-seconds (MKS) units.
0057 ///
0058 
0059 // Collision
0060 
0061 /// The maximum number of contact points between two convex shapes.
0062 #define b2_maxManifoldPoints    2
0063 
0064 /// The maximum number of vertices on a convex polygon. You cannot increase
0065 /// this too much because b2BlockAllocator has a maximum object size.
0066 #define b2_maxPolygonVertices   8
0067 
0068 /// This is used to fatten AABBs in the dynamic tree. This allows proxies
0069 /// to move by a small amount without triggering a tree adjustment.
0070 /// This is in meters.
0071 #define b2_aabbExtension        0.1f
0072 
0073 /// This is used to fatten AABBs in the dynamic tree. This is used to predict
0074 /// the future position based on the current displacement.
0075 /// This is a dimensionless multiplier.
0076 #define b2_aabbMultiplier       2.0f
0077 
0078 /// A small length used as a collision and constraint tolerance. Usually it is
0079 /// chosen to be numerically significant, but visually insignificant.
0080 #define b2_linearSlop           0.005f
0081 
0082 /// A small angle used as a collision and constraint tolerance. Usually it is
0083 /// chosen to be numerically significant, but visually insignificant.
0084 #define b2_angularSlop          (2.0f / 180.0f * b2_pi)
0085 
0086 /// The radius of the polygon/edge shape skin. This should not be modified. Making
0087 /// this smaller means polygons will have an insufficient buffer for continuous collision.
0088 /// Making it larger may create artifacts for vertex collision.
0089 #define b2_polygonRadius        (2.0f * b2_linearSlop)
0090 
0091 /// Maximum number of sub-steps per contact in continuous physics simulation.
0092 #define b2_maxSubSteps          8
0093 
0094 
0095 // Dynamics
0096 
0097 /// Maximum number of contacts to be handled to solve a TOI impact.
0098 #define b2_maxTOIContacts           32
0099 
0100 /// A velocity threshold for elastic collisions. Any collision with a relative linear
0101 /// velocity below this threshold will be treated as inelastic.
0102 #define b2_velocityThreshold        0.0f
0103 
0104 /// The maximum linear position correction used when solving constraints. This helps to
0105 /// prevent overshoot.
0106 #define b2_maxLinearCorrection      0.2f
0107 
0108 /// The maximum angular position correction used when solving constraints. This helps to
0109 /// prevent overshoot.
0110 #define b2_maxAngularCorrection     (8.0f / 180.0f * b2_pi)
0111 
0112 /// The maximum linear velocity of a body. This limit is very large and is used
0113 /// to prevent numerical problems. You shouldn't need to adjust this.
0114 #define b2_maxTranslation           2.0f
0115 #define b2_maxTranslationSquared    (b2_maxTranslation * b2_maxTranslation)
0116 
0117 /// The maximum angular velocity of a body. This limit is very large and is used
0118 /// to prevent numerical problems. You shouldn't need to adjust this.
0119 #define b2_maxRotation              (0.5f * b2_pi)
0120 #define b2_maxRotationSquared       (b2_maxRotation * b2_maxRotation)
0121 
0122 /// This scale factor controls how fast overlap is resolved. Ideally this would be 1 so
0123 /// that overlap is removed in one time step. However using values close to 1 often lead
0124 /// to overshoot.
0125 #define b2_contactBaumgarte         0.2f
0126 
0127 
0128 // Sleep
0129 
0130 /// The time that a body must be still before it will go to sleep.
0131 #define b2_timeToSleep              0.5f
0132 
0133 /// A body cannot sleep if its linear velocity is above this tolerance.
0134 #define b2_linearSleepTolerance     0.01f
0135 
0136 /// A body cannot sleep if its angular velocity is above this tolerance.
0137 #define b2_angularSleepTolerance    (2.0f / 180.0f * b2_pi)
0138 
0139 // Memory Allocation
0140 
0141 /// Implement this function to use your own memory allocator.
0142 void* b2Alloc(int32 size);
0143 
0144 /// If you implement b2Alloc, you should also implement this function.
0145 void b2Free(void* mem);
0146 
0147 /// Version numbering scheme.
0148 /// See http://en.wikipedia.org/wiki/Software_versioning
0149 struct b2Version
0150 {
0151     int32 major;        ///< significant changes
0152     int32 minor;        ///< incremental changes
0153     int32 revision;     ///< bug fixes
0154 };
0155 
0156 /// Current version.
0157 extern b2Version b2_version;
0158 
0159 /// Friction mixing law. Feel free to customize this.
0160 inline qreal b2MixFriction(qreal friction1, qreal friction2)
0161 {
0162     return std::sqrt(friction1 * friction2);
0163 }
0164 
0165 /// Restitution mixing law. Feel free to customize this.
0166 inline qreal b2MixRestitution(qreal restitution1, qreal restitution2)
0167 {
0168     return restitution1 > restitution2 ? restitution1 : restitution2;
0169 }
0170 
0171 #endif