-
Notifications
You must be signed in to change notification settings - Fork 2
/
Ball.cpp
175 lines (154 loc) · 4 KB
/
Ball.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
/*
* Caleb Everett
* graphics final
*
* Ball.cpp
*/
#if !defined(BALL_CPP)
#define BALL_CPP
#include "Ball.h"
#define MASS .6
#define RADIUS 1.125
#define SOLID 0
#define STRIPE 1
#define CUE 2
#define EIGHT 3
#define WHITE 0
#define YELLOW 1
#define BLUE 2
#define RED 3
#define PURPLE 4
#define ORANGE 5
#define GREEN 6
#define BROWN 7
#define BLACK 8
Ball::Ball()
{
Ball(0,RADIUS,0, WHITE, SOLID);
}
Ball::Ball(float xTmp, float yTmp, float zTmp, int colorTmp, int typeTmp)
{
x = xTmp;
y = yTmp;
z = zTmp;
color = colorTmp;
type = typeTmp;
}
void Ball::add(btDiscreteDynamicsWorld *world)
{
shape = new btSphereShape(RADIUS);
motionState =
new btDefaultMotionState(btTransform(btQuaternion(rand(),rand(),rand(),1), btVector3(x,y,z)));
btScalar mass = MASS;
btVector3 inertia(0,0,0);
shape->calculateLocalInertia(mass,inertia);
btRigidBody::btRigidBodyConstructionInfo rigidBodyCI(mass,motionState,shape,inertia);
rigidBody = new btRigidBody(rigidBodyCI);
rigidBody->setRestitution(0.9);
rigidBody->setDamping(0,0.8);
world->addRigidBody(rigidBody);
}
void Ball::draw(){
glPushMatrix();
glMultMatrixf(ballMatrix);
// draws the sphere with a red stripe
int j, i; // iterators
int q = 5; // slices
int p = 10; // stacks
float R = RADIUS; // radius
float Color[3] = {0, 0, 0};
switch(color)
{
case WHITE:
Color[0] = 1.0; Color[1] = 1.0; Color[2] = 1.0;
break;
case YELLOW:
Color[0] = 1.0; Color[1] = 1.0; Color[2] = 0.0;
break;
case BLUE:
Color[0] = 0.0; Color[1] = 0.0; Color[2] = 1.0;
break;
case RED:
Color[0] = 1.0; Color[1] = 0.0; Color[2] = 0.0;
break;
case PURPLE:
Color[0] = 0.5; Color[1] = 0.0; Color[2] = 0.5;
break;
case ORANGE:
Color[0] = 1.0; Color[1] = 0.5; Color[2] = 0.0;
break;
case GREEN:
Color[0] = 0.0; Color[1] = 1.0; Color[2] = 0.0;
break;
case BROWN:
Color[0] = 0.4; Color[1] = 0.2; Color[2] = 0.0;
break;
case BLACK:
Color[0] = 0.0; Color[1] = 0.0; Color[2] = 0.0;
break;
default:
break;
}
for(j = 0; j < q; j++)
{
if ((j < q * .25) || type == SOLID) glColor3f(Color[0], Color[1], Color[2]);
else glColor3f(1.0, 1.0, 1.0); // white for outer area
if (type == SOLID && (j > q * .55)) glColor3f(1.0, 1.0, 1.0);
glBegin(GL_TRIANGLE_STRIP);
for(i = 0; i <= p; i++)
{
glVertex3f( R * cos( (float)(j+1)/q * PI/2.0 ) * cos( 2.0 * (float)i/p * PI ),
R * sin( (float)(j+1)/q * PI/2.0 ),
R * cos( (float)(j+1)/q * PI/2.0 ) * sin( 2.0 * (float)i/p * PI ) );
glVertex3f( R * cos( (float)j/q * PI/2.0 ) * cos( 2.0 * (float)i/p * PI ),
R * sin( (float)j/q * PI/2.0 ),
R * cos( (float)j/q * PI/2.0 ) * sin( 2.0 * (float)i/p * PI ) );
}
glEnd();
if (type == SOLID && (j > q * .5)) glColor3f(Color[0], Color[1],Color[2]);
glBegin(GL_TRIANGLE_STRIP);
for(i = 0; i <= p; i++)
{
glVertex3f( -R * cos( (float)(j+1)/q * PI/2.0 ) * cos( 2.0 * (float)i/p * PI ),
-R * sin( (float)(j+1)/q * PI/2.0 ),
-R * cos( (float)(j+1)/q * PI/2.0 ) * sin( 2.0 * (float)i/p * PI ) );
glVertex3f( -R * cos( (float)j/q * PI/2.0 ) * cos( 2.0 * (float)i/p * PI ),
-R * sin( (float)j/q * PI/2.0 ),
-R * cos( (float)j/q * PI/2.0 ) * sin( 2.0 * (float)i/p * PI ) );
}
glEnd();
}
glPopMatrix();
}
void Ball::update()
{
btTransform trans;
rigidBody->getMotionState()->getWorldTransform(trans);
trans.getOpenGLMatrix(ballMatrix); // set ballMatrix to the matrix from opengl
}
void Ball::reset()
{
rigidBody->setLinearVelocity(btVector3(0, 0, 0));
rigidBody->setAngularVelocity(btVector3(0, 0, 0));
delete rigidBody->getMotionState();
motionState =
new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1), btVector3(x, y, z)));
rigidBody->setMotionState(motionState);
}
btRigidBody* Ball::getBody()
{
return rigidBody;
}
btTransform Ball::getTrans()
{
btTransform trans;
rigidBody->getMotionState()->getWorldTransform(trans);
return trans;
}
Ball::~Ball()
{
// dynamicsWorld->removeRigidBody(rigidBody);
// delete shape;
// delete rigidBody;
}
#endif