]> git.jsancho.org Git - lugaru.git/blob - Source/Animation/Muscle.cpp
Moved Joint and Muscle classes to their own files
[lugaru.git] / Source / Animation / Muscle.cpp
1 /*
2 Copyright (C) 2003, 2010 - Wolfire Games
3 Copyright (C) 2010-2016 - Lugaru contributors (see AUTHORS file)
4
5 This file is part of Lugaru.
6
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.
11
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.
16
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/>.
19 */
20
21 #include "Animation/Muscle.h"
22
23 extern float multiplier;
24 extern bool freeze;
25
26 Muscle::Muscle()
27 {
28     vertices = 0;
29     verticeslow = 0;
30     verticesclothes = 0;
31
32     numvertices = 0;
33     numverticeslow = 0;
34     numverticesclothes = 0;
35     length = 0;
36     targetlength = 0;
37     parent1 = 0;
38     parent2 = 0;
39     maxlength = 0;
40     minlength = 0;
41     type = boneconnect;
42     visible = 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;
47
48     strength = 0;
49 }
50
51 Muscle::~Muscle()
52 {
53     free(vertices);
54     free(verticeslow);
55     free(verticesclothes);
56 }
57
58
59 /* EFFECT
60  * sets strength, length,
61  *      parent1->position, parent2->position,
62  *      parent1->velocity, parent2->velocity
63  * used for ragdolls?
64  *
65  * USES:
66  * Skeleton::DoConstraints
67  */
68 void Muscle::DoConstraint(bool spinny)
69 {
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;
73
74     float oldlength = length;
75
76     if (type != boneconnect) {
77         relaxlength = findDistance(&parent1->position, &parent2->position);
78     }
79
80     if (type == boneconnect) {
81         strength = 1;
82     }
83     if (type == constraint) {
84         strength = 0;
85     }
86
87     // clamp strength
88     if (strength < 0)
89         strength = 0;
90     if (strength > 1)
91         strength = 1;
92
93     length -= (length - relaxlength) * (1 - strength) * multiplier * 10000;
94     length -= (length - targetlength) * (strength) * multiplier * 10000;
95     if (strength == 0)
96         length = relaxlength;
97
98     if ((relaxlength - length > 0 && relaxlength - oldlength < 0) || (relaxlength - length < 0 && relaxlength - oldlength > 0))
99         length = relaxlength;
100
101     // clamp length
102     if (length < minlength)
103         length = minlength;
104     if (length > maxlength)
105         length = maxlength;
106
107     if (length == relaxlength)
108         return;
109
110     // relax muscle?
111
112     //Find midpoint
113     XYZ midp = (parent1->position * parent1->mass + parent2->position * parent2->mass) / (parent1->mass + parent2->mass);
114
115     //Find vector from midpoint to second vector
116     XYZ vel = parent2->position - midp;
117
118     //Change to unit vector
119     Normalise(&vel);
120
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;
127     } else {
128         parent1->velocity = parent1->velocity + (newpoint1 - parent1->position);
129         parent2->velocity = parent2->velocity + (newpoint2 - parent2->position);
130     }
131
132     //Move child point to within certain distance of parent point
133     parent1->position = newpoint1;
134     parent2->position = newpoint2;
135 }