Debian Codegraph @

demo/Player.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_private.h"
23 #include "ChipmunkDemo.h"
24
25 #define PLAYER_VELOCITY 500.0
26
27 #define PLAYER_GROUND_ACCEL_TIME 0.1
28 #define PLAYER_GROUND_ACCEL (PLAYER_VELOCITY/PLAYER_GROUND_ACCEL_TIME)
29
30 #define PLAYER_AIR_ACCEL_TIME 0.25
31 #define PLAYER_AIR_ACCEL (PLAYER_VELOCITY/PLAYER_AIR_ACCEL_TIME)
32
33 #define JUMP_HEIGHT 50.0
34 #define JUMP_BOOST_HEIGHT 55.0
35 #define FALL_VELOCITY 900.0
36 #define GRAVITY 2000.0
37
38 static cpBody *playerBody = NULL;
39 static cpShape *playerShape = NULL;
40
41 static cpFloat remainingBoost = 0;
42 static cpBool grounded = cpFalse;
43 static cpBool lastJumpState = cpFalse;
44
45 static void
46 SelectPlayerGroundNormal(cpBody *body, cpArbiter *arb, cpVect *groundNormal){
47 cpVect n = cpvneg(cpArbiterGetNormal(arb));
48
49 if(n.y > groundNormal->y){
50 (*groundNormal) = n;
51 }
52 }
53
54 static void
55 playerUpdateVelocity(cpBody *body, cpVect gravity, cpFloat damping, cpFloat dt)
56 {
57 int jumpState = (ChipmunkDemoKeyboard.y > 0.0f);
58
59 // Grab the grounding normal from last frame
60 cpVect groundNormal = cpvzero;
61 cpBodyEachArbiter(playerBody, (cpBodyArbiterIteratorFunc)SelectPlayerGroundNormal, &groundNormal);
62
63 grounded = (groundNormal.y > 0.0);
64 if(groundNormal.y < 0.0f) remainingBoost = 0.0f;
65
66 // Do a normal-ish update
67 cpBool boost = (jumpState && remainingBoost > 0.0f);
68 cpVect g = (boost ? cpvzero : gravity);
69 cpBodyUpdateVelocity(body, g, damping, dt);
70
71 // Target horizontal speed for air/ground control
72 cpFloat target_vx = PLAYER_VELOCITY*ChipmunkDemoKeyboard.x;
73
74 // Update the surface velocity and friction
75 // Note that the "feet" move in the opposite direction of the player.
76 cpVect surface_v = cpv(-target_vx, 0.0);
77 playerShape->surfaceV = surface_v;
78 playerShape->u = (grounded ? PLAYER_GROUND_ACCEL/GRAVITY : 0.0);
79
80 // Apply air control if not grounded
81 if(!grounded){
82 // Smoothly accelerate the velocity
83 playerBody->v.x = cpflerpconst(playerBody->v.x, target_vx, PLAYER_AIR_ACCEL*dt);
84 }
85
86 body->v.y = cpfclamp(body->v.y, -FALL_VELOCITY, INFINITY);
87 }
88
89 static void
90 update(cpSpace *space, double dt)
91 {
92 int jumpState = (ChipmunkDemoKeyboard.y > 0.0f);
93
94 // If the jump key was just pressed this frame, jump!
95 if(jumpState && !lastJumpState && grounded){
96 cpFloat jump_v = cpfsqrt(2.0*JUMP_HEIGHT*GRAVITY);
97 playerBody->v = cpvadd(playerBody->v, cpv(0.0, jump_v));
98
99 remainingBoost = JUMP_BOOST_HEIGHT/jump_v;
100 }
101
102 // Step the space
103 cpSpaceStep(space, dt);
104
105 remainingBoost -= dt;
106 lastJumpState = jumpState;
107 }
108
109 static cpSpace *
110 init(void)
111 {
112 cpSpace *space = cpSpaceNew();
113 space->iterations = 10;
114 space->gravity = cpv(0, -GRAVITY);
115 // space->sleepTimeThreshold = 1000;
116
117 cpBody *body, *staticBody = cpSpaceGetStaticBody(space);
118 cpShape *shape;
119
120 // Create segments around the edge of the screen.
121 shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(-320,240), 0.0f));
122 shape->e = 1.0f; shape->u = 1.0f;
123 cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);
124
125 shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(320,-240), cpv(320,240), 0.0f));
126 shape->e = 1.0f; shape->u = 1.0f;
127 cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);
128
129 shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(320,-240), 0.0f));
130 shape->e = 1.0f; shape->u = 1.0f;
131 cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);
132
133 shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,240), cpv(320,240), 0.0f));
134 shape->e = 1.0f; shape->u = 1.0f;
135 cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);
136
137 // Set up the player
138 body = cpSpaceAddBody(space, cpBodyNew(1.0f, INFINITY));
139 body->p = cpv(0, -200);
140 body->velocity_func = playerUpdateVelocity;
141 playerBody = body;
142
143 shape = cpSpaceAddShape(space, cpBoxShapeNew2(body, cpBBNew(-15.0, -27.5, 15.0, 27.5), 10.0));
144 // shape = cpSpaceAddShape(space, cpSegmentShapeNew(playerBody, cpvzero, cpv(0, radius), radius));
145 shape->e = 0.0f; shape->u = 0.0f;
146 shape->type = 1;
147 playerShape = shape;
148
149 // Add some boxes to jump on
150 for(int i=0; i<6; i++){
151 for(int j=0; j<3; j++){
152 body = cpSpaceAddBody(space, cpBodyNew(4.0f, INFINITY));
153 body->p = cpv(100 + j*60, -200 + i*60);
154
155 shape = cpSpaceAddShape(space, cpBoxShapeNew(body, 50, 50, 0.0));
156 shape->e = 0.0f; shape->u = 0.7f;
157 }
158 }
159
160 return space;
161 }
162
163 static void
164 destroy(cpSpace *space)
165 {
166 ChipmunkDemoFreeSpaceChildren(space);
167 cpSpaceFree(space);
168 }
169
170 ChipmunkDemo Player = {
171 "Platformer Player Controls",
172 1.0/180.0,
173 init,
174 update,
175 ChipmunkDemoDefaultDrawImpl,
176 destroy,
177 };