Debian Codegraph @

demo/Planet.c

raw symbols SCIP
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 "chipmunk/chipmunk.h"
23 #include "ChipmunkDemo.h"
24
25 static cpBody *planetBody;
26
27 static cpFloat gravityStrength = 5.0e6f;
28
29 static void
30 update(cpSpace *space, double dt)
31 {
32 cpSpaceStep(space, dt);
33 }
34
35 static void
36 planetGravityVelocityFunc(cpBody *body, cpVect gravity, cpFloat damping, cpFloat dt)
37 {
38 // Gravitational acceleration is proportional to the inverse square of
39 // distance, and directed toward the origin. The central planet is assumed
40 // to be massive enough that it affects the satellites but not vice versa.
41 cpVect p = cpBodyGetPosition(body);
42 cpFloat sqdist = cpvlengthsq(p);
43 cpVect g = cpvmult(p, -gravityStrength / (sqdist * cpfsqrt(sqdist)));
44
45 cpBodyUpdateVelocity(body, g, damping, dt);
46 }
47
48 static cpVect
49 rand_pos(cpFloat radius)
50 {
51 cpVect v;
52 do {
53 v = cpv(frand()*(640 - 2*radius) - (320 - radius), frand()*(480 - 2*radius) - (240 - radius));
54 } while(cpvlength(v) < 85.0f);
55
56 return v;
57 }
58
59 static void
60 add_box(cpSpace *space)
61 {
62 const cpFloat size = 10.0f;
63 const cpFloat mass = 1.0f;
64
65 cpVect verts[] = {
66 cpv(-size,-size),
67 cpv(-size, size),
68 cpv( size, size),
69 cpv( size,-size),
70 };
71
72 cpFloat radius = cpvlength(cpv(size, size));
73 cpVect pos = rand_pos(radius);
74
75 cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForPoly(mass, 4, verts, cpvzero, 0.0f)));
76 cpBodySetVelocityUpdateFunc(body, planetGravityVelocityFunc);
77 cpBodySetPosition(body, pos);
78
79 // Set the box's velocity to put it into a circular orbit from its
80 // starting position.
81 cpFloat r = cpvlength(pos);
82 cpFloat v = cpfsqrt(gravityStrength / r) / r;
83 cpBodySetVelocity(body, cpvmult(cpvperp(pos), v));
84
85 // Set the box's angular velocity to match its orbital period and
86 // align its initial angle with its position.
87 cpBodySetAngularVelocity(body, v);
88 cpBodySetAngle(body, cpfatan2(pos.y, pos.x));
89
90 cpShape *shape = cpSpaceAddShape(space, cpPolyShapeNew(body, 4, verts, cpTransformIdentity, 0.0));
91 cpShapeSetElasticity(shape, 0.0f);
92 cpShapeSetFriction(shape, 0.7f);
93 }
94
95 static cpSpace *
96 init(void)
97 {
98 // Create a rouge body to control the planet manually.
99 cpSpace *space = cpSpaceNew();
100 cpSpaceSetIterations(space, 20);
101
102 planetBody = cpSpaceAddBody(space, cpBodyNewKinematic());
103 cpBodySetAngularVelocity(planetBody, 0.2f);
104
105 for(int i=0; i<30; i++){
106 add_box(space);
107 }
108
109 cpShape *shape = cpSpaceAddShape(space, cpCircleShapeNew(planetBody, 70.0f, cpvzero));
110 cpShapeSetElasticity(shape, 1.0f);
111 cpShapeSetFriction(shape, 1.0f);
112 cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);
113
114 return space;
115 }
116
117 static void
118 destroy(cpSpace *space)
119 {
120 ChipmunkDemoFreeSpaceChildren(space);
121 cpSpaceFree(space);
122 }
123
124 ChipmunkDemo Planet = {
125 "Planet",
126 1.0/60.0,
127 init,
128 update,
129 ChipmunkDemoDefaultDrawImpl,
130 destroy,
131 };