| 115 | } |
| 116 | |
| 117 | static void ShatterCell(cpSpace* space, |
| 118 | cpShape* shape, |
| 119 | cpVect cell, |
| 120 | int cell_i, |
| 121 | int cell_j, |
| 122 | struct WorleyContex* context) |
| 123 | { |
| 124 | // printf("cell %dx%d: (% 5.2f, % 5.2f)\n", cell_i, cell_j, cell.x, cell.y); |
| 125 | |
| 126 | cpBody* body = cpShapeGetBody(shape); |
| 127 | |
| 128 | cpVect* ping = (cpVect*)alloca(MAX_VERTEXES_PER_VORONOI * sizeof(cpVect)); |
| 129 | cpVect* pong = (cpVect*)alloca(MAX_VERTEXES_PER_VORONOI * sizeof(cpVect)); |
| 130 | |
| 131 | int count = cpPolyShapeGetCount(shape); |
| 132 | count = (count > MAX_VERTEXES_PER_VORONOI ? MAX_VERTEXES_PER_VORONOI : count); |
| 133 | |
| 134 | for (int i = 0; i < count; i++) |
| 135 | { |
| 136 | ping[i] = cpBodyLocalToWorld(body, cpPolyShapeGetVert(shape, i)); |
| 137 | } |
| 138 | |
| 139 | for (int i = 0; i < context->width; i++) |
| 140 | { |
| 141 | for (int j = 0; j < context->height; j++) |
| 142 | { |
| 143 | if (!(i == cell_i && j == cell_j) && cpShapePointQuery(shape, cell, NULL) < 0.0f) |
| 144 | { |
| 145 | count = ClipCell(shape, cell, i, j, context, ping, pong, count); |
| 146 | memcpy(ping, pong, count * sizeof(cpVect)); |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | cpVect centroid = cpCentroidForPoly(count, ping); |
| 152 | cpFloat mass = cpAreaForPoly(count, ping, 0.0f) * DENSITY; |
| 153 | cpFloat moment = cpMomentForPoly(mass, count, ping, cpvneg(centroid), 0.0f); |
| 154 | |
| 155 | cpBody* new_body = cpSpaceAddBody(space, cpBodyNew(mass, moment)); |
| 156 | cpBodySetPosition(new_body, centroid); |
| 157 | cpBodySetVelocity(new_body, cpBodyGetVelocityAtWorldPoint(body, centroid)); |
| 158 | cpBodySetAngularVelocity(new_body, cpBodyGetAngularVelocity(body)); |
| 159 | |
| 160 | cpTransform transform = cpTransformTranslate(cpvneg(centroid)); |
| 161 | cpShape* new_shape = cpSpaceAddShape(space, cpPolyShapeNew(new_body, count, ping, transform, 0.0)); |
| 162 | // Copy whatever properties you have set on the original shape that are important |
| 163 | cpShapeSetFriction(new_shape, cpShapeGetFriction(shape)); |
| 164 | } |
| 165 | |
| 166 | static void ShatterShape(cpSpace* space, cpShape* shape, cpFloat cellSize, cpVect focus) |
| 167 | { |
no test coverage detected