From 1d589bc3024e30dd51e41b6041b57649190bef4d Mon Sep 17 00:00:00 2001 From: =?utf8?q?C=C3=B4me=20Chilliet?= Date: Fri, 9 Dec 2016 23:13:00 +0700 Subject: [PATCH] Moved Joint and Muscle classes to their own files --- CMakeLists.txt | 4 + Source/Animation/Joint.cpp | 38 ++++++++++ Source/Animation/Joint.h | 53 +++++++++++++ Source/Animation/Muscle.cpp | 135 ++++++++++++++++++++++++++++++++++ Source/Animation/Muscle.h | 57 ++++++++++++++ Source/Animation/Skeleton.cpp | 123 ++----------------------------- Source/Animation/Skeleton.h | 74 +------------------ 7 files changed, 295 insertions(+), 189 deletions(-) create mode 100644 Source/Animation/Joint.cpp create mode 100644 Source/Animation/Joint.h create mode 100644 Source/Animation/Muscle.cpp create mode 100644 Source/Animation/Muscle.h diff --git a/CMakeLists.txt b/CMakeLists.txt index ddcbd70..c1a3e6b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -40,6 +40,8 @@ set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/") set(LUGARU_SRCS ${SRCDIR}/main.cpp ${SRCDIR}/Animation/Animation.cpp + ${SRCDIR}/Animation/Joint.cpp + ${SRCDIR}/Animation/Muscle.cpp ${SRCDIR}/Animation/Skeleton.cpp ${SRCDIR}/Frustum.cpp ${SRCDIR}/Account.cpp @@ -77,6 +79,8 @@ set(LUGARU_SRCS set(LUGARU_H ${SRCDIR}/Animation/Animation.h + ${SRCDIR}/Animation/Joint.h + ${SRCDIR}/Animation/Muscle.h ${SRCDIR}/Animation/Skeleton.h ${SRCDIR}/Frustum.h ${SRCDIR}/Account.h diff --git a/Source/Animation/Joint.cpp b/Source/Animation/Joint.cpp new file mode 100644 index 0000000..760e49b --- /dev/null +++ b/Source/Animation/Joint.cpp @@ -0,0 +1,38 @@ +/* +Copyright (C) 2003, 2010 - Wolfire Games +Copyright (C) 2010-2016 - 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 . +*/ + +#include "Animation/Joint.h" + +Joint::Joint() : + blurred(0), + length(0), + mass(0), + lower(false), + hasparent(false), + locked(false), + modelnum(0), + visible(false), + parent(nullptr), + sametwist(false), + label(0), + hasgun(0), + delay(0) +{ +} diff --git a/Source/Animation/Joint.h b/Source/Animation/Joint.h new file mode 100644 index 0000000..9073e7a --- /dev/null +++ b/Source/Animation/Joint.h @@ -0,0 +1,53 @@ +/* +Copyright (C) 2003, 2010 - Wolfire Games +Copyright (C) 2010-2016 - 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 . +*/ + +#ifndef _JOINT_H_ +#define _JOINT_H_ + +#include "Quaternions.h" + +class Joint +{ +public: + XYZ position; + XYZ oldposition; + XYZ realoldposition; + XYZ velocity; + XYZ oldvelocity; + XYZ startpos; + float blurred; + float length; + float mass; + bool lower; + bool hasparent; + bool locked; + int modelnum; + bool visible; + Joint* parent; + bool sametwist; + int label; + int hasgun; + float delay; + XYZ velchange; + + Joint(); +}; + +#endif diff --git a/Source/Animation/Muscle.cpp b/Source/Animation/Muscle.cpp new file mode 100644 index 0000000..4a5a047 --- /dev/null +++ b/Source/Animation/Muscle.cpp @@ -0,0 +1,135 @@ +/* +Copyright (C) 2003, 2010 - Wolfire Games +Copyright (C) 2010-2016 - 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 . +*/ + +#include "Animation/Muscle.h" + +extern float multiplier; +extern bool freeze; + +Muscle::Muscle() +{ + vertices = 0; + verticeslow = 0; + verticesclothes = 0; + + numvertices = 0; + numverticeslow = 0; + numverticesclothes = 0; + length = 0; + targetlength = 0; + parent1 = 0; + parent2 = 0; + maxlength = 0; + minlength = 0; + type = boneconnect; + visible = 0; + 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; +} + +Muscle::~Muscle() +{ + free(vertices); + free(verticeslow); + free(verticesclothes); +} + + +/* 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; +} diff --git a/Source/Animation/Muscle.h b/Source/Animation/Muscle.h new file mode 100644 index 0000000..cb85f85 --- /dev/null +++ b/Source/Animation/Muscle.h @@ -0,0 +1,57 @@ +/* +Copyright (C) 2003, 2010 - Wolfire Games +Copyright (C) 2010-2016 - 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 . +*/ + +#ifndef _MUSCLE_H_ +#define _MUSCLE_H_ + +#include "Animation/Joint.h" + +enum muscle_type {boneconnect, constraint, muscle}; + +class Muscle +{ +public: + int numvertices; + int* vertices; + int numverticeslow; + int* verticeslow; + int numverticesclothes; + int* verticesclothes; + float length; + float targetlength; + Joint* parent1; + Joint* parent2; + float maxlength; + float minlength; + muscle_type type; + bool visible; + float rotate1, rotate2, rotate3; + float lastrotate1, lastrotate2, lastrotate3; + float oldrotate1, oldrotate2, oldrotate3; + float newrotate1, newrotate2, newrotate3; + + float strength; + + Muscle(); + ~Muscle(); + void DoConstraint(bool spinny); +}; + +#endif diff --git a/Source/Animation/Skeleton.cpp b/Source/Animation/Skeleton.cpp index aac06ee..ee19b81 100644 --- a/Source/Animation/Skeleton.cpp +++ b/Source/Animation/Skeleton.cpp @@ -47,84 +47,6 @@ void dealloc2(void* param) free(param); } -enum {boneconnect, constraint, muscle}; - - -/* 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; -} - /* EFFECT * sets forward, lowforward, specialforward[] * @@ -1118,38 +1040,6 @@ Skeleton::~Skeleton() joints = 0; } -Muscle::Muscle() -{ - vertices = 0; - verticeslow = 0; - verticesclothes = 0; - - numvertices = 0; - numverticeslow = 0; - numverticesclothes = 0; - length = 0; - targetlength = 0; - parent1 = 0; - parent2 = 0; - maxlength = 0; - minlength = 0; - type = 0; - visible = 0; - 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; -} - -Muscle::~Muscle() -{ - dealloc2(vertices); - dealloc2(verticeslow); - dealloc2(verticesclothes); -} - #if 0 // the following functions are not used anywhere @@ -1187,19 +1077,18 @@ void Skeleton::Draw(int muscleview) { static float jointcolor[4]; - if (muscleview != 2) { - jointcolor[0] = 0; - jointcolor[1] = 0; - jointcolor[2] = .5; - jointcolor[3] = 1; - } - if (muscleview == 2) { jointcolor[0] = 0; jointcolor[1] = 0; jointcolor[2] = 0; jointcolor[3] = .5; + } else { + jointcolor[0] = 0; + jointcolor[1] = 0; + jointcolor[2] = .5; + jointcolor[3] = 1; } + //Calc motionblur-ness for (int i = 0; i < num_joints; i++) { joints[i].oldposition = joints[i].position; diff --git a/Source/Animation/Skeleton.h b/Source/Animation/Skeleton.h index 7c482f9..fe1ceb1 100644 --- a/Source/Animation/Skeleton.h +++ b/Source/Animation/Skeleton.h @@ -22,8 +22,6 @@ along with Lugaru. If not, see . #define _SKELETON_H_ #include "Models.h" -#include "Quaternions.h" - /**> HEADER FILES <**/ #include "gamegl.h" @@ -32,6 +30,8 @@ along with Lugaru. If not, see . #include "Sprite.h" #include "binio.h" #include "Animation/Animation.h" +#include "Animation/Joint.h" +#include "Animation/Muscle.h" enum bodyparts { head, neck, @@ -42,76 +42,6 @@ enum bodyparts { rightknee, rightankle, rightfoot }; -class Joint -{ -public: - XYZ position; - XYZ oldposition; - XYZ realoldposition; - XYZ velocity; - XYZ oldvelocity; - XYZ startpos; - float blurred; - float length; - float mass; - bool lower; - bool hasparent; - bool locked; - int modelnum; - bool visible; - Joint* parent; - bool sametwist; - int label; - int hasgun; - float delay; - XYZ velchange; - - Joint() { - blurred = 0; - length = 0; - mass = 0; - lower = 0; - hasparent = 0; - locked = 0; - modelnum = 0; - visible = 0; - parent = 0; - sametwist = 0; - label = 0; - hasgun = 0; - delay = 0; - } -}; - -class Muscle -{ -public: - int numvertices; - int* vertices; - int numverticeslow; - int* verticeslow; - int numverticesclothes; - int* verticesclothes; - float length; - float targetlength; - Joint* parent1; - Joint* parent2; - float maxlength; - float minlength; - int type; - bool visible; - float rotate1, rotate2, rotate3; - float lastrotate1, lastrotate2, lastrotate3; - float oldrotate1, oldrotate2, oldrotate3; - float newrotate1, newrotate2, newrotate3; - - float strength; - - Muscle(); - ~Muscle(); - void DoConstraint(bool spinny); -}; - const int max_joints = 50; class Skeleton -- 2.39.2