-
Notifications
You must be signed in to change notification settings - Fork 0
/
crab.cpp
181 lines (150 loc) · 4.24 KB
/
crab.cpp
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
#ifndef __APPLE__
#include <GL/glut.h>
#else
#include <GLUT/glut.h>
#endif
#include "crab.h"
#define PI 3.14159
// static variables
GLfloat crab::material[4] = {0.5f, 0.5f, 0.5f, 1.f};
GLfloat crab::shininess = 50.f;
// default constructor
crab::crab() {
x = 1; y = -5; z = +25;
xInc = 0.01; yInc = 0.0; zInc = +0.05;
quadric = gluNewQuadric();
oldIncrement=sqrt((xInc*xInc)+(zInc*zInc))*0.1;
}
// constructor with parameters
crab::crab(float posx, float posz,float velx, float velz) {
x = posx; y = -5; z=posz;
xInc = velx; yInc = 0.0; zInc = velz;
quadric = gluNewQuadric();
oldIncrement=sqrt((xInc*xInc)+(zInc*zInc))*0.1;
}
// method used to update the position of the crab
// It tries to put some smooth random in the movements of the crabs.
void crab::animate(void) {
float speed = sqrt((xInc*xInc)+(zInc*zInc));
maxIncrement = speed*0.1;
float Increment = (2*maxIncrement*((float)rand()/(float)RAND_MAX))-maxIncrement;
float zdelta;
Increment = (oldIncrement*9+Increment)/10;
xInc=xInc+Increment;
//speed = sqrt((xInc*xInc)+(zInc*zInc));
if ((speed * speed)>(xInc * xInc)) {
zdelta = sqrt((speed * speed)-(xInc * xInc));
if (zInc >= 0)
zInc = +zdelta;
else
zInc = -zdelta;
}
else {
zInc=0;
xInc=speed;
}
oldIncrement=Increment;
}
// method used to modelize the crab
void crab::draw(void) {
// increment the crab position
x += xInc;
y += yInc;
z += zInc;
glPushMatrix();
glTranslatef(x, y, z); // move to where it is right now
glRotatef(180, 0, 0, 1);
// getCrossProduct(xInc, yInc, zInc, 0, 0, 1); // get cross product // debug
// glRotatef(getAngle(xInc, yInc, zInc, 0, 0, 1), xcp, ycp, zcp); // rotate to look to where it is moving
//glRotatef(180, xInc, yInc, zInc-(vectorDistance(xInc,yInc, zInc)));
//glRotatef(180,)
// std::cout << "Angle : " << getAngle(xInc,yInc,zInc,0,0,1) << std::endl;
// std::cout << "vector : " << x << " " << y << " " << z << std::endl;
if ((xcp != 0.0) || (ycp != 0.0) || (zcp != 0.0)) { // orient fish to look where it's going
glRotatef(-getAngle(xInc,yInc,zInc,0,0,1), 0, 1.0, 0);
}
else {
if(getAngle(xInc,yInc,zInc,0,0,1)>179.5)
glRotatef(180.0, 0, 1.0, 0);
}
glColor3f(1.0f, 0.45f, 0.45f);
// body
glPushMatrix();
glScalef(1.0f, 0.5f, 1.0f);
gluSphere(quadric, 0.3f, 16, 16);
glPopMatrix();
// legs
glPushMatrix();
drawLegs();
glScalef(-1.f, 1.f, 1.f);
glFrontFace(GL_CW);
drawLegs();
glFrontFace(GL_CCW);
glPopMatrix();
glColor3f(0.0f, 0.0f, 0.0f);
// left eye
glTranslatef(-0.06f, 0.0f, 0.3f);
glutSolidSphere(0.05f, 12, 8);
// right eye
glTranslatef(0.12f, 0.0f, 0.0f);
glutSolidSphere(0.05f, 12, 8);
glPopMatrix();
}
// Draws a leg using an angle
void crab::drawLeg(GLfloat jointAngle, GLfloat jointOffset)
{
// draw first part of a leg
glPushMatrix();
glTranslatef(-0.38f, 0.0f, 0.0f);
glScalef(3.0f, 1.0f, 1.0f);
glutSolidCube(0.06f);
glPopMatrix();
// draw second part of a leg
glPushMatrix();
glTranslatef(-0.53f, jointOffset, 0.0f);
glRotatef(jointAngle, 0.0f, 0.0f, 1.0f);
glScalef(4.0f, 1.0f, 1.0f);
glutSolidCube(0.06f);
glPopMatrix();
}
// Draws one leg
void crab::drawLeg()
{
drawLeg(-45.0f, 0.075f);
}
// Draws complete set of legs (side legs)
void crab::drawLegs()
{
glColor3f(1.0f, 0.55f, 0.55f);
// draw three side legs
for (GLfloat i = -15.0f; i <= 15.0f; i += 15.0f)
{
glPushMatrix();
glTranslatef(0.0f, 0.0f, -0.025f);
glRotatef(i, 0.0f, 1.0f, 0.0f);
drawLeg();
glPopMatrix();
}
// draw fourth leg
glPushMatrix();
glTranslatef(0.0f, 0.0f, -0.00f);
glRotatef(-65.0f, -0.2f, 1.0f, 0.0f);
drawLeg(0.0f, 0.0f);
glPopMatrix();
// alteration of the color
glColor3f(1.0f, 0.65f, 0.65f);
// front leg
glPushMatrix();
glTranslatef(0.0f, 0.0f, 0.0f);
glRotatef(55.0f, 0.0f, 1.0f, 0.0f);
glRotatef(90.0f, 1.0f, 0.0f, 0.0f);
drawLeg();
glPopMatrix();
// arms
glPushMatrix();
glTranslatef(0.24f, 0.0f, 0.725f);
glRotatef(-15.0f, 0.0f, 1.0f, 0.0f);
glRotatef(90.0f, 1.0f, 0.0f, 0.0f);
drawLeg(-60.0f, 0.1f);
glPopMatrix();
}