1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
|
/*
Copyright (C) 2003, 2010 - Wolfire Games
Copyright (C) 2010-2017 - Lugaru contributors (see AUTHORS file)
This file is part of Lugaru.
Lugaru is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Lugaru is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Lugaru. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Animation/Muscle.hpp"
#include "Utils/binio.h"
extern float multiplier;
extern bool freeze;
Muscle::Muscle()
: length(0)
, targetlength(0)
, parent1(0)
, parent2(0)
, maxlength(0)
, minlength(0)
, type(boneconnect)
, visible(false)
, rotate1(0)
, rotate2(0)
, rotate3(0)
, lastrotate1(0)
, lastrotate2(0)
, lastrotate3(0)
, oldrotate1(0)
, oldrotate2(0)
, oldrotate3(0)
, newrotate1(0)
, newrotate2(0)
, newrotate3(0)
,
strength(0)
{
}
void Muscle::load(FILE* tfile, int vertexNum, std::vector<Joint>& joints)
{
int numvertices, vertice, parentID;
// read info
funpackf(tfile, "Bf Bf Bf Bf Bf Bi Bi", &length, &targetlength, &minlength, &maxlength, &strength, &type, &numvertices);
// read vertices
for (int j = 0; j < numvertices; j++) {
funpackf(tfile, "Bi", &vertice);
if (vertice < vertexNum) {
vertices.push_back(vertice);
}
}
// read more info
funpackf(tfile, "Bb Bi", &visible, &parentID);
parent1 = &joints[parentID];
funpackf(tfile, "Bi", &parentID);
parent2 = &joints[parentID];
}
void Muscle::loadVerticesLow(FILE* tfile, int vertexNum)
{
int numvertices, vertice;
// read numvertices
funpackf(tfile, "Bi", &numvertices);
// read vertices
for (int j = 0; j < numvertices; j++) {
funpackf(tfile, "Bi", &vertice);
if (vertice < vertexNum) {
verticeslow.push_back(vertice);
}
}
}
void Muscle::loadVerticesClothes(FILE* tfile, int vertexNum)
{
int numvertices, vertice;
// read numvertices
funpackf(tfile, "Bi", &numvertices);
// read vertices
for (int j = 0; j < numvertices; j++) {
funpackf(tfile, "Bi", &vertice);
if (vertice < vertexNum) {
verticesclothes.push_back(vertice);
}
}
}
/* EFFECT
* sets strength, length,
* parent1->position, parent2->position,
* parent1->velocity, parent2->velocity
* used for ragdolls?
*
* USES:
* Skeleton::DoConstraints
*/
void Muscle::DoConstraint(bool spinny)
{
// FIXME: relaxlength shouldn't be static, but may not always be set
// so I don't want to change the existing behavior even though it's probably a bug
static float relaxlength;
float oldlength = length;
if (type != boneconnect) {
relaxlength = findDistance(&parent1->position, &parent2->position);
}
if (type == boneconnect) {
strength = 1;
}
if (type == constraint) {
strength = 0;
}
// clamp strength
if (strength < 0) {
strength = 0;
}
if (strength > 1) {
strength = 1;
}
length -= (length - relaxlength) * (1 - strength) * multiplier * 10000;
length -= (length - targetlength) * strength * multiplier * 10000;
if (strength == 0) {
length = relaxlength;
}
if ((relaxlength - length > 0 && relaxlength - oldlength < 0) || (relaxlength - length < 0 && relaxlength - oldlength > 0)) {
length = relaxlength;
}
// clamp length
if (length < minlength) {
length = minlength;
}
if (length > maxlength) {
length = maxlength;
}
if (length == relaxlength) {
return;
}
// relax muscle?
//Find midpoint
XYZ midp = (parent1->position * parent1->mass + parent2->position * parent2->mass) / (parent1->mass + parent2->mass);
//Find vector from midpoint to second vector
XYZ vel = parent2->position - midp;
//Change to unit vector
Normalise(&vel);
//Apply velocity change
XYZ newpoint1 = midp - vel * length * (parent2->mass / (parent1->mass + parent2->mass));
XYZ newpoint2 = midp + vel * length * (parent1->mass / (parent1->mass + parent2->mass));
if (!freeze && spinny) {
parent1->velocity = parent1->velocity + (newpoint1 - parent1->position) / multiplier / 4;
parent2->velocity = parent2->velocity + (newpoint2 - parent2->position) / multiplier / 4;
} else {
parent1->velocity = parent1->velocity + (newpoint1 - parent1->position);
parent2->velocity = parent2->velocity + (newpoint2 - parent2->position);
}
//Move child point to within certain distance of parent point
parent1->position = newpoint1;
parent2->position = newpoint2;
}
|