annotate trunk/chipmunkd/cpBody.d @ 23:4ceef5833c8c

updated to chipmunk 5.3.3
author Extrawurst
date Fri, 10 Dec 2010 02:10:27 +0100
parents df4ebc8add66
children 4541ca17975b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
1
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
2 // written in the D programming language
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
3
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
4 module chipmunkd.cpBody;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
5
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
6 import chipmunkd.chipmunk;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
7
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
8 alias void function(cpBody *_body, cpVect gravity, cpFloat damping, cpFloat dt)cpBodyVelocityFunc;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
9 alias void function(cpBody *_body, cpFloat dt)cpBodyPositionFunc;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
10
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
11 //extern cpBodyVelocityFunc cpBodyUpdateVelocityDefault;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
12 //extern cpBodyPositionFunc cpBodyUpdatePositionDefault;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
13
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
14 // Structure to hold information about the contact graph components
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
15 // when putting groups of objects to sleep.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
16 // No interesting user accessible fields.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
17 struct cpComponentNode {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
18 cpBody *parent;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
19 cpBody *next;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
20 int rank;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
21 cpFloat idleTime;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
22 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
23
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
24 struct cpBody{
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
25 // *** Integration Functions.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
26
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
27 // Function that is called to integrate the body's velocity. (Defaults to cpBodyUpdateVelocity)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
28 cpBodyVelocityFunc velocity_func;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
29
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
30 // Function that is called to integrate the body's position. (Defaults to cpBodyUpdatePosition)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
31 cpBodyPositionFunc position_func;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
32
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
33 // *** Mass Properties
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
34
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
35 // Mass and it's inverse.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
36 // Always use cpBodySetMass() whenever changing the mass as these values must agree.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
37 cpFloat m, m_inv;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
38
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
39 // Moment of inertia and it's inverse.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
40 // Always use cpBodySetMoment() whenever changing the moment as these values must agree.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
41 cpFloat i, i_inv;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
42
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
43 // *** Positional Properties
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
44
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
45 // Linear components of motion (position, velocity, and force)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
46 cpVect p, v, f;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
47
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
48 // Angular components of motion (angle, angular velocity, and torque)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
49 // Always use cpBodySetAngle() to set the angle of the body as a and rot must agree.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
50 cpFloat a, w, t;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
51
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
52 // Cached unit length vector representing the angle of the body.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
53 // Used for fast vector rotation using cpvrotate().
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
54 cpVect rot;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
55
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
56 // *** User Definable Fields
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
57
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
58 // User defined data pointer.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
59 cpDataPointer data;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
60
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
61 // *** Other Fields
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
62
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
63 // Maximum velocities this body can move at after integrating velocity
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
64 cpFloat v_limit, w_limit;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
65
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
66 // *** Internally Used Fields
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
67
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
68 // Velocity bias values used when solving penetrations and correcting constraints.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
69 cpVect v_bias;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
70 cpFloat w_bias;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
71
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
72 // Space this body has been added to
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
73 cpSpace* space;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
74
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
75 // Pointer to the shape list.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
76 // Shapes form a linked list using cpShape.next when added to a space.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
77 cpShape *shapesList;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
78
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
79 // Used by cpSpaceStep() to store contact graph information.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
80 cpComponentNode node;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
81 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
82
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
83 //// Basic allocation/destruction functions
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
84 //cpBody *cpBodyAlloc(void);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
85 //cpBody *cpBodyInit(cpBody *body, cpFloat m, cpFloat i);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
86 //cpBody *cpBodyNew(cpFloat m, cpFloat i);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
87 //
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
88 //void cpBodyDestroy(cpBody *body);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
89 //void cpBodyFree(cpBody *body);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
90 //
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
91 //// Wake up a sleeping or idle body. (defined in cpSpace.c)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
92 //void cpBodyActivate(cpBody *body);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
93 //
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
94 //// Force a body to sleep;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
95 //void cpBodySleep(cpBody *body);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
96 ////void cpBodySleepGroup(cpBody *body, ...);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
97
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
98 static cpBool
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
99 cpBodyIsSleeping(const cpBody *_body)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
100 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
101 return (_body.node.next !is null);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
102 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
103
23
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
104 static cpBool
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
105 cpBodyIsStatic(const cpBody *_body)
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
106 {
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
107 return _body.node.idleTime == INFINITY;
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
108 }
4
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
109
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
110 static cpBool
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
111 cpBodyIsRogue(const cpBody* _body)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
112 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
113 return (_body.space is null);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
114 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
115
13
c03a41d47b60 - finished all constraints properties
Extrawurst
parents: 4
diff changeset
116 //TODO
4
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
117 //#define CP_DefineBodyGetter(type, member, name) \
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
118 //static inline type cpBodyGet##name(const cpBody *body){return body.member;}
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
119 //
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
120 //#define CP_DefineBodySetter(type, member, name) \
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
121 //static inline void \
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
122 //cpBodySet##name(cpBody *body, const type value){ \
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
123 // cpBodyActivate(body); \
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
124 // body.member = value; \
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
125 //} \
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
126
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
127 //#define CP_DefineBodyProperty(type, member, name) \
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
128 //CP_DefineBodyGetter(type, member, name) \
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
129 //CP_DefineBodySetter(type, member, name)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
130
14
d88862c82f06 more properties
Extrawurst
parents: 13
diff changeset
131 template CP_DefineBodyGetter(string type,string member,string name)
d88862c82f06 more properties
Extrawurst
parents: 13
diff changeset
132 {
d88862c82f06 more properties
Extrawurst
parents: 13
diff changeset
133 enum CP_DefineBodyGetter =
d88862c82f06 more properties
Extrawurst
parents: 13
diff changeset
134 "static "~type~" BodyGet"~name~"(const cpBody *_body){"~
d88862c82f06 more properties
Extrawurst
parents: 13
diff changeset
135 "return _body."~member~";"~
d88862c82f06 more properties
Extrawurst
parents: 13
diff changeset
136 "}";
d88862c82f06 more properties
Extrawurst
parents: 13
diff changeset
137 }
d88862c82f06 more properties
Extrawurst
parents: 13
diff changeset
138
d88862c82f06 more properties
Extrawurst
parents: 13
diff changeset
139 template CP_DefineBodySetter(string type,string member,string name)
d88862c82f06 more properties
Extrawurst
parents: 13
diff changeset
140 {
d88862c82f06 more properties
Extrawurst
parents: 13
diff changeset
141 enum CP_DefineBodySetter =
d88862c82f06 more properties
Extrawurst
parents: 13
diff changeset
142 "static void BodySet"~name~"(cpBody *_body, const "~type~" value){"~
d88862c82f06 more properties
Extrawurst
parents: 13
diff changeset
143 "cpBodyActivate(_body);"~
d88862c82f06 more properties
Extrawurst
parents: 13
diff changeset
144 "_body."~member~" = value;"~
d88862c82f06 more properties
Extrawurst
parents: 13
diff changeset
145 "}";
d88862c82f06 more properties
Extrawurst
parents: 13
diff changeset
146 }
d88862c82f06 more properties
Extrawurst
parents: 13
diff changeset
147
d88862c82f06 more properties
Extrawurst
parents: 13
diff changeset
148 template CP_DefineBodyProperty(string type,string member,string name)
d88862c82f06 more properties
Extrawurst
parents: 13
diff changeset
149 {
d88862c82f06 more properties
Extrawurst
parents: 13
diff changeset
150 enum CP_DefineBodyProperty =
d88862c82f06 more properties
Extrawurst
parents: 13
diff changeset
151 CP_DefineBodyGetter!(type,member,name)~CP_DefineBodySetter!(type,member,name);
d88862c82f06 more properties
Extrawurst
parents: 13
diff changeset
152
d88862c82f06 more properties
Extrawurst
parents: 13
diff changeset
153 }
4
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
154
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
155 //// Accessors for cpBody struct members
14
d88862c82f06 more properties
Extrawurst
parents: 13
diff changeset
156 mixin(CP_DefineBodyGetter!("cpFloat","m","Mass"));
4
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
157 //void cpBodySetMass(cpBody *body, cpFloat m);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
158 //
14
d88862c82f06 more properties
Extrawurst
parents: 13
diff changeset
159 mixin(CP_DefineBodyGetter!("cpFloat","i","Moment"));
4
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
160 //void cpBodySetMoment(cpBody *body, cpFloat i);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
161 //
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
162 //
14
d88862c82f06 more properties
Extrawurst
parents: 13
diff changeset
163
d88862c82f06 more properties
Extrawurst
parents: 13
diff changeset
164 mixin(CP_DefineBodyProperty!("cpVect","p","Pos"));
d88862c82f06 more properties
Extrawurst
parents: 13
diff changeset
165 mixin(CP_DefineBodyProperty!("cpVect","v","Vel"));
d88862c82f06 more properties
Extrawurst
parents: 13
diff changeset
166 mixin(CP_DefineBodyProperty!("cpVect","f","Force"));
d88862c82f06 more properties
Extrawurst
parents: 13
diff changeset
167 mixin(CP_DefineBodyProperty!("cpFloat","a","Angle"));
d88862c82f06 more properties
Extrawurst
parents: 13
diff changeset
168 mixin(CP_DefineBodyProperty!("cpFloat","w","AngVel"));
d88862c82f06 more properties
Extrawurst
parents: 13
diff changeset
169 mixin(CP_DefineBodyProperty!("cpFloat","t","Torque"));
d88862c82f06 more properties
Extrawurst
parents: 13
diff changeset
170 mixin(CP_DefineBodyProperty!("cpVect","rot","Rot"));
d88862c82f06 more properties
Extrawurst
parents: 13
diff changeset
171 mixin(CP_DefineBodyProperty!("cpFloat","v_limit","VelLimit"));
d88862c82f06 more properties
Extrawurst
parents: 13
diff changeset
172 mixin(CP_DefineBodyProperty!("cpFloat","w_limit","AngVelLimit"));
4
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
173
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
174 //// Modify the velocity of the body so that it will move to the specified absolute coordinates in the next timestep.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
175 //// Intended for objects that are moved manually with a custom velocity integration function.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
176 //void cpBodySlew(cpBody *body, cpVect pos, cpFloat dt);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
177 //
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
178 //// Default Integration functions.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
179 //void cpBodyUpdateVelocity(cpBody *body, cpVect gravity, cpFloat damping, cpFloat dt);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
180 //void cpBodyUpdatePosition(cpBody *body, cpFloat dt);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
181
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
182 // Convert body local to world coordinates
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
183 static cpVect
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
184 cpBodyLocal2World(const cpBody *_body, const cpVect v)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
185 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
186 return cpvadd(_body.p, cpvrotate(v, _body.rot));
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
187 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
188
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
189 // Convert world to body local coordinates
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
190 static cpVect
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
191 cpBodyWorld2Local(const cpBody *_body, const cpVect v)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
192 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
193 return cpvunrotate(cpvsub(v, _body.p), _body.rot);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
194 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
195
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
196 // Apply an impulse (in world coordinates) to the body at a point relative to the center of gravity (also in world coordinates).
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
197 static void
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
198 cpBodyApplyImpulse(cpBody *_body, const cpVect j, const cpVect r)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
199 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
200 _body.v = cpvadd(_body.v, cpvmult(j, _body.m_inv));
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
201 _body.w += _body.i_inv*cpvcross(r, j);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
202 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
203
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
204 //// Zero the forces on a body.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
205 //void cpBodyResetForces(cpBody *body);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
206 //// Apply a force (in world coordinates) to a body at a point relative to the center of gravity (also in world coordinates).
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
207 //void cpBodyApplyForce(cpBody *body, const cpVect f, const cpVect r);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
208
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
209 static cpFloat
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
210 cpBodyKineticEnergy(const cpBody *_body)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
211 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
212 // Need to do some fudging to avoid NaNs
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
213 cpFloat vsq = cpvdot(_body.v, _body.v);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
214 cpFloat wsq = _body.w*_body.w;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
215 return (vsq ? vsq*_body.m : 0.0f) + (wsq ? wsq*_body.i : 0.0f);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
216 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
217
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
218 //// Apply a damped spring force between two bodies.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
219 //// Warning: Large damping values can be unstable. Use a cpDampedSpring constraint for this instead.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
220 //void cpApplyDampedSpring(cpBody *a, cpBody *b, cpVect anchr1, cpVect anchr2, cpFloat rlen, cpFloat k, cpFloat dmp, cpFloat dt);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
221
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
222 // initialized in cpInitChipmunk()
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
223 cpBody cpStaticBodySingleton;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
224
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
225 cpBody*
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
226 cpBodyAlloc()
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
227 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
228 return cast(cpBody *)cpmalloc(cpBody.sizeof);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
229 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
230
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
231 cpBodyVelocityFunc cpBodyUpdateVelocityDefault = &cpBodyUpdateVelocity;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
232 cpBodyPositionFunc cpBodyUpdatePositionDefault = &cpBodyUpdatePosition;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
233
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
234 cpBody*
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
235 cpBodyInit(cpBody *_body, cpFloat m, cpFloat i)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
236 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
237 _body.velocity_func = cpBodyUpdateVelocityDefault;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
238 _body.position_func = cpBodyUpdatePositionDefault;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
239
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
240 cpBodySetMass(_body, m);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
241 cpBodySetMoment(_body, i);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
242
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
243 _body.p = cpvzero;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
244 _body.v = cpvzero;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
245 _body.f = cpvzero;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
246
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
247 cpBodySetAngle(_body, 0.0f);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
248 _body.w = 0.0f;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
249 _body.t = 0.0f;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
250
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
251 _body.v_bias = cpvzero;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
252 _body.w_bias = 0.0f;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
253
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
254 _body.data = null;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
255 _body.v_limit = INFINITY;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
256 _body.w_limit = INFINITY;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
257
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
258 _body.space = null;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
259 _body.shapesList = null;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
260
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
261 cpComponentNode node = {null, null, 0, 0.0f};
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
262 _body.node = node;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
263
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
264 return _body;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
265 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
266
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
267 cpBody*
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
268 cpBodyNew(cpFloat m, cpFloat i)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
269 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
270 return cpBodyInit(cpBodyAlloc(), m, i);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
271 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
272
23
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
273 cpBody *
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
274 cpBodyInitStatic(cpBody *_body)
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
275 {
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
276 cpBodyInit(_body, cast(cpFloat)INFINITY, cast(cpFloat)INFINITY);
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
277 _body.node.idleTime = cast(cpFloat)INFINITY;
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
278
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
279 return _body;
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
280 }
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
281
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
282 cpBody *
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
283 cpBodyNewStatic()
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
284 {
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
285 return cpBodyInitStatic(cpBodyAlloc());
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
286 }
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
287
4
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
288 void cpBodyDestroy(cpBody *_body){}
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
289
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
290 void
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
291 cpBodyFree(cpBody *_body)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
292 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
293 if(_body){
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
294 cpBodyDestroy(_body);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
295 cpfree(_body);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
296 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
297 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
298
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
299 void
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
300 cpBodySetMass(cpBody *_body, cpFloat mass)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
301 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
302 _body.m = mass;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
303 _body.m_inv = 1.0f/mass;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
304 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
305
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
306 void
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
307 cpBodySetMoment(cpBody *_body, cpFloat moment)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
308 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
309 _body.i = moment;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
310 _body.i_inv = 1.0f/moment;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
311 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
312
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
313 void
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
314 cpBodySetAngle(cpBody *_body, cpFloat angle)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
315 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
316 _body.a = angle;//fmod(a, (cpFloat)M_PI*2.0f);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
317 _body.rot = cpvforangle(angle);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
318 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
319
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
320 void
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
321 cpBodySlew(cpBody *_body, cpVect pos, cpFloat dt)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
322 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
323 cpVect delta = cpvsub(pos, _body.p);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
324 _body.v = cpvmult(delta, 1.0f/dt);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
325 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
326
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
327 void
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
328 cpBodyUpdateVelocity(cpBody *_body, cpVect gravity, cpFloat damping, cpFloat dt)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
329 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
330 _body.v = cpvclamp(
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
331 cpvadd(cpvmult(_body.v, damping), cpvmult(cpvadd(gravity, cpvmult(_body.f, _body.m_inv)), dt)),
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
332 _body.v_limit);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
333
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
334 cpFloat w_limit = _body.w_limit;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
335 _body.w = cpfclamp(_body.w*damping + _body.t*_body.i_inv*dt, -w_limit, w_limit);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
336 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
337
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
338 void
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
339 cpBodyUpdatePosition(cpBody *_body, cpFloat dt)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
340 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
341 _body.p = cpvadd(_body.p, cpvmult(cpvadd(_body.v, _body.v_bias), dt));
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
342 cpBodySetAngle(_body, _body.a + (_body.w + _body.w_bias)*dt);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
343
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
344 _body.v_bias = cpvzero;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
345 _body.w_bias = 0.0f;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
346 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
347
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
348 void
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
349 cpBodyResetForces(cpBody *_body)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
350 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
351 _body.f = cpvzero;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
352 _body.t = 0.0f;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
353 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
354
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
355 void
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
356 cpBodyApplyForce(cpBody *_body, cpVect force, cpVect r)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
357 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
358 _body.f = cpvadd(_body.f, force);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
359 _body.t += cpvcross(r, force);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
360 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
361
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
362 void
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
363 cpApplyDampedSpring(cpBody *a, cpBody *b, cpVect anchr1, cpVect anchr2, cpFloat rlen, cpFloat k, cpFloat dmp, cpFloat dt)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
364 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
365 // Calculate the world space anchor coordinates.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
366 cpVect r1 = cpvrotate(anchr1, a.rot);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
367 cpVect r2 = cpvrotate(anchr2, b.rot);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
368
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
369 cpVect delta = cpvsub(cpvadd(b.p, r2), cpvadd(a.p, r1));
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
370 cpFloat dist = cpvlength(delta);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
371 cpVect n = dist ? cpvmult(delta, 1.0f/dist) : cpvzero;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
372
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
373 cpFloat f_spring = (dist - rlen)*k;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
374
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
375 // Calculate the world relative velocities of the anchor points.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
376 cpVect v1 = cpvadd(a.v, cpvmult(cpvperp(r1), a.w));
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
377 cpVect v2 = cpvadd(b.v, cpvmult(cpvperp(r2), b.w));
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
378
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
379 // Calculate the damping force.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
380 // This really should be in the impulse solver and can produce problems when using large damping values.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
381 cpFloat vrn = cpvdot(cpvsub(v2, v1), n);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
382 cpFloat f_damp = vrn*cpfmin(dmp, 1.0f/(dt*(a.m_inv + b.m_inv)));
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
383
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
384 // Apply!
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
385 cpVect f = cpvmult(n, f_spring + f_damp);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
386 cpBodyApplyForce(a, f, r1);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
387 cpBodyApplyForce(b, cpvneg(f), r2);
23
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
388 }