-
Notifications
You must be signed in to change notification settings - Fork 0
/
environment.c
executable file
·175 lines (126 loc) · 3.61 KB
/
environment.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include "environment.h"
#include "creature.h"
#include "chipmunk.h"
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#define GROUND_LENGTH 100000
typedef struct creatureListNode *creatureListNode_t;
struct creatureListNode {
Creature creature;
creatureListNode_t next;
};
struct environment {
cpSpace *space;
cpBody *staticBody;
int width;
int height;
cpFloat groundHeight;
};
Environment createEnvironment( int width, int height ) {
Environment env = malloc( sizeof( struct environment ) );
assert( env != NULL );
/*
* Simulation parameters
*/
env->width = width;
env->height = height;
/*
* Create the Space
*/
env->space = cpSpaceNew( );
env->space->iterations = 10; // physics accuracy
cpSpaceResizeStaticHash( env->space, 30.0f, 1000 );
cpSpaceResizeActiveHash( env->space, 30.0f, 1000 );
env->space->gravity = cpv( 0, -200 );
// create a static physics body (fixed in space)
env->staticBody = cpBodyNew( INFINITY, INFINITY );
/*
* Create the Ground
*/
// space left/right of the center point
cpFloat halfWidth = width/2.0f * GROUND_LENGTH;
// space top/bottom of the center point
cpFloat halfHeight = height/2.0f;
// height of ground
env->groundHeight = 10.0f;
/*
printf( "%f, %f\n", halfWidth,-halfHeight );
printf( "%f, %f\n", -halfWidth,-halfHeight );
printf( "%f, %f\n", -halfWidth,-halfHeight+groundHeight );
printf( "%f, %f\n", halfWidth,-halfHeight+groundHeight );
*/
// polygon for ground shape
cpVect groundPoly[] = {
cpv( halfWidth, -halfHeight ),
cpv( -halfWidth, -halfHeight ),
cpv( -halfWidth, -halfHeight+env->groundHeight ),
cpv( halfWidth, -halfHeight+env->groundHeight )
};
// create a ground shape, attached to the staticBody
cpShape *ground = cpPolyShapeNew( env->staticBody, 4, groundPoly, cpvzero );
ground->e = 1.0f; ground->u = 1.0f;
ground->group = 0;
// add this ground shape to the physics space
cpSpaceAddStaticShape( env->space, ground );
return env;
}
void updateEnvironment( Environment env ) {
cpSpaceStep( env->space, 1.0f/60.0f );
}
void destroyEnvironment( Environment env ) {
cpBodyFree( env->staticBody );
cpSpaceFreeChildren( env->space );
cpSpaceFree( env->space );
free( env );
}
cpSpace *getEnvironmentSpace( Environment env ) {
return env->space;
}
void displayEnvironment( Environment env, char *message, cpVect center ) {
#ifndef NOGRAPHICS
static double lastX = 0.0;
double changeRate = 0.95;
double y = -center.y;
drawSpaceOptions options = {
0,
0,
1,
4.0f,
0.0f,
1.5f,
};
glClear( GL_COLOR_BUFFER_BIT );
// space left/right of the center point
cpFloat halfWidth = env->width/2.0f;
// space top/bottom of the center point
cpFloat halfHeight = env->height/2.0f;
glPushMatrix(); {
if ( y > 0.0f ) y = 0.0f;
glTranslatef(lastX, y, 0.0f);
lastX = changeRate * lastX + (1.0f-changeRate) * -center.x;
glColor3f( 1.0f, 0.0f, 0.0f );
glBegin(GL_LINES); {
glVertex2f(0.0f, halfHeight/4.0f);
glVertex2f(20.0f, halfHeight/4.0f - 10.0f);
glVertex2f(20.0f, halfHeight/4.0f - 10.0f);
glVertex2f(0.0f, halfHeight/4.0f - 20.0f);
glVertex2f(0.0, halfHeight/4.0f);
glVertex2f(0.0, -halfHeight+env->groundHeight);
} glEnd();
drawSpace( env->space, &options );
} glPopMatrix();
glPushMatrix(); {
glTranslatef(0.0, y, 0.0f);
glColor3f( 0.0f, 0.0f, 0.0f );
// draw ground line
glBegin(GL_LINES); {
glVertex2f(-halfWidth, -halfHeight+env->groundHeight);
glVertex2f(halfWidth, -halfHeight+env->groundHeight);
} glEnd();
} glPopMatrix();
if ( center.x)
drawString( -300, -210, message );
glutSwapBuffers();
#endif
}