Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/collisionshapes/CompoundCollisionShape.cc @ 10652

Last change on this file since 10652 was 10624, checked in by landauf, 9 years ago

merged branch core7 back to trunk

  • Property svn:eol-style set to native
File size: 11.0 KB
RevLine 
[2303]1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
[2304]23 *      Reto Grieder
[2303]24 *   Co-authors:
25 *      ...
26 *
27 */
28
[8706]29/**
30    @file CompoundCollisionShape.cc
31    @brief Implementation of the CompoundCollisionShape class.
32*/
33
[2303]34#include "CompoundCollisionShape.h"
35
[3196]36#include <BulletCollision/CollisionShapes/btCompoundShape.h>
[2303]37
38#include "core/CoreIncludes.h"
39#include "core/XMLPort.h"
40#include "tools/BulletConversions.h"
41
42namespace orxonox
43{
[9667]44    RegisterClass(CompoundCollisionShape);
[2303]45
[8706]46    /**
47    @brief
48        Constructor. Registers and initializes the object.
49    */
[9667]50    CompoundCollisionShape::CompoundCollisionShape(Context* context) : CollisionShape(context)
[2303]51    {
52        RegisterObject(CompoundCollisionShape);
53
54        this->compoundShape_  = new btCompoundShape();
55    }
56
[8706]57    /**
58    @brief
59        Destructor.
60        Deletes all its children.
61    */
[2303]62    CompoundCollisionShape::~CompoundCollisionShape()
63    {
64        if (this->isInitialized())
[2423]65        {
[2514]66            // Delete all children
[2527]67            for (std::map<CollisionShape*, btCollisionShape*>::iterator it = this->attachedShapes_.begin();
68                it != this->attachedShapes_.end(); ++it)
[2477]69            {
[2514]70                // make sure that the child doesn't want to detach itself --> speedup because of the missing update
[2562]71                it->first->notifyDetached();
[5929]72                it->first->destroy();
[10624]73                if (this->collisionShape_ == it->second)
74                    this->collisionShape_ = NULL; // don't destroy it twice
[2477]75            }
76
[2303]77            delete this->compoundShape_;
[10624]78            if (this->collisionShape_ == this->compoundShape_)
79                this->collisionShape_ = NULL; // don't destroy it twice
[2423]80        }
[2303]81    }
82
83    void CompoundCollisionShape::XMLPort(Element& xmlelement, XMLPort::Mode mode)
84    {
85        SUPER(CompoundCollisionShape, XMLPort, xmlelement, mode);
[2374]86        // Attached collision shapes
[2527]87        XMLPortObject(CompoundCollisionShape, CollisionShape, "", attach, detach, xmlelement, mode);
[2303]88    }
89
[8706]90    /**
91    @brief
92        Attach the input CollisionShape to the CompoundCollisionShape.
93    @param shape
94        A pointer to the CollisionShape that is to be attached.
95    */
[2527]96    void CompoundCollisionShape::attach(CollisionShape* shape)
[2374]97    {
[8706]98        // If either the input shape is NULL or we try to attach the CollisionShape to itself.
[2423]99        if (!shape || static_cast<CollisionShape*>(this) == shape)
100            return;
[8706]101
[2527]102        if (this->attachedShapes_.find(shape) != this->attachedShapes_.end())
[2423]103        {
[8858]104            orxout(internal_warning) << "Attaching a CollisionShape twice is not yet supported." << endl;
[2423]105            return;
106        }
[2562]107
[8706]108        // Notify the CollisionShape that it is being attached to the CompoundCollisionShape.
[2562]109        if (!shape->notifyBeingAttached(this))
110            return;
111
[8706]112        // Attach it.
[2527]113        this->attachedShapes_[shape] = shape->getCollisionShape();
[2423]114
[8706]115        // Only actually attach if we didn't pick a CompoundCollisionShape with no content.
[2423]116        if (shape->getCollisionShape())
117        {
[3196]118            btTransform transf(multi_cast<btQuaternion>(shape->getOrientation()), multi_cast<btVector3>(shape->getPosition()));
[8706]119            // Add the btCollisionShape of the CollisionShape as a child shape to the btCompoundShape of the CompoundCollisionShape.
[2423]120            this->compoundShape_->addChildShape(transf, shape->getCollisionShape());
121
122            this->updatePublicShape();
123        }
[2374]124    }
125
[8706]126    /**
127    @brief
128        Detach the input CollisionShape form the CompoundCollisionShape.
129    @param shape
130        A pointer to the CollisionShape to be detached.
131    */
[2527]132    void CompoundCollisionShape::detach(CollisionShape* shape)
[2303]133    {
[8706]134        // If the input CollisionShape is actually attached.
[2527]135        if (this->attachedShapes_.find(shape) != this->attachedShapes_.end())
[2423]136        {
[2527]137            this->attachedShapes_.erase(shape);
[2423]138            if (shape->getCollisionShape())
[8706]139                this->compoundShape_->removeChildShape(shape->getCollisionShape()); // TODO: Apparently this is broken?
[2562]140            shape->notifyDetached();
[2423]141
142            this->updatePublicShape();
143        }
[2527]144        else
[8858]145            orxout(internal_warning) << "Cannot detach non child collision shape" << endl;
[2423]146    }
147
[8706]148    /**
149    @brief
150        Detach all attached CollisionShapes.
151    */
[2527]152    void CompoundCollisionShape::detachAll()
[2423]153    {
[2527]154        while (this->attachedShapes_.size() > 0)
155            this->detach(this->attachedShapes_.begin()->first);
[2423]156    }
157
[8706]158    /**
159    @brief
160        Update the input CollisionShape that is attached to the CompoundCollisionShape.
161        This is called when the input shape's internal collision shape (a btCollisionShape) has changed.
162    @param shape
163        A pointer to the CollisionShape to be updated.
164    */
[2527]165    void CompoundCollisionShape::updateAttachedShape(CollisionShape* shape)
[2423]166    {
[2403]167        if (!shape)
[2374]168            return;
[8706]169
[2527]170        std::map<CollisionShape*, btCollisionShape*>::iterator it = this->attachedShapes_.find(shape);
[8706]171        // Check whether the input shape belongs to this CompoundCollisionShape.
[2527]172        if (it == this->attachedShapes_.end())
[2423]173        {
[8858]174            orxout(internal_warning) << "Cannot update child shape: Instance not a child." << endl;
[2423]175            return;
176        }
[2374]177
[2423]178        // Remove old btCollisionShape, stored in the children map
179        if (it->second)
[8706]180            this->compoundShape_->removeChildShape(it->second); // TODO: Apparently this is broken?
181
182        // Only actually attach if we didn't pick a CompoundCollisionShape with no content
[2403]183        if (shape->getCollisionShape())
[2374]184        {
[3196]185            btTransform transf(multi_cast<btQuaternion>(shape->getOrientation()), multi_cast<btVector3>(shape->getPosition()));
[2403]186            this->compoundShape_->addChildShape(transf, shape->getCollisionShape());
[2423]187            it->second = shape->getCollisionShape();
188        }
[2403]189
[2423]190        this->updatePublicShape();
191    }
192
[8706]193    /**
194    @brief
195        Updates the public shape, the collision shape this CompoundCollisionShape has to the outside.
196    */
[2423]197    void CompoundCollisionShape::updatePublicShape()
198    {
[8706]199        btCollisionShape* primitive = 0; // The primitive shape, if there is one.
200        bool bPrimitive = true; // Whether the CompoundCollisionShape has just one non-empty CollisionShape. And that shape also has no transformation.
201        bool bEmpty = true; // Whether the CompoundCollisionShape is empty.
202        // Iterate over all CollisionShapes that belong to this CompoundCollisionShape.
[2527]203        for (std::map<CollisionShape*, btCollisionShape*>::const_iterator it = this->attachedShapes_.begin(); it != this->attachedShapes_.end(); ++it)
[2423]204        {
[8706]205            // TODO: Make sure this is correct.
[2423]206            if (it->second)
[2403]207            {
[2423]208                bEmpty = false;
[8706]209                if (!it->first->hasTransform() && bPrimitive)
[2423]210                    primitive = it->second;
211                else
[8706]212                {
[2423]213                    bPrimitive = false;
[8706]214                    break;
215                }
[2403]216            }
[2374]217        }
[8706]218
219        // If there is no non-empty CollisionShape.
[2423]220        if (bEmpty)
[2463]221        {
[8706]222            // If there was none all along, nothing needs to be changed.
[2463]223            if (this->collisionShape_ == 0)
224                return;
[2423]225            this->collisionShape_ = 0;
[2463]226        }
[8706]227        // If the CompoundCollisionShape is just a primitive.
228        // Only one shape to be added, no transform; return it directly.
[2423]229        else if (bPrimitive)
230            this->collisionShape_ = primitive;
[8706]231        // Make sure we use the compound shape when returning a btCollisionShape.
[2423]232        else
233            this->collisionShape_ = this->compoundShape_;
[8706]234
[2423]235        this->updateParent();
236    }
[2374]237
[8706]238    /**
239    @brief
240        Get the attached CollisionShape at the given index.
241    @param index
242        The index of the desired CollisionShape.
243    @return
244        Returns a pointer to the attached CollisionShape at the given index.
245    */
[2527]246    CollisionShape* CompoundCollisionShape::getAttachedShape(unsigned int index) const
[2303]247    {
[2423]248        unsigned int i = 0;
[2527]249        for (std::map<CollisionShape*, btCollisionShape*>::const_iterator it = this->attachedShapes_.begin(); it != this->attachedShapes_.end(); ++it)
[2423]250        {
251            if (i == index)
252                return it->first;
253            ++i;
254        }
255        return 0;
[2303]256    }
[8706]257
258    /**
259    @brief
260        Is called when the scale of the CompoundCollisionShape has changed.
261        Iterates over all attached CollisionShapes and scales them, then recomputes their compound shape.
262    */
263    void CompoundCollisionShape::changedScale()
264    {
265        CollisionShape::changedScale();
266
267        std::vector<CollisionShape*> shapes;
268        // Iterate through all attached CollisionShapes and add them to the list of shapes.
269        for(std::map<CollisionShape*, btCollisionShape*>::iterator it = this->attachedShapes_.begin(); it != this->attachedShapes_.end(); it++)
270            shapes.push_back(it->first);
271
272        // Delete the compound shape and create a new one.
273        delete this->compoundShape_;
274        this->compoundShape_ = new btCompoundShape();
275
276        // Re-attach all CollisionShapes.
277        for(std::vector<CollisionShape*>::iterator it = shapes.begin(); it != shapes.end(); it++)
278        {
279            CollisionShape* shape = *it;
280            shape->setScale3D(this->getScale3D());
281            // Only actually attach if we didn't pick a CompoundCollisionShape with no content.
282            if (shape->getCollisionShape())
283            {
284                btTransform transf(multi_cast<btQuaternion>(shape->getOrientation()), multi_cast<btVector3>(shape->getPosition()));
285                // Add the btCollisionShape of the CollisionShape as a child shape to the btCompoundShape of the CompoundCollisionShape.
286                this->compoundShape_->addChildShape(transf, shape->getCollisionShape());
287            }
288        }
289
290        this->updatePublicShape();
291
292        /*
293        // Iterate through all attached CollisionShapes
294        for(std::map<CollisionShape*, btCollisionShape*>::const_iterator it = this->attachedShapes_.begin(); it != this->attachedShapes_.end(); it++)
295        {
296            // Rescale the CollisionShape.
297            it->first->setScale3D(this->getScale3D());
298            this->updateAttachedShape(it->first);
299        }
300
301        this->updatePublicShape();*/
302    }
[2303]303}
Note: See TracBrowser for help on using the repository browser.