2 Copyright (C) 2003, 2010 - Wolfire Games
3 Copyright (C) 2010-2016 - Lugaru contributors (see AUTHORS file)
5 This file is part of Lugaru.
7 Lugaru is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 Lugaru is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Lugaru. If not, see <http://www.gnu.org/licenses/>.
21 /**> HEADER FILES <**/
24 #include "openal_wrapper.h"
25 #include "Animation.h"
27 extern float multiplier;
29 extern Terrain terrain;
30 extern Objects objects;
31 extern int environment;
32 extern float camerashake;
35 extern int tutoriallevel;
37 extern int whichjointstartarray[26];
38 extern int whichjointendarray[26];
40 extern bool visibleloading;
44 void dealloc2(void* param)
49 enum {boneconnect, constraint, muscle};
53 * sets strength, length,
54 * parent1->position, parent2->position,
55 * parent1->velocity, parent2->velocity
59 * Skeleton::DoConstraints
61 void Muscle::DoConstraint(bool spinny)
63 // FIXME: relaxlength shouldn't be static, but may not always be set
64 // so I don't want to change the existing behavior even though it's probably a bug
65 static float relaxlength;
67 float oldlength = length;
69 if (type != boneconnect)
70 relaxlength = findDistance(&parent1->position, &parent2->position);
72 if (type == boneconnect)
74 if (type == constraint)
83 length -= (length - relaxlength) * (1 - strength) * multiplier * 10000;
84 length -= (length - targetlength) * (strength) * multiplier * 10000;
88 if ((relaxlength - length > 0 && relaxlength - oldlength < 0) || (relaxlength - length < 0 && relaxlength - oldlength > 0))
92 if (length < minlength)
94 if (length > maxlength)
97 if (length == relaxlength)
103 XYZ midp = (parent1->position * parent1->mass + parent2->position * parent2->mass) / (parent1->mass + parent2->mass);
105 //Find vector from midpoint to second vector
106 XYZ vel = parent2->position - midp;
108 //Change to unit vector
111 //Apply velocity change
112 XYZ newpoint1 = midp - vel * length * (parent2->mass / (parent1->mass + parent2->mass));
113 XYZ newpoint2 = midp + vel * length * (parent1->mass / (parent1->mass + parent2->mass));
114 if (!freeze && spinny) {
115 parent1->velocity = parent1->velocity + (newpoint1 - parent1->position) / multiplier / 4;
116 parent2->velocity = parent2->velocity + (newpoint2 - parent2->position) / multiplier / 4;
118 parent1->velocity = parent1->velocity + (newpoint1 - parent1->position);
119 parent2->velocity = parent2->velocity + (newpoint2 - parent2->position);
122 //Move child point to within certain distance of parent point
123 parent1->position = newpoint1;
124 parent2->position = newpoint2;
128 * sets forward, lowforward, specialforward[]
132 * Person/Person::DoAnimations
133 * Person/Person::DrawSkeleton
135 void Skeleton::FindForwards()
137 //Find forward vectors
138 CrossProduct(joints[forwardjoints[1]].position - joints[forwardjoints[0]].position, joints[forwardjoints[2]].position - joints[forwardjoints[0]].position, &forward);
141 CrossProduct(joints[lowforwardjoints[1]].position - joints[lowforwardjoints[0]].position, joints[lowforwardjoints[2]].position - joints[lowforwardjoints[0]].position, &lowforward);
142 Normalise(&lowforward);
145 specialforward[0] = forward;
147 specialforward[1] = jointPos(rightshoulder) + jointPos(rightwrist);
148 specialforward[1] = jointPos(rightelbow) - specialforward[1] / 2;
149 specialforward[1] += forward * .4;
150 Normalise(&specialforward[1]);
151 specialforward[2] = jointPos(leftshoulder) + jointPos(leftwrist);
152 specialforward[2] = jointPos(leftelbow) - specialforward[2] / 2;
153 specialforward[2] += forward * .4;
154 Normalise(&specialforward[2]);
156 specialforward[3] = jointPos(righthip) + jointPos(rightankle);
157 specialforward[3] = specialforward[3] / 2 - jointPos(rightknee);
158 specialforward[3] += lowforward * .4;
159 Normalise(&specialforward[3]);
160 specialforward[4] = jointPos(lefthip) + jointPos(leftankle);
161 specialforward[4] = specialforward[4] / 2 - jointPos(leftknee);
162 specialforward[4] += lowforward * .4;
163 Normalise(&specialforward[4]);
170 * Person/Person::RagDoll
171 * Person/Person::DoStuff
174 float Skeleton::DoConstraints(XYZ *coords, float *scale)
176 float friction = 1.5;
177 const float elasticity = .3;
179 const int numrepeats = 3;
180 float groundlevel = .15;
189 float damage = 0; // eventually returned from function
190 bool breaking = false;
193 freetime += multiplier;
195 whichpatchx = coords->x / (terrain.size / subdivision * terrain.scale);
196 whichpatchz = coords->z / (terrain.size / subdivision * terrain.scale);
198 terrainlight = *coords;
199 objects.SphereCheckPossible(&terrainlight, 1);
202 for (i = 0; i < num_joints; i++) {
203 joints[i].position = joints[i].position + joints[i].velocity * multiplier;
205 switch (joints[i].label) {
222 joints[i].position.y -= groundlevel;
223 joints[i].oldvelocity = joints[i].velocity;
226 float tempmult = multiplier;
227 //multiplier/=numrepeats;
229 for (j = 0; j < numrepeats; j++) {
231 // right leg constraints?
232 if (!joint(rightknee).locked && !joint(righthip).locked) {
233 temp = jointPos(rightknee) - (jointPos(righthip) + jointPos(rightankle)) / 2;
234 while (normaldotproduct(temp, lowforward) > -.1 && !sphere_line_intersection(&jointPos(righthip), &jointPos(rightankle), &jointPos(rightknee), &r)) {
235 jointPos(rightknee) -= lowforward * .05;
237 jointVel(rightknee) -= lowforward * .05 / multiplier / 4;
239 jointVel(rightknee) -= lowforward * .05;
240 jointPos(rightankle) += lowforward * .025;
242 jointVel(rightankle) += lowforward * .025 / multiplier / 4;
244 jointVel(rightankle) += lowforward * .25;
245 jointPos(righthip) += lowforward * .025;
247 jointVel(righthip) += lowforward * .025 / multiplier / 4;
249 jointVel(righthip) += lowforward * .025;
250 temp = jointPos(rightknee) - (jointPos(righthip) + jointPos(rightankle)) / 2;
254 // left leg constraints?
255 if (!joint(leftknee).locked && !joint(lefthip).locked) {
256 temp = jointPos(leftknee) - (jointPos(lefthip) + jointPos(leftankle)) / 2;
257 while (normaldotproduct(temp, lowforward) > -.1 && !sphere_line_intersection(&jointPos(lefthip), &jointPos(leftankle), &jointPos(leftknee), &r)) {
258 jointPos(leftknee) -= lowforward * .05;
260 jointVel(leftknee) -= lowforward * .05 / multiplier / 4;
262 jointVel(leftknee) -= lowforward * .05;
263 jointPos(leftankle) += lowforward * .025;
265 jointVel(leftankle) += lowforward * .025 / multiplier / 4;
267 jointVel(leftankle) += lowforward * .25;
268 jointPos(lefthip) += lowforward * .025;
270 jointVel(lefthip) += lowforward * .025 / multiplier / 4;
272 jointVel(lefthip) += lowforward * .025;
273 temp = jointPos(leftknee) - (jointPos(lefthip) + jointPos(leftankle)) / 2;
277 for (i = 0; i < num_joints; i++) {
278 if (joints[i].locked && !spinny && findLengthfast(&joints[i].velocity) > 320)
279 joints[i].locked = 0;
280 if (spinny && findLengthfast(&joints[i].velocity) > 600)
281 joints[i].locked = 0;
282 if (joints[i].delay > 0) {
284 for (j = 0; j < num_joints; j++) {
285 if (joints[j].locked)
289 joints[i].delay -= multiplier * 3;
294 for (i = 0; i < num_muscles; i++) {
296 muscles[i].DoConstraint(spinny);
299 for (i = 0; i < num_joints; i++) {
303 if (joints[i].position.y * (*scale) + coords->y < terrain.getHeight(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z) + groundlevel) {
306 if (joints[i].label == groin && !joints[i].locked && joints[i].delay <= 0) {
307 joints[i].locked = 1;
309 if (tutoriallevel != 1 || id == 0) {
310 emit_sound_at(landsound1, joints[i].position * (*scale) + *coords, 128.);
315 if (joints[i].label == head && !joints[i].locked && joints[i].delay <= 0) {
316 joints[i].locked = 1;
318 if (tutoriallevel != 1 || id == 0) {
319 emit_sound_at(landsound2, joints[i].position * (*scale) + *coords, 128.);
323 terrainnormal = terrain.getNormal(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z);
324 ReflectVector(&joints[i].velocity, &terrainnormal);
325 bounceness = terrainnormal * findLength(&joints[i].velocity) * (abs(normaldotproduct(joints[i].velocity, terrainnormal)));
326 if (!joints[i].locked)
327 damage += findLengthfast(&bounceness) / 4000;
328 if (findLengthfast(&joints[i].velocity) < findLengthfast(&bounceness))
330 frictionness = abs(normaldotproduct(joints[i].velocity, terrainnormal));
331 joints[i].velocity -= bounceness;
332 if (1 - friction * frictionness > 0)
333 joints[i].velocity *= 1 - friction * frictionness;
335 joints[i].velocity = 0;
337 if (tutoriallevel != 1 || id == 0)
338 if (findLengthfast(&bounceness) > 8000 && breaking) {
339 // FIXME: this crashes because k is not initialized!
340 // to reproduce, type 'wolfie' in console and play a while
341 // I'll just comment it out for now
342 //objects.model[k].MakeDecal(breakdecal, DoRotation(temp - objects.position[k], 0, -objects.yaw[k], 0), .4, .5, Random() % 360);
343 Sprite::MakeSprite(cloudsprite, joints[i].position * (*scale) + *coords, joints[i].velocity * .06, 1, 1, 1, 4, .2);
347 emit_sound_at(breaksound2, joints[i].position * (*scale) + *coords);
349 addEnvSound(*coords, 64);
352 if (findLengthfast(&bounceness) > 2500) {
353 Normalise(&bounceness);
354 bounceness = bounceness * 50;
357 joints[i].velocity += bounceness * elasticity;
359 if (findLengthfast(&joints[i].velocity) > findLengthfast(&joints[i].oldvelocity)) {
361 joints[i].velocity = joints[i].oldvelocity;
365 if (joints[i].locked == 0)
366 if (findLengthfast(&joints[i].velocity) < 1)
367 joints[i].locked = 1;
369 if (environment == snowyenvironment && findLengthfast(&bounceness) > 500 && terrain.getOpacity(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z) < .2) {
370 terrainlight = terrain.getLighting(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z);
371 Sprite::MakeSprite(cloudsprite, joints[i].position * (*scale) + *coords, joints[i].velocity * .06, terrainlight.x, terrainlight.y, terrainlight.z, .5, .7);
373 terrain.MakeDecal(bodyprintdecal, joints[i].position * (*scale) + *coords, .4, .4, 0);
374 } else if (environment == desertenvironment && findLengthfast(&bounceness) > 500 && terrain.getOpacity(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z) < .2) {
375 terrainlight = terrain.getLighting(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z);
376 Sprite::MakeSprite(cloudsprite, joints[i].position * (*scale) + *coords, joints[i].velocity * .06, terrainlight.x * 190 / 255, terrainlight.y * 170 / 255, terrainlight.z * 108 / 255, .5, .7);
379 else if (environment == grassyenvironment && findLengthfast(&bounceness) > 500 && terrain.getOpacity(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z) < .2) {
380 terrainlight = terrain.getLighting(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z);
381 Sprite::MakeSprite(cloudsprite, joints[i].position * (*scale) + *coords, joints[i].velocity * .06, terrainlight.x * 90 / 255, terrainlight.y * 70 / 255, terrainlight.z * 8 / 255, .5, .5);
382 } else if (findLengthfast(&bounceness) > 500)
383 Sprite::MakeSprite(cloudsprite, joints[i].position * (*scale) + *coords, joints[i].velocity * .06, terrainlight.x, terrainlight.y, terrainlight.z, .5, .2);
386 joints[i].position.y = (terrain.getHeight(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z) + groundlevel - coords->y) / (*scale);
390 if (terrain.patchobjectnum[whichpatchx][whichpatchz])
391 for (m = 0; m < terrain.patchobjectnum[whichpatchx][whichpatchz]; m++) {
392 k = terrain.patchobjects[whichpatchx][whichpatchz][m];
393 if (k < objects.numobjects && k >= 0)
394 if (objects.possible[k]) {
395 friction = objects.friction[k];
396 XYZ start = joints[i].realoldposition;
397 XYZ end = joints[i].position * (*scale) + *coords;
398 whichhit = objects.model[k].LineCheckPossible(&start, &end, &temp, &objects.position[k], &objects.yaw[k]);
399 if (whichhit != -1) {
400 if (joints[i].label == groin && !joints[i].locked && joints[i].delay <= 0) {
401 joints[i].locked = 1;
403 if (tutoriallevel != 1 || id == 0) {
404 emit_sound_at(landsound1, joints[i].position * (*scale) + *coords, 128.);
409 if (joints[i].label == head && !joints[i].locked && joints[i].delay <= 0) {
410 joints[i].locked = 1;
412 if (tutoriallevel != 1 || id == 0) {
413 emit_sound_at(landsound2, joints[i].position * (*scale) + *coords, 128.);
417 terrainnormal = DoRotation(objects.model[k].facenormals[whichhit], 0, objects.yaw[k], 0) * -1;
418 if (terrainnormal.y > .8)
420 bounceness = terrainnormal * findLength(&joints[i].velocity) * (abs(normaldotproduct(joints[i].velocity, terrainnormal)));
421 if (findLengthfast(&joints[i].velocity) > findLengthfast(&joints[i].oldvelocity)) {
423 joints[i].velocity = joints[i].oldvelocity;
425 if (tutoriallevel != 1 || id == 0)
426 if (findLengthfast(&bounceness) > 4000 && breaking) {
427 objects.model[k].MakeDecal(breakdecal, DoRotation(temp - objects.position[k], 0, -objects.yaw[k], 0), .4, .5, Random() % 360);
428 Sprite::MakeSprite(cloudsprite, joints[i].position * (*scale) + *coords, joints[i].velocity * .06, 1, 1, 1, 4, .2);
432 emit_sound_at(breaksound2, joints[i].position * (*scale) + *coords);
434 addEnvSound(*coords, 64);
436 if (objects.type[k] == treetrunktype) {
437 objects.rotx[k] += joints[i].velocity.x * multiplier * .4;
438 objects.roty[k] += joints[i].velocity.z * multiplier * .4;
439 objects.rotx[k + 1] += joints[i].velocity.x * multiplier * .4;
440 objects.roty[k + 1] += joints[i].velocity.z * multiplier * .4;
442 if (!joints[i].locked)
443 damage += findLengthfast(&bounceness) / 2500;
444 ReflectVector(&joints[i].velocity, &terrainnormal);
445 frictionness = abs(normaldotproduct(joints[i].velocity, terrainnormal));
446 joints[i].velocity -= bounceness;
447 if (1 - friction * frictionness > 0)
448 joints[i].velocity *= 1 - friction * frictionness;
450 joints[i].velocity = 0;
451 if (findLengthfast(&bounceness) > 2500) {
452 Normalise(&bounceness);
453 bounceness = bounceness * 50;
455 joints[i].velocity += bounceness * elasticity;
458 if (!joints[i].locked)
459 if (findLengthfast(&joints[i].velocity) < 1) {
460 joints[i].locked = 1;
462 if (findLengthfast(&bounceness) > 500)
463 Sprite::MakeSprite(cloudsprite, joints[i].position * (*scale) + *coords, joints[i].velocity * .06, 1, 1, 1, .5, .2);
464 joints[i].position = (temp - *coords) / (*scale) + terrainnormal * .005;
470 joints[i].realoldposition = joints[i].position * (*scale) + *coords;
473 multiplier = tempmult;
476 if (terrain.patchobjectnum[whichpatchx][whichpatchz])
477 for (m = 0; m < terrain.patchobjectnum[whichpatchx][whichpatchz]; m++) {
478 k = terrain.patchobjects[whichpatchx][whichpatchz][m];
479 if (objects.possible[k]) {
480 for (i = 0; i < 26; i++) {
481 //Make this less stupid
482 XYZ start = joints[jointlabels[whichjointstartarray[i]]].position * (*scale) + *coords;
483 XYZ end = joints[jointlabels[whichjointendarray[i]]].position * (*scale) + *coords;
484 whichhit = objects.model[k].LineCheckSlidePossible(&start, &end, &temp, &objects.position[k], &objects.yaw[k]);
485 if (whichhit != -1) {
486 joints[jointlabels[whichjointendarray[i]]].position = (end - *coords) / (*scale);
487 for (j = 0; j < num_muscles; j++) {
488 if ((muscles[j].parent1->label == whichjointstartarray[i] && muscles[j].parent2->label == whichjointendarray[i]) || (muscles[j].parent2->label == whichjointstartarray[i] && muscles[j].parent1->label == whichjointendarray[i]))
489 muscles[j].DoConstraint(spinny);
496 for (i = 0; i < num_joints; i++) {
497 switch (joints[i].label) {
513 joints[i].position.y += groundlevel;
515 if (joints[i].label == lefthip || joints[i].label == leftknee || joints[i].label == leftankle || joints[i].label == righthip || joints[i].label == rightknee || joints[i].label == rightankle)
517 if (joints[i].locked) {
526 for (i = 0; i < num_muscles; i++) {
527 if (muscles[i].type == boneconnect)
528 muscles[i].DoConstraint(0);
536 * applies gravity to the skeleton
539 * Person/Person::DoStuff
541 void Skeleton::DoGravity(float *scale)
544 for (i = 0; i < num_joints; i++) {
547 ((joints[i].label != leftknee) && (joints[i].label != rightknee)) ||
548 (lowforward.y > -.1) ||
551 ((joints[i].label != leftelbow) && (joints[i].label != rightelbow)) ||
555 joints[i].velocity.y += gravity * multiplier / (*scale);
560 * set muscles[which].rotate1
564 * special case if animation == hanganim
566 void Skeleton::FindRotationMuscle(int which, int animation)
571 p1 = muscles[which].parent1->position;
572 p2 = muscles[which].parent2->position;
573 dist = findDistance(&p1, &p2);
574 if (p1.y - p2.y <= dist)
575 muscles[which].rotate2 = asin((p1.y - p2.y) / dist);
576 if (p1.y - p2.y > dist)
577 muscles[which].rotate2 = asin(1.f);
578 muscles[which].rotate2 *= 360.0 / 6.2831853;
582 dist = findDistance(&p1, &p2);
583 if (p1.z - p2.z <= dist)
584 muscles[which].rotate1 = acos((p1.z - p2.z) / dist);
585 if (p1.z - p2.z > dist)
586 muscles[which].rotate1 = acos(1.f);
587 muscles[which].rotate1 *= 360.0 / 6.2831853;
589 muscles[which].rotate1 = 360 - muscles[which].rotate1;
590 if (!isnormal(muscles[which].rotate1))
591 muscles[which].rotate1 = 0;
592 if (!isnormal(muscles[which].rotate2))
593 muscles[which].rotate2 = 0;
595 const int label1 = muscles[which].parent1->label;
596 const int label2 = muscles[which].parent2->label;
599 fwd = specialforward[0];
605 fwd = specialforward[1];
611 fwd = specialforward[2];
617 fwd = specialforward[3];
623 fwd = specialforward[4];
626 if (muscles[which].parent1->lower)
633 if (animation == hanganim) {
634 if (label1 == righthand || label2 == righthand) {
638 if (label1 == lefthand || label2 == lefthand) {
645 if (label1 == rightfoot || label2 == rightfoot) {
648 if (label1 == leftfoot || label2 == leftfoot) {
653 fwd = DoRotation(fwd, 0, muscles[which].rotate1 - 90, 0);
654 fwd = DoRotation(fwd, 0, 0, muscles[which].rotate2 - 90);
656 fwd /= findLength(&fwd);
657 if (fwd.z <= 1 && fwd.z >= -1)
658 muscles[which].rotate3 = acos(0 - fwd.z);
660 muscles[which].rotate3 = acos(-1.f);
661 muscles[which].rotate3 *= 360.0 / 6.2831853;
663 muscles[which].rotate3 = 360 - muscles[which].rotate3;
664 if (!isnormal(muscles[which].rotate3))
665 muscles[which].rotate3 = 0;
669 * load an animation from file
671 void Animation::Load(const char *filename, int aheight, int aattack)
678 const char *anim_prefix = ":Data:Animations:";
683 // concatenate anim_prefix + filename
684 int len = strlen(anim_prefix) + strlen(filename);
685 char *buf = new char[len + 1];
686 snprintf(buf, len + 1, "%s%s", anim_prefix, filename);
687 // Changing the filename into something the OS can understand
688 char *fixedFN = ConvertFileName(buf);
691 LOG(std::string("Loading animation...") + fixedFN);
693 // clear existing data
700 Game::LoadingScreen();
702 // read file in binary mode
703 tfile = fopen( fixedFN, "rb" );
705 // read numframes, joints to know how much memory to allocate
706 funpackf(tfile, "Bi Bi", &numframes, &joints);
708 // allocate memory for everything
710 position = (XYZ**)malloc(sizeof(XYZ*) * joints);
711 for (i = 0; i < joints; i++)
712 position[i] = (XYZ*)malloc(sizeof(XYZ) * numframes);
714 twist = (float**)malloc(sizeof(float*) * joints);
715 for (i = 0; i < joints; i++)
716 twist[i] = (float*)malloc(sizeof(float) * numframes);
718 twist2 = (float**)malloc(sizeof(float*) * joints);
719 for (i = 0; i < joints; i++)
720 twist2[i] = (float*)malloc(sizeof(float) * numframes);
722 speed = (float*)malloc(sizeof(float) * numframes);
724 onground = (bool**)malloc(sizeof(bool*) * joints);
725 for (i = 0; i < joints; i++)
726 onground[i] = (bool*)malloc(sizeof(bool) * numframes);
728 forward = (XYZ*)malloc(sizeof(XYZ) * numframes);
729 weapontarget = (XYZ*)malloc(sizeof(XYZ) * numframes);
730 label = (int*)malloc(sizeof(int) * numframes);
732 // read binary data as animation
735 for (i = 0; i < numframes; i++) {
736 // for each joint in the skeleton...
737 for (j = 0; j < joints; j++) {
738 // read joint position
739 funpackf(tfile, "Bf Bf Bf", &position[j][i].x, &position[j][i].y, &position[j][i].z);
741 for (j = 0; j < joints; j++) {
743 funpackf(tfile, "Bf", &twist[j][i]);
745 for (j = 0; j < joints; j++) {
746 // read onground (boolean)
748 funpackf(tfile, "Bb", &uch);
749 onground[j][i] = (uch != 0);
751 // read frame speed (?)
752 funpackf(tfile, "Bf", &speed[i]);
754 // read twist2 for whole animation
755 for (i = 0; i < numframes; i++) {
756 for (j = 0; j < joints; j++) {
757 funpackf(tfile, "Bf", &twist2[j][i]);
760 // read label for each frame
761 for (i = 0; i < numframes; i++) {
762 funpackf(tfile, "Bf", &label[i]);
764 // read weapontargetnum
765 funpackf(tfile, "Bi", &weapontargetnum);
766 // read weapontarget positions for each frame
767 for (i = 0; i < numframes; i++) {
768 funpackf(tfile, "Bf Bf Bf", &weapontarget[i].x, &weapontarget[i].y, &weapontarget[i].z);
775 // find average position of certain joints on last frames
776 // and save in endoffset
777 // (not sure what exactly this accomplishes. the y < 1 test confuses me.)
778 for (j = 0; j < joints; j++) {
779 if (position[j][numframes - 1].y < 1)
780 endoffset += position[j][numframes - 1];
790 * takes filenames for three skeleton files and various models
792 void Skeleton::Load(const char *filename, const char *lowfilename, const char *clothesfilename,
793 const char *modelfilename, const char *model2filename,
794 const char *model3filename, const char *model4filename,
795 const char *model5filename, const char *model6filename,
796 const char *model7filename, const char *modellowfilename,
797 const char *modelclothesfilename, bool clothes)
810 // load various models
811 // rotate, scale, do normals, do texcoords for each as needed
813 model[0].loadnotex(modelfilename);
814 model[1].loadnotex(model2filename);
815 model[2].loadnotex(model3filename);
816 model[3].loadnotex(model4filename);
817 model[4].loadnotex(model5filename);
818 model[5].loadnotex(model6filename);
819 model[6].loadnotex(model7filename);
821 for (i = 0; i < num_models; i++) {
822 model[i].Rotate(180, 0, 0);
823 model[i].Scale(.04, .04, .04);
824 model[i].CalculateNormals(0);
827 drawmodel.load(modelfilename, 0);
828 drawmodel.Rotate(180, 0, 0);
829 drawmodel.Scale(.04, .04, .04);
830 drawmodel.FlipTexCoords();
831 if (tutoriallevel == 1 && id != 0)
832 drawmodel.UniformTexCoords();
833 if (tutoriallevel == 1 && id != 0)
834 drawmodel.ScaleTexCoords(0.1);
835 drawmodel.CalculateNormals(0);
837 modellow.loadnotex(modellowfilename);
838 modellow.Rotate(180, 0, 0);
839 modellow.Scale(.04, .04, .04);
840 modellow.CalculateNormals(0);
842 drawmodellow.load(modellowfilename, 0);
843 drawmodellow.Rotate(180, 0, 0);
844 drawmodellow.Scale(.04, .04, .04);
845 drawmodellow.FlipTexCoords();
846 if (tutoriallevel == 1 && id != 0)
847 drawmodellow.UniformTexCoords();
848 if (tutoriallevel == 1 && id != 0)
849 drawmodellow.ScaleTexCoords(0.1);
850 drawmodellow.CalculateNormals(0);
853 modelclothes.loadnotex(modelclothesfilename);
854 modelclothes.Rotate(180, 0, 0);
855 modelclothes.Scale(.041, .04, .041);
856 modelclothes.CalculateNormals(0);
858 drawmodelclothes.load(modelclothesfilename, 0);
859 drawmodelclothes.Rotate(180, 0, 0);
860 drawmodelclothes.Scale(.04, .04, .04);
861 drawmodelclothes.FlipTexCoords();
862 drawmodelclothes.CalculateNormals(0);
865 // FIXME: three similar blocks follow, one for each of:
866 // filename, lowfilename, clothesfilename
870 tfile = fopen( ConvertFileName(filename), "rb" );
872 if (1) { // FIXME: should this be if(tfile) ?
874 funpackf(tfile, "Bi", &num_joints);
878 delete [] joints; //dealloc2(joints);
879 joints = (Joint*)new Joint[num_joints];
881 // read info for each joint
882 for (i = 0; i < num_joints; i++) {
883 funpackf(tfile, "Bf Bf Bf Bf Bf", &joints[i].position.x, &joints[i].position.y, &joints[i].position.z, &joints[i].length, &joints[i].mass);
884 funpackf(tfile, "Bb Bb", &joints[i].hasparent, &joints[i].locked);
885 funpackf(tfile, "Bi", &joints[i].modelnum);
886 funpackf(tfile, "Bb Bb", &joints[i].visible, &joints[i].sametwist);
887 funpackf(tfile, "Bi Bi", &joints[i].label, &joints[i].hasgun);
888 funpackf(tfile, "Bb", &joints[i].lower);
889 funpackf(tfile, "Bi", &parentID);
890 if (joints[i].hasparent)
891 joints[i].parent = &joints[parentID];
892 joints[i].velocity = 0;
893 joints[i].oldposition = joints[i].position;
897 funpackf(tfile, "Bi", &num_muscles);
901 delete [] muscles; //dealloc2(muscles);
902 muscles = (Muscle*)new Muscle[num_muscles]; //malloc(sizeof(Muscle)*num_muscles);
904 // for each muscle...
905 for (i = 0; i < num_muscles; i++) {
907 funpackf(tfile, "Bf Bf Bf Bf Bf Bi Bi", &muscles[i].length, &muscles[i].targetlength, &muscles[i].minlength, &muscles[i].maxlength, &muscles[i].strength, &muscles[i].type, &muscles[i].numvertices);
909 // allocate memory for vertices
910 muscles[i].vertices = (int*)malloc(sizeof(int) * muscles[i].numvertices);
914 for (j = 0; j < muscles[i].numvertices - edit; j++) {
915 funpackf(tfile, "Bi", &muscles[i].vertices[j + edit]);
916 if (muscles[i].vertices[j + edit] >= model[0].vertexNum) {
917 muscles[i].numvertices--;
923 funpackf(tfile, "Bb Bi", &muscles[i].visible, &parentID);
924 muscles[i].parent1 = &joints[parentID];
925 funpackf(tfile, "Bi", &parentID);
926 muscles[i].parent2 = &joints[parentID];
929 // read forwardjoints (?)
930 for (j = 0; j < 3; j++) {
931 funpackf(tfile, "Bi", &forwardjoints[j]);
933 // read lowforwardjoints (?)
934 for (j = 0; j < 3; j++) {
935 funpackf(tfile, "Bi", &lowforwardjoints[j]);
939 for (j = 0; j < num_muscles; j++) {
940 for (i = 0; i < muscles[j].numvertices; i++) {
941 for (int k = 0; k < num_models; k++) {
942 if (muscles[j].numvertices && muscles[j].vertices[i] < model[k].vertexNum)
943 model[k].owner[muscles[j].vertices[i]] = j;
948 // calculate some stuff
950 for (i = 0; i < num_joints; i++) {
951 joints[i].startpos = joints[i].position;
953 for (i = 0; i < num_muscles; i++) {
954 FindRotationMuscle(i, -1);
956 // this seems to use opengl purely for matrix calculations
957 for (int k = 0; k < num_models; k++) {
958 for (i = 0; i < model[k].vertexNum; i++) {
959 model[k].vertex[i] = model[k].vertex[i] - (muscles[model[k].owner[i]].parent1->position + muscles[model[k].owner[i]].parent2->position) / 2;
960 glMatrixMode(GL_MODELVIEW);
963 glRotatef(muscles[model[k].owner[i]].rotate3, 0, 1, 0);
964 glRotatef(muscles[model[k].owner[i]].rotate2 - 90, 0, 0, 1);
965 glRotatef(muscles[model[k].owner[i]].rotate1 - 90, 0, 1, 0);
966 glTranslatef(model[k].vertex[i].x, model[k].vertex[i].y, model[k].vertex[i].z);
967 glGetFloatv(GL_MODELVIEW_MATRIX, M);
968 model[k].vertex[i].x = M[12] * 1;
969 model[k].vertex[i].y = M[13] * 1;
970 model[k].vertex[i].z = M[14] * 1;
973 model[k].CalculateNormals(0);
980 tfile = fopen( ConvertFileName(lowfilename), "rb" );
982 if (1) { // FIXME: should this be if(tfile) ?
983 // skip joints section
985 lSize = sizeof(num_joints);
986 fseek(tfile, lSize, SEEK_CUR);
987 for (i = 0; i < num_joints; i++) {
1001 fseek(tfile, lSize, SEEK_CUR);
1003 if (joints[i].hasparent)
1004 joints[i].parent = &joints[parentID];
1005 joints[i].velocity = 0;
1006 joints[i].oldposition = joints[i].position;
1010 funpackf(tfile, "Bi", &num_muscles);
1012 for (i = 0; i < num_muscles; i++) {
1014 lSize = sizeof(float)
1020 fseek(tfile, lSize, SEEK_CUR);
1022 // read numverticeslow
1023 funpackf(tfile, "Bi", &muscles[i].numverticeslow);
1025 if (muscles[i].numverticeslow) {
1027 muscles[i].verticeslow = (int*)malloc(sizeof(int) * muscles[i].numverticeslow);
1031 for (j = 0; j < muscles[i].numverticeslow - edit; j++) {
1032 funpackf(tfile, "Bi", &muscles[i].verticeslow[j + edit]);
1033 if (muscles[i].verticeslow[j + edit] >= modellow.vertexNum) {
1034 muscles[i].numverticeslow--;
1041 lSize = 1; //sizeof(bool);
1042 fseek ( tfile, lSize, SEEK_CUR);
1043 lSize = sizeof(int);
1044 fseek ( tfile, lSize, SEEK_CUR);
1045 fseek ( tfile, lSize, SEEK_CUR);
1048 for (j = 0; j < num_muscles; j++) {
1049 for (i = 0; i < muscles[j].numverticeslow; i++) {
1050 if (muscles[j].verticeslow[i] < modellow.vertexNum)
1051 modellow.owner[muscles[j].verticeslow[i]] = j;
1055 // use opengl for its matrix math
1056 for (i = 0; i < modellow.vertexNum; i++) {
1057 modellow.vertex[i] = modellow.vertex[i] - (muscles[modellow.owner[i]].parent1->position + muscles[modellow.owner[i]].parent2->position) / 2;
1058 glMatrixMode(GL_MODELVIEW);
1061 glRotatef(muscles[modellow.owner[i]].rotate3, 0, 1, 0);
1062 glRotatef(muscles[modellow.owner[i]].rotate2 - 90, 0, 0, 1);
1063 glRotatef(muscles[modellow.owner[i]].rotate1 - 90, 0, 1, 0);
1064 glTranslatef(modellow.vertex[i].x, modellow.vertex[i].y, modellow.vertex[i].z);
1065 glGetFloatv(GL_MODELVIEW_MATRIX, M);
1066 modellow.vertex[i].x = M[12];
1067 modellow.vertex[i].y = M[13];
1068 modellow.vertex[i].z = M[14];
1072 modellow.CalculateNormals(0);
1078 tfile = fopen( ConvertFileName(clothesfilename), "rb" ); // FIXME: where's the check for valid load
1081 lSize = sizeof(num_joints);
1082 fseek ( tfile, lSize, SEEK_CUR);
1084 for (i = 0; i < num_joints; i++) {
1098 fseek(tfile, lSize, SEEK_CUR);
1100 if (joints[i].hasparent)
1101 joints[i].parent = &joints[parentID];
1102 joints[i].velocity = 0;
1103 joints[i].oldposition = joints[i].position;
1107 funpackf(tfile, "Bi", &num_muscles);
1109 for (i = 0; i < num_muscles; i++) {
1111 lSize = sizeof(float)
1117 fseek(tfile, lSize, SEEK_CUR);
1119 // read numverticesclothes
1120 funpackf(tfile, "Bi", &muscles[i].numverticesclothes);
1122 // read verticesclothes
1123 if (muscles[i].numverticesclothes) {
1124 muscles[i].verticesclothes = (int*)malloc(sizeof(int) * muscles[i].numverticesclothes);
1126 for (j = 0; j < muscles[i].numverticesclothes - edit; j++) {
1127 funpackf(tfile, "Bi", &muscles[i].verticesclothes[j + edit]);
1128 if (muscles[i].verticesclothes[j + edit] >= modelclothes.vertexNum) {
1129 muscles[i].numverticesclothes--;
1136 lSize = 1; //sizeof(bool);
1137 fseek ( tfile, lSize, SEEK_CUR);
1138 lSize = sizeof(int);
1139 fseek ( tfile, lSize, SEEK_CUR);
1140 fseek ( tfile, lSize, SEEK_CUR);
1144 lSize = sizeof(int);
1145 for (j = 0; j < num_muscles; j++) {
1146 for (i = 0; i < muscles[j].numverticesclothes; i++) {
1147 if (muscles[j].numverticesclothes && muscles[j].verticesclothes[i] < modelclothes.vertexNum)
1148 modelclothes.owner[muscles[j].verticesclothes[i]] = j;
1152 // use opengl for its matrix math
1153 for (i = 0; i < modelclothes.vertexNum; i++) {
1154 modelclothes.vertex[i] = modelclothes.vertex[i] - (muscles[modelclothes.owner[i]].parent1->position + muscles[modelclothes.owner[i]].parent2->position) / 2;
1155 glMatrixMode(GL_MODELVIEW);
1158 glRotatef(muscles[modelclothes.owner[i]].rotate3, 0, 1, 0);
1159 glRotatef(muscles[modelclothes.owner[i]].rotate2 - 90, 0, 0, 1);
1160 glRotatef(muscles[modelclothes.owner[i]].rotate1 - 90, 0, 1, 0);
1161 glTranslatef(modelclothes.vertex[i].x, modelclothes.vertex[i].y, modelclothes.vertex[i].z);
1162 glGetFloatv(GL_MODELVIEW_MATRIX, M);
1163 modelclothes.vertex[i].x = M[12];
1164 modelclothes.vertex[i].y = M[13];
1165 modelclothes.vertex[i].z = M[14];
1169 modelclothes.CalculateNormals(0);
1173 for (i = 0; i < num_joints; i++) {
1174 for (j = 0; j < num_joints; j++) {
1175 if (joints[i].label == j)
1183 Animation::Animation()
1189 weapontargetnum = 0;
1201 Animation::~Animation()
1206 void Animation::deallocate()
1211 for (i = 0; i < joints; i++)
1212 dealloc2(position[i]);
1219 for (i = 0; i < joints; i++)
1227 for (i = 0; i < joints; i++)
1228 dealloc2(twist2[i]);
1235 for (i = 0; i < joints; i++)
1236 dealloc2(onground[i]);
1251 dealloc2(weapontarget);
1261 Skeleton::Skeleton()
1269 memset(forwardjoints, 0, sizeof(forwardjoints));
1274 memset(lowforwardjoints, 0, sizeof(lowforwardjoints));
1277 // XYZ specialforward[5];
1278 memset(jointlabels, 0, sizeof(jointlabels));
1282 // Model modelclothes;
1286 // Model drawmodellow;
1287 // Model drawmodelclothes;
1292 memset(skinText, 0, sizeof(skinText));
1309 Skeleton::~Skeleton()
1326 verticesclothes = 0;
1330 numverticesclothes = 0;
1339 rotate1 = 0, rotate2 = 0, rotate3 = 0;
1340 lastrotate1 = 0, lastrotate2 = 0, lastrotate3 = 0;
1341 oldrotate1 = 0, oldrotate2 = 0, oldrotate3 = 0;
1342 newrotate1 = 0, newrotate2 = 0, newrotate3 = 0;
1350 dealloc2(verticeslow);
1351 dealloc2(verticesclothes);
1354 Animation & Animation::operator = (const Animation & ani)
1358 bool allocate = ((ani.numframes != numframes) || (ani.joints != joints));
1363 numframes = ani.numframes;
1364 height = ani.height;
1365 attack = ani.attack;
1366 joints = ani.joints;
1367 weapontargetnum = ani.weapontargetnum;
1368 offset = ani.offset;
1371 position = (XYZ**)malloc(sizeof(XYZ*)*ani.joints);
1372 for (i = 0; i < ani.joints; i++) {
1374 position[i] = (XYZ*)malloc(sizeof(XYZ) * ani.numframes);
1375 memcpy(position[i], ani.position[i], sizeof(XYZ)*ani.numframes);
1379 twist = (float**)malloc(sizeof(float*)*ani.joints);
1380 for (i = 0; i < ani.joints; i++) {
1382 twist[i] = (float*)malloc(sizeof(float) * ani.numframes);
1383 memcpy(twist[i], ani.twist[i], sizeof(float)*ani.numframes);
1387 twist2 = (float**)malloc(sizeof(float*)*ani.joints);
1388 for (i = 0; i < ani.joints; i++) {
1390 twist2[i] = (float*)malloc(sizeof(float) * ani.numframes);
1391 memcpy(twist2[i], ani.twist2[i], sizeof(float)*ani.numframes);
1395 speed = (float*)malloc(sizeof(float) * ani.numframes);
1396 memcpy(speed, ani.speed, sizeof(float)*ani.numframes);
1399 onground = (bool**)malloc(sizeof(bool*)*ani.joints);
1400 for (i = 0; i < ani.joints; i++) {
1402 onground[i] = (bool*)malloc(sizeof(bool) * ani.numframes);
1403 memcpy(onground[i], ani.onground[i], sizeof(bool)*ani.numframes);
1407 forward = (XYZ*)malloc(sizeof(XYZ) * ani.numframes);
1408 memcpy(forward, ani.forward, sizeof(XYZ)*ani.numframes);
1411 weapontarget = (XYZ*)malloc(sizeof(XYZ) * ani.numframes);
1412 memcpy(weapontarget, ani.weapontarget, sizeof(XYZ)*ani.numframes);
1415 label = (int*)malloc(sizeof(int) * ani.numframes);
1416 memcpy(label, ani.label, sizeof(int)*ani.numframes);
1426 // the following functions are not used anywhere
1429 * sets forward, lowforward, specialforward[]
1434 void Skeleton::FindForwardsfirst()
1436 //Find forward vectors
1437 CrossProduct(joints[forwardjoints[1]].position - joints[forwardjoints[0]].position, joints[forwardjoints[2]].position - joints[forwardjoints[0]].position, &forward);
1438 Normalise(&forward);
1440 CrossProduct(joints[lowforwardjoints[1]].position - joints[lowforwardjoints[0]].position, joints[lowforwardjoints[2]].position - joints[lowforwardjoints[0]].position, &lowforward);
1441 Normalise(&lowforward);
1444 specialforward[0] = forward;
1445 specialforward[1] = forward;
1446 specialforward[2] = forward;
1447 specialforward[3] = forward;
1448 specialforward[4] = forward;
1457 void Skeleton::Draw(int muscleview)
1459 static float jointcolor[4];
1461 if (muscleview != 2) {
1468 if (muscleview == 2) {
1474 //Calc motionblur-ness
1475 for (int i = 0; i < num_joints; i++) {
1476 joints[i].oldposition = joints[i].position;
1477 joints[i].blurred = findDistance(&joints[i].position, &joints[i].oldposition) * 100;
1478 if (joints[i].blurred < 1)
1479 joints[i].blurred = 1;
1485 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1487 for (int i = 0; i < num_joints; i++) {
1488 if (joints[i].hasparent) {
1489 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / joints[i].blurred);
1490 glVertex3f(joints[i].position.x, joints[i].position.y, joints[i].position.z);
1491 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / joints[i].parent->blurred);
1492 glVertex3f(joints[i].parent->position.x, joints[i].parent->position.y, joints[i].parent->position.z);
1493 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / joints[i].parent->blurred);
1494 glVertex3f(joints[i].parent->oldposition.x, joints[i].parent->oldposition.y, joints[i].parent->oldposition.z);
1495 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / joints[i].blurred);
1496 glVertex3f(joints[i].oldposition.x, joints[i].oldposition.y, joints[i].oldposition.z);
1499 for (int i = 0; i < num_muscles; i++) {
1500 if (muscles[i].type == boneconnect) {
1501 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / muscles[i].parent2->blurred);
1502 glVertex3f(muscles[i].parent1->position.x, muscles[i].parent1->position.y, muscles[i].parent1->position.z);
1503 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / muscles[i].parent2->blurred);
1504 glVertex3f(muscles[i].parent2->position.x, muscles[i].parent2->position.y, muscles[i].parent2->position.z);
1505 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / muscles[i].parent2->blurred);
1506 glVertex3f(muscles[i].parent2->oldposition.x, muscles[i].parent2->oldposition.y, muscles[i].parent2->oldposition.z);
1507 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / muscles[i].parent1->blurred);
1508 glVertex3f(muscles[i].parent1->oldposition.x, muscles[i].parent1->oldposition.y, muscles[i].parent1->oldposition.z);
1514 for (int i = 0; i < num_joints; i++) {
1515 if (joints[i].hasparent) {
1516 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / joints[i].blurred);
1517 glVertex3f(joints[i].position.x, joints[i].position.y, joints[i].position.z);
1518 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / joints[i].parent->blurred);
1519 glVertex3f(joints[i].parent->position.x, joints[i].parent->position.y, joints[i].parent->position.z);
1522 for (int i = 0; i < num_muscles; i++) {
1523 if (muscles[i].type == boneconnect) {
1524 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / muscles[i].parent1->blurred);
1525 glVertex3f(muscles[i].parent1->position.x, muscles[i].parent1->position.y, muscles[i].parent1->position.z);
1526 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / muscles[i].parent2->blurred);
1527 glVertex3f(muscles[i].parent2->position.x, muscles[i].parent2->position.y, muscles[i].parent2->position.z);
1530 glColor3f(.6, .6, 0);
1531 if (muscleview == 1)
1532 for (int i = 0; i < num_muscles; i++) {
1533 if (muscles[i].type != boneconnect) {
1534 glVertex3f(muscles[i].parent1->position.x, muscles[i].parent1->position.y, muscles[i].parent1->position.z);
1535 glVertex3f(muscles[i].parent2->position.x, muscles[i].parent2->position.y, muscles[i].parent2->position.z);
1540 if (muscleview != 2) {
1543 for (int i = 0; i < num_joints; i++) {
1545 glColor4f(0, 0, .5, 1);
1547 glColor4f(1, 1, 0, 1);
1548 if (joints[i].locked && i != selected)
1549 glColor4f(1, 0, 0, 1);
1550 glVertex3f(joints[i].position.x, joints[i].position.y, joints[i].position.z);
1555 //Set old position to current position
1556 if (muscleview == 2)
1557 for (int i = 0; i < num_joints; i++) {
1558 joints[i].oldposition = joints[i].position;
1568 void Skeleton::AddJoint(float x, float y, float z, int which)
1570 if (num_joints < max_joints - 1) {
1571 joints[num_joints].velocity = 0;
1572 joints[num_joints].position.x = x;
1573 joints[num_joints].position.y = y;
1574 joints[num_joints].position.z = z;
1575 joints[num_joints].mass = 1;
1576 joints[num_joints].locked = 0;
1578 joints[num_joints].hasparent = 0;
1580 if (which < num_joints && which >= 0)
1581 AddMuscle(num_joints - 1, which, 0, 10, boneconnect);
1590 void Skeleton::DeleteJoint(int whichjoint)
1592 if (whichjoint < num_joints && whichjoint >= 0) {
1593 joints[whichjoint].velocity = joints[num_joints - 1].velocity;
1594 joints[whichjoint].position = joints[num_joints - 1].position;
1595 joints[whichjoint].oldposition = joints[num_joints - 1].oldposition;
1596 joints[whichjoint].hasparent = joints[num_joints - 1].hasparent;
1597 joints[whichjoint].parent = joints[num_joints - 1].parent;
1598 joints[whichjoint].length = joints[num_joints - 1].length;
1599 joints[whichjoint].locked = joints[num_joints - 1].locked;
1600 joints[whichjoint].modelnum = joints[num_joints - 1].modelnum;
1601 joints[whichjoint].visible = joints[num_joints - 1].visible;
1603 for (int i = 0; i < num_muscles; i++) {
1604 while (muscles[i].parent1 == &joints[whichjoint] && i < num_muscles)DeleteMuscle(i);
1605 while (muscles[i].parent2 == &joints[whichjoint] && i < num_muscles)DeleteMuscle(i);
1607 for (int i = 0; i < num_muscles; i++) {
1608 while (muscles[i].parent1 == &joints[num_joints - 1] && i < num_muscles)muscles[i].parent1 = &joints[whichjoint];
1609 while (muscles[i].parent2 == &joints[num_joints - 1] && i < num_muscles)muscles[i].parent2 = &joints[whichjoint];
1611 for (int i = 0; i < num_joints; i++) {
1612 if (joints[i].parent == &joints[whichjoint])
1613 joints[i].hasparent = 0;
1615 for (int i = 0; i < num_joints; i++) {
1616 if (joints[i].parent == &joints[num_joints - 1])
1617 joints[i].parent = &joints[whichjoint];
1627 * Skeleton::DeleteJoint - UNUSED
1629 void Skeleton::DeleteMuscle(int whichmuscle)
1631 if (whichmuscle < num_muscles) {
1632 muscles[whichmuscle].minlength = muscles[num_muscles - 1].minlength;
1633 muscles[whichmuscle].maxlength = muscles[num_muscles - 1].maxlength;
1634 muscles[whichmuscle].strength = muscles[num_muscles - 1].strength;
1635 muscles[whichmuscle].parent1 = muscles[num_muscles - 1].parent1;
1636 muscles[whichmuscle].parent2 = muscles[num_muscles - 1].parent2;
1637 muscles[whichmuscle].length = muscles[num_muscles - 1].length;
1638 muscles[whichmuscle].visible = muscles[num_muscles - 1].visible;
1639 muscles[whichmuscle].type = muscles[num_muscles - 1].type;
1640 muscles[whichmuscle].targetlength = muscles[num_muscles - 1].targetlength;
1651 void Skeleton::SetJoint(float x, float y, float z, int which, int whichjoint)
1653 if (whichjoint < num_joints) {
1654 joints[whichjoint].velocity = 0;
1655 joints[whichjoint].position.x = x;
1656 joints[whichjoint].position.y = y;
1657 joints[whichjoint].position.z = z;
1659 if (which >= num_joints || which < 0)
1660 joints[whichjoint].hasparent = 0;
1661 if (which < num_joints && which >= 0) {
1662 joints[whichjoint].parent = &joints[which];
1663 joints[whichjoint].hasparent = 1;
1664 joints[whichjoint].length = findDistance(&joints[whichjoint].position, &joints[whichjoint].parent->position);
1672 * Skeleton::AddJoint - UNUSED
1674 void Skeleton::AddMuscle(int attach1, int attach2, float minlength, float maxlength, int type)
1676 const int max_muscles = 100; // FIXME: Probably can be dropped
1677 if (num_muscles < max_muscles - 1 && attach1 < num_joints && attach1 >= 0 && attach2 < num_joints && attach2 >= 0 && attach1 != attach2) {
1678 muscles[num_muscles].parent1 = &joints[attach1];
1679 muscles[num_muscles].parent2 = &joints[attach2];
1680 muscles[num_muscles].length = findDistance(&muscles[num_muscles].parent1->position, &muscles[num_muscles].parent2->position);
1681 muscles[num_muscles].targetlength = findDistance(&muscles[num_muscles].parent1->position, &muscles[num_muscles].parent2->position);
1682 muscles[num_muscles].strength = .7;
1683 muscles[num_muscles].type = type;
1684 muscles[num_muscles].minlength = minlength;
1685 muscles[num_muscles].maxlength = maxlength;
1696 void Skeleton::MusclesSet()
1698 for (int i = 0; i < num_muscles; i++) {
1699 muscles[i].length = findDistance(&muscles[i].parent1->position, &muscles[i].parent2->position);
1708 void Skeleton::DoBalance()
1711 newpoint=joints[0].position;
1712 newpoint.x=(joints[2].position.x+joints[4].position.x)/2;
1713 newpoint.z=(joints[2].position.z+joints[4].position.z)/2;
1714 joints[0].velocity=joints[0].velocity+(newpoint-joints[0].position);
1715 //Move child point to within certain distance of parent point
1716 joints[0].position=newpoint;