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 #include "Animation/Muscle.h"
23 extern float multiplier;
34 numverticesclothes = 0;
43 rotate1 = 0, rotate2 = 0, rotate3 = 0;
44 lastrotate1 = 0, lastrotate2 = 0, lastrotate3 = 0;
45 oldrotate1 = 0, oldrotate2 = 0, oldrotate3 = 0;
46 newrotate1 = 0, newrotate2 = 0, newrotate3 = 0;
55 free(verticesclothes);
60 * sets strength, length,
61 * parent1->position, parent2->position,
62 * parent1->velocity, parent2->velocity
66 * Skeleton::DoConstraints
68 void Muscle::DoConstraint(bool spinny)
70 // FIXME: relaxlength shouldn't be static, but may not always be set
71 // so I don't want to change the existing behavior even though it's probably a bug
72 static float relaxlength;
74 float oldlength = length;
76 if (type != boneconnect) {
77 relaxlength = findDistance(&parent1->position, &parent2->position);
80 if (type == boneconnect) {
83 if (type == constraint) {
93 length -= (length - relaxlength) * (1 - strength) * multiplier * 10000;
94 length -= (length - targetlength) * (strength) * multiplier * 10000;
98 if ((relaxlength - length > 0 && relaxlength - oldlength < 0) || (relaxlength - length < 0 && relaxlength - oldlength > 0))
102 if (length < minlength)
104 if (length > maxlength)
107 if (length == relaxlength)
113 XYZ midp = (parent1->position * parent1->mass + parent2->position * parent2->mass) / (parent1->mass + parent2->mass);
115 //Find vector from midpoint to second vector
116 XYZ vel = parent2->position - midp;
118 //Change to unit vector
121 //Apply velocity change
122 XYZ newpoint1 = midp - vel * length * (parent2->mass / (parent1->mass + parent2->mass));
123 XYZ newpoint2 = midp + vel * length * (parent1->mass / (parent1->mass + parent2->mass));
124 if (!freeze && spinny) {
125 parent1->velocity = parent1->velocity + (newpoint1 - parent1->position) / multiplier / 4;
126 parent2->velocity = parent2->velocity + (newpoint2 - parent2->position) / multiplier / 4;
128 parent1->velocity = parent1->velocity + (newpoint1 - parent1->position);
129 parent2->velocity = parent2->velocity + (newpoint2 - parent2->position);
132 //Move child point to within certain distance of parent point
133 parent1->position = newpoint1;
134 parent2->position = newpoint2;