first commit
[blok] / Box2D / Source / Common / b2Settings.h
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
19 #ifndef B2_SETTINGS_H
20 #define B2_SETTINGS_H
21
22 #include <assert.h>
23 #include <math.h>
24
25 #define B2_NOT_USED(x) x
26 #define b2Assert(A) assert(A)
27
28
29 // need to include NDS jtypes.h instead of 
30 // usual typedefs because NDS jtypes defines
31 // them slightly differently, oh well.
32 #ifdef TARGET_IS_NDS
33
34 #include "jtypes.h"
35
36 #else
37
38 typedef signed char     int8;
39 typedef signed short int16;
40 typedef signed int int32;
41 typedef unsigned char uint8;
42 typedef unsigned short uint16;
43 typedef unsigned int uint32;
44
45 #endif
46
47 #ifdef  TARGET_FLOAT32_IS_FIXED
48
49 #include "Fixed.h"
50
51 typedef Fixed float32;
52 #define B2_FLT_MAX      FIXED_MAX
53 #define B2_FLT_EPSILON  FIXED_EPSILON
54 #define B2FORCE_SCALE(x)        ((x)<<7)
55 #define B2FORCE_INV_SCALE(x)    ((x)>>7)
56
57 #else
58
59 typedef float float32;
60 #define B2_FLT_MAX      FLT_MAX
61 #define B2_FLT_EPSILON  FLT_EPSILON
62 #define B2FORCE_SCALE(x)        (x)
63 #define B2FORCE_INV_SCALE(x)    (x)
64
65 #endif
66
67 const float32 b2_pi = 3.14159265359f;
68
69 /// @file
70 /// Global tuning constants based on meters-kilograms-seconds (MKS) units.
71 ///
72
73 // Collision
74 const int32 b2_maxManifoldPoints = 2;
75 const int32 b2_maxPolygonVertices = 8;
76 const int32 b2_maxProxies = 512;                                // this must be a power of two
77 const int32 b2_maxPairs = 8 * b2_maxProxies;    // this must be a power of two
78
79 // Dynamics
80
81 /// A small length used as a collision and constraint tolerance. Usually it is
82 /// chosen to be numerically significant, but visually insignificant.
83 const float32 b2_linearSlop = 0.005f;   // 0.5 cm
84
85 /// A small angle used as a collision and constraint tolerance. Usually it is
86 /// chosen to be numerically significant, but visually insignificant.
87 const float32 b2_angularSlop = 2.0f / 180.0f * b2_pi;                   // 2 degrees
88
89 /// Continuous collision detection (CCD) works with core, shrunken shapes. This is the
90 /// amount by which shapes are automatically shrunk to work with CCD. This must be
91 /// larger than b2_linearSlop.
92 const float32 b2_toiSlop = 8.0f * b2_linearSlop;
93
94 /// Maximum number of contacts to be handled to solve a TOI island.
95 const int32 b2_maxTOIContactsPerIsland = 32;
96
97 /// A velocity threshold for elastic collisions. Any collision with a relative linear
98 /// velocity below this threshold will be treated as inelastic.
99 const float32 b2_velocityThreshold = 1.0f;              // 1 m/s
100
101 /// The maximum linear position correction used when solving constraints. This helps to
102 /// prevent overshoot.
103 const float32 b2_maxLinearCorrection = 0.2f;    // 20 cm
104
105 /// The maximum angular position correction used when solving constraints. This helps to
106 /// prevent overshoot.
107 const float32 b2_maxAngularCorrection = 8.0f / 180.0f * b2_pi;                  // 8 degrees
108
109 /// The maximum linear velocity of a body. This limit is very large and is used
110 /// to prevent numerical problems. You shouldn't need to adjust this.
111 #ifdef TARGET_FLOAT32_IS_FIXED
112 const float32 b2_maxLinearVelocity = 100.0f;
113 #else
114 const float32 b2_maxLinearVelocity = 200.0f;
115 const float32 b2_maxLinearVelocitySquared = b2_maxLinearVelocity * b2_maxLinearVelocity;
116 #endif
117
118 /// The maximum angular velocity of a body. This limit is very large and is used
119 /// to prevent numerical problems. You shouldn't need to adjust this.
120 const float32 b2_maxAngularVelocity = 250.0f;
121 #ifndef TARGET_FLOAT32_IS_FIXED
122 const float32 b2_maxAngularVelocitySquared = b2_maxAngularVelocity * b2_maxAngularVelocity;
123 #endif
124
125 /// This scale factor controls how fast overlap is resolved. Ideally this would be 1 so
126 /// that overlap is removed in one time step. However using values close to 1 often lead
127 /// to overshoot.
128 const float32 b2_contactBaumgarte = 0.2f;
129
130 // Sleep
131
132 /// The time that a body must be still before it will go to sleep.
133 const float32 b2_timeToSleep = 0.5f;                                                                    // half a second
134
135 /// A body cannot sleep if its linear velocity is above this tolerance.
136 const float32 b2_linearSleepTolerance = 0.01f;          // 1 cm/s
137
138 /// A body cannot sleep if its angular velocity is above this tolerance.
139 const float32 b2_angularSleepTolerance = 2.0f / 180.0f;         // 2 degrees/s
140
141 // Memory Allocation
142
143 /// The current number of bytes allocated through b2Alloc.
144 extern int32 b2_byteCount;
145
146 /// Implement this function to use your own memory allocator.
147 void* b2Alloc(int32 size);
148
149 /// If you implement b2Alloc, you should also implement this function.
150 void b2Free(void* mem);
151
152 /// Version numbering scheme.
153 /// See http://en.wikipedia.org/wiki/Software_versioning
154 struct b2Version
155 {
156         int32 major;            ///< significant changes
157         int32 minor;            ///< incremental changes
158         int32 revision;         ///< bug fixes
159 };
160
161 /// Current version.
162 extern b2Version b2_version;
163
164 /// Friction mixing law. Feel free to customize this.
165 inline float32 b2MixFriction(float32 friction1, float32 friction2)
166 {
167         return sqrtf(friction1 * friction2);
168 }
169
170 /// Restitution mixing law. Feel free to customize this.
171 inline float32 b2MixRestitution(float32 restitution1, float32 restitution2)
172 {
173         return restitution1 > restitution2 ? restitution1 : restitution2;
174 }
175
176 #endif