b2Settings.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Copyright (c) 2006-2007 Erin Catto http://www.gphysics.com
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. * Permission is granted to anyone to use this software for any purpose,
  8. * including commercial applications, and to alter it and redistribute it
  9. * freely, subject to the following restrictions:
  10. * 1. The origin of this software must not be misrepresented; you must not
  11. * claim that you wrote the original software. If you use this software
  12. * in a product, an acknowledgment in the product documentation would be
  13. * appreciated but is not required.
  14. * 2. Altered source versions must be plainly marked as such, and must not be
  15. * misrepresented as being the original software.
  16. * 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #ifndef B2_SETTINGS_H
  19. #define B2_SETTINGS_H
  20. #include <assert.h>
  21. #include <math.h>
  22. #define B2_NOT_USED(x) x
  23. #define b2Assert(A) assert(A)
  24. // need to include NDS jtypes.h instead of
  25. // usual typedefs because NDS jtypes defines
  26. // them slightly differently, oh well.
  27. #ifdef TARGET_IS_NDS
  28. #include "jtypes.h"
  29. #else
  30. typedef signed char int8;
  31. typedef signed short int16;
  32. typedef signed int int32;
  33. typedef unsigned char uint8;
  34. typedef unsigned short uint16;
  35. typedef unsigned int uint32;
  36. #endif
  37. #ifdef TARGET_FLOAT32_IS_FIXED
  38. #include "Fixed.h"
  39. typedef Fixed float32;
  40. #define B2_FLT_MAX FIXED_MAX
  41. #define B2_FLT_EPSILON FIXED_EPSILON
  42. #define B2FORCE_SCALE(x) ((x)<<7)
  43. #define B2FORCE_INV_SCALE(x) ((x)>>7)
  44. #else
  45. typedef float float32;
  46. #define B2_FLT_MAX FLT_MAX
  47. #define B2_FLT_EPSILON FLT_EPSILON
  48. #define B2FORCE_SCALE(x) (x)
  49. #define B2FORCE_INV_SCALE(x) (x)
  50. #endif
  51. const float32 b2_pi = 3.14159265359f;
  52. /// @file
  53. /// Global tuning constants based on meters-kilograms-seconds (MKS) units.
  54. ///
  55. // Collision
  56. const int32 b2_maxManifoldPoints = 2;
  57. const int32 b2_maxPolygonVertices = 8;
  58. const int32 b2_maxProxies = 512; // this must be a power of two
  59. const int32 b2_maxPairs = 8 * b2_maxProxies; // this must be a power of two
  60. // Dynamics
  61. /// A small length used as a collision and constraint tolerance. Usually it is
  62. /// chosen to be numerically significant, but visually insignificant.
  63. const float32 b2_linearSlop = 0.005f; // 0.5 cm
  64. /// A small angle used as a collision and constraint tolerance. Usually it is
  65. /// chosen to be numerically significant, but visually insignificant.
  66. const float32 b2_angularSlop = 2.0f / 180.0f * b2_pi; // 2 degrees
  67. /// Continuous collision detection (CCD) works with core, shrunken shapes. This is the
  68. /// amount by which shapes are automatically shrunk to work with CCD. This must be
  69. /// larger than b2_linearSlop.
  70. const float32 b2_toiSlop = 8.0f * b2_linearSlop;
  71. /// Maximum number of contacts to be handled to solve a TOI island.
  72. const int32 b2_maxTOIContactsPerIsland = 32;
  73. /// Maximum number of joints to be handled to solve a TOI island.
  74. const int32 b2_maxTOIJointsPerIsland = 32;
  75. /// A velocity threshold for elastic collisions. Any collision with a relative linear
  76. /// velocity below this threshold will be treated as inelastic.
  77. const float32 b2_velocityThreshold = 1.0f; // 1 m/s
  78. /// The maximum linear position correction used when solving constraints. This helps to
  79. /// prevent overshoot.
  80. const float32 b2_maxLinearCorrection = 0.2f; // 20 cm
  81. /// The maximum angular position correction used when solving constraints. This helps to
  82. /// prevent overshoot.
  83. const float32 b2_maxAngularCorrection = 8.0f / 180.0f * b2_pi; // 8 degrees
  84. /// The maximum linear velocity of a body. This limit is very large and is used
  85. /// to prevent numerical problems. You shouldn't need to adjust this.
  86. #ifdef TARGET_FLOAT32_IS_FIXED
  87. const float32 b2_maxLinearVelocity = 100.0f;
  88. #else
  89. const float32 b2_maxLinearVelocity = 200.0f;
  90. const float32 b2_maxLinearVelocitySquared = b2_maxLinearVelocity * b2_maxLinearVelocity;
  91. #endif
  92. /// The maximum angular velocity of a body. This limit is very large and is used
  93. /// to prevent numerical problems. You shouldn't need to adjust this.
  94. const float32 b2_maxAngularVelocity = 250.0f;
  95. #ifndef TARGET_FLOAT32_IS_FIXED
  96. const float32 b2_maxAngularVelocitySquared = b2_maxAngularVelocity * b2_maxAngularVelocity;
  97. #endif
  98. /// This scale factor controls how fast overlap is resolved. Ideally this would be 1 so
  99. /// that overlap is removed in one time step. However using values close to 1 often lead
  100. /// to overshoot.
  101. const float32 b2_contactBaumgarte = 0.2f;
  102. // Sleep
  103. /// The time that a body must be still before it will go to sleep.
  104. const float32 b2_timeToSleep = 0.5f; // half a second
  105. /// A body cannot sleep if its linear velocity is above this tolerance.
  106. const float32 b2_linearSleepTolerance = 0.01f; // 1 cm/s
  107. /// A body cannot sleep if its angular velocity is above this tolerance.
  108. const float32 b2_angularSleepTolerance = 2.0f / 180.0f; // 2 degrees/s
  109. // Memory Allocation
  110. /// The current number of bytes allocated through b2Alloc.
  111. extern int32 b2_byteCount;
  112. /// Implement this function to use your own memory allocator.
  113. void* b2Alloc(int32 size);
  114. /// If you implement b2Alloc, you should also implement this function.
  115. void b2Free(void* mem);
  116. /// Version numbering scheme.
  117. /// See http://en.wikipedia.org/wiki/Software_versioning
  118. struct b2Version
  119. {
  120. int32 major; ///< significant changes
  121. int32 minor; ///< incremental changes
  122. int32 revision; ///< bug fixes
  123. };
  124. /// Current version.
  125. extern b2Version b2_version;
  126. /// Friction mixing law. Feel free to customize this.
  127. inline float32 b2MixFriction(float32 friction1, float32 friction2)
  128. {
  129. return sqrtf(friction1 * friction2);
  130. }
  131. /// Restitution mixing law. Feel free to customize this.
  132. inline float32 b2MixRestitution(float32 restitution1, float32 restitution2)
  133. {
  134. return restitution1 > restitution2 ? restitution1 : restitution2;
  135. }
  136. #endif