Debian Codegraph @

demo/ContactPoints.c_

raw
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/chipmunk_private.h"
28 #include "ChipmunkDemo.h"
29
30 static cpBool NeverCollide(cpArbiter *arb, cpSpace *space, void *data){return cpFalse;}
31
32 static void
33 update(cpSpace *space)
34 {
35 int steps = 1;
36 cpFloat dt = 1.0f/60.0f/(cpFloat)steps;
37
38 for(int i=0; i<steps; i++){
39 cpSpaceStep(space, dt);
40 }
41 }
42
43 static cpSpace *
44 init(void)
45 {
46 cpSpace *space = cpSpaceNew();
47 cpSpaceSetIterations(space, 5);
48 cpSpaceSetDamping(space, 0.1f);
49
50 cpSpaceSetDefaultCollisionHandler(space, NeverCollide, NULL, NULL, NULL, NULL);
51
52 {
53 cpFloat mass = 1.0f;
54 cpFloat length = 100.0f;
55 cpVect a = cpv(-length/2.0f, 0.0f), b = cpv(length/2.0f, 0.0f);
56
57 cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForSegment(mass, a, b)));
58 cpBodySetPos(body, cpv(-160.0f, -80.0f));
59
60 cpSpaceAddShape(space, cpSegmentShapeNew(body, a, b, 30.0f));
61 }
62
63 {
64 cpFloat mass = 1.0f;
65 cpFloat length = 100.0f;
66 cpVect a = cpv(-length/2.0f, 0.0f), b = cpv(length/2.0f, 0.0f);
67
68 cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForSegment(mass, a, b)));
69 cpBodySetPos(body, cpv(-160.0f, 80.0f));
70
71 cpSpaceAddShape(space, cpSegmentShapeNew(body, a, b, 20.0f));
72 }
73
74 {
75 cpFloat mass = 1.0f;
76 const int NUM_VERTS = 5;
77
78 cpVect verts[NUM_VERTS];
79 for(int i=0; i<NUM_VERTS; i++){
80 cpFloat angle = -2*M_PI*i/((cpFloat) NUM_VERTS);
81 verts[i] = cpv(40*cos(angle), 40*sin(angle));
82 }
83
84 cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForPoly(mass, NUM_VERTS, verts, cpvzero)));
85 cpBodySetPos(body, cpv(-0.0f, -80.0f));
86
87 cpSpaceAddShape(space, cpPolyShapeNew(body, NUM_VERTS, verts, cpvzero));
88 }
89
90 {
91 cpFloat mass = 1.0f;
92 const int NUM_VERTS = 4;
93
94 cpVect verts[NUM_VERTS];
95 for(int i=0; i<NUM_VERTS; i++){
96 cpFloat angle = -2*M_PI*i/((cpFloat) NUM_VERTS);
97 verts[i] = cpv(60*cos(angle), 60*sin(angle));
98 }
99
100 cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForPoly(mass, NUM_VERTS, verts, cpvzero)));
101 cpBodySetPos(body, cpv(-0.0f, 80.0f));
102
103 cpSpaceAddShape(space, cpPolyShapeNew(body, NUM_VERTS, verts, cpvzero));
104 }
105
106 {
107 cpFloat mass = 1.0f;
108 cpFloat r = 60.0f;
109
110 cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, INFINITY));
111 cpBodySetPos(body, cpv(160.0, -80.0f));
112
113 cpSpaceAddShape(space, cpCircleShapeNew(body, r, cpvzero));
114 }
115
116 {
117 cpFloat mass = 1.0f;
118 cpFloat r = 40.0f;
119
120 cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, INFINITY));
121 cpBodySetPos(body, cpv(160.0, 80.0f));
122
123 cpSpaceAddShape(space, cpCircleShapeNew(body, r, cpvzero));
124 }
125
126 return space;
127 }
128
129 static void
130 destroy(cpSpace *space)
131 {
132 ChipmunkDemoFreeSpaceChildren(space);
133 cpSpaceFree(space);
134 }
135
136 ChipmunkDemo ContactPoints = {
137 "ContactPoints",
138 1.0/60.0,
139 init,
140 update,
141 ChipmunkDemoDefaultDrawImpl,
142 destroy,
143 };