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"
26 #include "Utils/Folders.h"
28 extern float multiplier;
30 extern Terrain terrain;
31 extern Objects objects;
32 extern int environment;
33 extern float camerashake;
36 extern int tutoriallevel;
38 extern int whichjointstartarray[26];
39 extern int whichjointendarray[26];
41 extern bool visibleloading;
45 void dealloc2(void* param)
50 enum {boneconnect, constraint, muscle};
54 * sets strength, length,
55 * parent1->position, parent2->position,
56 * parent1->velocity, parent2->velocity
60 * Skeleton::DoConstraints
62 void Muscle::DoConstraint(bool spinny)
64 // FIXME: relaxlength shouldn't be static, but may not always be set
65 // so I don't want to change the existing behavior even though it's probably a bug
66 static float relaxlength;
68 float oldlength = length;
70 if (type != boneconnect)
71 relaxlength = findDistance(&parent1->position, &parent2->position);
73 if (type == boneconnect)
75 if (type == constraint)
84 length -= (length - relaxlength) * (1 - strength) * multiplier * 10000;
85 length -= (length - targetlength) * (strength) * multiplier * 10000;
89 if ((relaxlength - length > 0 && relaxlength - oldlength < 0) || (relaxlength - length < 0 && relaxlength - oldlength > 0))
93 if (length < minlength)
95 if (length > maxlength)
98 if (length == relaxlength)
104 XYZ midp = (parent1->position * parent1->mass + parent2->position * parent2->mass) / (parent1->mass + parent2->mass);
106 //Find vector from midpoint to second vector
107 XYZ vel = parent2->position - midp;
109 //Change to unit vector
112 //Apply velocity change
113 XYZ newpoint1 = midp - vel * length * (parent2->mass / (parent1->mass + parent2->mass));
114 XYZ newpoint2 = midp + vel * length * (parent1->mass / (parent1->mass + parent2->mass));
115 if (!freeze && spinny) {
116 parent1->velocity = parent1->velocity + (newpoint1 - parent1->position) / multiplier / 4;
117 parent2->velocity = parent2->velocity + (newpoint2 - parent2->position) / multiplier / 4;
119 parent1->velocity = parent1->velocity + (newpoint1 - parent1->position);
120 parent2->velocity = parent2->velocity + (newpoint2 - parent2->position);
123 //Move child point to within certain distance of parent point
124 parent1->position = newpoint1;
125 parent2->position = newpoint2;
129 * sets forward, lowforward, specialforward[]
133 * Person/Person::DoAnimations
134 * Person/Person::DrawSkeleton
136 void Skeleton::FindForwards()
138 //Find forward vectors
139 CrossProduct(joints[forwardjoints[1]].position - joints[forwardjoints[0]].position, joints[forwardjoints[2]].position - joints[forwardjoints[0]].position, &forward);
142 CrossProduct(joints[lowforwardjoints[1]].position - joints[lowforwardjoints[0]].position, joints[lowforwardjoints[2]].position - joints[lowforwardjoints[0]].position, &lowforward);
143 Normalise(&lowforward);
146 specialforward[0] = forward;
148 specialforward[1] = jointPos(rightshoulder) + jointPos(rightwrist);
149 specialforward[1] = jointPos(rightelbow) - specialforward[1] / 2;
150 specialforward[1] += forward * .4;
151 Normalise(&specialforward[1]);
152 specialforward[2] = jointPos(leftshoulder) + jointPos(leftwrist);
153 specialforward[2] = jointPos(leftelbow) - specialforward[2] / 2;
154 specialforward[2] += forward * .4;
155 Normalise(&specialforward[2]);
157 specialforward[3] = jointPos(righthip) + jointPos(rightankle);
158 specialforward[3] = specialforward[3] / 2 - jointPos(rightknee);
159 specialforward[3] += lowforward * .4;
160 Normalise(&specialforward[3]);
161 specialforward[4] = jointPos(lefthip) + jointPos(leftankle);
162 specialforward[4] = specialforward[4] / 2 - jointPos(leftknee);
163 specialforward[4] += lowforward * .4;
164 Normalise(&specialforward[4]);
171 * Person/Person::RagDoll
172 * Person/Person::DoStuff
175 float Skeleton::DoConstraints(XYZ *coords, float *scale)
177 float friction = 1.5;
178 const float elasticity = .3;
180 const int numrepeats = 3;
181 float groundlevel = .15;
190 float damage = 0; // eventually returned from function
191 bool breaking = false;
194 freetime += multiplier;
196 whichpatchx = coords->x / (terrain.size / subdivision * terrain.scale);
197 whichpatchz = coords->z / (terrain.size / subdivision * terrain.scale);
199 terrainlight = *coords;
200 objects.SphereCheckPossible(&terrainlight, 1);
203 for (i = 0; i < num_joints; i++) {
204 joints[i].position = joints[i].position + joints[i].velocity * multiplier;
206 switch (joints[i].label) {
223 joints[i].position.y -= groundlevel;
224 joints[i].oldvelocity = joints[i].velocity;
227 float tempmult = multiplier;
228 //multiplier/=numrepeats;
230 for (j = 0; j < numrepeats; j++) {
232 // right leg constraints?
233 if (!joint(rightknee).locked && !joint(righthip).locked) {
234 temp = jointPos(rightknee) - (jointPos(righthip) + jointPos(rightankle)) / 2;
235 while (normaldotproduct(temp, lowforward) > -.1 && !sphere_line_intersection(&jointPos(righthip), &jointPos(rightankle), &jointPos(rightknee), &r)) {
236 jointPos(rightknee) -= lowforward * .05;
238 jointVel(rightknee) -= lowforward * .05 / multiplier / 4;
240 jointVel(rightknee) -= lowforward * .05;
241 jointPos(rightankle) += lowforward * .025;
243 jointVel(rightankle) += lowforward * .025 / multiplier / 4;
245 jointVel(rightankle) += lowforward * .25;
246 jointPos(righthip) += lowforward * .025;
248 jointVel(righthip) += lowforward * .025 / multiplier / 4;
250 jointVel(righthip) += lowforward * .025;
251 temp = jointPos(rightknee) - (jointPos(righthip) + jointPos(rightankle)) / 2;
255 // left leg constraints?
256 if (!joint(leftknee).locked && !joint(lefthip).locked) {
257 temp = jointPos(leftknee) - (jointPos(lefthip) + jointPos(leftankle)) / 2;
258 while (normaldotproduct(temp, lowforward) > -.1 && !sphere_line_intersection(&jointPos(lefthip), &jointPos(leftankle), &jointPos(leftknee), &r)) {
259 jointPos(leftknee) -= lowforward * .05;
261 jointVel(leftknee) -= lowforward * .05 / multiplier / 4;
263 jointVel(leftknee) -= lowforward * .05;
264 jointPos(leftankle) += lowforward * .025;
266 jointVel(leftankle) += lowforward * .025 / multiplier / 4;
268 jointVel(leftankle) += lowforward * .25;
269 jointPos(lefthip) += lowforward * .025;
271 jointVel(lefthip) += lowforward * .025 / multiplier / 4;
273 jointVel(lefthip) += lowforward * .025;
274 temp = jointPos(leftknee) - (jointPos(lefthip) + jointPos(leftankle)) / 2;
278 for (i = 0; i < num_joints; i++) {
279 if (joints[i].locked && !spinny && findLengthfast(&joints[i].velocity) > 320)
280 joints[i].locked = 0;
281 if (spinny && findLengthfast(&joints[i].velocity) > 600)
282 joints[i].locked = 0;
283 if (joints[i].delay > 0) {
285 for (j = 0; j < num_joints; j++) {
286 if (joints[j].locked)
290 joints[i].delay -= multiplier * 3;
295 for (i = 0; i < num_muscles; i++) {
297 muscles[i].DoConstraint(spinny);
300 for (i = 0; i < num_joints; i++) {
304 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) {
307 if (joints[i].label == groin && !joints[i].locked && joints[i].delay <= 0) {
308 joints[i].locked = 1;
310 if (tutoriallevel != 1 || id == 0) {
311 emit_sound_at(landsound1, joints[i].position * (*scale) + *coords, 128.);
316 if (joints[i].label == head && !joints[i].locked && joints[i].delay <= 0) {
317 joints[i].locked = 1;
319 if (tutoriallevel != 1 || id == 0) {
320 emit_sound_at(landsound2, joints[i].position * (*scale) + *coords, 128.);
324 terrainnormal = terrain.getNormal(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z);
325 ReflectVector(&joints[i].velocity, &terrainnormal);
326 bounceness = terrainnormal * findLength(&joints[i].velocity) * (abs(normaldotproduct(joints[i].velocity, terrainnormal)));
327 if (!joints[i].locked)
328 damage += findLengthfast(&bounceness) / 4000;
329 if (findLengthfast(&joints[i].velocity) < findLengthfast(&bounceness))
331 frictionness = abs(normaldotproduct(joints[i].velocity, terrainnormal));
332 joints[i].velocity -= bounceness;
333 if (1 - friction * frictionness > 0)
334 joints[i].velocity *= 1 - friction * frictionness;
336 joints[i].velocity = 0;
338 if (tutoriallevel != 1 || id == 0)
339 if (findLengthfast(&bounceness) > 8000 && breaking) {
340 // FIXME: this crashes because k is not initialized!
341 // to reproduce, type 'wolfie' in console and play a while
342 // I'll just comment it out for now
343 //objects.model[k].MakeDecal(breakdecal, DoRotation(temp - objects.position[k], 0, -objects.yaw[k], 0), .4, .5, Random() % 360);
344 Sprite::MakeSprite(cloudsprite, joints[i].position * (*scale) + *coords, joints[i].velocity * .06, 1, 1, 1, 4, .2);
348 emit_sound_at(breaksound2, joints[i].position * (*scale) + *coords);
350 addEnvSound(*coords, 64);
353 if (findLengthfast(&bounceness) > 2500) {
354 Normalise(&bounceness);
355 bounceness = bounceness * 50;
358 joints[i].velocity += bounceness * elasticity;
360 if (findLengthfast(&joints[i].velocity) > findLengthfast(&joints[i].oldvelocity)) {
362 joints[i].velocity = joints[i].oldvelocity;
366 if (joints[i].locked == 0)
367 if (findLengthfast(&joints[i].velocity) < 1)
368 joints[i].locked = 1;
370 if (environment == snowyenvironment && findLengthfast(&bounceness) > 500 && terrain.getOpacity(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z) < .2) {
371 terrainlight = terrain.getLighting(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z);
372 Sprite::MakeSprite(cloudsprite, joints[i].position * (*scale) + *coords, joints[i].velocity * .06, terrainlight.x, terrainlight.y, terrainlight.z, .5, .7);
374 terrain.MakeDecal(bodyprintdecal, joints[i].position * (*scale) + *coords, .4, .4, 0);
375 } else if (environment == desertenvironment && findLengthfast(&bounceness) > 500 && terrain.getOpacity(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z) < .2) {
376 terrainlight = terrain.getLighting(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z);
377 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);
380 else if (environment == grassyenvironment && findLengthfast(&bounceness) > 500 && terrain.getOpacity(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z) < .2) {
381 terrainlight = terrain.getLighting(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z);
382 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);
383 } else if (findLengthfast(&bounceness) > 500)
384 Sprite::MakeSprite(cloudsprite, joints[i].position * (*scale) + *coords, joints[i].velocity * .06, terrainlight.x, terrainlight.y, terrainlight.z, .5, .2);
387 joints[i].position.y = (terrain.getHeight(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z) + groundlevel - coords->y) / (*scale);
391 if (terrain.patchobjectnum[whichpatchx][whichpatchz])
392 for (m = 0; m < terrain.patchobjectnum[whichpatchx][whichpatchz]; m++) {
393 k = terrain.patchobjects[whichpatchx][whichpatchz][m];
394 if (k < objects.numobjects && k >= 0)
395 if (objects.possible[k]) {
396 friction = objects.friction[k];
397 XYZ start = joints[i].realoldposition;
398 XYZ end = joints[i].position * (*scale) + *coords;
399 whichhit = objects.model[k].LineCheckPossible(&start, &end, &temp, &objects.position[k], &objects.yaw[k]);
400 if (whichhit != -1) {
401 if (joints[i].label == groin && !joints[i].locked && joints[i].delay <= 0) {
402 joints[i].locked = 1;
404 if (tutoriallevel != 1 || id == 0) {
405 emit_sound_at(landsound1, joints[i].position * (*scale) + *coords, 128.);
410 if (joints[i].label == head && !joints[i].locked && joints[i].delay <= 0) {
411 joints[i].locked = 1;
413 if (tutoriallevel != 1 || id == 0) {
414 emit_sound_at(landsound2, joints[i].position * (*scale) + *coords, 128.);
418 terrainnormal = DoRotation(objects.model[k].facenormals[whichhit], 0, objects.yaw[k], 0) * -1;
419 if (terrainnormal.y > .8)
421 bounceness = terrainnormal * findLength(&joints[i].velocity) * (abs(normaldotproduct(joints[i].velocity, terrainnormal)));
422 if (findLengthfast(&joints[i].velocity) > findLengthfast(&joints[i].oldvelocity)) {
424 joints[i].velocity = joints[i].oldvelocity;
426 if (tutoriallevel != 1 || id == 0)
427 if (findLengthfast(&bounceness) > 4000 && breaking) {
428 objects.model[k].MakeDecal(breakdecal, DoRotation(temp - objects.position[k], 0, -objects.yaw[k], 0), .4, .5, Random() % 360);
429 Sprite::MakeSprite(cloudsprite, joints[i].position * (*scale) + *coords, joints[i].velocity * .06, 1, 1, 1, 4, .2);
433 emit_sound_at(breaksound2, joints[i].position * (*scale) + *coords);
435 addEnvSound(*coords, 64);
437 if (objects.type[k] == treetrunktype) {
438 objects.rotx[k] += joints[i].velocity.x * multiplier * .4;
439 objects.roty[k] += joints[i].velocity.z * multiplier * .4;
440 objects.rotx[k + 1] += joints[i].velocity.x * multiplier * .4;
441 objects.roty[k + 1] += joints[i].velocity.z * multiplier * .4;
443 if (!joints[i].locked)
444 damage += findLengthfast(&bounceness) / 2500;
445 ReflectVector(&joints[i].velocity, &terrainnormal);
446 frictionness = abs(normaldotproduct(joints[i].velocity, terrainnormal));
447 joints[i].velocity -= bounceness;
448 if (1 - friction * frictionness > 0)
449 joints[i].velocity *= 1 - friction * frictionness;
451 joints[i].velocity = 0;
452 if (findLengthfast(&bounceness) > 2500) {
453 Normalise(&bounceness);
454 bounceness = bounceness * 50;
456 joints[i].velocity += bounceness * elasticity;
459 if (!joints[i].locked)
460 if (findLengthfast(&joints[i].velocity) < 1) {
461 joints[i].locked = 1;
463 if (findLengthfast(&bounceness) > 500)
464 Sprite::MakeSprite(cloudsprite, joints[i].position * (*scale) + *coords, joints[i].velocity * .06, 1, 1, 1, .5, .2);
465 joints[i].position = (temp - *coords) / (*scale) + terrainnormal * .005;
471 joints[i].realoldposition = joints[i].position * (*scale) + *coords;
474 multiplier = tempmult;
477 if (terrain.patchobjectnum[whichpatchx][whichpatchz])
478 for (m = 0; m < terrain.patchobjectnum[whichpatchx][whichpatchz]; m++) {
479 k = terrain.patchobjects[whichpatchx][whichpatchz][m];
480 if (objects.possible[k]) {
481 for (i = 0; i < 26; i++) {
482 //Make this less stupid
483 XYZ start = joints[jointlabels[whichjointstartarray[i]]].position * (*scale) + *coords;
484 XYZ end = joints[jointlabels[whichjointendarray[i]]].position * (*scale) + *coords;
485 whichhit = objects.model[k].LineCheckSlidePossible(&start, &end, &temp, &objects.position[k], &objects.yaw[k]);
486 if (whichhit != -1) {
487 joints[jointlabels[whichjointendarray[i]]].position = (end - *coords) / (*scale);
488 for (j = 0; j < num_muscles; j++) {
489 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]))
490 muscles[j].DoConstraint(spinny);
497 for (i = 0; i < num_joints; i++) {
498 switch (joints[i].label) {
514 joints[i].position.y += groundlevel;
516 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)
518 if (joints[i].locked) {
527 for (i = 0; i < num_muscles; i++) {
528 if (muscles[i].type == boneconnect)
529 muscles[i].DoConstraint(0);
537 * applies gravity to the skeleton
540 * Person/Person::DoStuff
542 void Skeleton::DoGravity(float *scale)
545 for (i = 0; i < num_joints; i++) {
548 ((joints[i].label != leftknee) && (joints[i].label != rightknee)) ||
549 (lowforward.y > -.1) ||
552 ((joints[i].label != leftelbow) && (joints[i].label != rightelbow)) ||
556 joints[i].velocity.y += gravity * multiplier / (*scale);
561 * set muscles[which].rotate1
565 * special case if animation == hanganim
567 void Skeleton::FindRotationMuscle(int which, int animation)
572 p1 = muscles[which].parent1->position;
573 p2 = muscles[which].parent2->position;
574 dist = findDistance(&p1, &p2);
575 if (p1.y - p2.y <= dist)
576 muscles[which].rotate2 = asin((p1.y - p2.y) / dist);
577 if (p1.y - p2.y > dist)
578 muscles[which].rotate2 = asin(1.f);
579 muscles[which].rotate2 *= 360.0 / 6.2831853;
583 dist = findDistance(&p1, &p2);
584 if (p1.z - p2.z <= dist)
585 muscles[which].rotate1 = acos((p1.z - p2.z) / dist);
586 if (p1.z - p2.z > dist)
587 muscles[which].rotate1 = acos(1.f);
588 muscles[which].rotate1 *= 360.0 / 6.2831853;
590 muscles[which].rotate1 = 360 - muscles[which].rotate1;
591 if (!isnormal(muscles[which].rotate1))
592 muscles[which].rotate1 = 0;
593 if (!isnormal(muscles[which].rotate2))
594 muscles[which].rotate2 = 0;
596 const int label1 = muscles[which].parent1->label;
597 const int label2 = muscles[which].parent2->label;
600 fwd = specialforward[0];
606 fwd = specialforward[1];
612 fwd = specialforward[2];
618 fwd = specialforward[3];
624 fwd = specialforward[4];
627 if (muscles[which].parent1->lower)
634 if (animation == hanganim) {
635 if (label1 == righthand || label2 == righthand) {
639 if (label1 == lefthand || label2 == lefthand) {
646 if (label1 == rightfoot || label2 == rightfoot) {
649 if (label1 == leftfoot || label2 == leftfoot) {
654 fwd = DoRotation(fwd, 0, muscles[which].rotate1 - 90, 0);
655 fwd = DoRotation(fwd, 0, 0, muscles[which].rotate2 - 90);
657 fwd /= findLength(&fwd);
658 if (fwd.z <= 1 && fwd.z >= -1)
659 muscles[which].rotate3 = acos(0 - fwd.z);
661 muscles[which].rotate3 = acos(-1.f);
662 muscles[which].rotate3 *= 360.0 / 6.2831853;
664 muscles[which].rotate3 = 360 - muscles[which].rotate3;
665 if (!isnormal(muscles[which].rotate3))
666 muscles[which].rotate3 = 0;
670 * load an animation from file
672 void Animation::Load(const std::string& filename, int aheight, int aattack)
680 // Changing the filename into something the OS can understand
681 std::string filepath = Folders::getResourcePath("Animations/"+filename);
683 LOG(std::string("Loading animation...") + filepath);
685 // clear existing data
692 Game::LoadingScreen();
694 // read file in binary mode
695 tfile = Folders::openMandatoryFile( filepath, "rb" );
697 // read numframes, joints to know how much memory to allocate
698 funpackf(tfile, "Bi Bi", &numframes, &joints);
700 // allocate memory for everything
702 position = (XYZ**)malloc(sizeof(XYZ*) * joints);
703 for (i = 0; i < joints; i++)
704 position[i] = (XYZ*)malloc(sizeof(XYZ) * numframes);
706 twist = (float**)malloc(sizeof(float*) * joints);
707 for (i = 0; i < joints; i++)
708 twist[i] = (float*)malloc(sizeof(float) * numframes);
710 twist2 = (float**)malloc(sizeof(float*) * joints);
711 for (i = 0; i < joints; i++)
712 twist2[i] = (float*)malloc(sizeof(float) * numframes);
714 speed = (float*)malloc(sizeof(float) * numframes);
716 onground = (bool**)malloc(sizeof(bool*) * joints);
717 for (i = 0; i < joints; i++)
718 onground[i] = (bool*)malloc(sizeof(bool) * numframes);
720 forward = (XYZ*)malloc(sizeof(XYZ) * numframes);
721 weapontarget = (XYZ*)malloc(sizeof(XYZ) * numframes);
722 label = (int*)malloc(sizeof(int) * numframes);
724 // read binary data as animation
727 for (i = 0; i < numframes; i++) {
728 // for each joint in the skeleton...
729 for (j = 0; j < joints; j++) {
730 // read joint position
731 funpackf(tfile, "Bf Bf Bf", &position[j][i].x, &position[j][i].y, &position[j][i].z);
733 for (j = 0; j < joints; j++) {
735 funpackf(tfile, "Bf", &twist[j][i]);
737 for (j = 0; j < joints; j++) {
738 // read onground (boolean)
740 funpackf(tfile, "Bb", &uch);
741 onground[j][i] = (uch != 0);
743 // read frame speed (?)
744 funpackf(tfile, "Bf", &speed[i]);
746 // read twist2 for whole animation
747 for (i = 0; i < numframes; i++) {
748 for (j = 0; j < joints; j++) {
749 funpackf(tfile, "Bf", &twist2[j][i]);
752 // read label for each frame
753 for (i = 0; i < numframes; i++) {
754 funpackf(tfile, "Bf", &label[i]);
756 // read weapontargetnum
757 funpackf(tfile, "Bi", &weapontargetnum);
758 // read weapontarget positions for each frame
759 for (i = 0; i < numframes; i++) {
760 funpackf(tfile, "Bf Bf Bf", &weapontarget[i].x, &weapontarget[i].y, &weapontarget[i].z);
766 // find average position of certain joints on last frames
767 // and save in endoffset
768 // (not sure what exactly this accomplishes. the y < 1 test confuses me.)
769 for (j = 0; j < joints; j++) {
770 if (position[j][numframes - 1].y < 1)
771 endoffset += position[j][numframes - 1];
781 * takes filenames for three skeleton files and various models
783 void Skeleton::Load(const std::string& filename, const std::string& lowfilename, const std::string& clothesfilename,
784 const std::string& modelfilename, const std::string& model2filename,
785 const std::string& model3filename, const std::string& model4filename,
786 const std::string& model5filename, const std::string& model6filename,
787 const std::string& model7filename, const std::string& modellowfilename,
788 const std::string& modelclothesfilename, bool clothes)
801 // load various models
802 // rotate, scale, do normals, do texcoords for each as needed
804 model[0].loadnotex(modelfilename);
805 model[1].loadnotex(model2filename);
806 model[2].loadnotex(model3filename);
807 model[3].loadnotex(model4filename);
808 model[4].loadnotex(model5filename);
809 model[5].loadnotex(model6filename);
810 model[6].loadnotex(model7filename);
812 for (i = 0; i < num_models; i++) {
813 model[i].Rotate(180, 0, 0);
814 model[i].Scale(.04, .04, .04);
815 model[i].CalculateNormals(0);
818 drawmodel.load(modelfilename, 0);
819 drawmodel.Rotate(180, 0, 0);
820 drawmodel.Scale(.04, .04, .04);
821 drawmodel.FlipTexCoords();
822 if (tutoriallevel == 1 && id != 0)
823 drawmodel.UniformTexCoords();
824 if (tutoriallevel == 1 && id != 0)
825 drawmodel.ScaleTexCoords(0.1);
826 drawmodel.CalculateNormals(0);
828 modellow.loadnotex(modellowfilename);
829 modellow.Rotate(180, 0, 0);
830 modellow.Scale(.04, .04, .04);
831 modellow.CalculateNormals(0);
833 drawmodellow.load(modellowfilename, 0);
834 drawmodellow.Rotate(180, 0, 0);
835 drawmodellow.Scale(.04, .04, .04);
836 drawmodellow.FlipTexCoords();
837 if (tutoriallevel == 1 && id != 0)
838 drawmodellow.UniformTexCoords();
839 if (tutoriallevel == 1 && id != 0)
840 drawmodellow.ScaleTexCoords(0.1);
841 drawmodellow.CalculateNormals(0);
844 modelclothes.loadnotex(modelclothesfilename);
845 modelclothes.Rotate(180, 0, 0);
846 modelclothes.Scale(.041, .04, .041);
847 modelclothes.CalculateNormals(0);
849 drawmodelclothes.load(modelclothesfilename, 0);
850 drawmodelclothes.Rotate(180, 0, 0);
851 drawmodelclothes.Scale(.04, .04, .04);
852 drawmodelclothes.FlipTexCoords();
853 drawmodelclothes.CalculateNormals(0);
856 // FIXME: three similar blocks follow, one for each of:
857 // filename, lowfilename, clothesfilename
861 tfile = Folders::openMandatoryFile( Folders::getResourcePath(filename), "rb" );
864 funpackf(tfile, "Bi", &num_joints);
868 delete [] joints; //dealloc2(joints);
869 joints = (Joint*)new Joint[num_joints];
871 // read info for each joint
872 for (i = 0; i < num_joints; i++) {
873 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);
874 funpackf(tfile, "Bb Bb", &joints[i].hasparent, &joints[i].locked);
875 funpackf(tfile, "Bi", &joints[i].modelnum);
876 funpackf(tfile, "Bb Bb", &joints[i].visible, &joints[i].sametwist);
877 funpackf(tfile, "Bi Bi", &joints[i].label, &joints[i].hasgun);
878 funpackf(tfile, "Bb", &joints[i].lower);
879 funpackf(tfile, "Bi", &parentID);
880 if (joints[i].hasparent)
881 joints[i].parent = &joints[parentID];
882 joints[i].velocity = 0;
883 joints[i].oldposition = joints[i].position;
887 funpackf(tfile, "Bi", &num_muscles);
891 delete [] muscles; //dealloc2(muscles);
892 muscles = (Muscle*)new Muscle[num_muscles]; //malloc(sizeof(Muscle)*num_muscles);
894 // for each muscle...
895 for (i = 0; i < num_muscles; i++) {
897 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);
899 // allocate memory for vertices
900 muscles[i].vertices = (int*)malloc(sizeof(int) * muscles[i].numvertices);
904 for (j = 0; j < muscles[i].numvertices - edit; j++) {
905 funpackf(tfile, "Bi", &muscles[i].vertices[j + edit]);
906 if (muscles[i].vertices[j + edit] >= model[0].vertexNum) {
907 muscles[i].numvertices--;
913 funpackf(tfile, "Bb Bi", &muscles[i].visible, &parentID);
914 muscles[i].parent1 = &joints[parentID];
915 funpackf(tfile, "Bi", &parentID);
916 muscles[i].parent2 = &joints[parentID];
919 // read forwardjoints (?)
920 for (j = 0; j < 3; j++) {
921 funpackf(tfile, "Bi", &forwardjoints[j]);
923 // read lowforwardjoints (?)
924 for (j = 0; j < 3; j++) {
925 funpackf(tfile, "Bi", &lowforwardjoints[j]);
929 for (j = 0; j < num_muscles; j++) {
930 for (i = 0; i < muscles[j].numvertices; i++) {
931 for (int k = 0; k < num_models; k++) {
932 if (muscles[j].numvertices && muscles[j].vertices[i] < model[k].vertexNum)
933 model[k].owner[muscles[j].vertices[i]] = j;
938 // calculate some stuff
940 for (i = 0; i < num_joints; i++) {
941 joints[i].startpos = joints[i].position;
943 for (i = 0; i < num_muscles; i++) {
944 FindRotationMuscle(i, -1);
946 // this seems to use opengl purely for matrix calculations
947 for (int k = 0; k < num_models; k++) {
948 for (i = 0; i < model[k].vertexNum; i++) {
949 model[k].vertex[i] = model[k].vertex[i] - (muscles[model[k].owner[i]].parent1->position + muscles[model[k].owner[i]].parent2->position) / 2;
950 glMatrixMode(GL_MODELVIEW);
953 glRotatef(muscles[model[k].owner[i]].rotate3, 0, 1, 0);
954 glRotatef(muscles[model[k].owner[i]].rotate2 - 90, 0, 0, 1);
955 glRotatef(muscles[model[k].owner[i]].rotate1 - 90, 0, 1, 0);
956 glTranslatef(model[k].vertex[i].x, model[k].vertex[i].y, model[k].vertex[i].z);
957 glGetFloatv(GL_MODELVIEW_MATRIX, M);
958 model[k].vertex[i].x = M[12] * 1;
959 model[k].vertex[i].y = M[13] * 1;
960 model[k].vertex[i].z = M[14] * 1;
963 model[k].CalculateNormals(0);
969 tfile = Folders::openMandatoryFile( Folders::getResourcePath(lowfilename), "rb" );
971 // skip joints section
973 lSize = sizeof(num_joints);
974 fseek(tfile, lSize, SEEK_CUR);
975 for (i = 0; i < num_joints; i++) {
989 fseek(tfile, lSize, SEEK_CUR);
991 if (joints[i].hasparent)
992 joints[i].parent = &joints[parentID];
993 joints[i].velocity = 0;
994 joints[i].oldposition = joints[i].position;
998 funpackf(tfile, "Bi", &num_muscles);
1000 for (i = 0; i < num_muscles; i++) {
1002 lSize = sizeof(float)
1008 fseek(tfile, lSize, SEEK_CUR);
1010 // read numverticeslow
1011 funpackf(tfile, "Bi", &muscles[i].numverticeslow);
1013 if (muscles[i].numverticeslow) {
1015 muscles[i].verticeslow = (int*)malloc(sizeof(int) * muscles[i].numverticeslow);
1019 for (j = 0; j < muscles[i].numverticeslow - edit; j++) {
1020 funpackf(tfile, "Bi", &muscles[i].verticeslow[j + edit]);
1021 if (muscles[i].verticeslow[j + edit] >= modellow.vertexNum) {
1022 muscles[i].numverticeslow--;
1029 lSize = 1; //sizeof(bool);
1030 fseek ( tfile, lSize, SEEK_CUR);
1031 lSize = sizeof(int);
1032 fseek ( tfile, lSize, SEEK_CUR);
1033 fseek ( tfile, lSize, SEEK_CUR);
1036 for (j = 0; j < num_muscles; j++) {
1037 for (i = 0; i < muscles[j].numverticeslow; i++) {
1038 if (muscles[j].verticeslow[i] < modellow.vertexNum)
1039 modellow.owner[muscles[j].verticeslow[i]] = j;
1043 // use opengl for its matrix math
1044 for (i = 0; i < modellow.vertexNum; i++) {
1045 modellow.vertex[i] = modellow.vertex[i] - (muscles[modellow.owner[i]].parent1->position + muscles[modellow.owner[i]].parent2->position) / 2;
1046 glMatrixMode(GL_MODELVIEW);
1049 glRotatef(muscles[modellow.owner[i]].rotate3, 0, 1, 0);
1050 glRotatef(muscles[modellow.owner[i]].rotate2 - 90, 0, 0, 1);
1051 glRotatef(muscles[modellow.owner[i]].rotate1 - 90, 0, 1, 0);
1052 glTranslatef(modellow.vertex[i].x, modellow.vertex[i].y, modellow.vertex[i].z);
1053 glGetFloatv(GL_MODELVIEW_MATRIX, M);
1054 modellow.vertex[i].x = M[12];
1055 modellow.vertex[i].y = M[13];
1056 modellow.vertex[i].z = M[14];
1060 modellow.CalculateNormals(0);
1065 tfile = Folders::openMandatoryFile( Folders::getResourcePath(clothesfilename), "rb" );
1068 lSize = sizeof(num_joints);
1069 fseek ( tfile, lSize, SEEK_CUR);
1071 for (i = 0; i < num_joints; i++) {
1085 fseek(tfile, lSize, SEEK_CUR);
1087 if (joints[i].hasparent)
1088 joints[i].parent = &joints[parentID];
1089 joints[i].velocity = 0;
1090 joints[i].oldposition = joints[i].position;
1094 funpackf(tfile, "Bi", &num_muscles);
1096 for (i = 0; i < num_muscles; i++) {
1098 lSize = sizeof(float)
1104 fseek(tfile, lSize, SEEK_CUR);
1106 // read numverticesclothes
1107 funpackf(tfile, "Bi", &muscles[i].numverticesclothes);
1109 // read verticesclothes
1110 if (muscles[i].numverticesclothes) {
1111 muscles[i].verticesclothes = (int*)malloc(sizeof(int) * muscles[i].numverticesclothes);
1113 for (j = 0; j < muscles[i].numverticesclothes - edit; j++) {
1114 funpackf(tfile, "Bi", &muscles[i].verticesclothes[j + edit]);
1115 if (muscles[i].verticesclothes[j + edit] >= modelclothes.vertexNum) {
1116 muscles[i].numverticesclothes--;
1123 lSize = 1; //sizeof(bool);
1124 fseek ( tfile, lSize, SEEK_CUR);
1125 lSize = sizeof(int);
1126 fseek ( tfile, lSize, SEEK_CUR);
1127 fseek ( tfile, lSize, SEEK_CUR);
1131 lSize = sizeof(int);
1132 for (j = 0; j < num_muscles; j++) {
1133 for (i = 0; i < muscles[j].numverticesclothes; i++) {
1134 if (muscles[j].numverticesclothes && muscles[j].verticesclothes[i] < modelclothes.vertexNum)
1135 modelclothes.owner[muscles[j].verticesclothes[i]] = j;
1139 // use opengl for its matrix math
1140 for (i = 0; i < modelclothes.vertexNum; i++) {
1141 modelclothes.vertex[i] = modelclothes.vertex[i] - (muscles[modelclothes.owner[i]].parent1->position + muscles[modelclothes.owner[i]].parent2->position) / 2;
1142 glMatrixMode(GL_MODELVIEW);
1145 glRotatef(muscles[modelclothes.owner[i]].rotate3, 0, 1, 0);
1146 glRotatef(muscles[modelclothes.owner[i]].rotate2 - 90, 0, 0, 1);
1147 glRotatef(muscles[modelclothes.owner[i]].rotate1 - 90, 0, 1, 0);
1148 glTranslatef(modelclothes.vertex[i].x, modelclothes.vertex[i].y, modelclothes.vertex[i].z);
1149 glGetFloatv(GL_MODELVIEW_MATRIX, M);
1150 modelclothes.vertex[i].x = M[12];
1151 modelclothes.vertex[i].y = M[13];
1152 modelclothes.vertex[i].z = M[14];
1156 modelclothes.CalculateNormals(0);
1160 for (i = 0; i < num_joints; i++) {
1161 for (j = 0; j < num_joints; j++) {
1162 if (joints[i].label == j)
1170 Animation::Animation()
1176 weapontargetnum = 0;
1188 Animation::~Animation()
1193 void Animation::deallocate()
1198 for (i = 0; i < joints; i++)
1199 dealloc2(position[i]);
1206 for (i = 0; i < joints; i++)
1214 for (i = 0; i < joints; i++)
1215 dealloc2(twist2[i]);
1222 for (i = 0; i < joints; i++)
1223 dealloc2(onground[i]);
1238 dealloc2(weapontarget);
1248 Skeleton::Skeleton()
1256 memset(forwardjoints, 0, sizeof(forwardjoints));
1261 memset(lowforwardjoints, 0, sizeof(lowforwardjoints));
1264 // XYZ specialforward[5];
1265 memset(jointlabels, 0, sizeof(jointlabels));
1269 // Model modelclothes;
1273 // Model drawmodellow;
1274 // Model drawmodelclothes;
1279 memset(skinText, 0, sizeof(skinText));
1296 Skeleton::~Skeleton()
1313 verticesclothes = 0;
1317 numverticesclothes = 0;
1326 rotate1 = 0, rotate2 = 0, rotate3 = 0;
1327 lastrotate1 = 0, lastrotate2 = 0, lastrotate3 = 0;
1328 oldrotate1 = 0, oldrotate2 = 0, oldrotate3 = 0;
1329 newrotate1 = 0, newrotate2 = 0, newrotate3 = 0;
1337 dealloc2(verticeslow);
1338 dealloc2(verticesclothes);
1341 Animation & Animation::operator = (const Animation & ani)
1345 bool allocate = ((ani.numframes != numframes) || (ani.joints != joints));
1350 numframes = ani.numframes;
1351 height = ani.height;
1352 attack = ani.attack;
1353 joints = ani.joints;
1354 weapontargetnum = ani.weapontargetnum;
1355 offset = ani.offset;
1358 position = (XYZ**)malloc(sizeof(XYZ*)*ani.joints);
1359 for (i = 0; i < ani.joints; i++) {
1361 position[i] = (XYZ*)malloc(sizeof(XYZ) * ani.numframes);
1362 memcpy(position[i], ani.position[i], sizeof(XYZ)*ani.numframes);
1366 twist = (float**)malloc(sizeof(float*)*ani.joints);
1367 for (i = 0; i < ani.joints; i++) {
1369 twist[i] = (float*)malloc(sizeof(float) * ani.numframes);
1370 memcpy(twist[i], ani.twist[i], sizeof(float)*ani.numframes);
1374 twist2 = (float**)malloc(sizeof(float*)*ani.joints);
1375 for (i = 0; i < ani.joints; i++) {
1377 twist2[i] = (float*)malloc(sizeof(float) * ani.numframes);
1378 memcpy(twist2[i], ani.twist2[i], sizeof(float)*ani.numframes);
1382 speed = (float*)malloc(sizeof(float) * ani.numframes);
1383 memcpy(speed, ani.speed, sizeof(float)*ani.numframes);
1386 onground = (bool**)malloc(sizeof(bool*)*ani.joints);
1387 for (i = 0; i < ani.joints; i++) {
1389 onground[i] = (bool*)malloc(sizeof(bool) * ani.numframes);
1390 memcpy(onground[i], ani.onground[i], sizeof(bool)*ani.numframes);
1394 forward = (XYZ*)malloc(sizeof(XYZ) * ani.numframes);
1395 memcpy(forward, ani.forward, sizeof(XYZ)*ani.numframes);
1398 weapontarget = (XYZ*)malloc(sizeof(XYZ) * ani.numframes);
1399 memcpy(weapontarget, ani.weapontarget, sizeof(XYZ)*ani.numframes);
1402 label = (int*)malloc(sizeof(int) * ani.numframes);
1403 memcpy(label, ani.label, sizeof(int)*ani.numframes);
1413 // the following functions are not used anywhere
1416 * sets forward, lowforward, specialforward[]
1421 void Skeleton::FindForwardsfirst()
1423 //Find forward vectors
1424 CrossProduct(joints[forwardjoints[1]].position - joints[forwardjoints[0]].position, joints[forwardjoints[2]].position - joints[forwardjoints[0]].position, &forward);
1425 Normalise(&forward);
1427 CrossProduct(joints[lowforwardjoints[1]].position - joints[lowforwardjoints[0]].position, joints[lowforwardjoints[2]].position - joints[lowforwardjoints[0]].position, &lowforward);
1428 Normalise(&lowforward);
1431 specialforward[0] = forward;
1432 specialforward[1] = forward;
1433 specialforward[2] = forward;
1434 specialforward[3] = forward;
1435 specialforward[4] = forward;
1444 void Skeleton::Draw(int muscleview)
1446 static float jointcolor[4];
1448 if (muscleview != 2) {
1455 if (muscleview == 2) {
1461 //Calc motionblur-ness
1462 for (int i = 0; i < num_joints; i++) {
1463 joints[i].oldposition = joints[i].position;
1464 joints[i].blurred = findDistance(&joints[i].position, &joints[i].oldposition) * 100;
1465 if (joints[i].blurred < 1)
1466 joints[i].blurred = 1;
1472 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1474 for (int i = 0; i < num_joints; i++) {
1475 if (joints[i].hasparent) {
1476 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / joints[i].blurred);
1477 glVertex3f(joints[i].position.x, joints[i].position.y, joints[i].position.z);
1478 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / joints[i].parent->blurred);
1479 glVertex3f(joints[i].parent->position.x, joints[i].parent->position.y, joints[i].parent->position.z);
1480 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / joints[i].parent->blurred);
1481 glVertex3f(joints[i].parent->oldposition.x, joints[i].parent->oldposition.y, joints[i].parent->oldposition.z);
1482 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / joints[i].blurred);
1483 glVertex3f(joints[i].oldposition.x, joints[i].oldposition.y, joints[i].oldposition.z);
1486 for (int i = 0; i < num_muscles; i++) {
1487 if (muscles[i].type == boneconnect) {
1488 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / muscles[i].parent2->blurred);
1489 glVertex3f(muscles[i].parent1->position.x, muscles[i].parent1->position.y, muscles[i].parent1->position.z);
1490 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / muscles[i].parent2->blurred);
1491 glVertex3f(muscles[i].parent2->position.x, muscles[i].parent2->position.y, muscles[i].parent2->position.z);
1492 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / muscles[i].parent2->blurred);
1493 glVertex3f(muscles[i].parent2->oldposition.x, muscles[i].parent2->oldposition.y, muscles[i].parent2->oldposition.z);
1494 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / muscles[i].parent1->blurred);
1495 glVertex3f(muscles[i].parent1->oldposition.x, muscles[i].parent1->oldposition.y, muscles[i].parent1->oldposition.z);
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);
1509 for (int i = 0; i < num_muscles; i++) {
1510 if (muscles[i].type == boneconnect) {
1511 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / muscles[i].parent1->blurred);
1512 glVertex3f(muscles[i].parent1->position.x, muscles[i].parent1->position.y, muscles[i].parent1->position.z);
1513 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / muscles[i].parent2->blurred);
1514 glVertex3f(muscles[i].parent2->position.x, muscles[i].parent2->position.y, muscles[i].parent2->position.z);
1517 glColor3f(.6, .6, 0);
1518 if (muscleview == 1)
1519 for (int i = 0; i < num_muscles; i++) {
1520 if (muscles[i].type != boneconnect) {
1521 glVertex3f(muscles[i].parent1->position.x, muscles[i].parent1->position.y, muscles[i].parent1->position.z);
1522 glVertex3f(muscles[i].parent2->position.x, muscles[i].parent2->position.y, muscles[i].parent2->position.z);
1527 if (muscleview != 2) {
1530 for (int i = 0; i < num_joints; i++) {
1532 glColor4f(0, 0, .5, 1);
1534 glColor4f(1, 1, 0, 1);
1535 if (joints[i].locked && i != selected)
1536 glColor4f(1, 0, 0, 1);
1537 glVertex3f(joints[i].position.x, joints[i].position.y, joints[i].position.z);
1542 //Set old position to current position
1543 if (muscleview == 2)
1544 for (int i = 0; i < num_joints; i++) {
1545 joints[i].oldposition = joints[i].position;
1555 void Skeleton::AddJoint(float x, float y, float z, int which)
1557 if (num_joints < max_joints - 1) {
1558 joints[num_joints].velocity = 0;
1559 joints[num_joints].position.x = x;
1560 joints[num_joints].position.y = y;
1561 joints[num_joints].position.z = z;
1562 joints[num_joints].mass = 1;
1563 joints[num_joints].locked = 0;
1565 joints[num_joints].hasparent = 0;
1567 if (which < num_joints && which >= 0)
1568 AddMuscle(num_joints - 1, which, 0, 10, boneconnect);
1577 void Skeleton::DeleteJoint(int whichjoint)
1579 if (whichjoint < num_joints && whichjoint >= 0) {
1580 joints[whichjoint].velocity = joints[num_joints - 1].velocity;
1581 joints[whichjoint].position = joints[num_joints - 1].position;
1582 joints[whichjoint].oldposition = joints[num_joints - 1].oldposition;
1583 joints[whichjoint].hasparent = joints[num_joints - 1].hasparent;
1584 joints[whichjoint].parent = joints[num_joints - 1].parent;
1585 joints[whichjoint].length = joints[num_joints - 1].length;
1586 joints[whichjoint].locked = joints[num_joints - 1].locked;
1587 joints[whichjoint].modelnum = joints[num_joints - 1].modelnum;
1588 joints[whichjoint].visible = joints[num_joints - 1].visible;
1590 for (int i = 0; i < num_muscles; i++) {
1591 while (muscles[i].parent1 == &joints[whichjoint] && i < num_muscles)DeleteMuscle(i);
1592 while (muscles[i].parent2 == &joints[whichjoint] && i < num_muscles)DeleteMuscle(i);
1594 for (int i = 0; i < num_muscles; i++) {
1595 while (muscles[i].parent1 == &joints[num_joints - 1] && i < num_muscles)muscles[i].parent1 = &joints[whichjoint];
1596 while (muscles[i].parent2 == &joints[num_joints - 1] && i < num_muscles)muscles[i].parent2 = &joints[whichjoint];
1598 for (int i = 0; i < num_joints; i++) {
1599 if (joints[i].parent == &joints[whichjoint])
1600 joints[i].hasparent = 0;
1602 for (int i = 0; i < num_joints; i++) {
1603 if (joints[i].parent == &joints[num_joints - 1])
1604 joints[i].parent = &joints[whichjoint];
1614 * Skeleton::DeleteJoint - UNUSED
1616 void Skeleton::DeleteMuscle(int whichmuscle)
1618 if (whichmuscle < num_muscles) {
1619 muscles[whichmuscle].minlength = muscles[num_muscles - 1].minlength;
1620 muscles[whichmuscle].maxlength = muscles[num_muscles - 1].maxlength;
1621 muscles[whichmuscle].strength = muscles[num_muscles - 1].strength;
1622 muscles[whichmuscle].parent1 = muscles[num_muscles - 1].parent1;
1623 muscles[whichmuscle].parent2 = muscles[num_muscles - 1].parent2;
1624 muscles[whichmuscle].length = muscles[num_muscles - 1].length;
1625 muscles[whichmuscle].visible = muscles[num_muscles - 1].visible;
1626 muscles[whichmuscle].type = muscles[num_muscles - 1].type;
1627 muscles[whichmuscle].targetlength = muscles[num_muscles - 1].targetlength;
1638 void Skeleton::SetJoint(float x, float y, float z, int which, int whichjoint)
1640 if (whichjoint < num_joints) {
1641 joints[whichjoint].velocity = 0;
1642 joints[whichjoint].position.x = x;
1643 joints[whichjoint].position.y = y;
1644 joints[whichjoint].position.z = z;
1646 if (which >= num_joints || which < 0)
1647 joints[whichjoint].hasparent = 0;
1648 if (which < num_joints && which >= 0) {
1649 joints[whichjoint].parent = &joints[which];
1650 joints[whichjoint].hasparent = 1;
1651 joints[whichjoint].length = findDistance(&joints[whichjoint].position, &joints[whichjoint].parent->position);
1659 * Skeleton::AddJoint - UNUSED
1661 void Skeleton::AddMuscle(int attach1, int attach2, float minlength, float maxlength, int type)
1663 const int max_muscles = 100; // FIXME: Probably can be dropped
1664 if (num_muscles < max_muscles - 1 && attach1 < num_joints && attach1 >= 0 && attach2 < num_joints && attach2 >= 0 && attach1 != attach2) {
1665 muscles[num_muscles].parent1 = &joints[attach1];
1666 muscles[num_muscles].parent2 = &joints[attach2];
1667 muscles[num_muscles].length = findDistance(&muscles[num_muscles].parent1->position, &muscles[num_muscles].parent2->position);
1668 muscles[num_muscles].targetlength = findDistance(&muscles[num_muscles].parent1->position, &muscles[num_muscles].parent2->position);
1669 muscles[num_muscles].strength = .7;
1670 muscles[num_muscles].type = type;
1671 muscles[num_muscles].minlength = minlength;
1672 muscles[num_muscles].maxlength = maxlength;
1683 void Skeleton::MusclesSet()
1685 for (int i = 0; i < num_muscles; i++) {
1686 muscles[i].length = findDistance(&muscles[i].parent1->position, &muscles[i].parent2->position);
1695 void Skeleton::DoBalance()
1698 newpoint=joints[0].position;
1699 newpoint.x=(joints[2].position.x+joints[4].position.x)/2;
1700 newpoint.z=(joints[2].position.z+joints[4].position.z)/2;
1701 joints[0].velocity=joints[0].velocity+(newpoint-joints[0].position);
1702 //Move child point to within certain distance of parent point
1703 joints[0].position=newpoint;