axmol/tests/cpp-tests/Source/ChipmunkTestBed/demo/Shatter.c

258 lines
8.2 KiB
C
Raw Normal View History

/* Copyright (c) 2007 Scott Lembcke
2021-06-30 09:52:54 +08:00
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
2021-06-30 09:52:54 +08:00
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
2021-06-30 09:52:54 +08:00
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <string.h>
#include "chipmunk/chipmunk.h"
#include "ChipmunkDemo.h"
2021-06-30 09:52:54 +08:00
#define DENSITY (1.0 / 10000.0)
#define MAX_VERTEXES_PER_VORONOI 16
2021-12-28 16:06:23 +08:00
struct WorleyContex
{
2021-06-30 09:52:54 +08:00
uint32_t seed;
cpFloat cellSize;
int width, height;
cpBB bb;
cpVect focus;
};
2021-12-28 16:06:23 +08:00
static inline cpVect HashVect(uint32_t x, uint32_t y, uint32_t seed)
{
2021-06-30 09:52:54 +08:00
// cpFloat border = 0.21f;
cpFloat border = 0.05f;
uint32_t h = (x * 1640531513 ^ y * 2654435789) + seed;
2021-12-28 16:06:23 +08:00
return cpv(cpflerp(border, 1.0f - border, (cpFloat)(h & 0xFFFF) / (cpFloat)0xFFFF),
cpflerp(border, 1.0f - border, (cpFloat)((h >> 16) & 0xFFFF) / (cpFloat)0xFFFF));
}
2021-12-28 16:06:23 +08:00
static cpVect WorleyPoint(int i, int j, struct WorleyContex* context)
{
2021-06-30 09:52:54 +08:00
cpFloat size = context->cellSize;
int width = context->width;
int height = context->height;
cpBB bb = context->bb;
// cpVect fv = cpv(0.5, 0.5);
cpVect fv = HashVect(i, j, context->seed);
return cpv(cpflerp(bb.l, bb.r, 0.5f) + size * (i + fv.x - width * 0.5f),
2021-12-28 16:06:23 +08:00
cpflerp(bb.b, bb.t, 0.5f) + size * (j + fv.y - height * 0.5f));
}
2021-12-28 16:06:23 +08:00
static int ClipCell(cpShape* shape,
cpVect center,
int i,
int j,
struct WorleyContex* context,
cpVect* verts,
cpVect* clipped,
int count)
{
2021-06-30 09:52:54 +08:00
cpVect other = WorleyPoint(i, j, context);
// printf(" other %dx%d: (% 5.2f, % 5.2f) ", i, j, other.x, other.y);
2021-12-28 16:06:23 +08:00
if (cpShapePointQuery(shape, other, NULL) > 0.0f)
{
2021-06-30 09:52:54 +08:00
// printf("excluded\n");
memcpy(clipped, verts, count * sizeof(cpVect));
return count;
2021-12-28 16:06:23 +08:00
}
else
{
2021-06-30 09:52:54 +08:00
// printf("clipped\n");
}
cpVect n = cpvsub(other, center);
cpFloat dist = cpvdot(n, cpvlerp(center, other, 0.5f));
int clipped_count = 0;
2021-12-28 16:06:23 +08:00
for (int j = 0, i = count - 1; j < count; i = j, j++)
{
2021-06-30 09:52:54 +08:00
cpVect a = verts[i];
cpFloat a_dist = cpvdot(a, n) - dist;
2021-12-28 16:06:23 +08:00
if (a_dist <= 0.0)
{
2021-06-30 09:52:54 +08:00
clipped[clipped_count] = a;
clipped_count++;
}
cpVect b = verts[j];
cpFloat b_dist = cpvdot(b, n) - dist;
2021-12-28 16:06:23 +08:00
if (a_dist * b_dist < 0.0f)
{
2021-06-30 09:52:54 +08:00
cpFloat t = cpfabs(a_dist) / (cpfabs(a_dist) + cpfabs(b_dist));
clipped[clipped_count] = cpvlerp(a, b, t);
clipped_count++;
}
}
return clipped_count;
}
2021-12-28 16:06:23 +08:00
static void ShatterCell(cpSpace* space,
cpShape* shape,
cpVect cell,
int cell_i,
int cell_j,
struct WorleyContex* context)
{
2021-06-30 09:52:54 +08:00
// printf("cell %dx%d: (% 5.2f, % 5.2f)\n", cell_i, cell_j, cell.x, cell.y);
cpBody* body = cpShapeGetBody(shape);
2021-12-28 16:06:23 +08:00
cpVect* ping = (cpVect*)alloca(MAX_VERTEXES_PER_VORONOI * sizeof(cpVect));
cpVect* pong = (cpVect*)alloca(MAX_VERTEXES_PER_VORONOI * sizeof(cpVect));
2021-06-30 09:52:54 +08:00
int count = cpPolyShapeGetCount(shape);
count = (count > MAX_VERTEXES_PER_VORONOI ? MAX_VERTEXES_PER_VORONOI : count);
2021-12-28 16:06:23 +08:00
for (int i = 0; i < count; i++)
{
2021-06-30 09:52:54 +08:00
ping[i] = cpBodyLocalToWorld(body, cpPolyShapeGetVert(shape, i));
}
2021-12-28 16:06:23 +08:00
for (int i = 0; i < context->width; i++)
{
for (int j = 0; j < context->height; j++)
{
if (!(i == cell_i && j == cell_j) && cpShapePointQuery(shape, cell, NULL) < 0.0f)
{
2021-06-30 09:52:54 +08:00
count = ClipCell(shape, cell, i, j, context, ping, pong, count);
memcpy(ping, pong, count * sizeof(cpVect));
}
}
}
cpVect centroid = cpCentroidForPoly(count, ping);
cpFloat mass = cpAreaForPoly(count, ping, 0.0f) * DENSITY;
cpFloat moment = cpMomentForPoly(mass, count, ping, cpvneg(centroid), 0.0f);
cpBody* new_body = cpSpaceAddBody(space, cpBodyNew(mass, moment));
cpBodySetPosition(new_body, centroid);
cpBodySetVelocity(new_body, cpBodyGetVelocityAtWorldPoint(body, centroid));
cpBodySetAngularVelocity(new_body, cpBodyGetAngularVelocity(body));
cpTransform transform = cpTransformTranslate(cpvneg(centroid));
cpShape* new_shape = cpSpaceAddShape(space, cpPolyShapeNew(new_body, count, ping, transform, 0.0));
// Copy whatever properties you have set on the original shape that are important
cpShapeSetFriction(new_shape, cpShapeGetFriction(shape));
}
2021-12-28 16:06:23 +08:00
static void ShatterShape(cpSpace* space, cpShape* shape, cpFloat cellSize, cpVect focus)
{
2021-06-30 09:52:54 +08:00
cpSpaceRemoveShape(space, shape);
cpSpaceRemoveBody(space, cpShapeGetBody(shape));
cpBB bb = cpShapeGetBB(shape);
2021-12-28 16:06:23 +08:00
int width = (int)((bb.r - bb.l) / cellSize) + 1;
int height = (int)((bb.t - bb.b) / cellSize) + 1;
2021-06-30 09:52:54 +08:00
// printf("Splitting as %dx%d\n", width, height);
struct WorleyContex context = {(uint32_t)rand(), cellSize, width, height, bb, focus};
2021-06-30 09:52:54 +08:00
2021-12-28 16:06:23 +08:00
for (int i = 0; i < context.width; i++)
{
for (int j = 0; j < context.height; j++)
{
2021-06-30 09:52:54 +08:00
cpVect cell = WorleyPoint(i, j, &context);
2021-12-28 16:06:23 +08:00
if (cpShapePointQuery(shape, cell, NULL) < 0.0f)
{
2021-06-30 09:52:54 +08:00
ShatterCell(space, shape, cell, i, j, &context);
}
}
}
cpBodyFree(cpShapeGetBody(shape));
cpShapeFree(shape);
}
2021-12-28 16:06:23 +08:00
static void update(cpSpace* space, double dt)
{
2021-06-30 09:52:54 +08:00
cpSpaceStep(space, dt);
2021-12-28 16:06:23 +08:00
if (ChipmunkDemoRightDown)
{
2021-06-30 09:52:54 +08:00
cpPointQueryInfo info;
2021-12-28 16:06:23 +08:00
if (cpSpacePointQueryNearest(space, ChipmunkDemoMouse, 0, GRAB_FILTER, &info))
{
2021-06-30 09:52:54 +08:00
cpBB bb = cpShapeGetBB(info.shape);
cpFloat cell_size = cpfmax(bb.r - bb.l, bb.t - bb.b) / 5.0f;
2021-12-28 16:06:23 +08:00
if (cell_size > 5.0f)
{
ShatterShape(space, (cpShape*)info.shape, cell_size, ChipmunkDemoMouse);
}
else
{
2021-06-30 09:52:54 +08:00
// printf("Too small to splinter %f\n", cell_size);
}
}
ChipmunkDemoRightDown = false;
}
}
2021-12-28 16:06:23 +08:00
static cpSpace* init(void)
{
2021-06-30 09:52:54 +08:00
ChipmunkDemoMessageString = "Right click something to shatter it.";
cpSpace* space = cpSpaceNew();
cpSpaceSetIterations(space, 30);
cpSpaceSetGravity(space, cpv(0, -500));
cpSpaceSetSleepTimeThreshold(space, 0.5f);
cpSpaceSetCollisionSlop(space, 0.5f);
cpBody *body, *staticBody = cpSpaceGetStaticBody(space);
cpShape* shape;
// Create segments around the edge of the screen.
shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-1000, -240), cpv(1000, -240), 0.0f));
cpShapeSetElasticity(shape, 1.0f);
cpShapeSetFriction(shape, 1.0f);
cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);
cpFloat width = 200.0f;
cpFloat height = 200.0f;
cpFloat mass = width * height * DENSITY;
cpFloat moment = cpMomentForBox(mass, width, height);
body = cpSpaceAddBody(space, cpBodyNew(mass, moment));
shape = cpSpaceAddShape(space, cpBoxShapeNew(body, width, height, 0.0));
cpShapeSetFriction(shape, 0.6f);
return space;
}
2021-12-28 16:06:23 +08:00
static void destroy(cpSpace* space)
{
2021-06-30 09:52:54 +08:00
ChipmunkDemoFreeSpaceChildren(space);
cpSpaceFree(space);
}
ChipmunkDemo Shatter = {
2021-12-28 16:06:23 +08:00
"Shatter.", 1.0f / 60.0f, init, update, ChipmunkDemoDefaultDrawImpl, destroy,
};