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 = fopen( filepath.c_str(), "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);
767 // find average position of certain joints on last frames
768 // and save in endoffset
769 // (not sure what exactly this accomplishes. the y < 1 test confuses me.)
770 for (j = 0; j < joints; j++) {
771 if (position[j][numframes - 1].y < 1)
772 endoffset += position[j][numframes - 1];
782 * takes filenames for three skeleton files and various models
784 void Skeleton::Load(const char *filename, const char *lowfilename, const char *clothesfilename,
785 const char *modelfilename, const char *model2filename,
786 const char *model3filename, const char *model4filename,
787 const char *model5filename, const char *model6filename,
788 const char *model7filename, const char *modellowfilename,
789 const char *modelclothesfilename, bool clothes)
802 // load various models
803 // rotate, scale, do normals, do texcoords for each as needed
805 model[0].loadnotex(modelfilename);
806 model[1].loadnotex(model2filename);
807 model[2].loadnotex(model3filename);
808 model[3].loadnotex(model4filename);
809 model[4].loadnotex(model5filename);
810 model[5].loadnotex(model6filename);
811 model[6].loadnotex(model7filename);
813 for (i = 0; i < num_models; i++) {
814 model[i].Rotate(180, 0, 0);
815 model[i].Scale(.04, .04, .04);
816 model[i].CalculateNormals(0);
819 drawmodel.load(modelfilename, 0);
820 drawmodel.Rotate(180, 0, 0);
821 drawmodel.Scale(.04, .04, .04);
822 drawmodel.FlipTexCoords();
823 if (tutoriallevel == 1 && id != 0)
824 drawmodel.UniformTexCoords();
825 if (tutoriallevel == 1 && id != 0)
826 drawmodel.ScaleTexCoords(0.1);
827 drawmodel.CalculateNormals(0);
829 modellow.loadnotex(modellowfilename);
830 modellow.Rotate(180, 0, 0);
831 modellow.Scale(.04, .04, .04);
832 modellow.CalculateNormals(0);
834 drawmodellow.load(modellowfilename, 0);
835 drawmodellow.Rotate(180, 0, 0);
836 drawmodellow.Scale(.04, .04, .04);
837 drawmodellow.FlipTexCoords();
838 if (tutoriallevel == 1 && id != 0)
839 drawmodellow.UniformTexCoords();
840 if (tutoriallevel == 1 && id != 0)
841 drawmodellow.ScaleTexCoords(0.1);
842 drawmodellow.CalculateNormals(0);
845 modelclothes.loadnotex(modelclothesfilename);
846 modelclothes.Rotate(180, 0, 0);
847 modelclothes.Scale(.041, .04, .041);
848 modelclothes.CalculateNormals(0);
850 drawmodelclothes.load(modelclothesfilename, 0);
851 drawmodelclothes.Rotate(180, 0, 0);
852 drawmodelclothes.Scale(.04, .04, .04);
853 drawmodelclothes.FlipTexCoords();
854 drawmodelclothes.CalculateNormals(0);
857 // FIXME: three similar blocks follow, one for each of:
858 // filename, lowfilename, clothesfilename
862 tfile = fopen( Folders::getResourcePath(filename).c_str(), "rb" );
864 if (1) { // FIXME: should this be if(tfile) ?
866 funpackf(tfile, "Bi", &num_joints);
870 delete [] joints; //dealloc2(joints);
871 joints = (Joint*)new Joint[num_joints];
873 // read info for each joint
874 for (i = 0; i < num_joints; i++) {
875 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);
876 funpackf(tfile, "Bb Bb", &joints[i].hasparent, &joints[i].locked);
877 funpackf(tfile, "Bi", &joints[i].modelnum);
878 funpackf(tfile, "Bb Bb", &joints[i].visible, &joints[i].sametwist);
879 funpackf(tfile, "Bi Bi", &joints[i].label, &joints[i].hasgun);
880 funpackf(tfile, "Bb", &joints[i].lower);
881 funpackf(tfile, "Bi", &parentID);
882 if (joints[i].hasparent)
883 joints[i].parent = &joints[parentID];
884 joints[i].velocity = 0;
885 joints[i].oldposition = joints[i].position;
889 funpackf(tfile, "Bi", &num_muscles);
893 delete [] muscles; //dealloc2(muscles);
894 muscles = (Muscle*)new Muscle[num_muscles]; //malloc(sizeof(Muscle)*num_muscles);
896 // for each muscle...
897 for (i = 0; i < num_muscles; i++) {
899 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);
901 // allocate memory for vertices
902 muscles[i].vertices = (int*)malloc(sizeof(int) * muscles[i].numvertices);
906 for (j = 0; j < muscles[i].numvertices - edit; j++) {
907 funpackf(tfile, "Bi", &muscles[i].vertices[j + edit]);
908 if (muscles[i].vertices[j + edit] >= model[0].vertexNum) {
909 muscles[i].numvertices--;
915 funpackf(tfile, "Bb Bi", &muscles[i].visible, &parentID);
916 muscles[i].parent1 = &joints[parentID];
917 funpackf(tfile, "Bi", &parentID);
918 muscles[i].parent2 = &joints[parentID];
921 // read forwardjoints (?)
922 for (j = 0; j < 3; j++) {
923 funpackf(tfile, "Bi", &forwardjoints[j]);
925 // read lowforwardjoints (?)
926 for (j = 0; j < 3; j++) {
927 funpackf(tfile, "Bi", &lowforwardjoints[j]);
931 for (j = 0; j < num_muscles; j++) {
932 for (i = 0; i < muscles[j].numvertices; i++) {
933 for (int k = 0; k < num_models; k++) {
934 if (muscles[j].numvertices && muscles[j].vertices[i] < model[k].vertexNum)
935 model[k].owner[muscles[j].vertices[i]] = j;
940 // calculate some stuff
942 for (i = 0; i < num_joints; i++) {
943 joints[i].startpos = joints[i].position;
945 for (i = 0; i < num_muscles; i++) {
946 FindRotationMuscle(i, -1);
948 // this seems to use opengl purely for matrix calculations
949 for (int k = 0; k < num_models; k++) {
950 for (i = 0; i < model[k].vertexNum; i++) {
951 model[k].vertex[i] = model[k].vertex[i] - (muscles[model[k].owner[i]].parent1->position + muscles[model[k].owner[i]].parent2->position) / 2;
952 glMatrixMode(GL_MODELVIEW);
955 glRotatef(muscles[model[k].owner[i]].rotate3, 0, 1, 0);
956 glRotatef(muscles[model[k].owner[i]].rotate2 - 90, 0, 0, 1);
957 glRotatef(muscles[model[k].owner[i]].rotate1 - 90, 0, 1, 0);
958 glTranslatef(model[k].vertex[i].x, model[k].vertex[i].y, model[k].vertex[i].z);
959 glGetFloatv(GL_MODELVIEW_MATRIX, M);
960 model[k].vertex[i].x = M[12] * 1;
961 model[k].vertex[i].y = M[13] * 1;
962 model[k].vertex[i].z = M[14] * 1;
965 model[k].CalculateNormals(0);
972 tfile = fopen( Folders::getResourcePath(lowfilename).c_str(), "rb" );
974 if (1) { // FIXME: should this be if(tfile) ?
975 // skip joints section
977 lSize = sizeof(num_joints);
978 fseek(tfile, lSize, SEEK_CUR);
979 for (i = 0; i < num_joints; i++) {
993 fseek(tfile, lSize, SEEK_CUR);
995 if (joints[i].hasparent)
996 joints[i].parent = &joints[parentID];
997 joints[i].velocity = 0;
998 joints[i].oldposition = joints[i].position;
1002 funpackf(tfile, "Bi", &num_muscles);
1004 for (i = 0; i < num_muscles; i++) {
1006 lSize = sizeof(float)
1012 fseek(tfile, lSize, SEEK_CUR);
1014 // read numverticeslow
1015 funpackf(tfile, "Bi", &muscles[i].numverticeslow);
1017 if (muscles[i].numverticeslow) {
1019 muscles[i].verticeslow = (int*)malloc(sizeof(int) * muscles[i].numverticeslow);
1023 for (j = 0; j < muscles[i].numverticeslow - edit; j++) {
1024 funpackf(tfile, "Bi", &muscles[i].verticeslow[j + edit]);
1025 if (muscles[i].verticeslow[j + edit] >= modellow.vertexNum) {
1026 muscles[i].numverticeslow--;
1033 lSize = 1; //sizeof(bool);
1034 fseek ( tfile, lSize, SEEK_CUR);
1035 lSize = sizeof(int);
1036 fseek ( tfile, lSize, SEEK_CUR);
1037 fseek ( tfile, lSize, SEEK_CUR);
1040 for (j = 0; j < num_muscles; j++) {
1041 for (i = 0; i < muscles[j].numverticeslow; i++) {
1042 if (muscles[j].verticeslow[i] < modellow.vertexNum)
1043 modellow.owner[muscles[j].verticeslow[i]] = j;
1047 // use opengl for its matrix math
1048 for (i = 0; i < modellow.vertexNum; i++) {
1049 modellow.vertex[i] = modellow.vertex[i] - (muscles[modellow.owner[i]].parent1->position + muscles[modellow.owner[i]].parent2->position) / 2;
1050 glMatrixMode(GL_MODELVIEW);
1053 glRotatef(muscles[modellow.owner[i]].rotate3, 0, 1, 0);
1054 glRotatef(muscles[modellow.owner[i]].rotate2 - 90, 0, 0, 1);
1055 glRotatef(muscles[modellow.owner[i]].rotate1 - 90, 0, 1, 0);
1056 glTranslatef(modellow.vertex[i].x, modellow.vertex[i].y, modellow.vertex[i].z);
1057 glGetFloatv(GL_MODELVIEW_MATRIX, M);
1058 modellow.vertex[i].x = M[12];
1059 modellow.vertex[i].y = M[13];
1060 modellow.vertex[i].z = M[14];
1064 modellow.CalculateNormals(0);
1070 tfile = fopen( Folders::getResourcePath(clothesfilename).c_str(), "rb" ); // FIXME: where's the check for valid load
1073 lSize = sizeof(num_joints);
1074 fseek ( tfile, lSize, SEEK_CUR);
1076 for (i = 0; i < num_joints; i++) {
1090 fseek(tfile, lSize, SEEK_CUR);
1092 if (joints[i].hasparent)
1093 joints[i].parent = &joints[parentID];
1094 joints[i].velocity = 0;
1095 joints[i].oldposition = joints[i].position;
1099 funpackf(tfile, "Bi", &num_muscles);
1101 for (i = 0; i < num_muscles; i++) {
1103 lSize = sizeof(float)
1109 fseek(tfile, lSize, SEEK_CUR);
1111 // read numverticesclothes
1112 funpackf(tfile, "Bi", &muscles[i].numverticesclothes);
1114 // read verticesclothes
1115 if (muscles[i].numverticesclothes) {
1116 muscles[i].verticesclothes = (int*)malloc(sizeof(int) * muscles[i].numverticesclothes);
1118 for (j = 0; j < muscles[i].numverticesclothes - edit; j++) {
1119 funpackf(tfile, "Bi", &muscles[i].verticesclothes[j + edit]);
1120 if (muscles[i].verticesclothes[j + edit] >= modelclothes.vertexNum) {
1121 muscles[i].numverticesclothes--;
1128 lSize = 1; //sizeof(bool);
1129 fseek ( tfile, lSize, SEEK_CUR);
1130 lSize = sizeof(int);
1131 fseek ( tfile, lSize, SEEK_CUR);
1132 fseek ( tfile, lSize, SEEK_CUR);
1136 lSize = sizeof(int);
1137 for (j = 0; j < num_muscles; j++) {
1138 for (i = 0; i < muscles[j].numverticesclothes; i++) {
1139 if (muscles[j].numverticesclothes && muscles[j].verticesclothes[i] < modelclothes.vertexNum)
1140 modelclothes.owner[muscles[j].verticesclothes[i]] = j;
1144 // use opengl for its matrix math
1145 for (i = 0; i < modelclothes.vertexNum; i++) {
1146 modelclothes.vertex[i] = modelclothes.vertex[i] - (muscles[modelclothes.owner[i]].parent1->position + muscles[modelclothes.owner[i]].parent2->position) / 2;
1147 glMatrixMode(GL_MODELVIEW);
1150 glRotatef(muscles[modelclothes.owner[i]].rotate3, 0, 1, 0);
1151 glRotatef(muscles[modelclothes.owner[i]].rotate2 - 90, 0, 0, 1);
1152 glRotatef(muscles[modelclothes.owner[i]].rotate1 - 90, 0, 1, 0);
1153 glTranslatef(modelclothes.vertex[i].x, modelclothes.vertex[i].y, modelclothes.vertex[i].z);
1154 glGetFloatv(GL_MODELVIEW_MATRIX, M);
1155 modelclothes.vertex[i].x = M[12];
1156 modelclothes.vertex[i].y = M[13];
1157 modelclothes.vertex[i].z = M[14];
1161 modelclothes.CalculateNormals(0);
1165 for (i = 0; i < num_joints; i++) {
1166 for (j = 0; j < num_joints; j++) {
1167 if (joints[i].label == j)
1175 Animation::Animation()
1181 weapontargetnum = 0;
1193 Animation::~Animation()
1198 void Animation::deallocate()
1203 for (i = 0; i < joints; i++)
1204 dealloc2(position[i]);
1211 for (i = 0; i < joints; i++)
1219 for (i = 0; i < joints; i++)
1220 dealloc2(twist2[i]);
1227 for (i = 0; i < joints; i++)
1228 dealloc2(onground[i]);
1243 dealloc2(weapontarget);
1253 Skeleton::Skeleton()
1261 memset(forwardjoints, 0, sizeof(forwardjoints));
1266 memset(lowforwardjoints, 0, sizeof(lowforwardjoints));
1269 // XYZ specialforward[5];
1270 memset(jointlabels, 0, sizeof(jointlabels));
1274 // Model modelclothes;
1278 // Model drawmodellow;
1279 // Model drawmodelclothes;
1284 memset(skinText, 0, sizeof(skinText));
1301 Skeleton::~Skeleton()
1318 verticesclothes = 0;
1322 numverticesclothes = 0;
1331 rotate1 = 0, rotate2 = 0, rotate3 = 0;
1332 lastrotate1 = 0, lastrotate2 = 0, lastrotate3 = 0;
1333 oldrotate1 = 0, oldrotate2 = 0, oldrotate3 = 0;
1334 newrotate1 = 0, newrotate2 = 0, newrotate3 = 0;
1342 dealloc2(verticeslow);
1343 dealloc2(verticesclothes);
1346 Animation & Animation::operator = (const Animation & ani)
1350 bool allocate = ((ani.numframes != numframes) || (ani.joints != joints));
1355 numframes = ani.numframes;
1356 height = ani.height;
1357 attack = ani.attack;
1358 joints = ani.joints;
1359 weapontargetnum = ani.weapontargetnum;
1360 offset = ani.offset;
1363 position = (XYZ**)malloc(sizeof(XYZ*)*ani.joints);
1364 for (i = 0; i < ani.joints; i++) {
1366 position[i] = (XYZ*)malloc(sizeof(XYZ) * ani.numframes);
1367 memcpy(position[i], ani.position[i], sizeof(XYZ)*ani.numframes);
1371 twist = (float**)malloc(sizeof(float*)*ani.joints);
1372 for (i = 0; i < ani.joints; i++) {
1374 twist[i] = (float*)malloc(sizeof(float) * ani.numframes);
1375 memcpy(twist[i], ani.twist[i], sizeof(float)*ani.numframes);
1379 twist2 = (float**)malloc(sizeof(float*)*ani.joints);
1380 for (i = 0; i < ani.joints; i++) {
1382 twist2[i] = (float*)malloc(sizeof(float) * ani.numframes);
1383 memcpy(twist2[i], ani.twist2[i], sizeof(float)*ani.numframes);
1387 speed = (float*)malloc(sizeof(float) * ani.numframes);
1388 memcpy(speed, ani.speed, sizeof(float)*ani.numframes);
1391 onground = (bool**)malloc(sizeof(bool*)*ani.joints);
1392 for (i = 0; i < ani.joints; i++) {
1394 onground[i] = (bool*)malloc(sizeof(bool) * ani.numframes);
1395 memcpy(onground[i], ani.onground[i], sizeof(bool)*ani.numframes);
1399 forward = (XYZ*)malloc(sizeof(XYZ) * ani.numframes);
1400 memcpy(forward, ani.forward, sizeof(XYZ)*ani.numframes);
1403 weapontarget = (XYZ*)malloc(sizeof(XYZ) * ani.numframes);
1404 memcpy(weapontarget, ani.weapontarget, sizeof(XYZ)*ani.numframes);
1407 label = (int*)malloc(sizeof(int) * ani.numframes);
1408 memcpy(label, ani.label, sizeof(int)*ani.numframes);
1418 // the following functions are not used anywhere
1421 * sets forward, lowforward, specialforward[]
1426 void Skeleton::FindForwardsfirst()
1428 //Find forward vectors
1429 CrossProduct(joints[forwardjoints[1]].position - joints[forwardjoints[0]].position, joints[forwardjoints[2]].position - joints[forwardjoints[0]].position, &forward);
1430 Normalise(&forward);
1432 CrossProduct(joints[lowforwardjoints[1]].position - joints[lowforwardjoints[0]].position, joints[lowforwardjoints[2]].position - joints[lowforwardjoints[0]].position, &lowforward);
1433 Normalise(&lowforward);
1436 specialforward[0] = forward;
1437 specialforward[1] = forward;
1438 specialforward[2] = forward;
1439 specialforward[3] = forward;
1440 specialforward[4] = forward;
1449 void Skeleton::Draw(int muscleview)
1451 static float jointcolor[4];
1453 if (muscleview != 2) {
1460 if (muscleview == 2) {
1466 //Calc motionblur-ness
1467 for (int i = 0; i < num_joints; i++) {
1468 joints[i].oldposition = joints[i].position;
1469 joints[i].blurred = findDistance(&joints[i].position, &joints[i].oldposition) * 100;
1470 if (joints[i].blurred < 1)
1471 joints[i].blurred = 1;
1477 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1479 for (int i = 0; i < num_joints; i++) {
1480 if (joints[i].hasparent) {
1481 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / joints[i].blurred);
1482 glVertex3f(joints[i].position.x, joints[i].position.y, joints[i].position.z);
1483 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / joints[i].parent->blurred);
1484 glVertex3f(joints[i].parent->position.x, joints[i].parent->position.y, joints[i].parent->position.z);
1485 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / joints[i].parent->blurred);
1486 glVertex3f(joints[i].parent->oldposition.x, joints[i].parent->oldposition.y, joints[i].parent->oldposition.z);
1487 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / joints[i].blurred);
1488 glVertex3f(joints[i].oldposition.x, joints[i].oldposition.y, joints[i].oldposition.z);
1491 for (int i = 0; i < num_muscles; i++) {
1492 if (muscles[i].type == boneconnect) {
1493 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / muscles[i].parent2->blurred);
1494 glVertex3f(muscles[i].parent1->position.x, muscles[i].parent1->position.y, muscles[i].parent1->position.z);
1495 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / muscles[i].parent2->blurred);
1496 glVertex3f(muscles[i].parent2->position.x, muscles[i].parent2->position.y, muscles[i].parent2->position.z);
1497 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / muscles[i].parent2->blurred);
1498 glVertex3f(muscles[i].parent2->oldposition.x, muscles[i].parent2->oldposition.y, muscles[i].parent2->oldposition.z);
1499 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / muscles[i].parent1->blurred);
1500 glVertex3f(muscles[i].parent1->oldposition.x, muscles[i].parent1->oldposition.y, muscles[i].parent1->oldposition.z);
1506 for (int i = 0; i < num_joints; i++) {
1507 if (joints[i].hasparent) {
1508 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / joints[i].blurred);
1509 glVertex3f(joints[i].position.x, joints[i].position.y, joints[i].position.z);
1510 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / joints[i].parent->blurred);
1511 glVertex3f(joints[i].parent->position.x, joints[i].parent->position.y, joints[i].parent->position.z);
1514 for (int i = 0; i < num_muscles; i++) {
1515 if (muscles[i].type == boneconnect) {
1516 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / muscles[i].parent1->blurred);
1517 glVertex3f(muscles[i].parent1->position.x, muscles[i].parent1->position.y, muscles[i].parent1->position.z);
1518 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / muscles[i].parent2->blurred);
1519 glVertex3f(muscles[i].parent2->position.x, muscles[i].parent2->position.y, muscles[i].parent2->position.z);
1522 glColor3f(.6, .6, 0);
1523 if (muscleview == 1)
1524 for (int i = 0; i < num_muscles; i++) {
1525 if (muscles[i].type != boneconnect) {
1526 glVertex3f(muscles[i].parent1->position.x, muscles[i].parent1->position.y, muscles[i].parent1->position.z);
1527 glVertex3f(muscles[i].parent2->position.x, muscles[i].parent2->position.y, muscles[i].parent2->position.z);
1532 if (muscleview != 2) {
1535 for (int i = 0; i < num_joints; i++) {
1537 glColor4f(0, 0, .5, 1);
1539 glColor4f(1, 1, 0, 1);
1540 if (joints[i].locked && i != selected)
1541 glColor4f(1, 0, 0, 1);
1542 glVertex3f(joints[i].position.x, joints[i].position.y, joints[i].position.z);
1547 //Set old position to current position
1548 if (muscleview == 2)
1549 for (int i = 0; i < num_joints; i++) {
1550 joints[i].oldposition = joints[i].position;
1560 void Skeleton::AddJoint(float x, float y, float z, int which)
1562 if (num_joints < max_joints - 1) {
1563 joints[num_joints].velocity = 0;
1564 joints[num_joints].position.x = x;
1565 joints[num_joints].position.y = y;
1566 joints[num_joints].position.z = z;
1567 joints[num_joints].mass = 1;
1568 joints[num_joints].locked = 0;
1570 joints[num_joints].hasparent = 0;
1572 if (which < num_joints && which >= 0)
1573 AddMuscle(num_joints - 1, which, 0, 10, boneconnect);
1582 void Skeleton::DeleteJoint(int whichjoint)
1584 if (whichjoint < num_joints && whichjoint >= 0) {
1585 joints[whichjoint].velocity = joints[num_joints - 1].velocity;
1586 joints[whichjoint].position = joints[num_joints - 1].position;
1587 joints[whichjoint].oldposition = joints[num_joints - 1].oldposition;
1588 joints[whichjoint].hasparent = joints[num_joints - 1].hasparent;
1589 joints[whichjoint].parent = joints[num_joints - 1].parent;
1590 joints[whichjoint].length = joints[num_joints - 1].length;
1591 joints[whichjoint].locked = joints[num_joints - 1].locked;
1592 joints[whichjoint].modelnum = joints[num_joints - 1].modelnum;
1593 joints[whichjoint].visible = joints[num_joints - 1].visible;
1595 for (int i = 0; i < num_muscles; i++) {
1596 while (muscles[i].parent1 == &joints[whichjoint] && i < num_muscles)DeleteMuscle(i);
1597 while (muscles[i].parent2 == &joints[whichjoint] && i < num_muscles)DeleteMuscle(i);
1599 for (int i = 0; i < num_muscles; i++) {
1600 while (muscles[i].parent1 == &joints[num_joints - 1] && i < num_muscles)muscles[i].parent1 = &joints[whichjoint];
1601 while (muscles[i].parent2 == &joints[num_joints - 1] && i < num_muscles)muscles[i].parent2 = &joints[whichjoint];
1603 for (int i = 0; i < num_joints; i++) {
1604 if (joints[i].parent == &joints[whichjoint])
1605 joints[i].hasparent = 0;
1607 for (int i = 0; i < num_joints; i++) {
1608 if (joints[i].parent == &joints[num_joints - 1])
1609 joints[i].parent = &joints[whichjoint];
1619 * Skeleton::DeleteJoint - UNUSED
1621 void Skeleton::DeleteMuscle(int whichmuscle)
1623 if (whichmuscle < num_muscles) {
1624 muscles[whichmuscle].minlength = muscles[num_muscles - 1].minlength;
1625 muscles[whichmuscle].maxlength = muscles[num_muscles - 1].maxlength;
1626 muscles[whichmuscle].strength = muscles[num_muscles - 1].strength;
1627 muscles[whichmuscle].parent1 = muscles[num_muscles - 1].parent1;
1628 muscles[whichmuscle].parent2 = muscles[num_muscles - 1].parent2;
1629 muscles[whichmuscle].length = muscles[num_muscles - 1].length;
1630 muscles[whichmuscle].visible = muscles[num_muscles - 1].visible;
1631 muscles[whichmuscle].type = muscles[num_muscles - 1].type;
1632 muscles[whichmuscle].targetlength = muscles[num_muscles - 1].targetlength;
1643 void Skeleton::SetJoint(float x, float y, float z, int which, int whichjoint)
1645 if (whichjoint < num_joints) {
1646 joints[whichjoint].velocity = 0;
1647 joints[whichjoint].position.x = x;
1648 joints[whichjoint].position.y = y;
1649 joints[whichjoint].position.z = z;
1651 if (which >= num_joints || which < 0)
1652 joints[whichjoint].hasparent = 0;
1653 if (which < num_joints && which >= 0) {
1654 joints[whichjoint].parent = &joints[which];
1655 joints[whichjoint].hasparent = 1;
1656 joints[whichjoint].length = findDistance(&joints[whichjoint].position, &joints[whichjoint].parent->position);
1664 * Skeleton::AddJoint - UNUSED
1666 void Skeleton::AddMuscle(int attach1, int attach2, float minlength, float maxlength, int type)
1668 const int max_muscles = 100; // FIXME: Probably can be dropped
1669 if (num_muscles < max_muscles - 1 && attach1 < num_joints && attach1 >= 0 && attach2 < num_joints && attach2 >= 0 && attach1 != attach2) {
1670 muscles[num_muscles].parent1 = &joints[attach1];
1671 muscles[num_muscles].parent2 = &joints[attach2];
1672 muscles[num_muscles].length = findDistance(&muscles[num_muscles].parent1->position, &muscles[num_muscles].parent2->position);
1673 muscles[num_muscles].targetlength = findDistance(&muscles[num_muscles].parent1->position, &muscles[num_muscles].parent2->position);
1674 muscles[num_muscles].strength = .7;
1675 muscles[num_muscles].type = type;
1676 muscles[num_muscles].minlength = minlength;
1677 muscles[num_muscles].maxlength = maxlength;
1688 void Skeleton::MusclesSet()
1690 for (int i = 0; i < num_muscles; i++) {
1691 muscles[i].length = findDistance(&muscles[i].parent1->position, &muscles[i].parent2->position);
1700 void Skeleton::DoBalance()
1703 newpoint=joints[0].position;
1704 newpoint.x=(joints[2].position.x+joints[4].position.x)/2;
1705 newpoint.z=(joints[2].position.z+joints[4].position.z)/2;
1706 joints[0].velocity=joints[0].velocity+(newpoint-joints[0].position);
1707 //Move child point to within certain distance of parent point
1708 joints[0].position=newpoint;