-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoaster.c
More file actions
executable file
·197 lines (167 loc) · 4.67 KB
/
coaster.c
File metadata and controls
executable file
·197 lines (167 loc) · 4.67 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/* Greg Bolsinga */
/* roller coaster */
/* April 28, 1992 */
/* cs319 Prof Hearn */
#include "coaster.h"
/* The following calculates the points of the coaster so that they */
/* don't have to be calculated each time it is drawn. It returns the */
/* number of points in the roller arrays. */
int getCoasterPts(void)
{
int i = 0;
float a;
float slope, b, theta, dy, dz, totalDist, maxy;
maxy = (5.0 * PI) / (RADIUSOUT / 4.0); /* the tallest the coaster gets */
slope = maxy / LENGTH; /* the slope of the falling part */
b = 0.0 - slope * (-LENGTH); /* the 'y-intercept' of the falling pt */
theta = atan(slope); /* This is for calculating the line */
dy = SPEED1 * JUMP * sin(theta); /* Change in y on falling pt */
dz = SPEED1 * JUMP * cos(theta); /* Change in z on the falling pt */
totalDist = sqrt(LENGTH * LENGTH + maxy * maxy); /*length of falling pt*/
/* calculates points for the first part of the flat straightaway */
for(a = -LENGTH / 2.0; a <= 0.0; a += SPEED2 * JUMP)
{
rollerout[i][0] = RADIUSOUT;
rollerin[i][0] = RADIUSIN;
rollerout[i][1] = rollerin[i][1] = 0.0;
rollerout[i][2] = rollerin[i][2] = a;
i++;
}
/* calculates points for the climbing part */
for(a = JUMP; a <= 5 * PI; a += JUMP)
{
rollerout[i][0] = RADIUSOUT * cos(a);
rollerin[i][0] = RADIUSIN * cos(a);
rollerout[i][1] = rollerin[i][1] = a / (RADIUSOUT / 4.0);
rollerout[i][2] = RADIUSOUT * sin(a);
rollerin[i][2] = RADIUSIN * sin(a);
i++;
}
/* calculates points for the downward slope straightaway */
for(a = SPEED1 * JUMP; a <= totalDist; a += SPEED1 * JUMP)
{
rollerout[i][0] = -RADIUSOUT;
rollerin[i][0] = -RADIUSIN;
rollerout[i][1] = rollerout[i-1][1] - dy;
rollerin[i][1] = rollerin[i-1][1] - dy;
rollerout[i][2] = rollerout[i-1][2] - dz;
rollerin[i][2] = rollerin[i-1][2] - dz;
i++;
}
/* calculates points for the flat half circle */
for(a = JUMP; a <= PI; a += JUMP)
{
rollerout[i][0] = RADIUSOUT * cos(a - PI);
rollerin[i][0] = RADIUSIN * cos(a - PI);
rollerout[i][1] = rollerin[i][1] = 0.0;
rollerout[i][2] = RADIUSOUT * sin(a - PI) - LENGTH;
rollerin[i][2] = RADIUSIN * sin(a - PI) - LENGTH;
i++;
}
/* calculates points for the last part of the flat straightaway */
for(a= -LENGTH; a <= -LENGTH / 2.0; a += SPEED2 * JUMP)
{
rollerout[i][0] = RADIUSOUT;
rollerin[i][0] = RADIUSIN;
rollerout[i][1] = rollerin[i][1] = 0.0;
rollerout[i][2] = rollerin[i][2] = a;
i++;
}
return i-1;
}
/* The following draws the struts of the roller coaster. */
void drawStrut(int i)
{
char Strut = 1;
int strut, height;
Coord support[3];
/* The following are struts which can't extend to the ground, */
/* since they would intersect the roller coaster track. */
if (i == 93 || i == 157)
{
strut = 155;
}
else if (i == 97 || i == 159)
{
strut = 161;
}
else if (i == 193)
{
strut = 191;
}
else if (i == 195)
{
strut = 197;
}
else
{
/* These extend to the ground if they don't */
/* intersect the track (see below) */
strut = i;
}
if (strut != i)
{
/* attach strut about 2/3 the way up */
height = 2.0 * rollerin[i][1] / 3.0;
}
else if ((i <= 33 || i >= 129 ) && (i <= 156 || i >= 161) && (i <= 192 || i >= 197))
{
/* This makes sure that only the highest parts of the */
/* roller coaster track draw their struts. */
height = -1.0;
}
else
{
Strut = 0;
}
if (Strut)
{
assignCoord(support, rollerin[strut]);
support[1] = height;
glBegin(GL_LINE_STRIP);
glVertex3fv(rollerin[i]);
glVertex3fv(support);
glEnd();
assignCoord(support, rollerout[strut]);
support[1] = height;
glBegin(GL_LINE_STRIP);
glVertex3fv(rollerout[i]);
glVertex3fv(support);
glEnd();
}
}
/* The following draws the roller coaster defined in the roller arrays */
/* Successive elements of the array are adjacent to each other on the */
/* roller coaster. This way, it can be easily drawn, as well as */
/* making it easier to define the roller coaster rider's path in space. */
void coaster(int numpts) /* This is the number of pts in the roller arrays */
{
int i, alternate = 0;
GLubyte tracks[3] = {51, 51, 51 };
glColor3ubv(tracks);
for(i = 0; i < numpts; i++)
{
glLineWidth(3);
glBegin(GL_LINE_STRIP);
glVertex3fv(rollerin[i]);
glVertex3fv(rollerin[i + 1]);
glEnd();
glBegin(GL_LINE_STRIP);
glVertex3fv(rollerout[i]);
glVertex3fv(rollerout[i + 1]);
glEnd();
glLineWidth(2);
/* alternate the beams of the track so that motion is better */
/* simulated. */
if (alternate)
{
glBegin(GL_LINE_STRIP);
glVertex3fv(rollerin[i]);
glVertex3fv(rollerout[i]);
glEnd();
drawStrut(i);
}
alternate = !alternate;
}
glLineWidth(1);
}