2021-06-23 23:09:48 +08:00
|
|
|
/* Copyright (c) 2007 Scott Lembcke
|
2021-06-26 17:46:06 +08:00
|
|
|
* Copyright (c) 2021 @aismann; Peter Eismann, Germany; dreifrankensoft
|
|
|
|
*
|
2021-06-23 23:09:48 +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-26 17:46:06 +08:00
|
|
|
*
|
2021-06-23 23:09:48 +08:00
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
2021-06-26 17:46:06 +08:00
|
|
|
*
|
2021-06-23 23:09:48 +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.
|
|
|
|
*/
|
2021-06-26 17:46:06 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2022-04-27 19:39:12 +08:00
|
|
|
#if !defined(_USE_MATH_DEFINES)
|
|
|
|
# define _USE_MATH_DEFINES
|
|
|
|
#endif
|
2021-06-26 17:46:06 +08:00
|
|
|
#include <math.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "chipmunk/chipmunk_private.h"
|
|
|
|
#include "chipmunk/chipmunk_unsafe.h"
|
|
|
|
#include "ChipmunkDemo.h"
|
2021-06-23 23:09:48 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
static void update(cpSpace* space, double dt)
|
|
|
|
{
|
2021-06-26 17:46:06 +08:00
|
|
|
cpSpaceStep(space, dt);
|
2021-06-23 23:09:48 +08:00
|
|
|
}
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
static cpSpace* init(void)
|
|
|
|
{
|
2021-06-26 17:46:06 +08:00
|
|
|
cpSpace* space = cpSpaceNew();
|
|
|
|
cpSpaceSetIterations(space, 5);
|
|
|
|
space->damping = 0.1;
|
2021-06-23 23:09:48 +08:00
|
|
|
|
2021-06-26 17:46:06 +08:00
|
|
|
cpShape *shape1, *shape2;
|
2021-06-23 23:09:48 +08:00
|
|
|
|
2021-06-26 17:46:06 +08:00
|
|
|
cpFloat mass = 1.0f;
|
2021-06-23 23:09:48 +08:00
|
|
|
|
2021-06-26 17:46:06 +08:00
|
|
|
{
|
|
|
|
cpFloat size = 100.0;
|
|
|
|
|
|
|
|
cpBody* body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForBox(mass, size, size)));
|
|
|
|
cpBodySetPosition(body, cpv(100.0, 50.0f));
|
|
|
|
|
|
|
|
shape1 = cpSpaceAddShape(space, cpBoxShapeNew(body, size, size, 0.0));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
cpFloat size = 100.0;
|
|
|
|
|
|
|
|
cpBody* body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForBox(mass, size, size)));
|
|
|
|
cpBodySetPosition(body, cpv(120.0, -40.0f));
|
|
|
|
cpBodySetAngle(body, 1e-2);
|
|
|
|
|
|
|
|
shape2 = cpSpaceAddShape(space, cpBoxShapeNew(body, size, size, 0.0));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
cpFloat size = 100.0;
|
|
|
|
const int NUM_VERTS = 5;
|
|
|
|
|
|
|
|
cpVect verts[NUM_VERTS];
|
2021-12-28 16:06:23 +08:00
|
|
|
for (int i = 0; i < NUM_VERTS; i++)
|
|
|
|
{
|
|
|
|
cpFloat angle = -2 * M_PI * i / ((cpFloat)NUM_VERTS);
|
2021-06-26 17:46:06 +08:00
|
|
|
verts[i] = cpv(size / 2.0 * cos(angle), size / 2.0 * sin(angle));
|
|
|
|
}
|
|
|
|
|
|
|
|
cpBody* body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForPoly(mass, NUM_VERTS, verts, cpvzero, 0.0)));
|
|
|
|
cpBodySetPosition(body, cpv(100.0, 50.0f));
|
|
|
|
|
|
|
|
shape1 = cpSpaceAddShape(space, cpPolyShapeNew(body, NUM_VERTS, verts, cpTransformIdentity, 0.0));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
cpFloat size = 100.0;
|
|
|
|
const int NUM_VERTS = 4;
|
|
|
|
|
|
|
|
cpVect verts[NUM_VERTS];
|
2021-12-28 16:06:23 +08:00
|
|
|
for (int i = 0; i < NUM_VERTS; i++)
|
|
|
|
{
|
|
|
|
cpFloat angle = -2 * M_PI * i / ((cpFloat)NUM_VERTS);
|
2021-06-26 17:46:06 +08:00
|
|
|
verts[i] = cpv(size / 2.0 * cos(angle), size / 2.0 * sin(angle));
|
|
|
|
}
|
|
|
|
|
|
|
|
cpBody* body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForPoly(mass, NUM_VERTS, verts, cpvzero, 0.0)));
|
|
|
|
cpBodySetPosition(body, cpv(100.0, -50.0f));
|
|
|
|
shape2 = cpSpaceAddShape(space, cpPolyShapeNew(body, NUM_VERTS, verts, cpTransformIdentity, 5.0));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
cpFloat size = 150.0;
|
|
|
|
cpFloat radius = 25.0;
|
|
|
|
|
|
|
|
cpVect a = cpv(size / 2.0, 0.0);
|
|
|
|
cpVect b = cpv(-size / 2.0, 0.0);
|
|
|
|
cpBody* body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForSegment(mass, a, b, 0.0)));
|
|
|
|
cpBodySetPosition(body, cpv(0, 25));
|
|
|
|
|
|
|
|
shape1 = cpSpaceAddShape(space, cpSegmentShapeNew(body, a, b, radius));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
cpFloat radius = 50.0;
|
|
|
|
|
|
|
|
cpBody* body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForCircle(mass, 0.0f, radius, cpvzero)));
|
|
|
|
cpBodySetPosition(body, cpv(0, -25));
|
|
|
|
|
|
|
|
shape2 = cpSpaceAddShape(space, cpCircleShapeNew(body, radius, cpvzero));
|
|
|
|
}
|
|
|
|
|
|
|
|
return space;
|
2021-06-23 23:09:48 +08:00
|
|
|
}
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
static void destroy(cpSpace* space)
|
|
|
|
{
|
2021-06-26 17:46:06 +08:00
|
|
|
ChipmunkDemoFreeSpaceChildren(space);
|
|
|
|
cpSpaceFree(space);
|
2021-06-23 23:09:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ChipmunkDemo Example = {
|
2021-12-28 16:06:23 +08:00
|
|
|
"Template to start (GJK)", 1.0f / 60.0f, init, update, ChipmunkDemoDefaultDrawImpl, destroy,
|
2021-06-23 23:09:48 +08:00
|
|
|
};
|