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 <string.h>
23
24 #include "chipmunk/chipmunk.h"
25
26 #include "ChipmunkDemo.h"
27
28 #define DENSITY (1.0/10000.0)
29
30 #define MAX_VERTEXES_PER_VORONOI 16
31
32 struct WorleyContex {
33 uint32_t seed;
34 cpFloat cellSize;
35 int width, height;
36 cpBB bb;
37 cpVect focus;
38 };
39
40 static inline cpVect
HashVect(uint32_t x,uint32_t y,uint32_t seed)41 HashVect(uint32_t x, uint32_t y, uint32_t seed)
42 {
43 // cpFloat border = 0.21f;
44 cpFloat border = 0.05f;
45 uint32_t h = (x*1640531513 ^ y*2654435789) + seed;
46
47 return cpv(
48 cpflerp(border, 1.0f - border, (cpFloat)( h & 0xFFFF)/(cpFloat)0xFFFF),
49 cpflerp(border, 1.0f - border, (cpFloat)((h>>16) & 0xFFFF)/(cpFloat)0xFFFF)
50 );
51 }
52
53 static cpVect
WorleyPoint(int i,int j,struct WorleyContex * context)54 WorleyPoint(int i, int j, struct WorleyContex *context)
55 {
56 cpFloat size = context->cellSize;
57 int width = context->width;
58 int height = context->height;
59 cpBB bb = context->bb;
60
61 // cpVect fv = cpv(0.5, 0.5);
62 cpVect fv = HashVect(i, j, context->seed);
63
64 return cpv(
65 cpflerp(bb.l, bb.r, 0.5f) + size*(i + fv.x - width*0.5f),
66 cpflerp(bb.b, bb.t, 0.5f) + size*(j + fv.y - height*0.5f)
67 );
68 }
69
70 static int
ClipCell(cpShape * shape,cpVect center,int i,int j,struct WorleyContex * context,cpVect * verts,cpVect * clipped,int count)71 ClipCell(cpShape *shape, cpVect center, int i, int j, struct WorleyContex *context, cpVect *verts, cpVect *clipped, int count)
72 {
73 cpVect other = WorleyPoint(i, j, context);
74 // printf(" other %dx%d: (% 5.2f, % 5.2f) ", i, j, other.x, other.y);
75 if(cpShapePointQuery(shape, other, NULL) > 0.0f){
76 // printf("excluded\n");
77 memcpy(clipped, verts, count*sizeof(cpVect));
78 return count;
79 } else {
80 // printf("clipped\n");
81 }
82
83 cpVect n = cpvsub(other, center);
84 cpFloat dist = cpvdot(n, cpvlerp(center, other, 0.5f));
85
86 int clipped_count = 0;
87 for(int j=0, i=count-1; j<count; i=j, j++){
88 cpVect a = verts[i];
89 cpFloat a_dist = cpvdot(a, n) - dist;
90
91 if(a_dist <= 0.0){
92 clipped[clipped_count] = a;
93 clipped_count++;
94 }
95
96 cpVect b = verts[j];
97 cpFloat b_dist = cpvdot(b, n) - dist;
98
99 if(a_dist*b_dist < 0.0f){
100 cpFloat t = cpfabs(a_dist)/(cpfabs(a_dist) + cpfabs(b_dist));
101
102 clipped[clipped_count] = cpvlerp(a, b, t);
103 clipped_count++;
104 }
105 }
106
107 return clipped_count;
108 }
109
110 static void
ShatterCell(cpSpace * space,cpShape * shape,cpVect cell,int cell_i,int cell_j,struct WorleyContex * context)111 ShatterCell(cpSpace *space, cpShape *shape, cpVect cell, int cell_i, int cell_j, struct WorleyContex *context)
112 {
113 // printf("cell %dx%d: (% 5.2f, % 5.2f)\n", cell_i, cell_j, cell.x, cell.y);
114
115 cpBody *body = cpShapeGetBody(shape);
116
117 cpVect *ping = (cpVect *)alloca(MAX_VERTEXES_PER_VORONOI*sizeof(cpVect));
118 cpVect *pong = (cpVect *)alloca(MAX_VERTEXES_PER_VORONOI*sizeof(cpVect));
119
120 int count = cpPolyShapeGetCount(shape);
121 count = (count > MAX_VERTEXES_PER_VORONOI ? MAX_VERTEXES_PER_VORONOI : count);
122
123 for(int i=0; i<count; i++){
124 ping[i] = cpBodyLocalToWorld(body, cpPolyShapeGetVert(shape, i));
125 }
126
127 for(int i=0; i<context->width; i++){
128 for(int j=0; j<context->height; j++){
129 if(
130 !(i == cell_i && j == cell_j) &&
131 cpShapePointQuery(shape, cell, NULL) < 0.0f
132 ){
133 count = ClipCell(shape, cell, i, j, context, ping, pong, count);
134 memcpy(ping, pong, count*sizeof(cpVect));
135 }
136 }
137 }
138
139 cpVect centroid = cpCentroidForPoly(count, ping);
140 cpFloat mass = cpAreaForPoly(count, ping, 0.0f)*DENSITY;
141 cpFloat moment = cpMomentForPoly(mass, count, ping, cpvneg(centroid), 0.0f);
142
143 cpBody *new_body = cpSpaceAddBody(space, cpBodyNew(mass, moment));
144 cpBodySetPosition(new_body, centroid);
145 cpBodySetVelocity(new_body, cpBodyGetVelocityAtWorldPoint(body, centroid));
146 cpBodySetAngularVelocity(new_body, cpBodyGetAngularVelocity(body));
147
148 cpTransform transform = cpTransformTranslate(cpvneg(centroid));
149 cpShape *new_shape = cpSpaceAddShape(space, cpPolyShapeNew(new_body, count, ping, transform, 0.0));
150 // Copy whatever properties you have set on the original shape that are important
151 cpShapeSetFriction(new_shape, cpShapeGetFriction(shape));
152 }
153
154 static void
ShatterShape(cpSpace * space,cpShape * shape,cpFloat cellSize,cpVect focus)155 ShatterShape(cpSpace *space, cpShape *shape, cpFloat cellSize, cpVect focus)
156 {
157 cpSpaceRemoveShape(space, shape);
158 cpSpaceRemoveBody(space, cpShapeGetBody(shape));
159
160 cpBB bb = cpShapeGetBB(shape);
161 int width = (int)((bb.r - bb.l)/cellSize) + 1;
162 int height = (int)((bb.t - bb.b)/cellSize) + 1;
163 // printf("Splitting as %dx%d\n", width, height);
164 struct WorleyContex context = {rand(), cellSize, width, height, bb, focus};
165
166 for(int i=0; i<context.width; i++){
167 for(int j=0; j<context.height; j++){
168 cpVect cell = WorleyPoint(i, j, &context);
169 if(cpShapePointQuery(shape, cell, NULL) < 0.0f){
170 ShatterCell(space, shape, cell, i, j, &context);
171 }
172 }
173 }
174
175 cpBodyFree(cpShapeGetBody(shape));
176 cpShapeFree(shape);
177 }
178
179 static void
update(cpSpace * space,double dt)180 update(cpSpace *space, double dt)
181 {
182 cpSpaceStep(space, dt);
183
184 if(ChipmunkDemoRightDown){
185 cpPointQueryInfo info;
186 if(cpSpacePointQueryNearest(space, ChipmunkDemoMouse, 0, GRAB_FILTER, &info)){
187 cpBB bb = cpShapeGetBB(info.shape);
188 cpFloat cell_size = cpfmax(bb.r - bb.l, bb.t - bb.b)/5.0f;
189 if(cell_size > 5.0f){
190 ShatterShape(space, (cpShape *)info.shape, cell_size, ChipmunkDemoMouse);
191 } else {
192 // printf("Too small to splinter %f\n", cell_size);
193 }
194 }
195 }
196 }
197
198 static cpSpace *
init(void)199 init(void)
200 {
201 ChipmunkDemoMessageString = "Right click something to shatter it.";
202
203 cpSpace *space = cpSpaceNew();
204 cpSpaceSetIterations(space, 30);
205 cpSpaceSetGravity(space, cpv(0, -500));
206 cpSpaceSetSleepTimeThreshold(space, 0.5f);
207 cpSpaceSetCollisionSlop(space, 0.5f);
208
209 cpBody *body, *staticBody = cpSpaceGetStaticBody(space);
210 cpShape *shape;
211
212 // Create segments around the edge of the screen.
213 shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-1000, -240), cpv( 1000, -240), 0.0f));
214 cpShapeSetElasticity(shape, 1.0f);
215 cpShapeSetFriction(shape, 1.0f);
216 cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);
217
218 cpFloat width = 200.0f;
219 cpFloat height = 200.0f;
220 cpFloat mass = width*height*DENSITY;
221 cpFloat moment = cpMomentForBox(mass, width, height);
222
223 body = cpSpaceAddBody(space, cpBodyNew(mass, moment));
224
225 shape = cpSpaceAddShape(space, cpBoxShapeNew(body, width, height, 0.0));
226 cpShapeSetFriction(shape, 0.6f);
227
228 return space;
229 }
230
231 static void
destroy(cpSpace * space)232 destroy(cpSpace *space)
233 {
234 ChipmunkDemoFreeSpaceChildren(space);
235 cpSpaceFree(space);
236 }
237
238 ChipmunkDemo Shatter = {
239 "Shatter.",
240 1.0f/60.0f,
241 init,
242 update,
243 ChipmunkDemoDefaultDrawImpl,
244 destroy,
245 };
246