2 Copyright (C) 2003, 2010 - Wolfire Games
3 Copyright (C) 2010-2017 - 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.hpp"
23 #include "Utils/binio.h"
25 extern float multiplier;
55 void Muscle::load(FILE* tfile, int vertexNum, std::vector<Joint>& joints)
57 int numvertices, vertice, parentID;
60 funpackf(tfile, "Bf Bf Bf Bf Bf Bi Bi", &length, &targetlength, &minlength, &maxlength, &strength, &type, &numvertices);
63 for (int j = 0; j < numvertices; j++) {
64 funpackf(tfile, "Bi", &vertice);
65 if (vertice < vertexNum) {
66 vertices.push_back(vertice);
71 funpackf(tfile, "Bb Bi", &visible, &parentID);
72 parent1 = &joints[parentID];
73 funpackf(tfile, "Bi", &parentID);
74 parent2 = &joints[parentID];
77 void Muscle::loadVerticesLow(FILE* tfile, int vertexNum)
79 int numvertices, vertice;
82 funpackf(tfile, "Bi", &numvertices);
85 for (int j = 0; j < numvertices; j++) {
86 funpackf(tfile, "Bi", &vertice);
87 if (vertice < vertexNum) {
88 verticeslow.push_back(vertice);
93 void Muscle::loadVerticesClothes(FILE* tfile, int vertexNum)
95 int numvertices, vertice;
98 funpackf(tfile, "Bi", &numvertices);
101 for (int j = 0; j < numvertices; j++) {
102 funpackf(tfile, "Bi", &vertice);
103 if (vertice < vertexNum) {
104 verticesclothes.push_back(vertice);
110 * sets strength, length,
111 * parent1->position, parent2->position,
112 * parent1->velocity, parent2->velocity
116 * Skeleton::DoConstraints
118 void Muscle::DoConstraint(bool spinny)
120 // FIXME: relaxlength shouldn't be static, but may not always be set
121 // so I don't want to change the existing behavior even though it's probably a bug
122 static float relaxlength;
124 float oldlength = length;
126 if (type != boneconnect) {
127 relaxlength = findDistance(&parent1->position, &parent2->position);
130 if (type == boneconnect) {
133 if (type == constraint) {
145 length -= (length - relaxlength) * (1 - strength) * multiplier * 10000;
146 length -= (length - targetlength) * strength * multiplier * 10000;
148 length = relaxlength;
151 if ((relaxlength - length > 0 && relaxlength - oldlength < 0) || (relaxlength - length < 0 && relaxlength - oldlength > 0)) {
152 length = relaxlength;
156 if (length < minlength) {
159 if (length > maxlength) {
163 if (length == relaxlength) {
170 XYZ midp = (parent1->position * parent1->mass + parent2->position * parent2->mass) / (parent1->mass + parent2->mass);
172 //Find vector from midpoint to second vector
173 XYZ vel = parent2->position - midp;
175 //Change to unit vector
178 //Apply velocity change
179 XYZ newpoint1 = midp - vel * length * (parent2->mass / (parent1->mass + parent2->mass));
180 XYZ newpoint2 = midp + vel * length * (parent1->mass / (parent1->mass + parent2->mass));
181 if (!freeze && spinny) {
182 parent1->velocity = parent1->velocity + (newpoint1 - parent1->position) / multiplier / 4;
183 parent2->velocity = parent2->velocity + (newpoint2 - parent2->position) / multiplier / 4;
185 parent1->velocity = parent1->velocity + (newpoint1 - parent1->position);
186 parent2->velocity = parent2->velocity + (newpoint2 - parent2->position);
189 //Move child point to within certain distance of parent point
190 parent1->position = newpoint1;
191 parent2->position = newpoint2;