Debian Codegraph @

demo/Sticky.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 {
26 COLLISION_TYPE_STICKY = 1,
27 };
28
29 #define STICK_SENSOR_THICKNESS 2.5f
30
31 static void
32 PostStepAddJoint(cpSpace *space, void *key, void *data)
33 {
34 // printf("Adding joint for %p\n", data);
35
36 cpConstraint *joint = (cpConstraint *)key;
37 cpSpaceAddConstraint(space, joint);
38 }
39
40 static cpBool
41 StickyPreSolve(cpArbiter *arb, cpSpace *space, void *data)
42 {
43 // We want to fudge the collisions a bit to allow shapes to overlap more.
44 // This simulates their squishy sticky surface, and more importantly
45 // keeps them from separating and destroying the joint.
46
47 // Track the deepest collision point and use that to determine if a rigid collision should occur.
48 cpFloat deepest = INFINITY;
49
50 // Grab the contact set and iterate over them.
51 cpContactPointSet contacts = cpArbiterGetContactPointSet(arb);
52 for(int i=0; i<contacts.count; i++){
53 // Sink the contact points into the surface of each shape.
54 contacts.points[i].pointA = cpvsub(contacts.points[i].pointA, cpvmult(contacts.normal, STICK_SENSOR_THICKNESS));
55 contacts.points[i].pointB = cpvadd(contacts.points[i].pointB, cpvmult(contacts.normal, STICK_SENSOR_THICKNESS));
56 deepest = cpfmin(deepest, contacts.points[i].distance);// + 2.0f*STICK_SENSOR_THICKNESS);
57 }
58
59 // Set the new contact point data.
60 cpArbiterSetContactPointSet(arb, &contacts);
61
62 // If the shapes are overlapping enough, then create a
63 // joint that sticks them together at the first contact point.
64 if(!cpArbiterGetUserData(arb) && deepest <= 0.0f){
65 CP_ARBITER_GET_BODIES(arb, bodyA, bodyB);
66
67 // Create a joint at the contact point to hold the body in place.
68 cpVect anchorA = cpBodyWorldToLocal(bodyA, contacts.points[0].pointA);
69 cpVect anchorB = cpBodyWorldToLocal(bodyB, contacts.points[0].pointB);
70 cpConstraint *joint = cpPivotJointNew2(bodyA, bodyB, anchorA, anchorB);
71
72 // Give it a finite force for the stickyness.
73 cpConstraintSetMaxForce(joint, 3e3);
74
75 // Schedule a post-step() callback to add the joint.
76 cpSpaceAddPostStepCallback(space, PostStepAddJoint, joint, NULL);
77
78 // Store the joint on the arbiter so we can remove it later.
79 cpArbiterSetUserData(arb, joint);
80 }
81
82 // Position correction and velocity are handled separately so changing
83 // the overlap distance alone won't prevent the collision from occuring.
84 // Explicitly the collision for this frame if the shapes don't overlap using the new distance.
85 return (deepest <= 0.0f);
86
87 // Lots more that you could improve upon here as well:
88 // * Modify the joint over time to make it plastic.
89 // * Modify the joint in the post-step to make it conditionally plastic (like clay).
90 // * Track a joint for the deepest contact point instead of the first.
91 // * Track a joint for each contact point. (more complicated since you only get one data pointer).
92 }
93
94 static void
95 PostStepRemoveJoint(cpSpace *space, void *key, void *data)
96 {
97 // printf("Removing joint for %p\n", data);
98
99 cpConstraint *joint = (cpConstraint *)key;
100 cpSpaceRemoveConstraint(space, joint);
101 cpConstraintFree(joint);
102 }
103
104 static void
105 StickySeparate(cpArbiter *arb, cpSpace *space, void *data)
106 {
107 cpConstraint *joint = (cpConstraint *)cpArbiterGetUserData(arb);
108
109 if(joint){
110 // The joint won't be removed until the step is done.
111 // Need to disable it so that it won't apply itself.
112 // Setting the force to 0 will do just that
113 cpConstraintSetMaxForce(joint, 0.0f);
114
115 // Perform the removal in a post-step() callback.
116 cpSpaceAddPostStepCallback(space, PostStepRemoveJoint, joint, NULL);
117
118 // NULL out the reference to the joint.
119 // Not required, but it's a good practice.
120 cpArbiterSetUserData(arb, NULL);
121 }
122 }
123
124 static void
125 update(cpSpace *space, double dt)
126 {
127 cpSpaceStep(space, dt);
128 }
129
130 static cpSpace *
131 init(void)
132 {
133 ChipmunkDemoMessageString = "Sticky collisions using the cpArbiter data pointer.";
134
135 cpSpace *space = cpSpaceNew();
136 cpSpaceSetIterations(space, 10);
137 cpSpaceSetGravity(space, cpv(0, -1000));
138 cpSpaceSetCollisionSlop(space, 2.0);
139
140 cpBody *staticBody = cpSpaceGetStaticBody(space);
141 cpShape *shape;
142
143 // Create segments around the edge of the screen.
144 shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-340,-260), cpv(-340, 260), 20.0f));
145 cpShapeSetElasticity(shape, 1.0f);
146 cpShapeSetFriction(shape, 1.0f);
147 cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);
148
149 shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv( 340,-260), cpv( 340, 260), 20.0f));
150 cpShapeSetElasticity(shape, 1.0f);
151 cpShapeSetFriction(shape, 1.0f);
152 cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);
153
154 shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-340,-260), cpv( 340,-260), 20.0f));
155 cpShapeSetElasticity(shape, 1.0f);
156 cpShapeSetFriction(shape, 1.0f);
157 cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);
158
159 shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-340, 260), cpv( 340, 260), 20.0f));
160 cpShapeSetElasticity(shape, 1.0f);
161 cpShapeSetFriction(shape, 1.0f);
162 cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);
163
164 for(int i=0; i<200; i++){
165 cpFloat mass = 0.15f;
166 cpFloat radius = 10.0f;
167
168 cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForCircle(mass, 0.0f, radius, cpvzero)));
169 cpBodySetPosition(body, cpv(cpflerp(-150.0f, 150.0f, frand()), cpflerp(-150.0f, 150.0f, frand())));
170
171 cpShape *shape = cpSpaceAddShape(space, cpCircleShapeNew(body, radius + STICK_SENSOR_THICKNESS, cpvzero));
172 cpShapeSetFriction(shape, 0.9f);
173 cpShapeSetCollisionType(shape, COLLISION_TYPE_STICKY);
174 }
175
176 cpCollisionHandler *handler = cpSpaceAddWildcardHandler(space, COLLISION_TYPE_STICKY);
177 handler->preSolveFunc = StickyPreSolve;
178 handler->separateFunc = StickySeparate;
179
180 return space;
181 }
182
183 static void
184 destroy(cpSpace *space)
185 {
186 ChipmunkDemoFreeSpaceChildren(space);
187 cpSpaceFree(space);
188 }
189
190 ChipmunkDemo Sticky = {
191 "Sticky Surfaces",
192 1.0/60.0,
193 init,
194 update,
195 ChipmunkDemoDefaultDrawImpl,
196 destroy,
197 };