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 <stdlib.h> 23#include <stdio.h> 24#include <math.h> 25#include <string.h> 26 27#include "chipmunk_private.h" 28#include "chipmunk_unsafe.h" 29#include "ChipmunkDemo.h" 30 31static cpShape *shape1, *shape2; 32 33static void 34update(cpSpace *space, cpFloat dt) 35{ 36 cpSpaceStep(space, dt); 37} 38 39static void 40draw(cpSpace *space) 41{ 42 ChipmunkDemoDefaultDrawImpl(space); 43 struct cpContact arr[CP_MAX_CONTACTS_PER_ARBITER]; 44// cpCollideShapes(shape1, shape2, (cpCollisionID[]){0}, arr); 45 cpCollisionInfo info = cpCollideShapes(shape2, shape1, 0x00000000, arr); 46} 47 48static cpSpace * 49init(void) 50{ 51 cpSpace *space = cpSpaceNew(); 52 cpSpaceSetIterations(space, 5); 53 space->damping = 0.1; 54 55 cpFloat mass = 1.0f; 56 57 { 58 cpFloat size = 100.0; 59 60 cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForBox(mass, size, size))); 61 cpBodySetPosition(body, cpv(100.0, 50.0f)); 62 63 shape1 = cpSpaceAddShape(space, cpBoxShapeNew(body, size, size, 0.0)); 64 shape1->group = 1; 65 }{ 66 cpFloat size = 100.0; 67 68 cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForBox(mass, size, size))); 69 cpBodySetPosition(body, cpv(120.0, -40.0f)); 70 cpBodySetAngle(body, 1e-2); 71 72 shape2 = cpSpaceAddShape(space, cpBoxShapeNew(body, size, size, 0.0)); 73 shape2->group = 1; 74 } 75 76// { 77// cpFloat size = 100.0; 78// const int NUM_VERTS = 5; 79// 80// cpVect verts[NUM_VERTS]; 81// for(int i=0; i<NUM_VERTS; i++){ 82// cpFloat angle = -2*M_PI*i/((cpFloat) NUM_VERTS); 83// verts[i] = cpv(size/2.0*cos(angle), size/2.0*sin(angle)); 84// } 85// 86// cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForPoly(mass, NUM_VERTS, verts, cpvzero))); 87// cpBodySetPosition(body, cpv(100.0, 50.0f)); 88// 89// shape1 = cpSpaceAddShape(space, cpPolyShapeNew(body, NUM_VERTS, verts, cpvzero)); 90// shape1->group = 1; 91// } 92// { 93// cpFloat size = 100.0; 94// const int NUM_VERTS = 4; 95// 96// cpVect verts[NUM_VERTS]; 97// for(int i=0; i<NUM_VERTS; i++){ 98// cpFloat angle = -2*M_PI*i/((cpFloat) NUM_VERTS); 99// verts[i] = cpv(size/2.0*cos(angle), size/2.0*sin(angle)); 100// } 101// 102// cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForPoly(mass, NUM_VERTS, verts, cpvzero))); 103// cpBodySetPosition(body, cpv(100.0, -50.0f)); 104// 105// shape2 = cpSpaceAddShape(space, cpPolyShapeNew(body, NUM_VERTS, verts, cpvzero)); 106// shape2->group = 1; 107// } 108// 109// { 110// cpFloat size = 150.0; 111// cpFloat radius = 25.0; 112// 113// cpVect a = cpv( size/2.0, 0.0); 114// cpVect b = cpv(-size/2.0, 0.0); 115// cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForSegment(mass, a, b))); 116// cpBodySetPosition(body, cpv(0, 25)); 117// 118// shape1 = cpSpaceAddShape(space, cpSegmentShapeNew(body, a, b, radius)); 119// shape1->group = 1; 120// } 121// { 122// cpFloat radius = 50.0; 123// 124// cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForCircle(mass, 0.0f, radius, cpvzero))); 125// cpBodySetPosition(body, cpv(0, -25)); 126// 127// shape2 = cpSpaceAddShape(space, cpCircleShapeNew(body, radius, cpvzero)); 128// shape2->group = 1; 129// } 130 131 return space; 132} 133 134static void 135destroy(cpSpace *space) 136{ 137 ChipmunkDemoFreeSpaceChildren(space); 138 cpSpaceFree(space); 139} 140 141ChipmunkDemo GJK = { 142 "GJK", 143 1.0f/60.0f, 144 init, 145 update, 146 draw, 147 destroy, 148}; 149