-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathrobot.js
249 lines (233 loc) · 8.91 KB
/
robot.js
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
/** Robot class for creating a bot in Planck.js' environment */
class Robot {
/**
* Takes in world, size, x, y, id
* @constructor
* @param {Planck.World} world
* @param {number} x
* @param {number} y
* @param {number} id
*/
constructor(world, x, y, id) {
this.world = world;
this.size = this.world.sceneSize;
this.x = x;
this.y = y;
this.id = id;
this.canUpdateScore = true;
this.bodyParts = {};
this.joints = {};
this.createBody(this.size, x, y);
this.jointsKeys = Object.keys(this.joints);
this.jointsLegKeys = [];
this.jointsKeys.forEach((part) => {
if (part.includes('Leg')) this.jointsLegKeys.push(part);
});
this.bodyPartsKeys = Object.keys(this.bodyParts);
this.legPartsKeys = [];
this.bodyPartsKeys.forEach((part) => {
if (part.includes('Leg')) this.legPartsKeys.push(part);
});
};
// METHODS FOR CREATING PLACNK.JS PHYSICS BODIES AND JOINTS
/**
* Creates robot bodies by constructing necessary shapes and joints
* @param {number} size - scene size
* @param {number} x - birth x position
* @param {number} y - birth y position
*/
createBody(size, x, y) {
this.coreBody(size, x, y);
this.limb('leftLeg', size / 3, x - size / 1.5, y - size / 0.285);
this.limb('rightLeg', size / 3, x + size / 1.5, y - size / 0.285);
this.limb('leftArm', size / 3.5, x - size * 3.6, y + size * 2.5, true);
this.limb('rightArm', size / 3.5, x + size * 1.9, y + size * 2.5, true, false);
const rotAngle = degToRad(25);
this.createJoint('leftLegUp', this.bodyParts.lower, this.bodyParts.leftLegUp, pl.Vec2(x - size / 1.3333, y - size / 2), rotAngle, rotAngle);
this.createJoint('rightLegUp', this.bodyParts.lower, this.bodyParts.rightLegUp, pl.Vec2(x + size / 1.3333, y - size / 2), rotAngle, rotAngle);
this.createJoint('leftArmUp', this.bodyParts.upper, this.bodyParts.leftArmUp, { x: x - size / 1.3333, y: y + size / 0.4 }, Math.PI / 3);
this.createJoint('rightArmUp', this.bodyParts.upper, this.bodyParts.rightArmUp, { x: x + size / 1.3333, y: y + size / 0.4 }, Math.PI / 3);
};
/**
* Creates robot chest and stomach by constructing necessary shapes and joints
* @param {number} size - scene size
* @param {number} x - birth x position
* @param {number} y - birth y position
*/
coreBody(size, x, y) {
const w = size, h = size;
const boxshpUp = pl.Box(w, h);
const boxshpLow = pl.Box(w, h / 2);
const headshp = pl.Circle(w / 1.5);
this.bodyParts.lower = this.body_fixture(x, y, boxshpLow);
this.bodyParts.head = this.body_fixture(x, y + h * 3.4, headshp);
this.bodyParts.upper = this.body_fixture(x, y + h * 1.5, boxshpUp);
this.createJoint('spine', this.bodyParts.upper, this.bodyParts.lower, { x: x, y: y + h / 2 }, Math.PI / 8);
this.createJoint('neck', this.bodyParts.upper, this.bodyParts.head, { x: x, y: y + h * 3 }, Math.PI / 6);
};
/**
* Creates robot arm/leg by constructing necessary shapes and joints
* @param {number} size - scene size
* @param {number} x - birth x position
* @param {number} y - birth y position
* @param {boolean} rotate - upper or lower limb
* @param {boolean} left - left or right limb
* @returns {Planck.DynamicBody} - limb
*/
limb(name, size, x, y, rotate = false, left = true) {
var w = size, h = 3 * size;
if (rotate) w = 3 * size, h = size;
const boxshp = pl.Box(w, h);
const jointName = name + 'Low';
const rotAngle = degToRad(25);
if (rotate) {
const upper = this.body_fixture(x + w * 2, y, boxshp);
const lower = this.body_fixture(x, y, boxshp);
this.createJoint(jointName, upper, lower, { x: x + w, y: y }, rotAngle);
if (left) {
this.bodyParts[name + 'Up'] = upper;
this.bodyParts[name + 'Low'] = lower;
return upper;
}
this.bodyParts[name + 'Up'] = lower;
this.bodyParts[name + 'Low'] = upper;
return lower;
}
const lower = this.body_fixture(x, y, boxshp);
const upper = this.body_fixture(x, y + h * 2, boxshp);
this.createJoint(jointName, upper, lower, { x: x, y: y + h }, rotAngle);
this.bodyParts[name + 'Up'] = upper;
this.bodyParts[name + 'Low'] = lower;
return upper;
};
/**
* Creates Planck body fixture and adds it to the world
* @param {number} x
* @param {number} y
* @param {Planck.Shape} shp
* @param {number} density
* @returns {Planck.DynamicBody}
*/
body_fixture(x, y, shp, density = 0.07) {
const body_part = this.world.createDynamicBody(Vec2(x, y));
body_part.createFixture(shp, density);
body_part.m_fixtureList.m_filterGroupIndex = -1;
return body_part;
};
/**
* Creates RevoluteJoint
* @param {string} name - label of joint
* @param {Planck.DynamicBody} bodyA - connecting bodyA
* @param {Planck.DynamicBody} bodyB - connecting bodyB
* @param {Planck.Vec2} anchor - anchor of joint
* @param {number} lowerAngle - min angle joint can reach
* @param {number} upperAngle - max angle joint can reach
*/
createJoint(name, bodyA, bodyB, anchor, lowerAngle, upperAngle = lowerAngle) {
const limits = {
lowerAngle: -lowerAngle,
upperAngle: upperAngle,
maxMotorTorque: 100.0,
enableLimit: true,
motorSpeed: 0,
enableMotor: true
}
const joint = this.world.createJoint(pl.RevoluteJoint(limits, bodyA, bodyB, anchor));
this.joints[name] = joint;
};
// METHODS THAT MAKE THE BOT MOVE AND INTERACT WITH THE NN AND GA ALGOS
/**
* Makes the bot move every 50 ms
*/
start() {
this.interval = setInterval(() => {
this.think();
this.updateScore();
}, 50);
};
/**
* Makes the bot move by getting an output fron neurla networks brain
*/
think(result) {
const MOTOR_SPEED = 30;
for (let i = 0; i < result.length; i++) {
const jt = this.joints[this.jointsLegKeys[i]];
if (result[i] > .5) {
jt.setMotorSpeed(MOTOR_SPEED);
} else {
jt.setMotorSpeed(-MOTOR_SPEED);
}
}
};
/**
* Creates input for the neural networks brain
* @returns {array} - the created input for the brain
*/
createBrainInput() {
let input = [];
this.jointsKeys.forEach((jointKey) => {
if (jointKey.includes('Leg')) {
var jt = this.joints[jointKey];
var value = this.mapRange(jt.getJointAngle(), jt.getLowerLimit(), jt.getUpperLimit());
input.push(value);
}
});
// head vertical position
const vtcl = this.world.vtcl;
const hztl = this.world.hztl;
const head_y = this.mapRange(this.bodyParts.head.c_position.c.y, vtcl.min, vtcl.max);
input.push(head_y);
const head_x = this.mapRange(this.bodyParts.head.c_position.c.x, hztl.min, hztl.max);
input.push(head_x);
return input;
};
/**
* Updates bot's fitness score by getting coordiantes of bot's head position
*/
updateScore(scoreHolder) {
const vtcl = this.world.vtcl;
var head_y = this.mapRange(this.bodyParts.head.c_position.c.y, vtcl.min, vtcl.max);
if (this.canUpdateScore) {
scoreHolder.score = this.bodyParts.head.c_position.c.x - this.x;
}
if (head_y < .6) {
this.canUpdateScore = false;
scoreHolder.score = - 1000;
};
};
// METHODS MISCELLAENOUS
/**
* Kills the bot and deallocs all memory the bot used
*/
kill() {
// remove from world
Object.values(this.joints).forEach((jointsPart) => {
this.world.destroyJoint(jointsPart);
});
Object.values(this.bodyParts).forEach((bodyPart) => {
this.world.destroyBody(bodyPart);
});
clearInterval(this.interval);
this.bodyParts = null;
this.bodyPartsKeys = null;
this.joints = null;
this.jointsKeys = null;
};
/**
* Maps a number from input range to output range
* @param {number} num
* @param {number} in_min
* @param {number} in_max
* @param {number} out_min
* @param {number} out_max
*/
mapRange(num, in_min, in_max, out_min = 0.0, out_max = 1.0) {
var result = (num - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
if (result > out_max) {
return out_max;
} else if (result < out_min) {
return out_min;
}
return result;
};
};