worldsim/src/phycompoundobject.cpp

00001 /********************************************************************************
00002  *  WorldSim -- library for robot simulations                                   *
00003  *  Copyright (C) 2008-2011 Gianluca Massera <emmegian@yahoo.it>                *
00004  *                                                                              *
00005  *  This program is free software; you can redistribute it and/or modify        *
00006  *  it under the terms of the GNU General Public License as published by        *
00007  *  the Free Software Foundation; either version 2 of the License, or           *
00008  *  (at your option) any later version.                                         *
00009  *                                                                              *
00010  *  This program is distributed in the hope that it will be useful,             *
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of              *
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               *
00013  *  GNU General Public License for more details.                                *
00014  *                                                                              *
00015  *  You should have received a copy of the GNU General Public License           *
00016  *  along with this program; if not, write to the Free Software                 *
00017  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA  *
00018  ********************************************************************************/
00019 
00020 #include "phycompoundobject.h"
00021 #include "private/phyobjectprivate.h"
00022 #include "private/worldprivate.h"
00023 #include <QString>
00024 
00025 namespace farsa {
00026 
00027 PhyCompoundObject::PhyCompoundObject( QVector<PhyObject*> objs, World* w, QString name, const wMatrix& tm )
00028     : PhyObject( w, name, tm, false ) {
00029     bodyv = objs;
00030     for( int i=0; i<bodyv.size(); i++ ) {
00031         bodyv[i]->setOwner(this, true);
00032     }
00033     w->pushObject( this );
00034     createPrivateObject();
00035     changedMatrix();
00036 }
00037 
00038 PhyCompoundObject::~PhyCompoundObject() {
00039     // Owned objects are deleted by Ownable destructor
00040 #ifdef WORLDSIM_USE_NEWTON
00041     /* nothing to do */
00042 #endif
00043 }
00044 
00045 #ifdef WORLDSIM_USE_NEWTON
00046 NewtonCollision* copyCollisionHelper( NewtonWorld* world, NewtonCollision* toCopy, const wMatrix& offsetMatrix ) {
00047     NewtonCollisionInfoRecord collisionInfo;
00048     NewtonCollisionGetInfo( toCopy, &collisionInfo );
00049     switch( collisionInfo.m_collisionType ) {
00050     case SERIALIZE_ID_BOX:
00051         return NewtonCreateBox( world, collisionInfo.m_box.m_x, collisionInfo.m_box.m_y, collisionInfo.m_box.m_z, 1, &offsetMatrix[0][0] );
00052     case SERIALIZE_ID_CONE:
00053         return NewtonCreateCone( world, collisionInfo.m_cone.m_r, collisionInfo.m_cone.m_height, 1, &offsetMatrix[0][0] );
00054     case SERIALIZE_ID_SPHERE:
00055         return NewtonCreateSphere( world, collisionInfo.m_sphere.m_r0, collisionInfo.m_sphere.m_r1, collisionInfo.m_sphere.m_r2, 1, &offsetMatrix[0][0] );
00056     case SERIALIZE_ID_CAPSULE:
00057         return NewtonCreateCapsule( world, collisionInfo.m_capsule.m_r0, collisionInfo.m_capsule.m_height, 1, &offsetMatrix[0][0] );
00058     case SERIALIZE_ID_CYLINDER:
00059         return NewtonCreateCylinder( world, collisionInfo.m_cylinder.m_r0, collisionInfo.m_cylinder.m_height, 1, &offsetMatrix[0][0] );
00060     default:
00061         qFatal( "A Primitive not supported by copyCollisionHelper has been used in creating a CompoundObject" );
00062         return NULL;
00063     }
00064     return NULL;
00065 }
00066 #endif
00067 
00068 void PhyCompoundObject::createPrivateObject() {
00069 #ifdef WORLDSIM_USE_NEWTON
00070     int dim = bodyv.size();
00071     NewtonCollision** temp = new NewtonCollision*[dim];
00072     real mass = 0.0;
00073     for( int i=0; i<dim; i++ ) {
00074         temp[i] = copyCollisionHelper( worldpriv->world, bodyv[i]->priv->collision, bodyv[i]->matrix() );
00075         mass += bodyv[i]->mass();
00076         world()->popObject( bodyv[i] );
00077     }
00078     NewtonCollision* c = NewtonCreateCompoundCollision( worldpriv->world, dim, temp, 1 );
00079     wMatrix initialTransformationMatrix = wMatrix::identity(); // The transformation matrix is set in other places
00080     priv->body = NewtonCreateBody( worldpriv->world, c, &initialTransformationMatrix[0][0] );
00081     priv->collision = c;
00082     for( int i=0; i<dim; i++ ) {
00083         NewtonDestroyBody( worldpriv->world, bodyv[i]->priv->body );
00084         NewtonReleaseCollision( worldpriv->world, bodyv[i]->priv->collision );
00085         bodyv[i]->priv->body = NULL;
00086         bodyv[i]->priv->collision = NULL;
00087         NewtonReleaseCollision( worldpriv->world, temp[i] );
00088     }
00089     delete []temp;
00090     NewtonBodySetAutoSleep( priv->body, 0 );
00091     setMass( mass );
00092     NewtonBodySetUserData( priv->body, this );
00093     NewtonBodySetLinearDamping( priv->body, 0.0 );
00094     wVector zero = wVector(0,0,0,0);
00095     NewtonBodySetAngularDamping( priv->body, &zero[0] );
00096     NewtonBodySetAutoSleep( priv->body, 0 );
00097     NewtonBodySetFreezeState( priv->body, 0 );
00098     // Sets the signal-wrappers callback
00099     NewtonBodySetTransformCallback( priv->body, (PhyObjectPrivate::setTransformHandler) );
00100     NewtonBodySetForceAndTorqueCallback( priv->body, (PhyObjectPrivate::applyForceAndTorqueHandler) );
00101 #endif
00102 }
00103 
00104 } // end namespace farsa