MCPcopy Create free account
hub / github.com/axmolengine/axmol / ShatterCell

Function ShatterCell

tests/cpp-tests/Source/ChipmunkTestBed/demo/Shatter.cpp:117–164  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

115}
116
117static 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
166static void ShatterShape(cpSpace* space, cpShape* shape, cpFloat cellSize, cpVect focus)
167{

Callers 1

ShatterShapeFunction · 0.85

Calls 15

cpShapeGetBodyFunction · 0.85
cpPolyShapeGetCountFunction · 0.85
cpBodyLocalToWorldFunction · 0.85
cpPolyShapeGetVertFunction · 0.85
cpShapePointQueryFunction · 0.85
ClipCellFunction · 0.85
cpCentroidForPolyFunction · 0.85
cpAreaForPolyFunction · 0.85
cpMomentForPolyFunction · 0.85
cpvnegFunction · 0.85
cpSpaceAddBodyFunction · 0.85
cpBodyNewFunction · 0.85

Tested by

no test coverage detected