-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshipFunc.c
executable file
·226 lines (167 loc) · 5.17 KB
/
shipFunc.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/***********************************************************************
* COSC1187 - Interactive 3D Graphics and Animation
* Assignment 2 - Ship functions
* Full Name : Alyssa Biasi
* Student Number : 3328976
***********************************************************************/
#include "utils.h"
/* Ship struct initialiser */
void initShip(Ship *ship, const char *meshFile, Camera cam, int num) {
ship->heading = 90;
ship->pitch = 0;
ship->roll = 0;
ship->displacement = -0.8;
ship->fVel = 0;
ship->tVel = 0;
ship->maxSpeed = 5.0;
ship->acc = 3.0;
ship->tAcc = 50.0;
ship->rot.x = ship->pitch;
ship->rot.y = ship->heading;
ship->rot.z = ship->roll;
ship->mesh = objMeshLoad(meshFile);
initCamera(&cam, ship->pos, ship->rot);
ship->cam = cam;
ship->player = num;
}
/* Resets ship for game start */
void resetShip(Ship *ship, Seabed *seabed, Vec3f init) {
float dy, height;
float y = -2; /* Account for trough of waves */
/* Checks init position isn't on seabed.
* Generates new (x,z) until a valid position is found. */
do {
init.x = 180*(rand()/(float)RAND_MAX) - 90;
init.z = 180*(rand()/(float)RAND_MAX) - 90;
height = findHeight(seabed, init.x, init.z);
/* Ignores if the seabed height is higher than y */
if(y>height) {
dy = y-height;
}
else {
dy = 0;
}
} while(dy < 7);
ship->pos = init;
ship->heading = 90;
ship->fVel = 0;
ship->tVel = 0;
ship->health = 50;
}
/* Draws ship mesh. */
void drawShipMesh(OBJMesh *mesh) {
int i;
glBegin(GL_TRIANGLES);
for (i = 0; i < mesh->numIndices; ++i) {
unsigned int index = mesh->indices[i];
float* vert = (float*)((void*)mesh->vertices + index * mesh->stride);
float* norm = (float*)((void*)vert + mesh->normalOffset);
if (mesh->hasNormals) {
glNormal3fv(norm);
}
glVertex3fv(vert);
}
glEnd();
}
/* Calculates ship's position. */
void calcShipPos(Ship *ship, Seabed *seabed, Keys *keys, float dt) {
Vec4f v;
Vec2f headingDir;
/* Forward speed of ship. */
if(keys->up) {
ship->fVel += ship->acc * dt;
}
if(keys->down) {
ship->fVel -= ship->acc * dt;
}
ship->fVel = clamp(ship->fVel, 0, ship->maxSpeed);
/* Turning speed of ship. */
if(keys->left) {
ship->tVel += ship->tAcc * dt;
}
if(keys->right) {
ship->tVel -= ship->tAcc * dt;
}
/* Limit turning speed. 5x forward vel in degrees per second. */
ship->tVel = clamp(ship->tVel, -fabs(ship->fVel)*5.0, fabs(ship->fVel)*5.0);
ship->tVel -= myCrossPlatformMin(fabs(ship->tVel), 0.5) * sign(ship->tVel);
/* Calculate heading vector of ship. */
headingDir.x = sinf(ship->heading*PI/180.0);
headingDir.y = cosf(ship->heading*PI/180.0);
/* Moving the ship */
ship->heading += ship->tVel * dt;
ship->pos.x += headingDir.x * ship->fVel * dt;
ship->pos.z += headingDir.y * ship->fVel * dt;
/* Keeps ship within the playable area */
if(ship->pos.x >= 100 || ship->pos.x <= -100 ||
ship->pos.z >= 100 || ship->pos.z <= -100)
{
resetShip(ship, seabed, newVec3f(5,0,5));
}
/* Calculate roll & pitch based on location. */
v = calcSineValue(ship->pos.x, ship->pos.z);
ship->pos.y = v.w - ship->displacement;
ship->roll = asin(v.x)*180.0/PI;
ship->pitch = asin(v.z)*180.0/PI;
ship->rot.x = ship->pitch;
ship->rot.y = ship->heading;
ship->rot.z = ship->roll;
/***********************************************************************
Forward and side vectors
***********************************************************************/
float sx, sy, sz, cx, cy, cz, theta;
Vec3f forward, side;
// rotation angle about Z-axis (roll)
theta = ship->roll * PI/180.0;
sz = sinf(theta);
cz = cosf(theta);
// rotation angle about Y-axis (yaw)
theta = ship->heading * PI/180.0;
sy = sinf(theta);
cy = cosf(theta);
// rotation angle about X-axis (pitch)
theta = ship->pitch * PI/180.0;
sx = sinf(theta);
cx = cosf(theta);
// determine forward vector
forward.x = sy*cz + cy*sx*sz;
forward.y = sy*sz - cy*sx*cz;
forward.z = cy*cx;
// determine side vector
side.x = cy*cz - sy*sx*sz;
side.y = cy*sz + sy*sx*cz;
side.z = -sy*cx;
ship->forward = forward;
ship->side = side;
}
/* Draws the ship */
void drawShip(Ship *ship, Controls *controls) {
static float diffuse[] = {0.89, 0.65, 0.41, 1.0};
static float ambient[] = {0.89, 0.65, 0.41, 1.0};
static float specular[] = {1.0, 1.0, 1.0, 1.0};
static float shininess = 256.0f;
glEnable(GL_LIGHTING);
glPushMatrix();
glTranslatef(ship->pos.x, ship->pos.y, ship->pos.z);
if(controls->axes) {
drawAxes(newVec3f(0,0,0), newVec3f(10,10,10));
}
glRotatef(ship->roll, 0,0,-1);
glRotatef(ship->pitch, 1,0,0);
glRotatef(ship->heading, 0,1,0);
if(controls->axes) {
drawAxes(newVec3f(0,0,0), newVec3f(10,10,10));
}
glScalef(0.1,0.1,0.1);
glPushMatrix();
glRotatef(-90, 0, 1, 0);
/* Apply material */
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, ambient);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diffuse);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular);
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shininess);
drawShipMesh(ship->mesh);
glPopMatrix();
glPopMatrix();
glPopMatrix();
}