2 Copyright (C) 2003, 2010 - Wolfire Games
4 This file is part of Lugaru.
6 Lugaru is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 Lugaru is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Lugaru. If not, see <http://www.gnu.org/licenses/>.
20 /**> HEADER FILES <**/
23 #include "openal_wrapper.h"
24 #include "Animation.h"
26 extern float multiplier;
28 extern Skeleton testskeleton;
29 extern Terrain terrain;
30 extern Objects objects;
31 extern int environment;
32 extern float camerashake;
35 extern XYZ envsound[30];
36 extern float envsoundvol[30];
37 extern int numenvsounds;
38 extern float envsoundlife[30];
39 extern int tutoriallevel;
41 extern int whichjointstartarray[26];
42 extern int whichjointendarray[26];
44 extern bool visibleloading;
48 void dealloc2(void* param)
53 enum {boneconnect, constraint, muscle};
57 * sets strength, length,
58 * parent1->position, parent2->position,
59 * parent1->velocity, parent2->velocity
63 * Skeleton::DoConstraints
65 void Muscle::DoConstraint(bool spinny)
67 // FIXME: relaxlength shouldn't be static, but may not always be set
68 // so I don't want to change the existing behavior even though it's probably a bug
69 static float relaxlength;
71 float oldlength = length;
73 if (type != boneconnect)
74 relaxlength = findDistance(&parent1->position, &parent2->position);
76 if (type == boneconnect)
78 if (type == constraint)
87 length -= (length - relaxlength) * (1 - strength) * multiplier * 10000;
88 length -= (length - targetlength) * (strength) * multiplier * 10000;
92 if ((relaxlength - length > 0 && relaxlength - oldlength < 0) || (relaxlength - length < 0 && relaxlength - oldlength > 0))
96 if (length < minlength)
98 if (length > maxlength)
101 if (length == relaxlength)
107 XYZ midp = (parent1->position * parent1->mass + parent2->position * parent2->mass) / (parent1->mass + parent2->mass);
109 //Find vector from midpoint to second vector
110 XYZ vel = parent2->position - midp;
112 //Change to unit vector
115 //Apply velocity change
116 XYZ newpoint1 = midp - vel * length * (parent2->mass / (parent1->mass + parent2->mass));
117 XYZ newpoint2 = midp + vel * length * (parent1->mass / (parent1->mass + parent2->mass));
118 if (!freeze && spinny) {
119 parent1->velocity = parent1->velocity + (newpoint1 - parent1->position) / multiplier / 4;
120 parent2->velocity = parent2->velocity + (newpoint2 - parent2->position) / multiplier / 4;
122 parent1->velocity = parent1->velocity + (newpoint1 - parent1->position);
123 parent2->velocity = parent2->velocity + (newpoint2 - parent2->position);
126 //Move child point to within certain distance of parent point
127 parent1->position = newpoint1;
128 parent2->position = newpoint2;
132 * sets forward, lowforward, specialforward[]
136 * Person/Person::DoAnimations
137 * Person/Person::DrawSkeleton
139 void Skeleton::FindForwards()
141 //Find forward vectors
142 CrossProduct(joints[forwardjoints[1]].position - joints[forwardjoints[0]].position, joints[forwardjoints[2]].position - joints[forwardjoints[0]].position, &forward);
145 CrossProduct(joints[lowforwardjoints[1]].position - joints[lowforwardjoints[0]].position, joints[lowforwardjoints[2]].position - joints[lowforwardjoints[0]].position, &lowforward);
146 Normalise(&lowforward);
149 specialforward[0] = forward;
151 specialforward[1] = jointPos(rightshoulder) + jointPos(rightwrist);
152 specialforward[1] = jointPos(rightelbow) - specialforward[1] / 2;
153 specialforward[1] += forward * .4;
154 Normalise(&specialforward[1]);
155 specialforward[2] = jointPos(leftshoulder) + jointPos(leftwrist);
156 specialforward[2] = jointPos(leftelbow) - specialforward[2] / 2;
157 specialforward[2] += forward * .4;
158 Normalise(&specialforward[2]);
160 specialforward[3] = jointPos(righthip) + jointPos(rightankle);
161 specialforward[3] = specialforward[3] / 2 - jointPos(rightknee);
162 specialforward[3] += lowforward * .4;
163 Normalise(&specialforward[3]);
164 specialforward[4] = jointPos(lefthip) + jointPos(leftankle);
165 specialforward[4] = specialforward[4] / 2 - jointPos(leftknee);
166 specialforward[4] += lowforward * .4;
167 Normalise(&specialforward[4]);
174 * Person/Person::RagDoll
175 * Person/Person::DoStuff
178 float Skeleton::DoConstraints(XYZ *coords, float *scale)
180 float friction = 1.5;
181 const float elasticity = .3;
183 const int numrepeats = 3;
184 float groundlevel = .15;
193 float damage = 0; // eventually returned from function
194 bool breaking = false;
197 freetime += multiplier;
199 whichpatchx = coords->x / (terrain.size / subdivision * terrain.scale);
200 whichpatchz = coords->z / (terrain.size / subdivision * terrain.scale);
202 terrainlight = *coords;
203 objects.SphereCheckPossible(&terrainlight, 1);
206 for (i = 0; i < num_joints; i++) {
207 joints[i].position = joints[i].position + joints[i].velocity * multiplier;
209 switch (joints[i].label) {
226 joints[i].position.y -= groundlevel;
227 joints[i].oldvelocity = joints[i].velocity;
230 float tempmult = multiplier;
231 //multiplier/=numrepeats;
233 for (j = 0; j < numrepeats; j++) {
235 // right leg constraints?
236 if (!joint(rightknee).locked && !joint(righthip).locked) {
237 temp = jointPos(rightknee) - (jointPos(righthip) + jointPos(rightankle)) / 2;
238 while (normaldotproduct(temp, lowforward) > -.1 && !sphere_line_intersection(&jointPos(righthip), &jointPos(rightankle), &jointPos(rightknee), &r)) {
239 jointPos(rightknee) -= lowforward * .05;
241 jointVel(rightknee) -= lowforward * .05 / multiplier / 4;
243 jointVel(rightknee) -= lowforward * .05;
244 jointPos(rightankle) += lowforward * .025;
246 jointVel(rightankle) += lowforward * .025 / multiplier / 4;
248 jointVel(rightankle) += lowforward * .25;
249 jointPos(righthip) += lowforward * .025;
251 jointVel(righthip) += lowforward * .025 / multiplier / 4;
253 jointVel(righthip) += lowforward * .025;
254 temp = jointPos(rightknee) - (jointPos(righthip) + jointPos(rightankle)) / 2;
258 // left leg constraints?
259 if (!joint(leftknee).locked && !joint(lefthip).locked) {
260 temp = jointPos(leftknee) - (jointPos(lefthip) + jointPos(leftankle)) / 2;
261 while (normaldotproduct(temp, lowforward) > -.1 && !sphere_line_intersection(&jointPos(lefthip), &jointPos(leftankle), &jointPos(leftknee), &r)) {
262 jointPos(leftknee) -= lowforward * .05;
264 jointVel(leftknee) -= lowforward * .05 / multiplier / 4;
266 jointVel(leftknee) -= lowforward * .05;
267 jointPos(leftankle) += lowforward * .025;
269 jointVel(leftankle) += lowforward * .025 / multiplier / 4;
271 jointVel(leftankle) += lowforward * .25;
272 jointPos(lefthip) += lowforward * .025;
274 jointVel(lefthip) += lowforward * .025 / multiplier / 4;
276 jointVel(lefthip) += lowforward * .025;
277 temp = jointPos(leftknee) - (jointPos(lefthip) + jointPos(leftankle)) / 2;
281 for (i = 0; i < num_joints; i++) {
282 if (joints[i].locked && !spinny && findLengthfast(&joints[i].velocity) > 320)
283 joints[i].locked = 0;
284 if (spinny && findLengthfast(&joints[i].velocity) > 600)
285 joints[i].locked = 0;
286 if (joints[i].delay > 0) {
288 for (j = 0; j < num_joints; j++) {
289 if (joints[j].locked)
293 joints[i].delay -= multiplier * 3;
298 for (i = 0; i < num_muscles; i++) {
300 muscles[i].DoConstraint(spinny);
303 for (i = 0; i < num_joints; i++) {
307 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) {
310 if (joints[i].label == groin && !joints[i].locked && joints[i].delay <= 0) {
311 joints[i].locked = 1;
313 if (tutoriallevel != 1 || id == 0) {
314 emit_sound_at(landsound1, joints[i].position * (*scale) + *coords, 128.);
319 if (joints[i].label == head && !joints[i].locked && joints[i].delay <= 0) {
320 joints[i].locked = 1;
322 if (tutoriallevel != 1 || id == 0) {
323 emit_sound_at(landsound2, joints[i].position * (*scale) + *coords, 128.);
327 terrainnormal = terrain.getNormal(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z);
328 ReflectVector(&joints[i].velocity, &terrainnormal);
329 bounceness = terrainnormal * findLength(&joints[i].velocity) * (abs(normaldotproduct(joints[i].velocity, terrainnormal)));
330 if (!joints[i].locked)
331 damage += findLengthfast(&bounceness) / 4000;
332 if (findLengthfast(&joints[i].velocity) < findLengthfast(&bounceness))
334 frictionness = abs(normaldotproduct(joints[i].velocity, terrainnormal));
335 joints[i].velocity -= bounceness;
336 if (1 - friction * frictionness > 0)
337 joints[i].velocity *= 1 - friction * frictionness;
339 joints[i].velocity = 0;
341 if (tutoriallevel != 1 || id == 0)
342 if (findLengthfast(&bounceness) > 8000 && breaking) {
343 // FIXME: this crashes because k is not initialized!
344 // to reproduce, type 'wolfie' in console and play a while
345 // I'll just comment it out for now
346 //objects.model[k].MakeDecal(breakdecal, DoRotation(temp - objects.position[k], 0, -objects.yaw[k], 0), .4, .5, Random() % 360);
347 Sprite::MakeSprite(cloudsprite, joints[i].position * (*scale) + *coords, joints[i].velocity * .06, 1, 1, 1, 4, .2);
351 emit_sound_at(breaksound2, joints[i].position * (*scale) + *coords);
353 envsound[numenvsounds] = *coords;
354 envsoundvol[numenvsounds] = 64;
355 envsoundlife[numenvsounds] = .4;
359 if (findLengthfast(&bounceness) > 2500) {
360 Normalise(&bounceness);
361 bounceness = bounceness * 50;
364 joints[i].velocity += bounceness * elasticity;
366 if (findLengthfast(&joints[i].velocity) > findLengthfast(&joints[i].oldvelocity)) {
368 joints[i].velocity = joints[i].oldvelocity;
372 if (joints[i].locked == 0)
373 if (findLengthfast(&joints[i].velocity) < 1)
374 joints[i].locked = 1;
376 if (environment == snowyenvironment && findLengthfast(&bounceness) > 500 && terrain.getOpacity(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z) < .2) {
377 terrainlight = terrain.getLighting(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z);
378 Sprite::MakeSprite(cloudsprite, joints[i].position * (*scale) + *coords, joints[i].velocity * .06, terrainlight.x, terrainlight.y, terrainlight.z, .5, .7);
380 terrain.MakeDecal(bodyprintdecal, joints[i].position * (*scale) + *coords, .4, .4, 0);
381 } else if (environment == desertenvironment && findLengthfast(&bounceness) > 500 && terrain.getOpacity(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z) < .2) {
382 terrainlight = terrain.getLighting(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z);
383 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);
386 else if (environment == grassyenvironment && findLengthfast(&bounceness) > 500 && terrain.getOpacity(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z) < .2) {
387 terrainlight = terrain.getLighting(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z);
388 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);
389 } else if (findLengthfast(&bounceness) > 500)
390 Sprite::MakeSprite(cloudsprite, joints[i].position * (*scale) + *coords, joints[i].velocity * .06, terrainlight.x, terrainlight.y, terrainlight.z, .5, .2);
393 joints[i].position.y = (terrain.getHeight(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z) + groundlevel - coords->y) / (*scale);
397 if (terrain.patchobjectnum[whichpatchx][whichpatchz])
398 for (m = 0; m < terrain.patchobjectnum[whichpatchx][whichpatchz]; m++) {
399 k = terrain.patchobjects[whichpatchx][whichpatchz][m];
400 if (k < objects.numobjects && k >= 0)
401 if (objects.possible[k]) {
402 friction = objects.friction[k];
403 XYZ start = joints[i].realoldposition;
404 XYZ end = joints[i].position * (*scale) + *coords;
405 whichhit = objects.model[k].LineCheckPossible(&start, &end, &temp, &objects.position[k], &objects.yaw[k]);
406 if (whichhit != -1) {
407 if (joints[i].label == groin && !joints[i].locked && joints[i].delay <= 0) {
408 joints[i].locked = 1;
410 if (tutoriallevel != 1 || id == 0) {
411 emit_sound_at(landsound1, joints[i].position * (*scale) + *coords, 128.);
416 if (joints[i].label == head && !joints[i].locked && joints[i].delay <= 0) {
417 joints[i].locked = 1;
419 if (tutoriallevel != 1 || id == 0) {
420 emit_sound_at(landsound2, joints[i].position * (*scale) + *coords, 128.);
424 terrainnormal = DoRotation(objects.model[k].facenormals[whichhit], 0, objects.yaw[k], 0) * -1;
425 if (terrainnormal.y > .8)
427 bounceness = terrainnormal * findLength(&joints[i].velocity) * (abs(normaldotproduct(joints[i].velocity, terrainnormal)));
428 if (findLengthfast(&joints[i].velocity) > findLengthfast(&joints[i].oldvelocity)) {
430 joints[i].velocity = joints[i].oldvelocity;
432 if (tutoriallevel != 1 || id == 0)
433 if (findLengthfast(&bounceness) > 4000 && breaking) {
434 objects.model[k].MakeDecal(breakdecal, DoRotation(temp - objects.position[k], 0, -objects.yaw[k], 0), .4, .5, Random() % 360);
435 Sprite::MakeSprite(cloudsprite, joints[i].position * (*scale) + *coords, joints[i].velocity * .06, 1, 1, 1, 4, .2);
439 emit_sound_at(breaksound2, joints[i].position * (*scale) + *coords);
441 envsound[numenvsounds] = *coords;
442 envsoundvol[numenvsounds] = 64;
443 envsoundlife[numenvsounds] = .4;
446 if (objects.type[k] == treetrunktype) {
447 objects.rotx[k] += joints[i].velocity.x * multiplier * .4;
448 objects.roty[k] += joints[i].velocity.z * multiplier * .4;
449 objects.rotx[k + 1] += joints[i].velocity.x * multiplier * .4;
450 objects.roty[k + 1] += joints[i].velocity.z * multiplier * .4;
452 if (!joints[i].locked)
453 damage += findLengthfast(&bounceness) / 2500;
454 ReflectVector(&joints[i].velocity, &terrainnormal);
455 frictionness = abs(normaldotproduct(joints[i].velocity, terrainnormal));
456 joints[i].velocity -= bounceness;
457 if (1 - friction * frictionness > 0)
458 joints[i].velocity *= 1 - friction * frictionness;
460 joints[i].velocity = 0;
461 if (findLengthfast(&bounceness) > 2500) {
462 Normalise(&bounceness);
463 bounceness = bounceness * 50;
465 joints[i].velocity += bounceness * elasticity;
468 if (!joints[i].locked)
469 if (findLengthfast(&joints[i].velocity) < 1) {
470 joints[i].locked = 1;
472 if (findLengthfast(&bounceness) > 500)
473 Sprite::MakeSprite(cloudsprite, joints[i].position * (*scale) + *coords, joints[i].velocity * .06, 1, 1, 1, .5, .2);
474 joints[i].position = (temp - *coords) / (*scale) + terrainnormal * .005;
480 joints[i].realoldposition = joints[i].position * (*scale) + *coords;
483 multiplier = tempmult;
486 if (terrain.patchobjectnum[whichpatchx][whichpatchz])
487 for (m = 0; m < terrain.patchobjectnum[whichpatchx][whichpatchz]; m++) {
488 k = terrain.patchobjects[whichpatchx][whichpatchz][m];
489 if (objects.possible[k]) {
490 for (i = 0; i < 26; i++) {
491 //Make this less stupid
492 XYZ start = joints[jointlabels[whichjointstartarray[i]]].position * (*scale) + *coords;
493 XYZ end = joints[jointlabels[whichjointendarray[i]]].position * (*scale) + *coords;
494 whichhit = objects.model[k].LineCheckSlidePossible(&start, &end, &temp, &objects.position[k], &objects.yaw[k]);
495 if (whichhit != -1) {
496 joints[jointlabels[whichjointendarray[i]]].position = (end - *coords) / (*scale);
497 for (j = 0; j < num_muscles; j++) {
498 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]))
499 muscles[j].DoConstraint(spinny);
506 for (i = 0; i < num_joints; i++) {
507 switch (joints[i].label) {
523 joints[i].position.y += groundlevel;
525 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)
527 if (joints[i].locked) {
536 for (i = 0; i < num_muscles; i++) {
537 if (muscles[i].type == boneconnect)
538 muscles[i].DoConstraint(0);
546 * applies gravity to the skeleton
549 * Person/Person::DoStuff
551 void Skeleton::DoGravity(float *scale)
554 for (i = 0; i < num_joints; i++) {
557 ((joints[i].label != leftknee) && (joints[i].label != rightknee)) ||
558 (lowforward.y > -.1) ||
561 ((joints[i].label != leftelbow) && (joints[i].label != rightelbow)) ||
565 joints[i].velocity.y += gravity * multiplier / (*scale);
570 * set muscles[which].rotate1
574 * special case if animation == hanganim
576 void Skeleton::FindRotationMuscle(int which, int animation)
581 p1 = muscles[which].parent1->position;
582 p2 = muscles[which].parent2->position;
583 dist = findDistance(&p1, &p2);
584 if (p1.y - p2.y <= dist)
585 muscles[which].rotate2 = asin((p1.y - p2.y) / dist);
586 if (p1.y - p2.y > dist)
587 muscles[which].rotate2 = asin(1.f);
588 muscles[which].rotate2 *= 360.0 / 6.2831853;
592 dist = findDistance(&p1, &p2);
593 if (p1.z - p2.z <= dist)
594 muscles[which].rotate1 = acos((p1.z - p2.z) / dist);
595 if (p1.z - p2.z > dist)
596 muscles[which].rotate1 = acos(1.f);
597 muscles[which].rotate1 *= 360.0 / 6.2831853;
599 muscles[which].rotate1 = 360 - muscles[which].rotate1;
600 if (!isnormal(muscles[which].rotate1))
601 muscles[which].rotate1 = 0;
602 if (!isnormal(muscles[which].rotate2))
603 muscles[which].rotate2 = 0;
605 const int label1 = muscles[which].parent1->label;
606 const int label2 = muscles[which].parent2->label;
609 fwd = specialforward[0];
615 fwd = specialforward[1];
621 fwd = specialforward[2];
627 fwd = specialforward[3];
633 fwd = specialforward[4];
636 if (muscles[which].parent1->lower)
643 if (animation == hanganim) {
644 if (label1 == righthand || label2 == righthand) {
648 if (label1 == lefthand || label2 == lefthand) {
655 if (label1 == rightfoot || label2 == rightfoot) {
658 if (label1 == leftfoot || label2 == leftfoot) {
663 fwd = DoRotation(fwd, 0, muscles[which].rotate1 - 90, 0);
664 fwd = DoRotation(fwd, 0, 0, muscles[which].rotate2 - 90);
666 fwd /= findLength(&fwd);
667 if (fwd.z <= 1 && fwd.z >= -1)
668 muscles[which].rotate3 = acos(0 - fwd.z);
670 muscles[which].rotate3 = acos(-1.f);
671 muscles[which].rotate3 *= 360.0 / 6.2831853;
673 muscles[which].rotate3 = 360 - muscles[which].rotate3;
674 if (!isnormal(muscles[which].rotate3))
675 muscles[which].rotate3 = 0;
679 * load an animation from file
681 void Animation::Load(const char *filename, int aheight, int aattack)
685 XYZ startoffset, endoffset;
688 const char *anim_prefix = ":Data:Animations:";
693 // concatenate anim_prefix + filename
694 int len = strlen(anim_prefix) + strlen(filename);
695 char *buf = new char[len + 1];
696 snprintf(buf, len + 1, "%s%s", anim_prefix, filename);
697 // Changing the filename into something the OS can understand
698 char *fixedFN = ConvertFileName(buf);
701 LOG(std::string("Loading animation...") + fixedFN);
703 // clear existing data
710 Game::LoadingScreen();
712 // read file in binary mode
713 tfile = fopen( fixedFN, "rb" );
715 // read numframes, joints to know how much memory to allocate
716 funpackf(tfile, "Bi Bi", &numframes, &joints);
718 // allocate memory for everything
720 position = (XYZ**)malloc(sizeof(XYZ*) * joints);
721 for (i = 0; i < joints; i++)
722 position[i] = (XYZ*)malloc(sizeof(XYZ) * numframes);
724 twist = (float**)malloc(sizeof(float*) * joints);
725 for (i = 0; i < joints; i++)
726 twist[i] = (float*)malloc(sizeof(float) * numframes);
728 twist2 = (float**)malloc(sizeof(float*) * joints);
729 for (i = 0; i < joints; i++)
730 twist2[i] = (float*)malloc(sizeof(float) * numframes);
732 speed = (float*)malloc(sizeof(float) * numframes);
734 onground = (bool**)malloc(sizeof(bool*) * joints);
735 for (i = 0; i < joints; i++)
736 onground[i] = (bool*)malloc(sizeof(bool) * numframes);
738 forward = (XYZ*)malloc(sizeof(XYZ) * numframes);
739 weapontarget = (XYZ*)malloc(sizeof(XYZ) * numframes);
740 label = (int*)malloc(sizeof(int) * numframes);
742 // read binary data as animation
745 for (i = 0; i < numframes; i++) {
746 // for each joint in the skeleton...
747 for (j = 0; j < joints; j++) {
748 // read joint position
749 funpackf(tfile, "Bf Bf Bf", &position[j][i].x, &position[j][i].y, &position[j][i].z);
751 for (j = 0; j < joints; j++) {
753 funpackf(tfile, "Bf", &twist[j][i]);
755 for (j = 0; j < joints; j++) {
756 // read onground (boolean)
758 funpackf(tfile, "Bb", &uch);
759 onground[j][i] = (uch != 0);
761 // read frame speed (?)
762 funpackf(tfile, "Bf", &speed[i]);
764 // read twist2 for whole animation
765 for (i = 0; i < numframes; i++) {
766 for (j = 0; j < joints; j++) {
767 funpackf(tfile, "Bf", &twist2[j][i]);
770 // read label for each frame
771 for (i = 0; i < numframes; i++) {
772 funpackf(tfile, "Bf", &label[i]);
774 // read weapontargetnum
775 funpackf(tfile, "Bi", &weapontargetnum);
776 // read weapontarget positions for each frame
777 for (i = 0; i < numframes; i++) {
778 funpackf(tfile, "Bf Bf Bf", &weapontarget[i].x, &weapontarget[i].y, &weapontarget[i].z);
786 // find average position of certain joints on first and last frames
787 // and save in startoffset, 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][0].y < 1)
791 startoffset += position[j][0];
792 if (position[j][numframes - 1].y < 1)
793 endoffset += position[j][numframes - 1];
795 startoffset /= joints;
804 * takes filenames for three skeleton files and various models
806 void Skeleton::Load(const char *filename, const char *lowfilename, const char *clothesfilename,
807 const char *modelfilename, const char *model2filename,
808 const char *model3filename, const char *model4filename,
809 const char *model5filename, const char *model6filename,
810 const char *model7filename, const char *modellowfilename,
811 const char *modelclothesfilename, bool clothes)
824 // load various models
825 // rotate, scale, do normals, do texcoords for each as needed
827 model[0].loadnotex(modelfilename);
828 model[1].loadnotex(model2filename);
829 model[2].loadnotex(model3filename);
830 model[3].loadnotex(model4filename);
831 model[4].loadnotex(model5filename);
832 model[5].loadnotex(model6filename);
833 model[6].loadnotex(model7filename);
835 for (i = 0; i < num_models; i++) {
836 model[i].Rotate(180, 0, 0);
837 model[i].Scale(.04, .04, .04);
838 model[i].CalculateNormals(0);
841 drawmodel.load(modelfilename, 0);
842 drawmodel.Rotate(180, 0, 0);
843 drawmodel.Scale(.04, .04, .04);
844 drawmodel.FlipTexCoords();
845 if (tutoriallevel == 1 && id != 0)
846 drawmodel.UniformTexCoords();
847 if (tutoriallevel == 1 && id != 0)
848 drawmodel.ScaleTexCoords(0.1);
849 drawmodel.CalculateNormals(0);
851 modellow.loadnotex(modellowfilename);
852 modellow.Rotate(180, 0, 0);
853 modellow.Scale(.04, .04, .04);
854 modellow.CalculateNormals(0);
856 drawmodellow.load(modellowfilename, 0);
857 drawmodellow.Rotate(180, 0, 0);
858 drawmodellow.Scale(.04, .04, .04);
859 drawmodellow.FlipTexCoords();
860 if (tutoriallevel == 1 && id != 0)
861 drawmodellow.UniformTexCoords();
862 if (tutoriallevel == 1 && id != 0)
863 drawmodellow.ScaleTexCoords(0.1);
864 drawmodellow.CalculateNormals(0);
867 modelclothes.loadnotex(modelclothesfilename);
868 modelclothes.Rotate(180, 0, 0);
869 modelclothes.Scale(.041, .04, .041);
870 modelclothes.CalculateNormals(0);
872 drawmodelclothes.load(modelclothesfilename, 0);
873 drawmodelclothes.Rotate(180, 0, 0);
874 drawmodelclothes.Scale(.04, .04, .04);
875 drawmodelclothes.FlipTexCoords();
876 drawmodelclothes.CalculateNormals(0);
879 // FIXME: three similar blocks follow, one for each of:
880 // filename, lowfilename, clothesfilename
884 tfile = fopen( ConvertFileName(filename), "rb" );
886 if (1) { // FIXME: should this be if(tfile) ?
888 funpackf(tfile, "Bi", &num_joints);
892 delete [] joints; //dealloc2(joints);
893 joints = (Joint*)new Joint[num_joints];
895 // read info for each joint
896 for (i = 0; i < num_joints; i++) {
897 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);
898 funpackf(tfile, "Bb Bb", &joints[i].hasparent, &joints[i].locked);
899 funpackf(tfile, "Bi", &joints[i].modelnum);
900 funpackf(tfile, "Bb Bb", &joints[i].visible, &joints[i].sametwist);
901 funpackf(tfile, "Bi Bi", &joints[i].label, &joints[i].hasgun);
902 funpackf(tfile, "Bb", &joints[i].lower);
903 funpackf(tfile, "Bi", &parentID);
904 if (joints[i].hasparent)
905 joints[i].parent = &joints[parentID];
906 joints[i].velocity = 0;
907 joints[i].oldposition = joints[i].position;
911 funpackf(tfile, "Bi", &num_muscles);
915 delete [] muscles; //dealloc2(muscles);
916 muscles = (Muscle*)new Muscle[num_muscles]; //malloc(sizeof(Muscle)*num_muscles);
918 // for each muscle...
919 for (i = 0; i < num_muscles; i++) {
921 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);
923 // allocate memory for vertices
924 muscles[i].vertices = (int*)malloc(sizeof(int) * muscles[i].numvertices);
928 for (j = 0; j < muscles[i].numvertices - edit; j++) {
929 funpackf(tfile, "Bi", &muscles[i].vertices[j + edit]);
930 if (muscles[i].vertices[j + edit] >= model[0].vertexNum) {
931 muscles[i].numvertices--;
937 funpackf(tfile, "Bb Bi", &muscles[i].visible, &parentID);
938 muscles[i].parent1 = &joints[parentID];
939 funpackf(tfile, "Bi", &parentID);
940 muscles[i].parent2 = &joints[parentID];
943 // read forwardjoints (?)
944 for (j = 0; j < 3; j++) {
945 funpackf(tfile, "Bi", &forwardjoints[j]);
947 // read lowforwardjoints (?)
948 for (j = 0; j < 3; j++) {
949 funpackf(tfile, "Bi", &lowforwardjoints[j]);
953 for (j = 0; j < num_muscles; j++) {
954 for (i = 0; i < muscles[j].numvertices; i++) {
955 for (int k = 0; k < num_models; k++) {
956 if (muscles[j].numvertices && muscles[j].vertices[i] < model[k].vertexNum)
957 model[k].owner[muscles[j].vertices[i]] = j;
962 // calculate some stuff
964 for (i = 0; i < num_joints; i++) {
965 joints[i].startpos = joints[i].position;
967 for (i = 0; i < num_muscles; i++) {
968 FindRotationMuscle(i, -1);
970 // this seems to use opengl purely for matrix calculations
971 for (int k = 0; k < num_models; k++) {
972 for (i = 0; i < model[k].vertexNum; i++) {
973 model[k].vertex[i] = model[k].vertex[i] - (muscles[model[k].owner[i]].parent1->position + muscles[model[k].owner[i]].parent2->position) / 2;
974 glMatrixMode(GL_MODELVIEW);
977 glRotatef(muscles[model[k].owner[i]].rotate3, 0, 1, 0);
978 glRotatef(muscles[model[k].owner[i]].rotate2 - 90, 0, 0, 1);
979 glRotatef(muscles[model[k].owner[i]].rotate1 - 90, 0, 1, 0);
980 glTranslatef(model[k].vertex[i].x, model[k].vertex[i].y, model[k].vertex[i].z);
981 glGetFloatv(GL_MODELVIEW_MATRIX, M);
982 model[k].vertex[i].x = M[12] * 1;
983 model[k].vertex[i].y = M[13] * 1;
984 model[k].vertex[i].z = M[14] * 1;
987 model[k].CalculateNormals(0);
994 tfile = fopen( ConvertFileName(lowfilename), "rb" );
996 if (1) { // FIXME: should this be if(tfile) ?
997 // skip joints section
999 lSize = sizeof(num_joints);
1000 fseek(tfile, lSize, SEEK_CUR);
1001 for (i = 0; i < num_joints; i++) {
1015 fseek(tfile, lSize, SEEK_CUR);
1017 if (joints[i].hasparent)
1018 joints[i].parent = &joints[parentID];
1019 joints[i].velocity = 0;
1020 joints[i].oldposition = joints[i].position;
1024 funpackf(tfile, "Bi", &num_muscles);
1026 for (i = 0; i < num_muscles; i++) {
1028 lSize = sizeof(float)
1034 fseek(tfile, lSize, SEEK_CUR);
1036 // read numverticeslow
1037 funpackf(tfile, "Bi", &muscles[i].numverticeslow);
1039 if (muscles[i].numverticeslow) {
1041 muscles[i].verticeslow = (int*)malloc(sizeof(int) * muscles[i].numverticeslow);
1045 for (j = 0; j < muscles[i].numverticeslow - edit; j++) {
1046 funpackf(tfile, "Bi", &muscles[i].verticeslow[j + edit]);
1047 if (muscles[i].verticeslow[j + edit] >= modellow.vertexNum) {
1048 muscles[i].numverticeslow--;
1055 lSize = 1; //sizeof(bool);
1056 fseek ( tfile, lSize, SEEK_CUR);
1057 lSize = sizeof(int);
1058 fseek ( tfile, lSize, SEEK_CUR);
1059 fseek ( tfile, lSize, SEEK_CUR);
1062 for (j = 0; j < num_muscles; j++) {
1063 for (i = 0; i < muscles[j].numverticeslow; i++) {
1064 if (muscles[j].verticeslow[i] < modellow.vertexNum)
1065 modellow.owner[muscles[j].verticeslow[i]] = j;
1069 // use opengl for its matrix math
1070 for (i = 0; i < modellow.vertexNum; i++) {
1071 modellow.vertex[i] = modellow.vertex[i] - (muscles[modellow.owner[i]].parent1->position + muscles[modellow.owner[i]].parent2->position) / 2;
1072 glMatrixMode(GL_MODELVIEW);
1075 glRotatef(muscles[modellow.owner[i]].rotate3, 0, 1, 0);
1076 glRotatef(muscles[modellow.owner[i]].rotate2 - 90, 0, 0, 1);
1077 glRotatef(muscles[modellow.owner[i]].rotate1 - 90, 0, 1, 0);
1078 glTranslatef(modellow.vertex[i].x, modellow.vertex[i].y, modellow.vertex[i].z);
1079 glGetFloatv(GL_MODELVIEW_MATRIX, M);
1080 modellow.vertex[i].x = M[12];
1081 modellow.vertex[i].y = M[13];
1082 modellow.vertex[i].z = M[14];
1086 modellow.CalculateNormals(0);
1092 tfile = fopen( ConvertFileName(clothesfilename), "rb" ); // FIXME: where's the check for valid load
1095 lSize = sizeof(num_joints);
1096 fseek ( tfile, lSize, SEEK_CUR);
1098 for (i = 0; i < num_joints; i++) {
1112 fseek(tfile, lSize, SEEK_CUR);
1114 if (joints[i].hasparent)
1115 joints[i].parent = &joints[parentID];
1116 joints[i].velocity = 0;
1117 joints[i].oldposition = joints[i].position;
1121 funpackf(tfile, "Bi", &num_muscles);
1123 for (i = 0; i < num_muscles; i++) {
1125 lSize = sizeof(float)
1131 fseek(tfile, lSize, SEEK_CUR);
1133 // read numverticesclothes
1134 funpackf(tfile, "Bi", &muscles[i].numverticesclothes);
1136 // read verticesclothes
1137 if (muscles[i].numverticesclothes) {
1138 muscles[i].verticesclothes = (int*)malloc(sizeof(int) * muscles[i].numverticesclothes);
1140 for (j = 0; j < muscles[i].numverticesclothes - edit; j++) {
1141 funpackf(tfile, "Bi", &muscles[i].verticesclothes[j + edit]);
1142 if (muscles[i].verticesclothes[j + edit] >= modelclothes.vertexNum) {
1143 muscles[i].numverticesclothes--;
1150 lSize = 1; //sizeof(bool);
1151 fseek ( tfile, lSize, SEEK_CUR);
1152 lSize = sizeof(int);
1153 fseek ( tfile, lSize, SEEK_CUR);
1154 fseek ( tfile, lSize, SEEK_CUR);
1158 lSize = sizeof(int);
1159 for (j = 0; j < num_muscles; j++) {
1160 for (i = 0; i < muscles[j].numverticesclothes; i++) {
1161 if (muscles[j].numverticesclothes && muscles[j].verticesclothes[i] < modelclothes.vertexNum)
1162 modelclothes.owner[muscles[j].verticesclothes[i]] = j;
1166 // use opengl for its matrix math
1167 for (i = 0; i < modelclothes.vertexNum; i++) {
1168 modelclothes.vertex[i] = modelclothes.vertex[i] - (muscles[modelclothes.owner[i]].parent1->position + muscles[modelclothes.owner[i]].parent2->position) / 2;
1169 glMatrixMode(GL_MODELVIEW);
1172 glRotatef(muscles[modelclothes.owner[i]].rotate3, 0, 1, 0);
1173 glRotatef(muscles[modelclothes.owner[i]].rotate2 - 90, 0, 0, 1);
1174 glRotatef(muscles[modelclothes.owner[i]].rotate1 - 90, 0, 1, 0);
1175 glTranslatef(modelclothes.vertex[i].x, modelclothes.vertex[i].y, modelclothes.vertex[i].z);
1176 glGetFloatv(GL_MODELVIEW_MATRIX, M);
1177 modelclothes.vertex[i].x = M[12];
1178 modelclothes.vertex[i].y = M[13];
1179 modelclothes.vertex[i].z = M[14];
1183 modelclothes.CalculateNormals(0);
1187 for (i = 0; i < num_joints; i++) {
1188 for (j = 0; j < num_joints; j++) {
1189 if (joints[i].label == j)
1197 Animation::Animation()
1203 weapontargetnum = 0;
1215 Animation::~Animation()
1220 void Animation::deallocate()
1225 for (i = 0; i < joints; i++)
1226 dealloc2(position[i]);
1233 for (i = 0; i < joints; i++)
1241 for (i = 0; i < joints; i++)
1242 dealloc2(twist2[i]);
1249 for (i = 0; i < joints; i++)
1250 dealloc2(onground[i]);
1265 dealloc2(weapontarget);
1275 Skeleton::Skeleton()
1283 memset(forwardjoints, 0, sizeof(forwardjoints));
1288 memset(lowforwardjoints, 0, sizeof(lowforwardjoints));
1291 // XYZ specialforward[5];
1292 memset(jointlabels, 0, sizeof(jointlabels));
1296 // Model modelclothes;
1300 // Model drawmodellow;
1301 // Model drawmodelclothes;
1306 memset(skinText, 0, sizeof(skinText));
1323 Skeleton::~Skeleton()
1340 verticesclothes = 0;
1344 numverticesclothes = 0;
1353 rotate1 = 0, rotate2 = 0, rotate3 = 0;
1354 lastrotate1 = 0, lastrotate2 = 0, lastrotate3 = 0;
1355 oldrotate1 = 0, oldrotate2 = 0, oldrotate3 = 0;
1356 newrotate1 = 0, newrotate2 = 0, newrotate3 = 0;
1364 dealloc2(verticeslow);
1365 dealloc2(verticesclothes);
1368 Animation & Animation::operator = (const Animation & ani)
1372 bool allocate = ((ani.numframes != numframes) || (ani.joints != joints));
1377 numframes = ani.numframes;
1378 height = ani.height;
1379 attack = ani.attack;
1380 joints = ani.joints;
1381 weapontargetnum = ani.weapontargetnum;
1382 offset = ani.offset;
1385 position = (XYZ**)malloc(sizeof(XYZ*)*ani.joints);
1386 for (i = 0; i < ani.joints; i++) {
1388 position[i] = (XYZ*)malloc(sizeof(XYZ) * ani.numframes);
1389 memcpy(position[i], ani.position[i], sizeof(XYZ)*ani.numframes);
1393 twist = (float**)malloc(sizeof(float*)*ani.joints);
1394 for (i = 0; i < ani.joints; i++) {
1396 twist[i] = (float*)malloc(sizeof(float) * ani.numframes);
1397 memcpy(twist[i], ani.twist[i], sizeof(float)*ani.numframes);
1401 twist2 = (float**)malloc(sizeof(float*)*ani.joints);
1402 for (i = 0; i < ani.joints; i++) {
1404 twist2[i] = (float*)malloc(sizeof(float) * ani.numframes);
1405 memcpy(twist2[i], ani.twist2[i], sizeof(float)*ani.numframes);
1409 speed = (float*)malloc(sizeof(float) * ani.numframes);
1410 memcpy(speed, ani.speed, sizeof(float)*ani.numframes);
1413 onground = (bool**)malloc(sizeof(bool*)*ani.joints);
1414 for (i = 0; i < ani.joints; i++) {
1416 onground[i] = (bool*)malloc(sizeof(bool) * ani.numframes);
1417 memcpy(onground[i], ani.onground[i], sizeof(bool)*ani.numframes);
1421 forward = (XYZ*)malloc(sizeof(XYZ) * ani.numframes);
1422 memcpy(forward, ani.forward, sizeof(XYZ)*ani.numframes);
1425 weapontarget = (XYZ*)malloc(sizeof(XYZ) * ani.numframes);
1426 memcpy(weapontarget, ani.weapontarget, sizeof(XYZ)*ani.numframes);
1429 label = (int*)malloc(sizeof(int) * ani.numframes);
1430 memcpy(label, ani.label, sizeof(int)*ani.numframes);
1440 // the following functions are not used anywhere
1443 * sets forward, lowforward, specialforward[]
1448 void Skeleton::FindForwardsfirst()
1450 //Find forward vectors
1451 CrossProduct(joints[forwardjoints[1]].position - joints[forwardjoints[0]].position, joints[forwardjoints[2]].position - joints[forwardjoints[0]].position, &forward);
1452 Normalise(&forward);
1454 CrossProduct(joints[lowforwardjoints[1]].position - joints[lowforwardjoints[0]].position, joints[lowforwardjoints[2]].position - joints[lowforwardjoints[0]].position, &lowforward);
1455 Normalise(&lowforward);
1458 specialforward[0] = forward;
1459 specialforward[1] = forward;
1460 specialforward[2] = forward;
1461 specialforward[3] = forward;
1462 specialforward[4] = forward;
1471 void Skeleton::Draw(int muscleview)
1473 static float jointcolor[4];
1475 if (muscleview != 2) {
1482 if (muscleview == 2) {
1488 //Calc motionblur-ness
1489 for (int i = 0; i < num_joints; i++) {
1490 joints[i].oldposition = joints[i].position;
1491 joints[i].blurred = findDistance(&joints[i].position, &joints[i].oldposition) * 100;
1492 if (joints[i].blurred < 1)
1493 joints[i].blurred = 1;
1499 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1501 for (int i = 0; i < num_joints; i++) {
1502 if (joints[i].hasparent) {
1503 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / joints[i].blurred);
1504 glVertex3f(joints[i].position.x, joints[i].position.y, joints[i].position.z);
1505 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / joints[i].parent->blurred);
1506 glVertex3f(joints[i].parent->position.x, joints[i].parent->position.y, joints[i].parent->position.z);
1507 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / joints[i].parent->blurred);
1508 glVertex3f(joints[i].parent->oldposition.x, joints[i].parent->oldposition.y, joints[i].parent->oldposition.z);
1509 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / joints[i].blurred);
1510 glVertex3f(joints[i].oldposition.x, joints[i].oldposition.y, joints[i].oldposition.z);
1513 for (int i = 0; i < num_muscles; i++) {
1514 if (muscles[i].type == boneconnect) {
1515 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / muscles[i].parent2->blurred);
1516 glVertex3f(muscles[i].parent1->position.x, muscles[i].parent1->position.y, muscles[i].parent1->position.z);
1517 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / muscles[i].parent2->blurred);
1518 glVertex3f(muscles[i].parent2->position.x, muscles[i].parent2->position.y, muscles[i].parent2->position.z);
1519 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / muscles[i].parent2->blurred);
1520 glVertex3f(muscles[i].parent2->oldposition.x, muscles[i].parent2->oldposition.y, muscles[i].parent2->oldposition.z);
1521 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / muscles[i].parent1->blurred);
1522 glVertex3f(muscles[i].parent1->oldposition.x, muscles[i].parent1->oldposition.y, muscles[i].parent1->oldposition.z);
1528 for (int i = 0; i < num_joints; i++) {
1529 if (joints[i].hasparent) {
1530 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / joints[i].blurred);
1531 glVertex3f(joints[i].position.x, joints[i].position.y, joints[i].position.z);
1532 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / joints[i].parent->blurred);
1533 glVertex3f(joints[i].parent->position.x, joints[i].parent->position.y, joints[i].parent->position.z);
1536 for (int i = 0; i < num_muscles; i++) {
1537 if (muscles[i].type == boneconnect) {
1538 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / muscles[i].parent1->blurred);
1539 glVertex3f(muscles[i].parent1->position.x, muscles[i].parent1->position.y, muscles[i].parent1->position.z);
1540 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / muscles[i].parent2->blurred);
1541 glVertex3f(muscles[i].parent2->position.x, muscles[i].parent2->position.y, muscles[i].parent2->position.z);
1544 glColor3f(.6, .6, 0);
1545 if (muscleview == 1)
1546 for (int i = 0; i < num_muscles; i++) {
1547 if (muscles[i].type != boneconnect) {
1548 glVertex3f(muscles[i].parent1->position.x, muscles[i].parent1->position.y, muscles[i].parent1->position.z);
1549 glVertex3f(muscles[i].parent2->position.x, muscles[i].parent2->position.y, muscles[i].parent2->position.z);
1554 if (muscleview != 2) {
1557 for (int i = 0; i < num_joints; i++) {
1559 glColor4f(0, 0, .5, 1);
1561 glColor4f(1, 1, 0, 1);
1562 if (joints[i].locked && i != selected)
1563 glColor4f(1, 0, 0, 1);
1564 glVertex3f(joints[i].position.x, joints[i].position.y, joints[i].position.z);
1569 //Set old position to current position
1570 if (muscleview == 2)
1571 for (int i = 0; i < num_joints; i++) {
1572 joints[i].oldposition = joints[i].position;
1582 void Skeleton::AddJoint(float x, float y, float z, int which)
1584 if (num_joints < max_joints - 1) {
1585 joints[num_joints].velocity = 0;
1586 joints[num_joints].position.x = x;
1587 joints[num_joints].position.y = y;
1588 joints[num_joints].position.z = z;
1589 joints[num_joints].mass = 1;
1590 joints[num_joints].locked = 0;
1592 joints[num_joints].hasparent = 0;
1594 if (which < num_joints && which >= 0)
1595 AddMuscle(num_joints - 1, which, 0, 10, boneconnect);
1604 void Skeleton::DeleteJoint(int whichjoint)
1606 if (whichjoint < num_joints && whichjoint >= 0) {
1607 joints[whichjoint].velocity = joints[num_joints - 1].velocity;
1608 joints[whichjoint].position = joints[num_joints - 1].position;
1609 joints[whichjoint].oldposition = joints[num_joints - 1].oldposition;
1610 joints[whichjoint].hasparent = joints[num_joints - 1].hasparent;
1611 joints[whichjoint].parent = joints[num_joints - 1].parent;
1612 joints[whichjoint].length = joints[num_joints - 1].length;
1613 joints[whichjoint].locked = joints[num_joints - 1].locked;
1614 joints[whichjoint].modelnum = joints[num_joints - 1].modelnum;
1615 joints[whichjoint].visible = joints[num_joints - 1].visible;
1617 for (int i = 0; i < num_muscles; i++) {
1618 while (muscles[i].parent1 == &joints[whichjoint] && i < num_muscles)DeleteMuscle(i);
1619 while (muscles[i].parent2 == &joints[whichjoint] && i < num_muscles)DeleteMuscle(i);
1621 for (int i = 0; i < num_muscles; i++) {
1622 while (muscles[i].parent1 == &joints[num_joints - 1] && i < num_muscles)muscles[i].parent1 = &joints[whichjoint];
1623 while (muscles[i].parent2 == &joints[num_joints - 1] && i < num_muscles)muscles[i].parent2 = &joints[whichjoint];
1625 for (int i = 0; i < num_joints; i++) {
1626 if (joints[i].parent == &joints[whichjoint])
1627 joints[i].hasparent = 0;
1629 for (int i = 0; i < num_joints; i++) {
1630 if (joints[i].parent == &joints[num_joints - 1])
1631 joints[i].parent = &joints[whichjoint];
1641 * Skeleton::DeleteJoint - UNUSED
1643 void Skeleton::DeleteMuscle(int whichmuscle)
1645 if (whichmuscle < num_muscles) {
1646 muscles[whichmuscle].minlength = muscles[num_muscles - 1].minlength;
1647 muscles[whichmuscle].maxlength = muscles[num_muscles - 1].maxlength;
1648 muscles[whichmuscle].strength = muscles[num_muscles - 1].strength;
1649 muscles[whichmuscle].parent1 = muscles[num_muscles - 1].parent1;
1650 muscles[whichmuscle].parent2 = muscles[num_muscles - 1].parent2;
1651 muscles[whichmuscle].length = muscles[num_muscles - 1].length;
1652 muscles[whichmuscle].visible = muscles[num_muscles - 1].visible;
1653 muscles[whichmuscle].type = muscles[num_muscles - 1].type;
1654 muscles[whichmuscle].targetlength = muscles[num_muscles - 1].targetlength;
1665 void Skeleton::SetJoint(float x, float y, float z, int which, int whichjoint)
1667 if (whichjoint < num_joints) {
1668 joints[whichjoint].velocity = 0;
1669 joints[whichjoint].position.x = x;
1670 joints[whichjoint].position.y = y;
1671 joints[whichjoint].position.z = z;
1673 if (which >= num_joints || which < 0)
1674 joints[whichjoint].hasparent = 0;
1675 if (which < num_joints && which >= 0) {
1676 joints[whichjoint].parent = &joints[which];
1677 joints[whichjoint].hasparent = 1;
1678 joints[whichjoint].length = findDistance(&joints[whichjoint].position, &joints[whichjoint].parent->position);
1686 * Skeleton::AddJoint - UNUSED
1688 void Skeleton::AddMuscle(int attach1, int attach2, float minlength, float maxlength, int type)
1690 const int max_muscles = 100; // FIXME: Probably can be dropped
1691 if (num_muscles < max_muscles - 1 && attach1 < num_joints && attach1 >= 0 && attach2 < num_joints && attach2 >= 0 && attach1 != attach2) {
1692 muscles[num_muscles].parent1 = &joints[attach1];
1693 muscles[num_muscles].parent2 = &joints[attach2];
1694 muscles[num_muscles].length = findDistance(&muscles[num_muscles].parent1->position, &muscles[num_muscles].parent2->position);
1695 muscles[num_muscles].targetlength = findDistance(&muscles[num_muscles].parent1->position, &muscles[num_muscles].parent2->position);
1696 muscles[num_muscles].strength = .7;
1697 muscles[num_muscles].type = type;
1698 muscles[num_muscles].minlength = minlength;
1699 muscles[num_muscles].maxlength = maxlength;
1710 void Skeleton::MusclesSet()
1712 for (int i = 0; i < num_muscles; i++) {
1713 muscles[i].length = findDistance(&muscles[i].parent1->position, &muscles[i].parent2->position);
1722 void Skeleton::DoBalance()
1725 newpoint=joints[0].position;
1726 newpoint.x=(joints[2].position.x+joints[4].position.x)/2;
1727 newpoint.z=(joints[2].position.z+joints[4].position.z)/2;
1728 joints[0].velocity=joints[0].velocity+(newpoint-joints[0].position);
1729 //Move child point to within certain distance of parent point
1730 joints[0].position=newpoint;