Debian Codegraph @

demo/OneWay.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 enum CollisionTypes {
26 COLLISION_TYPE_ONE_WAY = 1,
27 };
28
29 typedef struct OneWayPlatform {
30 cpVect n; // direction objects may pass through
31 } OneWayPlatform;
32
33 static OneWayPlatform platformInstance;
34
35 static cpBool
36 PreSolve(cpArbiter *arb, cpSpace *space, void *ignore)
37 {
38 CP_ARBITER_GET_SHAPES(arb, a, b);
39 OneWayPlatform *platform = (OneWayPlatform *)cpShapeGetUserData(a);
40
41 if(cpvdot(cpArbiterGetNormal(arb), platform->n) < 0){
42 return cpArbiterIgnore(arb);
43 }
44
45 return cpTrue;
46 }
47
48 static void
49 update(cpSpace *space, double dt)
50 {
51 cpSpaceStep(space, dt);
52 }
53
54 static cpSpace *
55 init(void)
56 {
57 ChipmunkDemoMessageString = "One way platforms are trivial in Chipmunk using a very simple collision callback.";
58
59 cpSpace *space = cpSpaceNew();
60 cpSpaceSetIterations(space, 10);
61 cpSpaceSetGravity(space, cpv(0, -100));
62
63 cpBody *body, *staticBody = cpSpaceGetStaticBody(space);
64 cpShape *shape;
65
66 // Create segments around the edge of the screen.
67 shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(-320,240), 0.0f));
68 cpShapeSetElasticity(shape, 1.0f);
69 cpShapeSetFriction(shape, 1.0f);
70 cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);
71
72 shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(320,-240), cpv(320,240), 0.0f));
73 cpShapeSetElasticity(shape, 1.0f);
74 cpShapeSetFriction(shape, 1.0f);
75 cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);
76
77 shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(320,-240), 0.0f));
78 cpShapeSetElasticity(shape, 1.0f);
79 cpShapeSetFriction(shape, 1.0f);
80 cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);
81
82 // Add our one way segment
83 shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-160,-100), cpv(160,-100), 10.0f));
84 cpShapeSetElasticity(shape, 1.0f);
85 cpShapeSetFriction(shape, 1.0f);
86 cpShapeSetCollisionType(shape, COLLISION_TYPE_ONE_WAY);
87 cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);
88
89 // We'll use the data pointer for the OneWayPlatform struct
90 platformInstance.n = cpv(0, 1); // let objects pass upwards
91 cpShapeSetUserData(shape, &platformInstance);
92
93
94 // Add a ball to test it out
95 cpFloat radius = 15.0f;
96 body = cpSpaceAddBody(space, cpBodyNew(10.0f, cpMomentForCircle(10.0f, 0.0f, radius, cpvzero)));
97 cpBodySetPosition(body, cpv(0, -200));
98 cpBodySetVelocity(body, cpv(0, 170));
99
100 shape = cpSpaceAddShape(space, cpCircleShapeNew(body, radius, cpvzero));
101 cpShapeSetElasticity(shape, 0.0f);
102 cpShapeSetFriction(shape, 0.9f);
103 cpShapeSetCollisionType(shape, 2);
104
105 cpCollisionHandler *handler = cpSpaceAddWildcardHandler(space, COLLISION_TYPE_ONE_WAY);
106 handler->preSolveFunc = PreSolve;
107
108 return space;
109 }
110
111 static void
112 destroy(cpSpace *space)
113 {
114 ChipmunkDemoFreeSpaceChildren(space);
115 cpSpaceFree(space);
116 }
117
118 ChipmunkDemo OneWay = {
119 "One Way Platforms",
120 1.0/60.0,
121 init,
122 update,
123 ChipmunkDemoDefaultDrawImpl,
124 destroy,
125 };