-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmydraw.cpp
More file actions
164 lines (145 loc) · 4.32 KB
/
mydraw.cpp
File metadata and controls
164 lines (145 loc) · 4.32 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
#include <ode/ode.h>
#include "drawstuff/drawstuff.h"
#include "common.h"
#include "mydraw.h"
#include <iostream>
// select correct drawing functions (Alon: this should not be here)
// draw a geom (from test_boxstack)
void drawGeom(dGeomID g, const dReal * pos, const dReal * R)
{
if (!g)
return;
if (!pos)
pos = dGeomGetPosition(g);
if (!R)
R = dGeomGetRotation(g);
int type = dGeomGetClass(g);
if (type == dBoxClass) {
dVector3 sides;
dGeomBoxGetLengths(g, sides);
dsDrawBox(pos, R, sides);
} else if (type == dSphereClass) {
dsDrawSphere(pos, R, dGeomSphereGetRadius(g));
} else if (type == dCCylinderClass) {
dReal radius, length;
dGeomCCylinderGetParams(g, &radius, &length);
dsDrawCappedCylinder(pos, R, length, radius);
} else if (type == dGeomTransformClass) {
dGeomID g2 = dGeomTransformGetGeom(g);
const dReal *pos2 = dGeomGetPosition(g2);
const dReal *R2 = dGeomGetRotation(g2);
dVector3 actual_pos;
dMatrix3 actual_R;
dMULTIPLY0_331(actual_pos, R, pos2);
actual_pos[0] += pos[0];
actual_pos[1] += pos[1];
actual_pos[2] += pos[2];
dMULTIPLY0_333(actual_R, R, R2);
drawGeom(g2, actual_pos, actual_R);
}
}
void drawCylinder(dGeomID g, const dReal * pos, const dReal * R)
{
if (!g)
return;
if (!pos)
pos = dGeomGetPosition(g);
if (!R)
R = dGeomGetRotation(g);
int type = dGeomGetClass(g);
if (type == dBoxClass) {
} else if (type == dSphereClass) {
float radius = dGeomSphereGetRadius(g);
float length = radius / 10.0;
dsDrawCylinder (pos, R, length, radius);
} else if (type == dCCylinderClass) {
dReal radius, length;
dGeomCCylinderGetParams(g, &radius, &length);
dsDrawCylinder(pos, R, length, radius);
} else if (type == dGeomTransformClass) {
dGeomID g2 = dGeomTransformGetGeom(g);
const dReal *pos2 = dGeomGetPosition(g2);
const dReal *R2 = dGeomGetRotation(g2);
dVector3 actual_pos;
dMatrix3 actual_R;
dMULTIPLY0_331(actual_pos, R, pos2);
actual_pos[0] += pos[0];
actual_pos[1] += pos[1];
actual_pos[2] += pos[2];
dMULTIPLY0_333(actual_R, R, R2);
drawCylinder(g2, actual_pos, actual_R);
}
}
void drawCCS(dBodyID bod, dReal ax_len)
{
int i;
const dReal *pos = dBodyGetPosition(bod);
const dReal *R = dBodyGetRotation(bod);
// draw X, Y, Z axes
dReal end[3];
for (i = 0; i < 3; ++i)
end[i] = pos[i] + ax_len * R[4 * i];
dsDrawLine(pos, end);
for (i = 0; i < 3; ++i)
end[i] = pos[i] + ax_len * R[1 + 4 * i];
dsDrawLine(pos, end);
for (i = 0; i < 3; ++i)
end[i] = pos[i] + ax_len * R[2 + 4 * i];
dsDrawLine(pos, end);
}
void drawForces(dBodyID bod)
{
/*
const dReal* pos = dBodyGetPosition(bod);
const dReal* force = dBodyGetOldForce(bod);
dReal end[3];
end[XX] = pos[XX] + force[XX];
end[YY] = pos[YY] + force[YY];
end[ZZ] = pos[ZZ] + force[ZZ];
dsDrawLine(pos, end);
*/
}
// creates a level view at the center of body. meaning h and p are calculated,
// and r = 0.
void aimCamera(dBodyID body)
{
const dReal *pos = dBodyGetPosition(body);
float xyz[3], hpr[3], delta[3];
int i;
dsGetViewpoint(xyz, hpr);
for (i = 0; i < 3; ++i)
delta[i] = pos[i] - xyz[i];
hpr[0] = 180.0 / M_PI * atan2(delta[YY], delta[XX]); // heading
hpr[1] = 180.0 / M_PI * atan2(delta[ZZ], sqrt(delta[XX] * delta[XX] + delta[YY] * delta[YY])); // pitch
//std::cout << "debug: heading: " << hpr[0] << "\n";
//std::cout << "debug: pitch: " << hpr[1] << "\n";
hpr[2] = 0;
dsSetViewpoint(xyz, hpr);
}
// creates a level view at the center of body. meaning h and p are calculated,
// and r = 0.
// also follow to a defined distance away - between to numbers
void aimCamera(dBodyID body, dReal min, dReal max)
{
const dReal *pos = dBodyGetPosition(body);
float xyz[3], hpr[3], delta[3];
int i;
dsGetViewpoint(xyz, hpr);
for (i = 0; i < 3; ++i)
delta[i] = pos[i] - xyz[i];
hpr[0] = 180.0 / M_PI * atan2(delta[YY], delta[XX]); // heading
hpr[1] = 180.0 / M_PI * atan2(delta[ZZ], sqrt(delta[XX] * delta[XX] + delta[YY] * delta[YY])); // pitch
//std::cout << "debug: heading: " << hpr[0] << "\n";
//std::cout << "debug: pitch: " << hpr[1] << "\n";
hpr[2] = 0;
dReal dist =
sqrt(xyz[0] * xyz[0] + xyz[1] * xyz[1] + xyz[2] * xyz[2]);
if (dist > max) {
dReal part =
(dist - max) / sqrt(delta[0] * delta[0] + delta[1] +
delta[1] + delta[2] * delta[2]);
for (i = 0; i < 3; ++i)
xyz[i] += part * delta[i];
}
dsSetViewpoint(xyz, hpr);
}