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 Skeleton testskeleton;
30 extern Terrain terrain;
31 extern Objects objects;
32 extern int environment;
33 extern float camerashake;
36 extern XYZ envsound[30];
37 extern float envsoundvol[30];
38 extern int numenvsounds;
39 extern float envsoundlife[30];
40 extern int tutoriallevel;
42 extern int whichjointstartarray[26];
43 extern int whichjointendarray[26];
45 extern bool visibleloading;
49 void dealloc2(void* param)
54 enum {boneconnect, constraint, muscle};
58 * sets strength, length,
59 * parent1->position, parent2->position,
60 * parent1->velocity, parent2->velocity
64 * Skeleton::DoConstraints
66 void Muscle::DoConstraint(bool spinny)
68 // FIXME: relaxlength shouldn't be static, but may not always be set
69 // so I don't want to change the existing behavior even though it's probably a bug
70 static float relaxlength;
72 float oldlength = length;
74 if (type != boneconnect)
75 relaxlength = findDistance(&parent1->position, &parent2->position);
77 if (type == boneconnect)
79 if (type == constraint)
88 length -= (length - relaxlength) * (1 - strength) * multiplier * 10000;
89 length -= (length - targetlength) * (strength) * multiplier * 10000;
93 if ((relaxlength - length > 0 && relaxlength - oldlength < 0) || (relaxlength - length < 0 && relaxlength - oldlength > 0))
97 if (length < minlength)
99 if (length > maxlength)
102 if (length == relaxlength)
108 XYZ midp = (parent1->position * parent1->mass + parent2->position * parent2->mass) / (parent1->mass + parent2->mass);
110 //Find vector from midpoint to second vector
111 XYZ vel = parent2->position - midp;
113 //Change to unit vector
116 //Apply velocity change
117 XYZ newpoint1 = midp - vel * length * (parent2->mass / (parent1->mass + parent2->mass));
118 XYZ newpoint2 = midp + vel * length * (parent1->mass / (parent1->mass + parent2->mass));
119 if (!freeze && spinny) {
120 parent1->velocity = parent1->velocity + (newpoint1 - parent1->position) / multiplier / 4;
121 parent2->velocity = parent2->velocity + (newpoint2 - parent2->position) / multiplier / 4;
123 parent1->velocity = parent1->velocity + (newpoint1 - parent1->position);
124 parent2->velocity = parent2->velocity + (newpoint2 - parent2->position);
127 //Move child point to within certain distance of parent point
128 parent1->position = newpoint1;
129 parent2->position = newpoint2;
133 * sets forward, lowforward, specialforward[]
137 * Person/Person::DoAnimations
138 * Person/Person::DrawSkeleton
140 void Skeleton::FindForwards()
142 //Find forward vectors
143 CrossProduct(joints[forwardjoints[1]].position - joints[forwardjoints[0]].position, joints[forwardjoints[2]].position - joints[forwardjoints[0]].position, &forward);
146 CrossProduct(joints[lowforwardjoints[1]].position - joints[lowforwardjoints[0]].position, joints[lowforwardjoints[2]].position - joints[lowforwardjoints[0]].position, &lowforward);
147 Normalise(&lowforward);
150 specialforward[0] = forward;
152 specialforward[1] = jointPos(rightshoulder) + jointPos(rightwrist);
153 specialforward[1] = jointPos(rightelbow) - specialforward[1] / 2;
154 specialforward[1] += forward * .4;
155 Normalise(&specialforward[1]);
156 specialforward[2] = jointPos(leftshoulder) + jointPos(leftwrist);
157 specialforward[2] = jointPos(leftelbow) - specialforward[2] / 2;
158 specialforward[2] += forward * .4;
159 Normalise(&specialforward[2]);
161 specialforward[3] = jointPos(righthip) + jointPos(rightankle);
162 specialforward[3] = specialforward[3] / 2 - jointPos(rightknee);
163 specialforward[3] += lowforward * .4;
164 Normalise(&specialforward[3]);
165 specialforward[4] = jointPos(lefthip) + jointPos(leftankle);
166 specialforward[4] = specialforward[4] / 2 - jointPos(leftknee);
167 specialforward[4] += lowforward * .4;
168 Normalise(&specialforward[4]);
175 * Person/Person::RagDoll
176 * Person/Person::DoStuff
179 float Skeleton::DoConstraints(XYZ *coords, float *scale)
181 float friction = 1.5;
182 const float elasticity = .3;
184 const int numrepeats = 3;
185 float groundlevel = .15;
194 float damage = 0; // eventually returned from function
195 bool breaking = false;
198 freetime += multiplier;
200 whichpatchx = coords->x / (terrain.size / subdivision * terrain.scale);
201 whichpatchz = coords->z / (terrain.size / subdivision * terrain.scale);
203 terrainlight = *coords;
204 objects.SphereCheckPossible(&terrainlight, 1);
207 for (i = 0; i < num_joints; i++) {
208 joints[i].position = joints[i].position + joints[i].velocity * multiplier;
210 switch (joints[i].label) {
227 joints[i].position.y -= groundlevel;
228 joints[i].oldvelocity = joints[i].velocity;
231 float tempmult = multiplier;
232 //multiplier/=numrepeats;
234 for (j = 0; j < numrepeats; j++) {
236 // right leg constraints?
237 if (!joint(rightknee).locked && !joint(righthip).locked) {
238 temp = jointPos(rightknee) - (jointPos(righthip) + jointPos(rightankle)) / 2;
239 while (normaldotproduct(temp, lowforward) > -.1 && !sphere_line_intersection(&jointPos(righthip), &jointPos(rightankle), &jointPos(rightknee), &r)) {
240 jointPos(rightknee) -= lowforward * .05;
242 jointVel(rightknee) -= lowforward * .05 / multiplier / 4;
244 jointVel(rightknee) -= lowforward * .05;
245 jointPos(rightankle) += lowforward * .025;
247 jointVel(rightankle) += lowforward * .025 / multiplier / 4;
249 jointVel(rightankle) += lowforward * .25;
250 jointPos(righthip) += lowforward * .025;
252 jointVel(righthip) += lowforward * .025 / multiplier / 4;
254 jointVel(righthip) += lowforward * .025;
255 temp = jointPos(rightknee) - (jointPos(righthip) + jointPos(rightankle)) / 2;
259 // left leg constraints?
260 if (!joint(leftknee).locked && !joint(lefthip).locked) {
261 temp = jointPos(leftknee) - (jointPos(lefthip) + jointPos(leftankle)) / 2;
262 while (normaldotproduct(temp, lowforward) > -.1 && !sphere_line_intersection(&jointPos(lefthip), &jointPos(leftankle), &jointPos(leftknee), &r)) {
263 jointPos(leftknee) -= lowforward * .05;
265 jointVel(leftknee) -= lowforward * .05 / multiplier / 4;
267 jointVel(leftknee) -= lowforward * .05;
268 jointPos(leftankle) += lowforward * .025;
270 jointVel(leftankle) += lowforward * .025 / multiplier / 4;
272 jointVel(leftankle) += lowforward * .25;
273 jointPos(lefthip) += lowforward * .025;
275 jointVel(lefthip) += lowforward * .025 / multiplier / 4;
277 jointVel(lefthip) += lowforward * .025;
278 temp = jointPos(leftknee) - (jointPos(lefthip) + jointPos(leftankle)) / 2;
282 for (i = 0; i < num_joints; i++) {
283 if (joints[i].locked && !spinny && findLengthfast(&joints[i].velocity) > 320)
284 joints[i].locked = 0;
285 if (spinny && findLengthfast(&joints[i].velocity) > 600)
286 joints[i].locked = 0;
287 if (joints[i].delay > 0) {
289 for (j = 0; j < num_joints; j++) {
290 if (joints[j].locked)
294 joints[i].delay -= multiplier * 3;
299 for (i = 0; i < num_muscles; i++) {
301 muscles[i].DoConstraint(spinny);
304 for (i = 0; i < num_joints; i++) {
308 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) {
311 if (joints[i].label == groin && !joints[i].locked && joints[i].delay <= 0) {
312 joints[i].locked = 1;
314 if (tutoriallevel != 1 || id == 0) {
315 emit_sound_at(landsound1, joints[i].position * (*scale) + *coords, 128.);
320 if (joints[i].label == head && !joints[i].locked && joints[i].delay <= 0) {
321 joints[i].locked = 1;
323 if (tutoriallevel != 1 || id == 0) {
324 emit_sound_at(landsound2, joints[i].position * (*scale) + *coords, 128.);
328 terrainnormal = terrain.getNormal(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z);
329 ReflectVector(&joints[i].velocity, &terrainnormal);
330 bounceness = terrainnormal * findLength(&joints[i].velocity) * (abs(normaldotproduct(joints[i].velocity, terrainnormal)));
331 if (!joints[i].locked)
332 damage += findLengthfast(&bounceness) / 4000;
333 if (findLengthfast(&joints[i].velocity) < findLengthfast(&bounceness))
335 frictionness = abs(normaldotproduct(joints[i].velocity, terrainnormal));
336 joints[i].velocity -= bounceness;
337 if (1 - friction * frictionness > 0)
338 joints[i].velocity *= 1 - friction * frictionness;
340 joints[i].velocity = 0;
342 if (tutoriallevel != 1 || id == 0)
343 if (findLengthfast(&bounceness) > 8000 && breaking) {
344 // FIXME: this crashes because k is not initialized!
345 // to reproduce, type 'wolfie' in console and play a while
346 // I'll just comment it out for now
347 //objects.model[k].MakeDecal(breakdecal, DoRotation(temp - objects.position[k], 0, -objects.yaw[k], 0), .4, .5, Random() % 360);
348 Sprite::MakeSprite(cloudsprite, joints[i].position * (*scale) + *coords, joints[i].velocity * .06, 1, 1, 1, 4, .2);
352 emit_sound_at(breaksound2, joints[i].position * (*scale) + *coords);
354 envsound[numenvsounds] = *coords;
355 envsoundvol[numenvsounds] = 64;
356 envsoundlife[numenvsounds] = .4;
360 if (findLengthfast(&bounceness) > 2500) {
361 Normalise(&bounceness);
362 bounceness = bounceness * 50;
365 joints[i].velocity += bounceness * elasticity;
367 if (findLengthfast(&joints[i].velocity) > findLengthfast(&joints[i].oldvelocity)) {
369 joints[i].velocity = joints[i].oldvelocity;
373 if (joints[i].locked == 0)
374 if (findLengthfast(&joints[i].velocity) < 1)
375 joints[i].locked = 1;
377 if (environment == snowyenvironment && findLengthfast(&bounceness) > 500 && terrain.getOpacity(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z) < .2) {
378 terrainlight = terrain.getLighting(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z);
379 Sprite::MakeSprite(cloudsprite, joints[i].position * (*scale) + *coords, joints[i].velocity * .06, terrainlight.x, terrainlight.y, terrainlight.z, .5, .7);
381 terrain.MakeDecal(bodyprintdecal, joints[i].position * (*scale) + *coords, .4, .4, 0);
382 } else if (environment == desertenvironment && findLengthfast(&bounceness) > 500 && terrain.getOpacity(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z) < .2) {
383 terrainlight = terrain.getLighting(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z);
384 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);
387 else if (environment == grassyenvironment && findLengthfast(&bounceness) > 500 && terrain.getOpacity(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z) < .2) {
388 terrainlight = terrain.getLighting(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z);
389 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);
390 } else if (findLengthfast(&bounceness) > 500)
391 Sprite::MakeSprite(cloudsprite, joints[i].position * (*scale) + *coords, joints[i].velocity * .06, terrainlight.x, terrainlight.y, terrainlight.z, .5, .2);
394 joints[i].position.y = (terrain.getHeight(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z) + groundlevel - coords->y) / (*scale);
398 if (terrain.patchobjectnum[whichpatchx][whichpatchz])
399 for (m = 0; m < terrain.patchobjectnum[whichpatchx][whichpatchz]; m++) {
400 k = terrain.patchobjects[whichpatchx][whichpatchz][m];
401 if (k < objects.numobjects && k >= 0)
402 if (objects.possible[k]) {
403 friction = objects.friction[k];
404 XYZ start = joints[i].realoldposition;
405 XYZ end = joints[i].position * (*scale) + *coords;
406 whichhit = objects.model[k].LineCheckPossible(&start, &end, &temp, &objects.position[k], &objects.yaw[k]);
407 if (whichhit != -1) {
408 if (joints[i].label == groin && !joints[i].locked && joints[i].delay <= 0) {
409 joints[i].locked = 1;
411 if (tutoriallevel != 1 || id == 0) {
412 emit_sound_at(landsound1, joints[i].position * (*scale) + *coords, 128.);
417 if (joints[i].label == head && !joints[i].locked && joints[i].delay <= 0) {
418 joints[i].locked = 1;
420 if (tutoriallevel != 1 || id == 0) {
421 emit_sound_at(landsound2, joints[i].position * (*scale) + *coords, 128.);
425 terrainnormal = DoRotation(objects.model[k].facenormals[whichhit], 0, objects.yaw[k], 0) * -1;
426 if (terrainnormal.y > .8)
428 bounceness = terrainnormal * findLength(&joints[i].velocity) * (abs(normaldotproduct(joints[i].velocity, terrainnormal)));
429 if (findLengthfast(&joints[i].velocity) > findLengthfast(&joints[i].oldvelocity)) {
431 joints[i].velocity = joints[i].oldvelocity;
433 if (tutoriallevel != 1 || id == 0)
434 if (findLengthfast(&bounceness) > 4000 && breaking) {
435 objects.model[k].MakeDecal(breakdecal, DoRotation(temp - objects.position[k], 0, -objects.yaw[k], 0), .4, .5, Random() % 360);
436 Sprite::MakeSprite(cloudsprite, joints[i].position * (*scale) + *coords, joints[i].velocity * .06, 1, 1, 1, 4, .2);
440 emit_sound_at(breaksound2, joints[i].position * (*scale) + *coords);
442 envsound[numenvsounds] = *coords;
443 envsoundvol[numenvsounds] = 64;
444 envsoundlife[numenvsounds] = .4;
447 if (objects.type[k] == treetrunktype) {
448 objects.rotx[k] += joints[i].velocity.x * multiplier * .4;
449 objects.roty[k] += joints[i].velocity.z * multiplier * .4;
450 objects.rotx[k + 1] += joints[i].velocity.x * multiplier * .4;
451 objects.roty[k + 1] += joints[i].velocity.z * multiplier * .4;
453 if (!joints[i].locked)
454 damage += findLengthfast(&bounceness) / 2500;
455 ReflectVector(&joints[i].velocity, &terrainnormal);
456 frictionness = abs(normaldotproduct(joints[i].velocity, terrainnormal));
457 joints[i].velocity -= bounceness;
458 if (1 - friction * frictionness > 0)
459 joints[i].velocity *= 1 - friction * frictionness;
461 joints[i].velocity = 0;
462 if (findLengthfast(&bounceness) > 2500) {
463 Normalise(&bounceness);
464 bounceness = bounceness * 50;
466 joints[i].velocity += bounceness * elasticity;
469 if (!joints[i].locked)
470 if (findLengthfast(&joints[i].velocity) < 1) {
471 joints[i].locked = 1;
473 if (findLengthfast(&bounceness) > 500)
474 Sprite::MakeSprite(cloudsprite, joints[i].position * (*scale) + *coords, joints[i].velocity * .06, 1, 1, 1, .5, .2);
475 joints[i].position = (temp - *coords) / (*scale) + terrainnormal * .005;
481 joints[i].realoldposition = joints[i].position * (*scale) + *coords;
484 multiplier = tempmult;
487 if (terrain.patchobjectnum[whichpatchx][whichpatchz])
488 for (m = 0; m < terrain.patchobjectnum[whichpatchx][whichpatchz]; m++) {
489 k = terrain.patchobjects[whichpatchx][whichpatchz][m];
490 if (objects.possible[k]) {
491 for (i = 0; i < 26; i++) {
492 //Make this less stupid
493 XYZ start = joints[jointlabels[whichjointstartarray[i]]].position * (*scale) + *coords;
494 XYZ end = joints[jointlabels[whichjointendarray[i]]].position * (*scale) + *coords;
495 whichhit = objects.model[k].LineCheckSlidePossible(&start, &end, &temp, &objects.position[k], &objects.yaw[k]);
496 if (whichhit != -1) {
497 joints[jointlabels[whichjointendarray[i]]].position = (end - *coords) / (*scale);
498 for (j = 0; j < num_muscles; j++) {
499 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]))
500 muscles[j].DoConstraint(spinny);
507 for (i = 0; i < num_joints; i++) {
508 switch (joints[i].label) {
524 joints[i].position.y += groundlevel;
526 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)
528 if (joints[i].locked) {
537 for (i = 0; i < num_muscles; i++) {
538 if (muscles[i].type == boneconnect)
539 muscles[i].DoConstraint(0);
547 * applies gravity to the skeleton
550 * Person/Person::DoStuff
552 void Skeleton::DoGravity(float *scale)
555 for (i = 0; i < num_joints; i++) {
558 ((joints[i].label != leftknee) && (joints[i].label != rightknee)) ||
559 (lowforward.y > -.1) ||
562 ((joints[i].label != leftelbow) && (joints[i].label != rightelbow)) ||
566 joints[i].velocity.y += gravity * multiplier / (*scale);
571 * set muscles[which].rotate1
575 * special case if animation == hanganim
577 void Skeleton::FindRotationMuscle(int which, int animation)
582 p1 = muscles[which].parent1->position;
583 p2 = muscles[which].parent2->position;
584 dist = findDistance(&p1, &p2);
585 if (p1.y - p2.y <= dist)
586 muscles[which].rotate2 = asin((p1.y - p2.y) / dist);
587 if (p1.y - p2.y > dist)
588 muscles[which].rotate2 = asin(1.f);
589 muscles[which].rotate2 *= 360.0 / 6.2831853;
593 dist = findDistance(&p1, &p2);
594 if (p1.z - p2.z <= dist)
595 muscles[which].rotate1 = acos((p1.z - p2.z) / dist);
596 if (p1.z - p2.z > dist)
597 muscles[which].rotate1 = acos(1.f);
598 muscles[which].rotate1 *= 360.0 / 6.2831853;
600 muscles[which].rotate1 = 360 - muscles[which].rotate1;
601 if (!isnormal(muscles[which].rotate1))
602 muscles[which].rotate1 = 0;
603 if (!isnormal(muscles[which].rotate2))
604 muscles[which].rotate2 = 0;
606 const int label1 = muscles[which].parent1->label;
607 const int label2 = muscles[which].parent2->label;
610 fwd = specialforward[0];
616 fwd = specialforward[1];
622 fwd = specialforward[2];
628 fwd = specialforward[3];
634 fwd = specialforward[4];
637 if (muscles[which].parent1->lower)
644 if (animation == hanganim) {
645 if (label1 == righthand || label2 == righthand) {
649 if (label1 == lefthand || label2 == lefthand) {
656 if (label1 == rightfoot || label2 == rightfoot) {
659 if (label1 == leftfoot || label2 == leftfoot) {
664 fwd = DoRotation(fwd, 0, muscles[which].rotate1 - 90, 0);
665 fwd = DoRotation(fwd, 0, 0, muscles[which].rotate2 - 90);
667 fwd /= findLength(&fwd);
668 if (fwd.z <= 1 && fwd.z >= -1)
669 muscles[which].rotate3 = acos(0 - fwd.z);
671 muscles[which].rotate3 = acos(-1.f);
672 muscles[which].rotate3 *= 360.0 / 6.2831853;
674 muscles[which].rotate3 = 360 - muscles[which].rotate3;
675 if (!isnormal(muscles[which].rotate3))
676 muscles[which].rotate3 = 0;
680 * load an animation from file
682 void Animation::Load(const char *filename, int aheight, int aattack)
689 const char *anim_prefix = ":Data:Animations:";
694 // concatenate anim_prefix + filename
695 int len = strlen(anim_prefix) + strlen(filename);
696 char *buf = new char[len + 1];
697 snprintf(buf, len + 1, "%s%s", anim_prefix, filename);
698 // Changing the filename into something the OS can understand
699 char *fixedFN = ConvertFileName(buf);
702 LOG(std::string("Loading animation...") + fixedFN);
704 // clear existing data
711 Game::LoadingScreen();
713 // read file in binary mode
714 tfile = fopen( fixedFN, "rb" );
716 // read numframes, joints to know how much memory to allocate
717 funpackf(tfile, "Bi Bi", &numframes, &joints);
719 // allocate memory for everything
721 position = (XYZ**)malloc(sizeof(XYZ*) * joints);
722 for (i = 0; i < joints; i++)
723 position[i] = (XYZ*)malloc(sizeof(XYZ) * numframes);
725 twist = (float**)malloc(sizeof(float*) * joints);
726 for (i = 0; i < joints; i++)
727 twist[i] = (float*)malloc(sizeof(float) * numframes);
729 twist2 = (float**)malloc(sizeof(float*) * joints);
730 for (i = 0; i < joints; i++)
731 twist2[i] = (float*)malloc(sizeof(float) * numframes);
733 speed = (float*)malloc(sizeof(float) * numframes);
735 onground = (bool**)malloc(sizeof(bool*) * joints);
736 for (i = 0; i < joints; i++)
737 onground[i] = (bool*)malloc(sizeof(bool) * numframes);
739 forward = (XYZ*)malloc(sizeof(XYZ) * numframes);
740 weapontarget = (XYZ*)malloc(sizeof(XYZ) * numframes);
741 label = (int*)malloc(sizeof(int) * numframes);
743 // read binary data as animation
746 for (i = 0; i < numframes; i++) {
747 // for each joint in the skeleton...
748 for (j = 0; j < joints; j++) {
749 // read joint position
750 funpackf(tfile, "Bf Bf Bf", &position[j][i].x, &position[j][i].y, &position[j][i].z);
752 for (j = 0; j < joints; j++) {
754 funpackf(tfile, "Bf", &twist[j][i]);
756 for (j = 0; j < joints; j++) {
757 // read onground (boolean)
759 funpackf(tfile, "Bb", &uch);
760 onground[j][i] = (uch != 0);
762 // read frame speed (?)
763 funpackf(tfile, "Bf", &speed[i]);
765 // read twist2 for whole animation
766 for (i = 0; i < numframes; i++) {
767 for (j = 0; j < joints; j++) {
768 funpackf(tfile, "Bf", &twist2[j][i]);
771 // read label for each frame
772 for (i = 0; i < numframes; i++) {
773 funpackf(tfile, "Bf", &label[i]);
775 // read weapontargetnum
776 funpackf(tfile, "Bi", &weapontargetnum);
777 // read weapontarget positions for each frame
778 for (i = 0; i < numframes; i++) {
779 funpackf(tfile, "Bf Bf Bf", &weapontarget[i].x, &weapontarget[i].y, &weapontarget[i].z);
786 // find average position of certain joints on last frames
787 // and save in endoffset
788 // (not sure what exactly this accomplishes. the y < 1 test confuses me.)
789 for (j = 0; j < joints; j++) {
790 if (position[j][numframes - 1].y < 1)
791 endoffset += position[j][numframes - 1];
801 * takes filenames for three skeleton files and various models
803 void Skeleton::Load(const char *filename, const char *lowfilename, const char *clothesfilename,
804 const char *modelfilename, const char *model2filename,
805 const char *model3filename, const char *model4filename,
806 const char *model5filename, const char *model6filename,
807 const char *model7filename, const char *modellowfilename,
808 const char *modelclothesfilename, bool clothes)
821 // load various models
822 // rotate, scale, do normals, do texcoords for each as needed
824 model[0].loadnotex(modelfilename);
825 model[1].loadnotex(model2filename);
826 model[2].loadnotex(model3filename);
827 model[3].loadnotex(model4filename);
828 model[4].loadnotex(model5filename);
829 model[5].loadnotex(model6filename);
830 model[6].loadnotex(model7filename);
832 for (i = 0; i < num_models; i++) {
833 model[i].Rotate(180, 0, 0);
834 model[i].Scale(.04, .04, .04);
835 model[i].CalculateNormals(0);
838 drawmodel.load(modelfilename, 0);
839 drawmodel.Rotate(180, 0, 0);
840 drawmodel.Scale(.04, .04, .04);
841 drawmodel.FlipTexCoords();
842 if (tutoriallevel == 1 && id != 0)
843 drawmodel.UniformTexCoords();
844 if (tutoriallevel == 1 && id != 0)
845 drawmodel.ScaleTexCoords(0.1);
846 drawmodel.CalculateNormals(0);
848 modellow.loadnotex(modellowfilename);
849 modellow.Rotate(180, 0, 0);
850 modellow.Scale(.04, .04, .04);
851 modellow.CalculateNormals(0);
853 drawmodellow.load(modellowfilename, 0);
854 drawmodellow.Rotate(180, 0, 0);
855 drawmodellow.Scale(.04, .04, .04);
856 drawmodellow.FlipTexCoords();
857 if (tutoriallevel == 1 && id != 0)
858 drawmodellow.UniformTexCoords();
859 if (tutoriallevel == 1 && id != 0)
860 drawmodellow.ScaleTexCoords(0.1);
861 drawmodellow.CalculateNormals(0);
864 modelclothes.loadnotex(modelclothesfilename);
865 modelclothes.Rotate(180, 0, 0);
866 modelclothes.Scale(.041, .04, .041);
867 modelclothes.CalculateNormals(0);
869 drawmodelclothes.load(modelclothesfilename, 0);
870 drawmodelclothes.Rotate(180, 0, 0);
871 drawmodelclothes.Scale(.04, .04, .04);
872 drawmodelclothes.FlipTexCoords();
873 drawmodelclothes.CalculateNormals(0);
876 // FIXME: three similar blocks follow, one for each of:
877 // filename, lowfilename, clothesfilename
881 tfile = fopen( ConvertFileName(filename), "rb" );
883 if (1) { // FIXME: should this be if(tfile) ?
885 funpackf(tfile, "Bi", &num_joints);
889 delete [] joints; //dealloc2(joints);
890 joints = (Joint*)new Joint[num_joints];
892 // read info for each joint
893 for (i = 0; i < num_joints; i++) {
894 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);
895 funpackf(tfile, "Bb Bb", &joints[i].hasparent, &joints[i].locked);
896 funpackf(tfile, "Bi", &joints[i].modelnum);
897 funpackf(tfile, "Bb Bb", &joints[i].visible, &joints[i].sametwist);
898 funpackf(tfile, "Bi Bi", &joints[i].label, &joints[i].hasgun);
899 funpackf(tfile, "Bb", &joints[i].lower);
900 funpackf(tfile, "Bi", &parentID);
901 if (joints[i].hasparent)
902 joints[i].parent = &joints[parentID];
903 joints[i].velocity = 0;
904 joints[i].oldposition = joints[i].position;
908 funpackf(tfile, "Bi", &num_muscles);
912 delete [] muscles; //dealloc2(muscles);
913 muscles = (Muscle*)new Muscle[num_muscles]; //malloc(sizeof(Muscle)*num_muscles);
915 // for each muscle...
916 for (i = 0; i < num_muscles; i++) {
918 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);
920 // allocate memory for vertices
921 muscles[i].vertices = (int*)malloc(sizeof(int) * muscles[i].numvertices);
925 for (j = 0; j < muscles[i].numvertices - edit; j++) {
926 funpackf(tfile, "Bi", &muscles[i].vertices[j + edit]);
927 if (muscles[i].vertices[j + edit] >= model[0].vertexNum) {
928 muscles[i].numvertices--;
934 funpackf(tfile, "Bb Bi", &muscles[i].visible, &parentID);
935 muscles[i].parent1 = &joints[parentID];
936 funpackf(tfile, "Bi", &parentID);
937 muscles[i].parent2 = &joints[parentID];
940 // read forwardjoints (?)
941 for (j = 0; j < 3; j++) {
942 funpackf(tfile, "Bi", &forwardjoints[j]);
944 // read lowforwardjoints (?)
945 for (j = 0; j < 3; j++) {
946 funpackf(tfile, "Bi", &lowforwardjoints[j]);
950 for (j = 0; j < num_muscles; j++) {
951 for (i = 0; i < muscles[j].numvertices; i++) {
952 for (int k = 0; k < num_models; k++) {
953 if (muscles[j].numvertices && muscles[j].vertices[i] < model[k].vertexNum)
954 model[k].owner[muscles[j].vertices[i]] = j;
959 // calculate some stuff
961 for (i = 0; i < num_joints; i++) {
962 joints[i].startpos = joints[i].position;
964 for (i = 0; i < num_muscles; i++) {
965 FindRotationMuscle(i, -1);
967 // this seems to use opengl purely for matrix calculations
968 for (int k = 0; k < num_models; k++) {
969 for (i = 0; i < model[k].vertexNum; i++) {
970 model[k].vertex[i] = model[k].vertex[i] - (muscles[model[k].owner[i]].parent1->position + muscles[model[k].owner[i]].parent2->position) / 2;
971 glMatrixMode(GL_MODELVIEW);
974 glRotatef(muscles[model[k].owner[i]].rotate3, 0, 1, 0);
975 glRotatef(muscles[model[k].owner[i]].rotate2 - 90, 0, 0, 1);
976 glRotatef(muscles[model[k].owner[i]].rotate1 - 90, 0, 1, 0);
977 glTranslatef(model[k].vertex[i].x, model[k].vertex[i].y, model[k].vertex[i].z);
978 glGetFloatv(GL_MODELVIEW_MATRIX, M);
979 model[k].vertex[i].x = M[12] * 1;
980 model[k].vertex[i].y = M[13] * 1;
981 model[k].vertex[i].z = M[14] * 1;
984 model[k].CalculateNormals(0);
991 tfile = fopen( ConvertFileName(lowfilename), "rb" );
993 if (1) { // FIXME: should this be if(tfile) ?
994 // skip joints section
996 lSize = sizeof(num_joints);
997 fseek(tfile, lSize, SEEK_CUR);
998 for (i = 0; i < num_joints; i++) {
1012 fseek(tfile, lSize, SEEK_CUR);
1014 if (joints[i].hasparent)
1015 joints[i].parent = &joints[parentID];
1016 joints[i].velocity = 0;
1017 joints[i].oldposition = joints[i].position;
1021 funpackf(tfile, "Bi", &num_muscles);
1023 for (i = 0; i < num_muscles; i++) {
1025 lSize = sizeof(float)
1031 fseek(tfile, lSize, SEEK_CUR);
1033 // read numverticeslow
1034 funpackf(tfile, "Bi", &muscles[i].numverticeslow);
1036 if (muscles[i].numverticeslow) {
1038 muscles[i].verticeslow = (int*)malloc(sizeof(int) * muscles[i].numverticeslow);
1042 for (j = 0; j < muscles[i].numverticeslow - edit; j++) {
1043 funpackf(tfile, "Bi", &muscles[i].verticeslow[j + edit]);
1044 if (muscles[i].verticeslow[j + edit] >= modellow.vertexNum) {
1045 muscles[i].numverticeslow--;
1052 lSize = 1; //sizeof(bool);
1053 fseek ( tfile, lSize, SEEK_CUR);
1054 lSize = sizeof(int);
1055 fseek ( tfile, lSize, SEEK_CUR);
1056 fseek ( tfile, lSize, SEEK_CUR);
1059 for (j = 0; j < num_muscles; j++) {
1060 for (i = 0; i < muscles[j].numverticeslow; i++) {
1061 if (muscles[j].verticeslow[i] < modellow.vertexNum)
1062 modellow.owner[muscles[j].verticeslow[i]] = j;
1066 // use opengl for its matrix math
1067 for (i = 0; i < modellow.vertexNum; i++) {
1068 modellow.vertex[i] = modellow.vertex[i] - (muscles[modellow.owner[i]].parent1->position + muscles[modellow.owner[i]].parent2->position) / 2;
1069 glMatrixMode(GL_MODELVIEW);
1072 glRotatef(muscles[modellow.owner[i]].rotate3, 0, 1, 0);
1073 glRotatef(muscles[modellow.owner[i]].rotate2 - 90, 0, 0, 1);
1074 glRotatef(muscles[modellow.owner[i]].rotate1 - 90, 0, 1, 0);
1075 glTranslatef(modellow.vertex[i].x, modellow.vertex[i].y, modellow.vertex[i].z);
1076 glGetFloatv(GL_MODELVIEW_MATRIX, M);
1077 modellow.vertex[i].x = M[12];
1078 modellow.vertex[i].y = M[13];
1079 modellow.vertex[i].z = M[14];
1083 modellow.CalculateNormals(0);
1089 tfile = fopen( ConvertFileName(clothesfilename), "rb" ); // FIXME: where's the check for valid load
1092 lSize = sizeof(num_joints);
1093 fseek ( tfile, lSize, SEEK_CUR);
1095 for (i = 0; i < num_joints; i++) {
1109 fseek(tfile, lSize, SEEK_CUR);
1111 if (joints[i].hasparent)
1112 joints[i].parent = &joints[parentID];
1113 joints[i].velocity = 0;
1114 joints[i].oldposition = joints[i].position;
1118 funpackf(tfile, "Bi", &num_muscles);
1120 for (i = 0; i < num_muscles; i++) {
1122 lSize = sizeof(float)
1128 fseek(tfile, lSize, SEEK_CUR);
1130 // read numverticesclothes
1131 funpackf(tfile, "Bi", &muscles[i].numverticesclothes);
1133 // read verticesclothes
1134 if (muscles[i].numverticesclothes) {
1135 muscles[i].verticesclothes = (int*)malloc(sizeof(int) * muscles[i].numverticesclothes);
1137 for (j = 0; j < muscles[i].numverticesclothes - edit; j++) {
1138 funpackf(tfile, "Bi", &muscles[i].verticesclothes[j + edit]);
1139 if (muscles[i].verticesclothes[j + edit] >= modelclothes.vertexNum) {
1140 muscles[i].numverticesclothes--;
1147 lSize = 1; //sizeof(bool);
1148 fseek ( tfile, lSize, SEEK_CUR);
1149 lSize = sizeof(int);
1150 fseek ( tfile, lSize, SEEK_CUR);
1151 fseek ( tfile, lSize, SEEK_CUR);
1155 lSize = sizeof(int);
1156 for (j = 0; j < num_muscles; j++) {
1157 for (i = 0; i < muscles[j].numverticesclothes; i++) {
1158 if (muscles[j].numverticesclothes && muscles[j].verticesclothes[i] < modelclothes.vertexNum)
1159 modelclothes.owner[muscles[j].verticesclothes[i]] = j;
1163 // use opengl for its matrix math
1164 for (i = 0; i < modelclothes.vertexNum; i++) {
1165 modelclothes.vertex[i] = modelclothes.vertex[i] - (muscles[modelclothes.owner[i]].parent1->position + muscles[modelclothes.owner[i]].parent2->position) / 2;
1166 glMatrixMode(GL_MODELVIEW);
1169 glRotatef(muscles[modelclothes.owner[i]].rotate3, 0, 1, 0);
1170 glRotatef(muscles[modelclothes.owner[i]].rotate2 - 90, 0, 0, 1);
1171 glRotatef(muscles[modelclothes.owner[i]].rotate1 - 90, 0, 1, 0);
1172 glTranslatef(modelclothes.vertex[i].x, modelclothes.vertex[i].y, modelclothes.vertex[i].z);
1173 glGetFloatv(GL_MODELVIEW_MATRIX, M);
1174 modelclothes.vertex[i].x = M[12];
1175 modelclothes.vertex[i].y = M[13];
1176 modelclothes.vertex[i].z = M[14];
1180 modelclothes.CalculateNormals(0);
1184 for (i = 0; i < num_joints; i++) {
1185 for (j = 0; j < num_joints; j++) {
1186 if (joints[i].label == j)
1194 Animation::Animation()
1200 weapontargetnum = 0;
1212 Animation::~Animation()
1217 void Animation::deallocate()
1222 for (i = 0; i < joints; i++)
1223 dealloc2(position[i]);
1230 for (i = 0; i < joints; i++)
1238 for (i = 0; i < joints; i++)
1239 dealloc2(twist2[i]);
1246 for (i = 0; i < joints; i++)
1247 dealloc2(onground[i]);
1262 dealloc2(weapontarget);
1272 Skeleton::Skeleton()
1280 memset(forwardjoints, 0, sizeof(forwardjoints));
1285 memset(lowforwardjoints, 0, sizeof(lowforwardjoints));
1288 // XYZ specialforward[5];
1289 memset(jointlabels, 0, sizeof(jointlabels));
1293 // Model modelclothes;
1297 // Model drawmodellow;
1298 // Model drawmodelclothes;
1303 memset(skinText, 0, sizeof(skinText));
1320 Skeleton::~Skeleton()
1337 verticesclothes = 0;
1341 numverticesclothes = 0;
1350 rotate1 = 0, rotate2 = 0, rotate3 = 0;
1351 lastrotate1 = 0, lastrotate2 = 0, lastrotate3 = 0;
1352 oldrotate1 = 0, oldrotate2 = 0, oldrotate3 = 0;
1353 newrotate1 = 0, newrotate2 = 0, newrotate3 = 0;
1361 dealloc2(verticeslow);
1362 dealloc2(verticesclothes);
1365 Animation & Animation::operator = (const Animation & ani)
1369 bool allocate = ((ani.numframes != numframes) || (ani.joints != joints));
1374 numframes = ani.numframes;
1375 height = ani.height;
1376 attack = ani.attack;
1377 joints = ani.joints;
1378 weapontargetnum = ani.weapontargetnum;
1379 offset = ani.offset;
1382 position = (XYZ**)malloc(sizeof(XYZ*)*ani.joints);
1383 for (i = 0; i < ani.joints; i++) {
1385 position[i] = (XYZ*)malloc(sizeof(XYZ) * ani.numframes);
1386 memcpy(position[i], ani.position[i], sizeof(XYZ)*ani.numframes);
1390 twist = (float**)malloc(sizeof(float*)*ani.joints);
1391 for (i = 0; i < ani.joints; i++) {
1393 twist[i] = (float*)malloc(sizeof(float) * ani.numframes);
1394 memcpy(twist[i], ani.twist[i], sizeof(float)*ani.numframes);
1398 twist2 = (float**)malloc(sizeof(float*)*ani.joints);
1399 for (i = 0; i < ani.joints; i++) {
1401 twist2[i] = (float*)malloc(sizeof(float) * ani.numframes);
1402 memcpy(twist2[i], ani.twist2[i], sizeof(float)*ani.numframes);
1406 speed = (float*)malloc(sizeof(float) * ani.numframes);
1407 memcpy(speed, ani.speed, sizeof(float)*ani.numframes);
1410 onground = (bool**)malloc(sizeof(bool*)*ani.joints);
1411 for (i = 0; i < ani.joints; i++) {
1413 onground[i] = (bool*)malloc(sizeof(bool) * ani.numframes);
1414 memcpy(onground[i], ani.onground[i], sizeof(bool)*ani.numframes);
1418 forward = (XYZ*)malloc(sizeof(XYZ) * ani.numframes);
1419 memcpy(forward, ani.forward, sizeof(XYZ)*ani.numframes);
1422 weapontarget = (XYZ*)malloc(sizeof(XYZ) * ani.numframes);
1423 memcpy(weapontarget, ani.weapontarget, sizeof(XYZ)*ani.numframes);
1426 label = (int*)malloc(sizeof(int) * ani.numframes);
1427 memcpy(label, ani.label, sizeof(int)*ani.numframes);
1437 // the following functions are not used anywhere
1440 * sets forward, lowforward, specialforward[]
1445 void Skeleton::FindForwardsfirst()
1447 //Find forward vectors
1448 CrossProduct(joints[forwardjoints[1]].position - joints[forwardjoints[0]].position, joints[forwardjoints[2]].position - joints[forwardjoints[0]].position, &forward);
1449 Normalise(&forward);
1451 CrossProduct(joints[lowforwardjoints[1]].position - joints[lowforwardjoints[0]].position, joints[lowforwardjoints[2]].position - joints[lowforwardjoints[0]].position, &lowforward);
1452 Normalise(&lowforward);
1455 specialforward[0] = forward;
1456 specialforward[1] = forward;
1457 specialforward[2] = forward;
1458 specialforward[3] = forward;
1459 specialforward[4] = forward;
1468 void Skeleton::Draw(int muscleview)
1470 static float jointcolor[4];
1472 if (muscleview != 2) {
1479 if (muscleview == 2) {
1485 //Calc motionblur-ness
1486 for (int i = 0; i < num_joints; i++) {
1487 joints[i].oldposition = joints[i].position;
1488 joints[i].blurred = findDistance(&joints[i].position, &joints[i].oldposition) * 100;
1489 if (joints[i].blurred < 1)
1490 joints[i].blurred = 1;
1496 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1498 for (int i = 0; i < num_joints; i++) {
1499 if (joints[i].hasparent) {
1500 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / joints[i].blurred);
1501 glVertex3f(joints[i].position.x, joints[i].position.y, joints[i].position.z);
1502 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / joints[i].parent->blurred);
1503 glVertex3f(joints[i].parent->position.x, joints[i].parent->position.y, joints[i].parent->position.z);
1504 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / joints[i].parent->blurred);
1505 glVertex3f(joints[i].parent->oldposition.x, joints[i].parent->oldposition.y, joints[i].parent->oldposition.z);
1506 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / joints[i].blurred);
1507 glVertex3f(joints[i].oldposition.x, joints[i].oldposition.y, joints[i].oldposition.z);
1510 for (int i = 0; i < num_muscles; i++) {
1511 if (muscles[i].type == boneconnect) {
1512 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / muscles[i].parent2->blurred);
1513 glVertex3f(muscles[i].parent1->position.x, muscles[i].parent1->position.y, muscles[i].parent1->position.z);
1514 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / muscles[i].parent2->blurred);
1515 glVertex3f(muscles[i].parent2->position.x, muscles[i].parent2->position.y, muscles[i].parent2->position.z);
1516 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / muscles[i].parent2->blurred);
1517 glVertex3f(muscles[i].parent2->oldposition.x, muscles[i].parent2->oldposition.y, muscles[i].parent2->oldposition.z);
1518 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / muscles[i].parent1->blurred);
1519 glVertex3f(muscles[i].parent1->oldposition.x, muscles[i].parent1->oldposition.y, muscles[i].parent1->oldposition.z);
1525 for (int i = 0; i < num_joints; i++) {
1526 if (joints[i].hasparent) {
1527 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / joints[i].blurred);
1528 glVertex3f(joints[i].position.x, joints[i].position.y, joints[i].position.z);
1529 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / joints[i].parent->blurred);
1530 glVertex3f(joints[i].parent->position.x, joints[i].parent->position.y, joints[i].parent->position.z);
1533 for (int i = 0; i < num_muscles; i++) {
1534 if (muscles[i].type == boneconnect) {
1535 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / muscles[i].parent1->blurred);
1536 glVertex3f(muscles[i].parent1->position.x, muscles[i].parent1->position.y, muscles[i].parent1->position.z);
1537 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / muscles[i].parent2->blurred);
1538 glVertex3f(muscles[i].parent2->position.x, muscles[i].parent2->position.y, muscles[i].parent2->position.z);
1541 glColor3f(.6, .6, 0);
1542 if (muscleview == 1)
1543 for (int i = 0; i < num_muscles; i++) {
1544 if (muscles[i].type != boneconnect) {
1545 glVertex3f(muscles[i].parent1->position.x, muscles[i].parent1->position.y, muscles[i].parent1->position.z);
1546 glVertex3f(muscles[i].parent2->position.x, muscles[i].parent2->position.y, muscles[i].parent2->position.z);
1551 if (muscleview != 2) {
1554 for (int i = 0; i < num_joints; i++) {
1556 glColor4f(0, 0, .5, 1);
1558 glColor4f(1, 1, 0, 1);
1559 if (joints[i].locked && i != selected)
1560 glColor4f(1, 0, 0, 1);
1561 glVertex3f(joints[i].position.x, joints[i].position.y, joints[i].position.z);
1566 //Set old position to current position
1567 if (muscleview == 2)
1568 for (int i = 0; i < num_joints; i++) {
1569 joints[i].oldposition = joints[i].position;
1579 void Skeleton::AddJoint(float x, float y, float z, int which)
1581 if (num_joints < max_joints - 1) {
1582 joints[num_joints].velocity = 0;
1583 joints[num_joints].position.x = x;
1584 joints[num_joints].position.y = y;
1585 joints[num_joints].position.z = z;
1586 joints[num_joints].mass = 1;
1587 joints[num_joints].locked = 0;
1589 joints[num_joints].hasparent = 0;
1591 if (which < num_joints && which >= 0)
1592 AddMuscle(num_joints - 1, which, 0, 10, boneconnect);
1601 void Skeleton::DeleteJoint(int whichjoint)
1603 if (whichjoint < num_joints && whichjoint >= 0) {
1604 joints[whichjoint].velocity = joints[num_joints - 1].velocity;
1605 joints[whichjoint].position = joints[num_joints - 1].position;
1606 joints[whichjoint].oldposition = joints[num_joints - 1].oldposition;
1607 joints[whichjoint].hasparent = joints[num_joints - 1].hasparent;
1608 joints[whichjoint].parent = joints[num_joints - 1].parent;
1609 joints[whichjoint].length = joints[num_joints - 1].length;
1610 joints[whichjoint].locked = joints[num_joints - 1].locked;
1611 joints[whichjoint].modelnum = joints[num_joints - 1].modelnum;
1612 joints[whichjoint].visible = joints[num_joints - 1].visible;
1614 for (int i = 0; i < num_muscles; i++) {
1615 while (muscles[i].parent1 == &joints[whichjoint] && i < num_muscles)DeleteMuscle(i);
1616 while (muscles[i].parent2 == &joints[whichjoint] && i < num_muscles)DeleteMuscle(i);
1618 for (int i = 0; i < num_muscles; i++) {
1619 while (muscles[i].parent1 == &joints[num_joints - 1] && i < num_muscles)muscles[i].parent1 = &joints[whichjoint];
1620 while (muscles[i].parent2 == &joints[num_joints - 1] && i < num_muscles)muscles[i].parent2 = &joints[whichjoint];
1622 for (int i = 0; i < num_joints; i++) {
1623 if (joints[i].parent == &joints[whichjoint])
1624 joints[i].hasparent = 0;
1626 for (int i = 0; i < num_joints; i++) {
1627 if (joints[i].parent == &joints[num_joints - 1])
1628 joints[i].parent = &joints[whichjoint];
1638 * Skeleton::DeleteJoint - UNUSED
1640 void Skeleton::DeleteMuscle(int whichmuscle)
1642 if (whichmuscle < num_muscles) {
1643 muscles[whichmuscle].minlength = muscles[num_muscles - 1].minlength;
1644 muscles[whichmuscle].maxlength = muscles[num_muscles - 1].maxlength;
1645 muscles[whichmuscle].strength = muscles[num_muscles - 1].strength;
1646 muscles[whichmuscle].parent1 = muscles[num_muscles - 1].parent1;
1647 muscles[whichmuscle].parent2 = muscles[num_muscles - 1].parent2;
1648 muscles[whichmuscle].length = muscles[num_muscles - 1].length;
1649 muscles[whichmuscle].visible = muscles[num_muscles - 1].visible;
1650 muscles[whichmuscle].type = muscles[num_muscles - 1].type;
1651 muscles[whichmuscle].targetlength = muscles[num_muscles - 1].targetlength;
1662 void Skeleton::SetJoint(float x, float y, float z, int which, int whichjoint)
1664 if (whichjoint < num_joints) {
1665 joints[whichjoint].velocity = 0;
1666 joints[whichjoint].position.x = x;
1667 joints[whichjoint].position.y = y;
1668 joints[whichjoint].position.z = z;
1670 if (which >= num_joints || which < 0)
1671 joints[whichjoint].hasparent = 0;
1672 if (which < num_joints && which >= 0) {
1673 joints[whichjoint].parent = &joints[which];
1674 joints[whichjoint].hasparent = 1;
1675 joints[whichjoint].length = findDistance(&joints[whichjoint].position, &joints[whichjoint].parent->position);
1683 * Skeleton::AddJoint - UNUSED
1685 void Skeleton::AddMuscle(int attach1, int attach2, float minlength, float maxlength, int type)
1687 const int max_muscles = 100; // FIXME: Probably can be dropped
1688 if (num_muscles < max_muscles - 1 && attach1 < num_joints && attach1 >= 0 && attach2 < num_joints && attach2 >= 0 && attach1 != attach2) {
1689 muscles[num_muscles].parent1 = &joints[attach1];
1690 muscles[num_muscles].parent2 = &joints[attach2];
1691 muscles[num_muscles].length = findDistance(&muscles[num_muscles].parent1->position, &muscles[num_muscles].parent2->position);
1692 muscles[num_muscles].targetlength = findDistance(&muscles[num_muscles].parent1->position, &muscles[num_muscles].parent2->position);
1693 muscles[num_muscles].strength = .7;
1694 muscles[num_muscles].type = type;
1695 muscles[num_muscles].minlength = minlength;
1696 muscles[num_muscles].maxlength = maxlength;
1707 void Skeleton::MusclesSet()
1709 for (int i = 0; i < num_muscles; i++) {
1710 muscles[i].length = findDistance(&muscles[i].parent1->position, &muscles[i].parent2->position);
1719 void Skeleton::DoBalance()
1722 newpoint=joints[0].position;
1723 newpoint.x=(joints[2].position.x+joints[4].position.x)/2;
1724 newpoint.z=(joints[2].position.z+joints[4].position.z)/2;
1725 joints[0].velocity=joints[0].velocity+(newpoint-joints[0].position);
1726 //Move child point to within certain distance of parent point
1727 joints[0].position=newpoint;