2010-02-15 23:27:35 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* Copyright (C) 2009 Daniel Chappuis *
|
|
|
|
****************************************************************************
|
|
|
|
* This file is part of ReactPhysics3D. *
|
|
|
|
* *
|
|
|
|
* ReactPhysics3D is free software: you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU Lesser General Public License as published *
|
|
|
|
* by the Free Software Foundation, either version 3 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* ReactPhysics3D 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 Lesser General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License *
|
|
|
|
* along with ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. *
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
// Libraries
|
|
|
|
#include "ConstraintSolver.h"
|
2010-03-17 19:19:11 +00:00
|
|
|
#include "LCPProjectedGaussSeidel.h"
|
2010-02-15 23:27:35 +00:00
|
|
|
|
2010-03-17 19:19:11 +00:00
|
|
|
using namespace reactphysics3d;
|
2010-02-15 23:27:35 +00:00
|
|
|
|
2010-03-17 19:19:11 +00:00
|
|
|
// Constructor
|
2010-04-06 16:45:48 +00:00
|
|
|
ConstraintSolver::ConstraintSolver(PhysicsWorld& physicsWorld)
|
|
|
|
:physicsWorld(physicsWorld), bodyMapping(0) , lcpSolver(LCPProjectedGaussSeidel(MAX_LCP_ITERATIONS)) {
|
|
|
|
|
2010-02-15 23:27:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Destructor
|
2010-03-17 19:19:11 +00:00
|
|
|
ConstraintSolver::~ConstraintSolver() {
|
2010-02-15 23:27:35 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate all the matrices needed to solve the LCP problem
|
2010-04-06 16:45:48 +00:00
|
|
|
void ConstraintSolver::allocate() {
|
|
|
|
unsigned int nbConstraints = 0;
|
2010-03-24 21:36:10 +00:00
|
|
|
|
2010-02-15 23:27:35 +00:00
|
|
|
// For each constraint
|
2010-04-07 14:40:46 +00:00
|
|
|
for (unsigned int c=0; c<physicsWorld.getConstraints().size(); c++) {
|
|
|
|
Constraint* constraint = physicsWorld.getConstraints().at(c);
|
|
|
|
|
2010-03-17 19:19:11 +00:00
|
|
|
// Evaluate the constraint
|
2010-04-07 14:40:46 +00:00
|
|
|
constraint->evaluate();
|
2010-03-17 19:19:11 +00:00
|
|
|
|
2010-02-15 23:27:35 +00:00
|
|
|
// If the constraint is active
|
2010-04-07 14:40:46 +00:00
|
|
|
if (constraint->isActive()) {
|
|
|
|
activeConstraints.push_back(constraint);
|
|
|
|
|
2010-04-12 22:27:50 +00:00
|
|
|
// Add the two bodies of the constraint in the constraintBodies list
|
|
|
|
constraintBodies.push_back(constraint->getBody1());
|
|
|
|
constraintBodies.push_back(constraint->getBody2());
|
|
|
|
|
2010-04-07 14:40:46 +00:00
|
|
|
// Fill in the body number maping
|
|
|
|
bodyNumberMapping.insert(std::pair<Body*, unsigned int>(constraint->getBody1(), bodyNumberMapping.size()));
|
|
|
|
bodyNumberMapping.insert(std::pair<Body*, unsigned int>(constraint->getBody1(), bodyNumberMapping.size()));
|
2010-02-15 23:27:35 +00:00
|
|
|
|
2010-04-06 16:45:48 +00:00
|
|
|
// Update the size of the jacobian matrix
|
2010-04-07 14:40:46 +00:00
|
|
|
nbConstraints += (1 + constraint->getNbAuxConstraints());
|
2010-02-15 23:27:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-07 14:40:46 +00:00
|
|
|
// Compute the number of bodies that are part of some active constraint
|
|
|
|
nbBodies = bodyNumberMapping.size();
|
|
|
|
|
2010-04-06 16:45:48 +00:00
|
|
|
bodyMapping = new Body**[nbConstraints];
|
|
|
|
for (unsigned int i=0; i<nbConstraints; i++) {
|
|
|
|
bodyMapping[i] = new Body*[2];
|
2010-02-15 23:27:35 +00:00
|
|
|
}
|
|
|
|
|
2010-04-06 16:45:48 +00:00
|
|
|
J_sp = Matrix(nbConstraints, 12);
|
|
|
|
errorValues = Vector(nbConstraints);
|
|
|
|
B_sp = Matrix(12, nbConstraints);
|
|
|
|
b = Vector(nbConstraints);
|
|
|
|
lambda = Vector(nbConstraints);
|
|
|
|
lowerBounds = Vector(nbConstraints);
|
|
|
|
upperBounds = Vector(nbConstraints);
|
|
|
|
Minv_sp = Matrix(6*nbBodies, 6);
|
2010-04-12 22:27:50 +00:00
|
|
|
Minv_sp.initWithValue(0.0);
|
2010-03-17 19:19:11 +00:00
|
|
|
V = Vector(6*nbBodies);
|
2010-04-06 16:45:48 +00:00
|
|
|
Fext = Vector(6*nbBodies);
|
2010-02-15 23:27:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fill in all the matrices needed to solve the LCP problem
|
2010-03-17 19:19:11 +00:00
|
|
|
// Notice that all the active constraints should have been evaluated first
|
|
|
|
void ConstraintSolver::fillInMatrices() {
|
|
|
|
|
|
|
|
// For each active constraint
|
2010-04-06 16:45:48 +00:00
|
|
|
for (unsigned int c=0; c<activeConstraints.size(); c++) {
|
|
|
|
Constraint* constraint = activeConstraints.at(c);
|
2010-03-17 19:19:11 +00:00
|
|
|
|
2010-04-06 16:45:48 +00:00
|
|
|
// Fill in the J_sp matrix
|
|
|
|
J_sp.fillInSubMatrix(c, 0, constraint->getBody1Jacobian());
|
|
|
|
J_sp.fillInSubMatrix(c, 6, constraint->getBody2Jacobian());
|
2010-04-07 14:40:46 +00:00
|
|
|
|
2010-04-06 16:45:48 +00:00
|
|
|
// Fill in the body mapping matrix
|
|
|
|
bodyMapping[c][0] = constraint->getBody1();
|
|
|
|
bodyMapping[c][1] = constraint->getBody2();
|
|
|
|
|
|
|
|
// Fill in the limit vectors for the constraint
|
|
|
|
lowerBounds.fillInSubVector(c, constraint->getLowerBound());
|
|
|
|
upperBounds.fillInSubVector(c, constraint->getUpperBound());
|
|
|
|
|
|
|
|
// Fill in the error vector
|
|
|
|
errorValues.fillInSubVector(c, constraint->getErrorValue());
|
|
|
|
|
|
|
|
unsigned int nbAuxConstraints = constraint->getNbAuxConstraints();
|
|
|
|
|
|
|
|
// If the current constraint has auxiliary constraints
|
|
|
|
if (nbAuxConstraints > 0) {
|
|
|
|
// Fill in the J_sp matrix
|
|
|
|
J_sp.fillInSubMatrix(c+1, 0, constraint->getAuxJacobian());
|
|
|
|
|
|
|
|
// For each auxiliary constraints
|
|
|
|
for (unsigned int i=1; i<nbAuxConstraints; i++) {
|
|
|
|
// Fill in the body mapping matrix
|
|
|
|
bodyMapping[c+i][0] = constraint->getBody1();
|
|
|
|
bodyMapping[c+i][1] = constraint->getBody2();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fill in the limit vectors for the auxilirary constraints
|
|
|
|
lowerBounds.fillInSubVector(c+1, constraint->getAuxLowerBounds());
|
|
|
|
upperBounds.fillInSubVector(c+1, constraint->getAuxUpperBounds());
|
|
|
|
}
|
2010-03-17 19:19:11 +00:00
|
|
|
}
|
|
|
|
|
2010-04-12 22:27:50 +00:00
|
|
|
// For each current body that is implied in some constraint
|
2010-04-07 14:40:46 +00:00
|
|
|
for (unsigned int b=0; b<nbBodies; b++) {
|
2010-04-12 22:27:50 +00:00
|
|
|
Body* body = constraintBodies.at(b);
|
|
|
|
unsigned int bodyNumber = bodyNumberMapping.at(body);
|
|
|
|
|
|
|
|
// TODO : Use polymorphism and remove this casting
|
|
|
|
RigidBody* rigidBody = dynamic_cast<RigidBody*>(body);
|
|
|
|
assert(rigidBody != 0);
|
|
|
|
|
2010-04-07 14:40:46 +00:00
|
|
|
// Compute the vector with velocities values
|
2010-04-12 22:27:50 +00:00
|
|
|
V.fillInSubVector(bodyNumber*6, rigidBody->getCurrentBodyState()->getLinearVelocity());
|
|
|
|
V.fillInSubVector(bodyNumber*6+3, rigidBody->getCurrentBodyState()->getAngularVelocity());
|
|
|
|
|
|
|
|
// Compute the vector with forces and torques values
|
|
|
|
Fext.fillInSubVector(bodyNumber*6, rigidBody->getCurrentBodyState()->getExternalForce());
|
|
|
|
Fext.fillInSubVector(bodyNumber*6+3, rigidBody->getCurrentBodyState()->getExternalTorque());
|
|
|
|
|
|
|
|
// Compute the inverse sparse mass matrix
|
|
|
|
Minv_sp.fillInSubMatrix(b*6, 0, rigidBody->getCurrentBodyState().getMassInverse().getValue() * Matrix::identity(3));
|
|
|
|
Minv_sp.fillInSubMatrix(b*6+3, 3, rigidBody->getCurrentBodyState().getInertiaTensorInverse());
|
2010-03-17 19:19:11 +00:00
|
|
|
}
|
|
|
|
}
|
2010-02-15 23:27:35 +00:00
|
|
|
|
2010-03-17 19:19:11 +00:00
|
|
|
// Free the memory that was allocated in the allocate() method
|
|
|
|
void ConstraintSolver::freeMemory() {
|
2010-04-07 14:40:46 +00:00
|
|
|
|
|
|
|
activeConstraints.clear();
|
|
|
|
bodyNumberMapping.clear();
|
|
|
|
|
2010-03-17 19:19:11 +00:00
|
|
|
// Free the bodyMaping array
|
|
|
|
for (unsigned int i=0; i<nbBodies; i++) {
|
|
|
|
delete[] bodyMapping[i];
|
|
|
|
}
|
|
|
|
delete[] bodyMapping;
|
2010-02-15 23:27:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Solve the current LCP problem
|
2010-04-07 14:40:46 +00:00
|
|
|
void ConstraintSolver::solve(double dt) {
|
2010-03-17 19:19:11 +00:00
|
|
|
// Allocate memory for the matrices
|
2010-04-07 14:40:46 +00:00
|
|
|
allocate();
|
2010-03-17 19:19:11 +00:00
|
|
|
|
|
|
|
// Fill-in all the matrices needed to solve the LCP problem
|
|
|
|
fillInMatrices();
|
|
|
|
|
|
|
|
// Solve the LCP problem (computation of lambda)
|
|
|
|
lcpSolver.solve(A, b, lowLimits, highLimits, lambda);
|
2010-02-15 23:27:35 +00:00
|
|
|
|
2010-03-17 19:19:11 +00:00
|
|
|
// TODO : Implement this method ...
|
2010-04-07 14:40:46 +00:00
|
|
|
|
|
|
|
freeMemory();
|
2010-02-15 23:27:35 +00:00
|
|
|
}
|