Debian Codegraph @

demo/Smooth.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 DrawContacts(cpArbiter *arb, cpSpace *space, void *data){
31 cpContactPointSet set = cpArbiterGetContactPointSet(arb);
32
33 for(int i=0; i<set.count; i++){
34 cpVect p = set.points[i].point;
35 ChipmunkDebugDrawDot(6.0, p, RGBAColor(1, 0, 0, 1));
36 ChipmunkDebugDrawSegment(p, cpvadd(p, cpvmult(set.points[i].normal, 10.0)), RGBAColor(1, 0, 0, 1));
37 }
38
39 return cpFalse;
40 // return cpTrue;
41 }
42
43 static void
44 update(cpSpace *space)
45 {
46 int steps = 1;
47 cpFloat dt = 1.0f/60.0f/(cpFloat)steps;
48
49 for(int i=0; i<steps; i++){
50 cpSpaceStep(space, dt);
51 }
52 }
53
54 #define MAX(a, b) (a > b ? a : b)
55 #define MIN(a, b) (a < b ? a : b)
56
57 static cpSpace *
58 init(void)
59 {
60 cpSpace *space = cpSpaceNew();
61 cpSpaceSetIterations(space, 5);
62 cpSpaceSetDamping(space, 0.1f);
63
64 cpSpaceSetDefaultCollisionHandler(space, NULL, DrawContacts, NULL, NULL, NULL);
65
66 {
67 cpFloat mass = 1.0f;
68 cpFloat length = 100.0f;
69 cpVect a = cpv(-length/2.0f, 0.0f), b = cpv(length/2.0f, 0.0f);
70
71 cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForSegment(mass, a, b)));
72 cpBodySetPos(body, cpv(-160.0f, 80.0f));
73
74 cpSpaceAddShape(space, cpSegmentShapeNew(body, a, b, 30.0f));
75 }
76
77 {
78 cpFloat mass = 1.0f;
79 const int NUM_VERTS = 5;
80
81 cpVect verts[NUM_VERTS];
82 for(int i=0; i<NUM_VERTS; i++){
83 cpFloat angle = -2*M_PI*i/((cpFloat) NUM_VERTS);
84 verts[i] = cpv(40*cos(angle), 40*sin(angle));
85 }
86
87 cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForPoly(mass, NUM_VERTS, verts, cpvzero)));
88 cpBodySetPos(body, cpv(-0.0f, 80.0f));
89
90 cpSpaceAddShape(space, cpPolyShapeNew(body, NUM_VERTS, verts, cpvzero));
91 }
92
93 {
94 cpFloat mass = 1.0f;
95 cpFloat r = 60.0f;
96
97 cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, INFINITY));
98 cpBodySetPos(body, cpv(160.0, 80.0f));
99
100 cpSpaceAddShape(space, cpCircleShapeNew(body, r, cpvzero));
101 }
102
103 cpBody *staticBody = cpSpaceGetStaticBody(space);
104
105 cpVect terrain[] = {
106 cpv(-320, -200),
107 cpv(-200, -100),
108 cpv( 0, -200),
109 cpv( 200, -100),
110 cpv( 320, -200),
111 };
112 int terrainCount = sizeof(terrain)/sizeof(*terrain);
113
114 for(int i=1; i<5; i++){
115 cpVect v0 = terrain[MAX(i-2, 0)];
116 cpVect v1 = terrain[i-1];
117 cpVect v2 = terrain[i];
118 cpVect v3 = terrain[MIN(i+1, terrainCount - 1)];
119
120 cpShape *seg = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, v1, v2, 10.0));
121 cpSegmentShapeSetNeighbors(seg, v0, v3);
122 }
123
124 return space;
125 }
126
127 static void
128 destroy(cpSpace *space)
129 {
130 ChipmunkDemoFreeSpaceChildren(space);
131 cpSpaceFree(space);
132 }
133
134 ChipmunkDemo Smooth = {
135 "Smooth",
136 1.0f/60.0f,
137 init,
138 update,
139 ChipmunkDemoDefaultDrawImpl,
140 destroy,
141 };