-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmboxlinkdesign.cpp
More file actions
149 lines (129 loc) · 4.04 KB
/
mboxlinkdesign.cpp
File metadata and controls
149 lines (129 loc) · 4.04 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
#include "mboxlinkdesign.h"
#include "error.h"
#include "myobj.h"
#include "track.h"
// forward declaration (implementation at end of file)
void CreateLink(dWorldID world, dSpaceID space,
dGeomID * geom, dBodyID * body,
const dReal(*link_sides)[3]);
int MBoxLinkDesign::getNG()
{
return LINK_PARTS;
};
void MBoxLinkDesign::create(dWorldID world, dSpaceID space, dGeomID * geom,
dBodyID * body)
{
CreateLink(world, space, geom, body, link_sides);
};
dReal MBoxLinkDesign::calcSprocketRadius(int num)
{
dReal rad = getJJLength() / 2 / tan(M_PI / num);
return rad - link_sides[0][ZZ] / 2; // this is without " - spacer"
};
dReal MBoxLinkDesign::calcJointArrow()
{
return link_sides[0][YY] + link_sides[2][YY] + link_sides[4][YY];
}
void MBoxLinkDesign::setJJLength(dReal new_jj)
{
dReal cur_jj = getJJLength();
for (int i = 0; i < LINK_PARTS; ++i) link_sides[i][XX] *= new_jj / cur_jj;
}
////////////////////////////////////////////////////////////
// CreateLinkGeom
//
// creates LINK_PARTS geometries
// (using CreateGeom. no "new" operator is used directly)
//
////
int CreateLinkGeom(dSpaceID space, dGeomID * link_g, dGeomID * g2,
const dReal(*link_sides)[3])
{
if (link_g == NULL || g2 == NULL) {
std::
cout <<
"warning: CreateLinkGeom called with null pointers\n";
return 0;
}
// create link geometry.
// it has some lee way, the hard coded stuff is the relation between the pieces
// and not actual lengths.
// create geoms (real and child) and give them correct size
for (int j = 0; j < LINK_PARTS; ++j) {
link_g[j] = dCreateGeomTransform(space);
g2[j] = dCreateBox(0, link_sides[j][0], link_sides[j][1],
link_sides[j][2]);
dGeomTransformSetGeom(link_g[j], g2[j]);
}
// give correct position
dGeomSetPosition(g2[0], -(link_sides[1][XX] + link_sides[0][XX]) / 2,
(2 * link_sides[1][YY] + link_sides[5][YY] -
link_sides[0][YY]) / 2, 0);
dGeomSetPosition(g2[1], 0, (link_sides[1][YY] + link_sides[5][YY]) / 2, 0);
dGeomSetPosition(g2[2], (link_sides[1][XX] + link_sides[2][XX]) / 2, 0, 0);
dGeomSetPosition(g2[3], 0, -(link_sides[3][YY] + link_sides[5][YY]) / 2, 0);
dGeomSetPosition(g2[4], -(link_sides[3][XX] + link_sides[4][XX]) / 2,
-(2 * link_sides[3][YY] + link_sides[5][YY] -
link_sides[4][YY]) / 2, 0);
dGeomSetPosition(g2[5], -(link_sides[1][XX] - link_sides[5][XX]) / 2, 0, 0);
dGeomSetPosition(g2[6], (link_sides[1][XX] - link_sides[6][XX]) / 2, 0, 0);
dReal outer_part_z = (link_sides[0][ZZ] + link_sides[7][ZZ]) / 2.0;
dGeomSetPosition(g2[7], 0, 0, -outer_part_z);
return LINK_PARTS;
}
// recieves MyObjectID assuming it has preallocated body, geom, joint
void CreateLink(dWorldID world, dSpaceID space,
dGeomID * geom, dBodyID * body,
const dReal(*link_sides)[3])
{
int i;
// PLAN
// ====
// create enough id's
// allocate mass
// allocate bodies
// allocate joints(or none)
// allocate geoms
//
//dGeomID* g2 = new dGeomID[LINK_PARTS];
dGeomID g2[LINK_PARTS];
// Body
// ====
dBodyID link;
body[0] = link = dBodyCreate(world);
// Geom
// ====
// TODO: use a group geom
int g_num = CreateLinkGeom(space, geom, g2, link_sides);
for (i = 0; i < g_num; ++i)
dGeomSetBody(geom[i], link);
// Mass
// ====
dMass m;
dMassSetZero(&m);
for (i = 0; i < LINK_PARTS; ++i) {
dMass m2;
dMassSetBox(&m2, 1, link_sides[i][XX], link_sides[i][YY],
link_sides[i][ZZ]);
const dReal *pos = dGeomGetPosition(g2[i]);
dMassTranslate(&m2, pos[0], pos[1], pos[2]);
pos = dGeomGetRotation(g2[i]);
dMassRotate(&m2, pos);
dMassAdd(&m, &m2);
}
//PEXP(m.c[0]);
//PEXP(m.c[1]);
//PEXP(m.c[2]);
// TODO: move all encapsulated objects so that the center of mass is (0,0,0)
{
for ( i = 0 ; i < LINK_PARTS ; i++) {
const dReal *pos = dGeomGetPosition(g2[i]);
dGeomSetPosition (g2[i], pos[0] - m.c[0], pos[1] - m.c[1],
pos[2] - m.c[2]);
}
dMassTranslate (&m, -m.c[0], -m.c[1], -m.c[2]);
}
dMassAdjust(&m, LINK_MASS);
dBodySetMass(link, &m);
//delete g2;
}