Chipmunk2D Pro API Reference  6.2.1
 All Classes Functions Variables Typedefs Properties Groups Pages
util.h
1 /* Copyright (c) 2007 Scott Lembcke
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a copy
4  * of this software and associated documentation files (the "Software"), to deal
5  * in the Software without restriction, including without limitation the rights
6  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7  * copies of the Software, and to permit persons to whom the Software is
8  * furnished to do so, subject to the following conditions:
9  *
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19  * SOFTWARE.
20  */
21 
22 // These are utility routines to use when creating custom constraints.
23 // I'm not sure if this should be part of the private API or not.
24 // I should probably clean up the naming conventions if it is...
25 
26 #define CP_DefineClassGetter(t) const cpConstraintClass * t##GetClass(void){return (cpConstraintClass *)&klass;}
27 
28 void cpConstraintInit(cpConstraint *constraint, const cpConstraintClass *klass, cpBody *a, cpBody *b);
29 
30 static inline cpVect
31 relative_velocity(cpBody *a, cpBody *b, cpVect r1, cpVect r2){
32  cpVect v1_sum = cpvadd(a->v, cpvmult(cpvperp(r1), a->w));
33  cpVect v2_sum = cpvadd(b->v, cpvmult(cpvperp(r2), b->w));
34 
35  return cpvsub(v2_sum, v1_sum);
36 }
37 
38 static inline cpFloat
39 normal_relative_velocity(cpBody *a, cpBody *b, cpVect r1, cpVect r2, cpVect n){
40  return cpvdot(relative_velocity(a, b, r1, r2), n);
41 }
42 
43 static inline void
44 apply_impulse(cpBody *body, cpVect j, cpVect r){
45  body->v = cpvadd(body->v, cpvmult(j, body->m_inv));
46  body->w += body->i_inv*cpvcross(r, j);
47 }
48 
49 static inline void
50 apply_impulses(cpBody *a , cpBody *b, cpVect r1, cpVect r2, cpVect j)
51 {
52  apply_impulse(a, cpvneg(j), r1);
53  apply_impulse(b, j, r2);
54 }
55 
56 static inline void
57 apply_bias_impulse(cpBody *body, cpVect j, cpVect r)
58 {
59  body->CP_PRIVATE(v_bias) = cpvadd(body->CP_PRIVATE(v_bias), cpvmult(j, body->m_inv));
60  body->CP_PRIVATE(w_bias) += body->i_inv*cpvcross(r, j);
61 }
62 
63 static inline void
64 apply_bias_impulses(cpBody *a , cpBody *b, cpVect r1, cpVect r2, cpVect j)
65 {
66  apply_bias_impulse(a, cpvneg(j), r1);
67  apply_bias_impulse(b, j, r2);
68 }
69 
70 static inline cpFloat
71 k_scalar_body(cpBody *body, cpVect r, cpVect n)
72 {
73  cpFloat rcn = cpvcross(r, n);
74  return body->m_inv + body->i_inv*rcn*rcn;
75 }
76 
77 static inline cpFloat
78 k_scalar(cpBody *a, cpBody *b, cpVect r1, cpVect r2, cpVect n)
79 {
80  cpFloat value = k_scalar_body(a, r1, n) + k_scalar_body(b, r2, n);
81  cpAssertSoft(value != 0.0, "Unsolvable collision or constraint.");
82 
83  return value;
84 }
85 
86 static inline cpMat2x2
87 k_tensor(cpBody *a, cpBody *b, cpVect r1, cpVect r2)
88 {
89  cpFloat m_sum = a->m_inv + b->m_inv;
90 
91  // start with Identity*m_sum
92  cpFloat k11 = m_sum, k12 = 0.0f;
93  cpFloat k21 = 0.0f, k22 = m_sum;
94 
95  // add the influence from r1
96  cpFloat a_i_inv = a->i_inv;
97  cpFloat r1xsq = r1.x * r1.x * a_i_inv;
98  cpFloat r1ysq = r1.y * r1.y * a_i_inv;
99  cpFloat r1nxy = -r1.x * r1.y * a_i_inv;
100  k11 += r1ysq; k12 += r1nxy;
101  k21 += r1nxy; k22 += r1xsq;
102 
103  // add the influnce from r2
104  cpFloat b_i_inv = b->i_inv;
105  cpFloat r2xsq = r2.x * r2.x * b_i_inv;
106  cpFloat r2ysq = r2.y * r2.y * b_i_inv;
107  cpFloat r2nxy = -r2.x * r2.y * b_i_inv;
108  k11 += r2ysq; k12 += r2nxy;
109  k21 += r2nxy; k22 += r2xsq;
110 
111  // invert
112  cpFloat det = k11*k22 - k12*k21;
113  cpAssertSoft(det != 0.0, "Unsolvable constraint.");
114 
115  cpFloat det_inv = 1.0f/det;
116  return cpMat2x2New(
117  k22*det_inv, -k12*det_inv,
118  -k21*det_inv, k11*det_inv
119  );
120 }
121 
122 static inline cpFloat
123 bias_coef(cpFloat errorBias, cpFloat dt)
124 {
125  return 1.0f - cpfpow(errorBias, dt);
126 }