chipmunk 4.1.0-x86-mswin32
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +38 -0
- data/ext/chipmunk/chipmunk.c +69 -0
- data/ext/chipmunk/chipmunk.h +91 -0
- data/ext/chipmunk/cpArbiter.c +263 -0
- data/ext/chipmunk/cpArbiter.h +85 -0
- data/ext/chipmunk/cpArray.c +114 -0
- data/ext/chipmunk/cpArray.h +45 -0
- data/ext/chipmunk/cpBB.c +46 -0
- data/ext/chipmunk/cpBB.h +53 -0
- data/ext/chipmunk/cpBody.c +180 -0
- data/ext/chipmunk/cpBody.h +132 -0
- data/ext/chipmunk/cpCollision.c +390 -0
- data/ext/chipmunk/cpCollision.h +23 -0
- data/ext/chipmunk/cpHashSet.c +219 -0
- data/ext/chipmunk/cpHashSet.h +79 -0
- data/ext/chipmunk/cpJoint.c +553 -0
- data/ext/chipmunk/cpJoint.h +122 -0
- data/ext/chipmunk/cpPolyShape.c +139 -0
- data/ext/chipmunk/cpPolyShape.h +92 -0
- data/ext/chipmunk/cpShape.c +244 -0
- data/ext/chipmunk/cpShape.h +141 -0
- data/ext/chipmunk/cpSpace.c +530 -0
- data/ext/chipmunk/cpSpace.h +120 -0
- data/ext/chipmunk/cpSpaceHash.c +455 -0
- data/ext/chipmunk/cpSpaceHash.h +100 -0
- data/ext/chipmunk/cpVect.c +63 -0
- data/ext/chipmunk/cpVect.h +106 -0
- data/ext/chipmunk/extconf.rb +4 -0
- data/ext/chipmunk/prime.h +68 -0
- data/ext/chipmunk/rb_chipmunk.c +109 -0
- data/ext/chipmunk/rb_chipmunk.h +77 -0
- data/ext/chipmunk/rb_cpBB.c +154 -0
- data/ext/chipmunk/rb_cpBody.c +239 -0
- data/ext/chipmunk/rb_cpJoint.c +136 -0
- data/ext/chipmunk/rb_cpShape.c +292 -0
- data/ext/chipmunk/rb_cpSpace.c +330 -0
- data/ext/chipmunk/rb_cpVect.c +250 -0
- data/lib/1.8/chipmunk.so +0 -0
- data/lib/1.9/chipmunk.so +0 -0
- data/lib/chipmunk.rb +15 -0
- metadata +92 -0
@@ -0,0 +1,85 @@
|
|
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
|
+
// Determines how fast penetrations resolve themselves.
|
23
|
+
extern cpFloat cp_bias_coef;
|
24
|
+
// Amount of allowed penetration. Used to reduce vibrating contacts.
|
25
|
+
extern cpFloat cp_collision_slop;
|
26
|
+
|
27
|
+
// Data structure for contact points.
|
28
|
+
typedef struct cpContact{
|
29
|
+
// Contact point and normal.
|
30
|
+
cpVect p, n;
|
31
|
+
// Penetration distance.
|
32
|
+
cpFloat dist;
|
33
|
+
|
34
|
+
// Calculated by cpArbiterPreStep().
|
35
|
+
cpVect r1, r2;
|
36
|
+
cpFloat nMass, tMass, bounce;
|
37
|
+
|
38
|
+
// Persistant contact information.
|
39
|
+
cpFloat jnAcc, jtAcc, jBias;
|
40
|
+
cpFloat bias;
|
41
|
+
|
42
|
+
// Hash value used to (mostly) uniquely identify a contact.
|
43
|
+
unsigned int hash;
|
44
|
+
} cpContact;
|
45
|
+
|
46
|
+
// Contacts are always allocated in groups.
|
47
|
+
cpContact* cpContactInit(cpContact *con, cpVect p, cpVect n, cpFloat dist, unsigned int hash);
|
48
|
+
|
49
|
+
// Sum the contact impulses. (Can be used after cpSpaceStep() returns)
|
50
|
+
cpVect cpContactsSumImpulses(cpContact *contacts, int numContacts);
|
51
|
+
cpVect cpContactsSumImpulsesWithFriction(cpContact *contacts, int numContacts);
|
52
|
+
|
53
|
+
// Data structure for tracking collisions between shapes.
|
54
|
+
typedef struct cpArbiter{
|
55
|
+
// Information on the contact points between the objects.
|
56
|
+
int numContacts;
|
57
|
+
cpContact *contacts;
|
58
|
+
|
59
|
+
// The two shapes involved in the collision.
|
60
|
+
cpShape *a, *b;
|
61
|
+
|
62
|
+
// Calculated by cpArbiterPreStep().
|
63
|
+
cpFloat u;
|
64
|
+
cpVect target_v;
|
65
|
+
|
66
|
+
// Time stamp of the arbiter. (from cpSpace)
|
67
|
+
int stamp;
|
68
|
+
} cpArbiter;
|
69
|
+
|
70
|
+
// Basic allocation/destruction functions.
|
71
|
+
cpArbiter* cpArbiterAlloc(void);
|
72
|
+
cpArbiter* cpArbiterInit(cpArbiter *arb, cpShape *a, cpShape *b, int stamp);
|
73
|
+
cpArbiter* cpArbiterNew(cpShape *a, cpShape *b, int stamp);
|
74
|
+
|
75
|
+
void cpArbiterDestroy(cpArbiter *arb);
|
76
|
+
void cpArbiterFree(cpArbiter *arb);
|
77
|
+
|
78
|
+
// These functions are all intended to be used internally.
|
79
|
+
// Inject new contact points into the arbiter while preserving contact history.
|
80
|
+
void cpArbiterInject(cpArbiter *arb, cpContact *contacts, int numContacts);
|
81
|
+
// Precalculate values used by the solver.
|
82
|
+
void cpArbiterPreStep(cpArbiter *arb, cpFloat dt_inv);
|
83
|
+
void cpArbiterApplyCachedImpulse(cpArbiter *arb);
|
84
|
+
// Run an iteration of the solver on the arbiter.
|
85
|
+
void cpArbiterApplyImpulse(cpArbiter *arb, cpFloat eCoef);
|
@@ -0,0 +1,114 @@
|
|
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
|
+
#include <stdlib.h>
|
23
|
+
#include <string.h>
|
24
|
+
|
25
|
+
#include "chipmunk.h"
|
26
|
+
|
27
|
+
|
28
|
+
//#define CP_ARRAY_INCREMENT 10
|
29
|
+
|
30
|
+
// NOTE: cpArray is rarely used and will probably go away.
|
31
|
+
|
32
|
+
cpArray*
|
33
|
+
cpArrayAlloc(void)
|
34
|
+
{
|
35
|
+
return (cpArray *)calloc(1, sizeof(cpArray));
|
36
|
+
}
|
37
|
+
|
38
|
+
cpArray*
|
39
|
+
cpArrayInit(cpArray *arr, int size)
|
40
|
+
{
|
41
|
+
arr->num = 0;
|
42
|
+
|
43
|
+
size = (size ? size : 4);
|
44
|
+
arr->max = size;
|
45
|
+
arr->arr = (void **)malloc(size*sizeof(void**));
|
46
|
+
|
47
|
+
return arr;
|
48
|
+
}
|
49
|
+
|
50
|
+
cpArray*
|
51
|
+
cpArrayNew(int size)
|
52
|
+
{
|
53
|
+
return cpArrayInit(cpArrayAlloc(), size);
|
54
|
+
}
|
55
|
+
|
56
|
+
void
|
57
|
+
cpArrayDestroy(cpArray *arr)
|
58
|
+
{
|
59
|
+
free(arr->arr);
|
60
|
+
}
|
61
|
+
|
62
|
+
void
|
63
|
+
cpArrayFree(cpArray *arr)
|
64
|
+
{
|
65
|
+
if(!arr) return;
|
66
|
+
cpArrayDestroy(arr);
|
67
|
+
free(arr);
|
68
|
+
}
|
69
|
+
|
70
|
+
void
|
71
|
+
cpArrayPush(cpArray *arr, void *object)
|
72
|
+
{
|
73
|
+
if(arr->num == arr->max){
|
74
|
+
arr->max *= 2;
|
75
|
+
arr->arr = (void **)realloc(arr->arr, arr->max*sizeof(void**));
|
76
|
+
}
|
77
|
+
|
78
|
+
arr->arr[arr->num] = object;
|
79
|
+
arr->num++;
|
80
|
+
}
|
81
|
+
|
82
|
+
void
|
83
|
+
cpArrayDeleteIndex(cpArray *arr, int index)
|
84
|
+
{
|
85
|
+
int last = --arr->num;
|
86
|
+
arr->arr[index] = arr->arr[last];
|
87
|
+
}
|
88
|
+
|
89
|
+
void
|
90
|
+
cpArrayDeleteObj(cpArray *arr, void *obj)
|
91
|
+
{
|
92
|
+
for(int i=0; i<arr->num; i++){
|
93
|
+
if(arr->arr[i] == obj){
|
94
|
+
cpArrayDeleteIndex(arr, i);
|
95
|
+
return;
|
96
|
+
}
|
97
|
+
}
|
98
|
+
}
|
99
|
+
|
100
|
+
void
|
101
|
+
cpArrayEach(cpArray *arr, cpArrayIter iterFunc, void *data)
|
102
|
+
{
|
103
|
+
for(int i=0; i<arr->num; i++)
|
104
|
+
iterFunc(arr->arr[i], data);
|
105
|
+
}
|
106
|
+
|
107
|
+
int
|
108
|
+
cpArrayContains(cpArray *arr, void *ptr)
|
109
|
+
{
|
110
|
+
for(int i=0; i<arr->num; i++)
|
111
|
+
if(arr->arr[i] == ptr) return 1;
|
112
|
+
|
113
|
+
return 0;
|
114
|
+
}
|
@@ -0,0 +1,45 @@
|
|
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
|
+
// NOTE: cpArray is rarely used and will probably go away.
|
23
|
+
|
24
|
+
typedef struct cpArray{
|
25
|
+
int num, max;
|
26
|
+
void **arr;
|
27
|
+
} cpArray;
|
28
|
+
|
29
|
+
typedef void (*cpArrayIter)(void *ptr, void *data);
|
30
|
+
|
31
|
+
cpArray *cpArrayAlloc(void);
|
32
|
+
cpArray *cpArrayInit(cpArray *arr, int size);
|
33
|
+
cpArray *cpArrayNew(int size);
|
34
|
+
|
35
|
+
void cpArrayDestroy(cpArray *arr);
|
36
|
+
void cpArrayFree(cpArray *arr);
|
37
|
+
|
38
|
+
void cpArrayClear(cpArray *arr);
|
39
|
+
|
40
|
+
void cpArrayPush(cpArray *arr, void *object);
|
41
|
+
void cpArrayDeleteIndex(cpArray *arr, int index);
|
42
|
+
void cpArrayDeleteObj(cpArray *arr, void *obj);
|
43
|
+
|
44
|
+
void cpArrayEach(cpArray *arr, cpArrayIter iterFunc, void *data);
|
45
|
+
int cpArrayContains(cpArray *arr, void *ptr);
|
data/ext/chipmunk/cpBB.c
ADDED
@@ -0,0 +1,46 @@
|
|
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
|
+
#include <math.h>
|
23
|
+
|
24
|
+
#include "chipmunk.h"
|
25
|
+
|
26
|
+
cpVect
|
27
|
+
cpBBClampVect(const cpBB bb, const cpVect v)
|
28
|
+
{
|
29
|
+
cpFloat x = cpfmin(cpfmax(bb.l, v.x), bb.r);
|
30
|
+
cpFloat y = cpfmin(cpfmax(bb.b, v.y), bb.t);
|
31
|
+
return cpv(x, y);
|
32
|
+
}
|
33
|
+
|
34
|
+
cpVect
|
35
|
+
cpBBWrapVect(const cpBB bb, const cpVect v)
|
36
|
+
{
|
37
|
+
cpFloat ix = fabsf(bb.r - bb.l);
|
38
|
+
cpFloat modx = fmodf(v.x - bb.l, ix);
|
39
|
+
cpFloat x = (modx > 0.0f) ? modx : modx + ix;
|
40
|
+
|
41
|
+
cpFloat iy = fabsf(bb.t - bb.b);
|
42
|
+
cpFloat mody = fmodf(v.y - bb.b, iy);
|
43
|
+
cpFloat y = (mody > 0.0f) ? mody : mody + iy;
|
44
|
+
|
45
|
+
return cpv(x + bb.l, y + bb.b);
|
46
|
+
}
|
data/ext/chipmunk/cpBB.h
ADDED
@@ -0,0 +1,53 @@
|
|
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
|
+
typedef struct cpBB{
|
23
|
+
cpFloat l, b, r ,t;
|
24
|
+
} cpBB;
|
25
|
+
|
26
|
+
static inline cpBB
|
27
|
+
cpBBNew(const cpFloat l, const cpFloat b,
|
28
|
+
const cpFloat r, const cpFloat t)
|
29
|
+
{
|
30
|
+
cpBB bb = {l, b, r, t};
|
31
|
+
return bb;
|
32
|
+
}
|
33
|
+
|
34
|
+
static inline int
|
35
|
+
cpBBintersects(const cpBB a, const cpBB b)
|
36
|
+
{
|
37
|
+
return (a.l<=b.r && b.l<=a.r && a.b<=b.t && b.b<=a.t);
|
38
|
+
}
|
39
|
+
|
40
|
+
static inline int
|
41
|
+
cpBBcontainsBB(const cpBB bb, const cpBB other)
|
42
|
+
{
|
43
|
+
return (bb.l < other.l && bb.r > other.r && bb.b < other.b && bb.t > other.t);
|
44
|
+
}
|
45
|
+
|
46
|
+
static inline int
|
47
|
+
cpBBcontainsVect(const cpBB bb, const cpVect v)
|
48
|
+
{
|
49
|
+
return (bb.l < v.x && bb.r > v.x && bb.b < v.y && bb.t > v.y);
|
50
|
+
}
|
51
|
+
|
52
|
+
cpVect cpBBClampVect(const cpBB bb, const cpVect v); // clamps the vector to lie within the bbox
|
53
|
+
cpVect cpBBWrapVect(const cpBB bb, const cpVect v); // wrap a vector to a bbox
|
@@ -0,0 +1,180 @@
|
|
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
|
+
#include <stdlib.h>
|
23
|
+
#include <math.h>
|
24
|
+
#include <float.h>
|
25
|
+
|
26
|
+
#include "chipmunk.h"
|
27
|
+
|
28
|
+
cpBody*
|
29
|
+
cpBodyAlloc(void)
|
30
|
+
{
|
31
|
+
return (cpBody *)malloc(sizeof(cpBody));
|
32
|
+
}
|
33
|
+
|
34
|
+
cpBody*
|
35
|
+
cpBodyInit(cpBody *body, cpFloat m, cpFloat i)
|
36
|
+
{
|
37
|
+
body->velocity_func = cpBodyUpdateVelocity;
|
38
|
+
body->position_func = cpBodyUpdatePosition;
|
39
|
+
|
40
|
+
cpBodySetMass(body, m);
|
41
|
+
cpBodySetMoment(body, i);
|
42
|
+
|
43
|
+
body->p = cpvzero;
|
44
|
+
body->v = cpvzero;
|
45
|
+
body->f = cpvzero;
|
46
|
+
|
47
|
+
cpBodySetAngle(body, 0.0f);
|
48
|
+
body->w = 0.0f;
|
49
|
+
body->t = 0.0f;
|
50
|
+
|
51
|
+
body->v_bias = cpvzero;
|
52
|
+
body->w_bias = 0.0f;
|
53
|
+
|
54
|
+
body->data = NULL;
|
55
|
+
// body->active = 1;
|
56
|
+
|
57
|
+
return body;
|
58
|
+
}
|
59
|
+
|
60
|
+
cpBody*
|
61
|
+
cpBodyNew(cpFloat m, cpFloat i)
|
62
|
+
{
|
63
|
+
return cpBodyInit(cpBodyAlloc(), m, i);
|
64
|
+
}
|
65
|
+
|
66
|
+
void cpBodyDestroy(cpBody *body){}
|
67
|
+
|
68
|
+
void
|
69
|
+
cpBodyFree(cpBody *body)
|
70
|
+
{
|
71
|
+
if(body) cpBodyDestroy(body);
|
72
|
+
free(body);
|
73
|
+
}
|
74
|
+
|
75
|
+
void
|
76
|
+
cpBodySetMass(cpBody *body, cpFloat m)
|
77
|
+
{
|
78
|
+
body->m = m;
|
79
|
+
body->m_inv = 1.0f/m;
|
80
|
+
}
|
81
|
+
|
82
|
+
void
|
83
|
+
cpBodySetMoment(cpBody *body, cpFloat i)
|
84
|
+
{
|
85
|
+
body->i = i;
|
86
|
+
body->i_inv = 1.0f/i;
|
87
|
+
}
|
88
|
+
|
89
|
+
void
|
90
|
+
cpBodySetAngle(cpBody *body, cpFloat a)
|
91
|
+
{
|
92
|
+
body->a = fmod(a, (cpFloat)M_PI*2.0f);
|
93
|
+
body->rot = cpvforangle(a);
|
94
|
+
}
|
95
|
+
|
96
|
+
void
|
97
|
+
cpBodySlew(cpBody *body, cpVect pos, cpFloat dt)
|
98
|
+
{
|
99
|
+
cpVect delta = cpvsub(pos, body->p);
|
100
|
+
body->v = cpvmult(delta, 1.0/dt);
|
101
|
+
}
|
102
|
+
|
103
|
+
void
|
104
|
+
cpBodyUpdateVelocity(cpBody *body, cpVect gravity, cpFloat damping, cpFloat dt)
|
105
|
+
{
|
106
|
+
body->v = cpvadd(cpvmult(body->v, damping), cpvmult(cpvadd(gravity, cpvmult(body->f, body->m_inv)), dt));
|
107
|
+
body->w = body->w*damping + body->t*body->i_inv*dt;
|
108
|
+
}
|
109
|
+
|
110
|
+
void
|
111
|
+
cpBodyUpdatePosition(cpBody *body, cpFloat dt)
|
112
|
+
{
|
113
|
+
body->p = cpvadd(body->p, cpvmult(cpvadd(body->v, body->v_bias), dt));
|
114
|
+
cpBodySetAngle(body, body->a + (body->w + body->w_bias)*dt);
|
115
|
+
|
116
|
+
body->v_bias = cpvzero;
|
117
|
+
body->w_bias = 0.0f;
|
118
|
+
}
|
119
|
+
|
120
|
+
void
|
121
|
+
cpBodyResetForces(cpBody *body)
|
122
|
+
{
|
123
|
+
body->f = cpvzero;
|
124
|
+
body->t = 0.0f;
|
125
|
+
}
|
126
|
+
|
127
|
+
void
|
128
|
+
cpBodyApplyForce(cpBody *body, cpVect f, cpVect r)
|
129
|
+
{
|
130
|
+
body->f = cpvadd(body->f, f);
|
131
|
+
body->t += cpvcross(r, f);
|
132
|
+
}
|
133
|
+
|
134
|
+
void
|
135
|
+
cpDampedSpring(cpBody *a, cpBody *b, cpVect anchr1, cpVect anchr2, cpFloat rlen, cpFloat k, cpFloat dmp, cpFloat dt)
|
136
|
+
{
|
137
|
+
// Calculate the world space anchor coordinates.
|
138
|
+
cpVect r1 = cpvrotate(anchr1, a->rot);
|
139
|
+
cpVect r2 = cpvrotate(anchr2, b->rot);
|
140
|
+
|
141
|
+
cpVect delta = cpvsub(cpvadd(b->p, r2), cpvadd(a->p, r1));
|
142
|
+
cpFloat dist = cpvlength(delta);
|
143
|
+
cpVect n = dist ? cpvmult(delta, 1.0f/dist) : cpvzero;
|
144
|
+
|
145
|
+
cpFloat f_spring = (dist - rlen)*k;
|
146
|
+
|
147
|
+
// Calculate the world relative velocities of the anchor points.
|
148
|
+
cpVect v1 = cpvadd(a->v, cpvmult(cpvperp(r1), a->w));
|
149
|
+
cpVect v2 = cpvadd(b->v, cpvmult(cpvperp(r2), b->w));
|
150
|
+
|
151
|
+
// Calculate the damping force.
|
152
|
+
// This really should be in the impulse solver and can produce problems when using large damping values.
|
153
|
+
cpFloat vrn = cpvdot(cpvsub(v2, v1), n);
|
154
|
+
cpFloat f_damp = vrn*cpfmin(dmp, 1.0f/(dt*(a->m_inv + b->m_inv)));
|
155
|
+
|
156
|
+
// Apply!
|
157
|
+
cpVect f = cpvmult(n, f_spring + f_damp);
|
158
|
+
cpBodyApplyForce(a, f, r1);
|
159
|
+
cpBodyApplyForce(b, cpvneg(f), r2);
|
160
|
+
}
|
161
|
+
|
162
|
+
//int
|
163
|
+
//cpBodyMarkLowEnergy(cpBody *body, cpFloat dvsq, int max)
|
164
|
+
//{
|
165
|
+
// cpFloat ke = body->m*cpvdot(body->v, body->v);
|
166
|
+
// cpFloat re = body->i*body->w*body->w;
|
167
|
+
//
|
168
|
+
// if(ke + re > body->m*dvsq)
|
169
|
+
// body->active = 1;
|
170
|
+
// else if(body->active)
|
171
|
+
// body->active = (body->active + 1)%(max + 1);
|
172
|
+
// else {
|
173
|
+
// body->v = cpvzero;
|
174
|
+
// body->v_bias = cpvzero;
|
175
|
+
// body->w = 0.0f;
|
176
|
+
// body->w_bias = 0.0f;
|
177
|
+
// }
|
178
|
+
//
|
179
|
+
// return body->active;
|
180
|
+
//}
|