-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.cpp
More file actions
402 lines (330 loc) · 11.1 KB
/
player.cpp
File metadata and controls
402 lines (330 loc) · 11.1 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#define _USE_MATH_DEFINES
#include <GL/glu.h>
#include "player.h"
#include <cmath>
Player::Player()
: m_position(0.0f, 0.0f, 0.0f),
m_rotation(0.0f, 0.0f, 0.0f),
m_handleLength(0.5f / 3.0f),
m_handleRadius(0.05f / 3.0f),
m_guardWidth(0.3f / 3.0f),
m_guardHeight(0.05f / 3.0f),
m_guardDepth(0.15f / 3.0f),
m_bladeLength(1.0f / 3.0f),
m_bladeWidth(0.08f / 3.0f),
m_bladeThickness(0.02f / 3.0f),
m_tipLength(0.2f / 3.0f),
m_segments(8),
m_handleColor(QColor(100, 70, 40)),
m_guardColor(QColor(255, 215, 0)),
m_bladeColor(QColor(200, 200, 220))
{
}
Player::~Player()
{
}
void Player::setPosition(const QVector3D &pos)
{
m_position = pos;
}
QVector3D Player::getPosition() const
{
return m_position;
}
void Player::setRotation(float angleX, float angleY, float angleZ)
{
m_rotation = QVector3D(angleX, angleY, angleZ);
}
QVector3D Player::getRotation() const
{
return m_rotation;
}
void Player::draw() const
{
glPushMatrix();
// Position the sword
glTranslatef(m_position.x(), m_position.y(), m_position.z());
// Apply rotation from user-provided values
glRotatef(m_rotation.x(), 1.0f, 0.0f, 0.0f);
glRotatef(m_rotation.y(), 0.0f, 1.0f, 0.0f);
glRotatef(m_rotation.z(), 0.0f, 0.0f, 1.0f);
// Add a 90° rotation around the Y-axis to align the guard with the X-axis
glRotatef(90.0f, 0.0f, 1.0f, 0.0f);
// Draw sword components
drawHandle();
drawGuard();
drawBlade();
drawBladeTip();
// Restore matrix
glPopMatrix();
}
void Player::drawHandle() const
{
GLUquadric *quadric = gluNewQuadric();
// Position for the handle (bottom part of the sword)
glPushMatrix();
// Rotate so the cylinder is vertical (along Y axis)
glRotatef(-90.0f, 1.0f, 0.0f, 0.0f);
// Set the main handle color (dark brown)
glColor3f(
m_handleColor.redF() * 0.8f,
m_handleColor.greenF() * 0.8f,
m_handleColor.blueF() * 0.8f);
// Draw the handle as a cylinder
gluCylinder(quadric, m_handleRadius, m_handleRadius, m_handleLength, m_segments, 1);
// Cap the bottom of the handle
gluDisk(quadric, 0.0f, m_handleRadius, m_segments, 1);
// Base pommel ring (wider, golden like the guard)
glColor3f(
m_guardColor.redF(),
m_guardColor.greenF(),
m_guardColor.blueF());
// Bottom ring (pommel)
float pommelRadius = m_handleRadius * 1.5f;
float pommelHeight = m_handleLength * 0.1f;
gluCylinder(quadric, pommelRadius, pommelRadius, pommelHeight, m_segments, 1);
gluDisk(quadric, 0.0f, pommelRadius, m_segments, 1);
glTranslatef(0.0f, 0.0f, pommelHeight);
gluDisk(quadric, 0.0f, pommelRadius, m_segments, 1);
// Middle part of the handle - leather wrapping texture effect
glColor3f(
m_handleColor.redF(),
m_handleColor.greenF() * 0.8f,
m_handleColor.blueF() * 0.6f); // Leather brown
float mainHandleStart = pommelHeight;
float mainHandleLength = m_handleLength * 0.8f;
glTranslatef(0.0f, 0.0f, 0.0f);
gluCylinder(quadric, m_handleRadius * 1.1f, m_handleRadius * 1.1f, mainHandleLength, m_segments, 1);
// Add texture effect with thin rings for leather wrapping
glColor3f(
m_handleColor.redF() * 0.7f,
m_handleColor.greenF() * 0.5f,
m_handleColor.blueF() * 0.3f); // Darker leather
float wrapSpacing = mainHandleLength / 8.0f;
for (int i = 0; i < 7; i++)
{
float wrapPosition = mainHandleStart + wrapSpacing * (i + 0.5f);
float wrapWidth = wrapSpacing * 0.3f;
glTranslatef(0.0f, 0.0f, wrapSpacing - wrapWidth);
gluCylinder(quadric, m_handleRadius * 1.15f, m_handleRadius * 1.15f, wrapWidth, m_segments, 1);
}
// Top decorative ring near the guard
glColor3f(
m_guardColor.redF(),
m_guardColor.greenF(),
m_guardColor.blueF());
float topRingHeight = m_handleLength * 0.1f;
float topRingStart = m_handleLength - topRingHeight;
glTranslatef(0.0f, 0.0f, mainHandleLength - (7 * wrapSpacing - 7 * wrapSpacing * 0.3f));
gluCylinder(quadric, m_handleRadius * 1.3f, m_handleRadius * 1.3f, topRingHeight, m_segments, 1);
gluDisk(quadric, m_handleRadius * 1.1f, m_handleRadius * 1.3f, m_segments, 1);
// Cap the top of the handle (at the guard)
glTranslatef(0.0f, 0.0f, topRingHeight);
gluDisk(quadric, 0.0f, m_handleRadius * 1.3f, m_segments, 1);
glPopMatrix();
gluDeleteQuadric(quadric);
}
void Player::drawGuard() const
{
// Set the guard color (yellow/gold)
glColor3f(
m_guardColor.redF(),
m_guardColor.greenF(),
m_guardColor.blueF());
// Position for the guard (above the handle)
glPushMatrix();
glTranslatef(0.0f, m_handleLength, 0.0f);
// Draw guard as a rectangular block
glPushMatrix();
glScalef(m_guardWidth, m_guardHeight, m_guardDepth);
glTranslatef(-0.5f, 0.0f, -0.5f); // Center the guard
// Front face
glBegin(GL_QUADS);
glNormal3f(0.0f, 0.0f, 1.0f);
glVertex3f(0.0f, 0.0f, 1.0f);
glVertex3f(1.0f, 0.0f, 1.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
glVertex3f(0.0f, 1.0f, 1.0f);
glEnd();
// Back face
glBegin(GL_QUADS);
glNormal3f(0.0f, 0.0f, -1.0f);
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(0.0f, 1.0f, 0.0f);
glVertex3f(1.0f, 1.0f, 0.0f);
glVertex3f(1.0f, 0.0f, 0.0f);
glEnd();
// Top face
glBegin(GL_QUADS);
glNormal3f(0.0f, 1.0f, 0.0f);
glVertex3f(0.0f, 1.0f, 0.0f);
glVertex3f(0.0f, 1.0f, 1.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
glVertex3f(1.0f, 1.0f, 0.0f);
glEnd();
// Bottom face
glBegin(GL_QUADS);
glNormal3f(0.0f, -1.0f, 0.0f);
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(1.0f, 0.0f, 0.0f);
glVertex3f(1.0f, 0.0f, 1.0f);
glVertex3f(0.0f, 0.0f, 1.0f);
glEnd();
// Left face
glBegin(GL_QUADS);
glNormal3f(-1.0f, 0.0f, 0.0f);
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(0.0f, 0.0f, 1.0f);
glVertex3f(0.0f, 1.0f, 1.0f);
glVertex3f(0.0f, 1.0f, 0.0f);
glEnd();
// Right face
glBegin(GL_QUADS);
glNormal3f(1.0f, 0.0f, 0.0f);
glVertex3f(1.0f, 0.0f, 0.0f);
glVertex3f(1.0f, 1.0f, 0.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
glVertex3f(1.0f, 0.0f, 1.0f);
glEnd();
glPopMatrix();
glPopMatrix();
}
void Player::drawBlade() const
{
// Set the blade color (silver)
glColor3f(
m_bladeColor.redF(),
m_bladeColor.greenF(),
m_bladeColor.blueF());
// Position for the blade (above the guard)
glPushMatrix();
glTranslatef(0.0f, m_handleLength + m_guardHeight, 0.0f);
// Draw blade as a rectangular block
glPushMatrix();
glScalef(m_bladeWidth, m_bladeLength, m_bladeThickness);
glTranslatef(-0.5f, 0.0f, -0.5f); // Center the blade
// Front face
glBegin(GL_QUADS);
glNormal3f(0.0f, 0.0f, 1.0f);
glVertex3f(0.0f, 0.0f, 1.0f);
glVertex3f(1.0f, 0.0f, 1.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
glVertex3f(0.0f, 1.0f, 1.0f);
glEnd();
// Back face
glBegin(GL_QUADS);
glNormal3f(0.0f, 0.0f, -1.0f);
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(0.0f, 1.0f, 0.0f);
glVertex3f(1.0f, 1.0f, 0.0f);
glVertex3f(1.0f, 0.0f, 0.0f);
glEnd();
// Top face
glBegin(GL_QUADS);
glNormal3f(0.0f, 1.0f, 0.0f);
glVertex3f(0.0f, 1.0f, 0.0f);
glVertex3f(0.0f, 1.0f, 1.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
glVertex3f(1.0f, 1.0f, 0.0f);
glEnd();
// Bottom face
glBegin(GL_QUADS);
glNormal3f(0.0f, -1.0f, 0.0f);
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(1.0f, 0.0f, 0.0f);
glVertex3f(1.0f, 0.0f, 1.0f);
glVertex3f(0.0f, 0.0f, 1.0f);
glEnd();
// Left face
glBegin(GL_QUADS);
glNormal3f(-1.0f, 0.0f, 0.0f);
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(0.0f, 0.0f, 1.0f);
glVertex3f(0.0f, 1.0f, 1.0f);
glVertex3f(0.0f, 1.0f, 0.0f);
glEnd();
// Right face
glBegin(GL_QUADS);
glNormal3f(1.0f, 0.0f, 0.0f);
glVertex3f(1.0f, 0.0f, 0.0f);
glVertex3f(1.0f, 1.0f, 0.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
glVertex3f(1.0f, 0.0f, 1.0f);
glEnd();
glPopMatrix();
glPopMatrix();
}
void Player::drawBladeTip() const
{
// Set the blade color (silver, same as blade)
glColor3f(
m_bladeColor.redF(),
m_bladeColor.greenF(),
m_bladeColor.blueF());
// Position for the blade tip (above the blade)
glPushMatrix();
glTranslatef(0.0f, m_handleLength + m_guardHeight + m_bladeLength, 0.0f);
// Draw blade tip as a pyramid
glPushMatrix();
// Pyramid base dimensions
float halfWidth = m_bladeWidth / 2.0f;
float halfThickness = m_bladeThickness / 2.0f;
// Pyramid base vertices (centered on origin)
float baseVertices[4][3] = {
{-halfWidth, 0.0f, -halfThickness},
{halfWidth, 0.0f, -halfThickness},
{halfWidth, 0.0f, halfThickness},
{-halfWidth, 0.0f, halfThickness}};
// Draw pyramid
glBegin(GL_TRIANGLES);
// Front face
glNormal3f(0.0f, m_tipLength, halfThickness);
glVertex3f(0.0f, m_tipLength, 0.0f); // Tip
glVertex3f(baseVertices[2][0], baseVertices[2][1], baseVertices[2][2]);
glVertex3f(baseVertices[3][0], baseVertices[3][1], baseVertices[3][2]);
// Back face
glNormal3f(0.0f, m_tipLength, -halfThickness);
glVertex3f(0.0f, m_tipLength, 0.0f); // Tip
glVertex3f(baseVertices[0][0], baseVertices[0][1], baseVertices[0][2]);
glVertex3f(baseVertices[1][0], baseVertices[1][1], baseVertices[1][2]);
// Left face
glNormal3f(-halfWidth, m_tipLength, 0.0f);
glVertex3f(0.0f, m_tipLength, 0.0f); // Tip
glVertex3f(baseVertices[3][0], baseVertices[3][1], baseVertices[3][2]);
glVertex3f(baseVertices[0][0], baseVertices[0][1], baseVertices[0][2]);
// Right face
glNormal3f(halfWidth, m_tipLength, 0.0f);
glVertex3f(0.0f, m_tipLength, 0.0f); // Tip
glVertex3f(baseVertices[1][0], baseVertices[1][1], baseVertices[1][2]);
glVertex3f(baseVertices[2][0], baseVertices[2][1], baseVertices[2][2]);
glEnd();
glPopMatrix();
glPopMatrix();
}
/**
* @brief Gets the position of the blade tip for more precise collision detection
* @return Position of the blade tip in world coordinates
*/
QVector3D Player::getBladeTipPosition() const
{
// Calculate the offset from the player position to the blade tip
float totalHeight = m_handleLength + m_guardHeight + m_bladeLength + m_tipLength;
// Start with the player position
QVector3D bladeTip = m_position;
// Apply rotations to calculate the blade tip position
float rotX = m_rotation.x() * M_PI / 180.0f;
float rotY = m_rotation.y() * M_PI / 180.0f;
float rotZ = m_rotation.z() * M_PI / 180.0f;
// Create a direction vector pointing along the blade
QVector3D bladeDir(0.0f, totalHeight, 0.0f);
// Apply Y rotation (most significant for sword orientation)
float cosY = cos(rotY);
float sinY = sin(rotY);
float newX = bladeDir.x() * cosY - bladeDir.z() * sinY;
float newZ = bladeDir.x() * sinY + bladeDir.z() * cosY;
bladeDir.setX(newX);
bladeDir.setZ(newZ);
// Add the direction vector to the base position
bladeTip += bladeDir;
return bladeTip;
}