Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentationFS14/src/orxonox/collisionshapes/CompoundCollisionShape.cc @ 10179

Last change on this file since 10179 was 10073, checked in by smerkli, 11 years ago

Merged modularships branch

  • Property svn:eol-style set to native
File size: 11.3 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
[10073]42#include "collisionshapes/WorldEntityCollisionShape.h"
43
[2303]44namespace orxonox
45{
[9667]46    RegisterClass(CompoundCollisionShape);
[2303]47
[8706]48    /**
49    @brief
50        Constructor. Registers and initializes the object.
51    */
[9667]52    CompoundCollisionShape::CompoundCollisionShape(Context* context) : CollisionShape(context)
[2303]53    {
54        RegisterObject(CompoundCollisionShape);
55
56        this->compoundShape_  = new btCompoundShape();
57    }
58
[8706]59    /**
60    @brief
61        Destructor.
62        Deletes all its children.
63    */
[2303]64    CompoundCollisionShape::~CompoundCollisionShape()
65    {
66        if (this->isInitialized())
[2423]67        {
[2514]68            // Delete all children
[2527]69            for (std::map<CollisionShape*, btCollisionShape*>::iterator it = this->attachedShapes_.begin();
70                it != this->attachedShapes_.end(); ++it)
[2477]71            {
[2514]72                // make sure that the child doesn't want to detach itself --> speedup because of the missing update
[2562]73                it->first->notifyDetached();
[5929]74                it->first->destroy();
[2477]75            }
76
[2303]77            delete this->compoundShape_;
[2423]78        }
[2303]79    }
80
81    void CompoundCollisionShape::XMLPort(Element& xmlelement, XMLPort::Mode mode)
82    {
83        SUPER(CompoundCollisionShape, XMLPort, xmlelement, mode);
[2374]84        // Attached collision shapes
[2527]85        XMLPortObject(CompoundCollisionShape, CollisionShape, "", attach, detach, xmlelement, mode);
[2303]86    }
87
[8706]88    /**
89    @brief
90        Attach the input CollisionShape to the CompoundCollisionShape.
91    @param shape
92        A pointer to the CollisionShape that is to be attached.
93    */
[2527]94    void CompoundCollisionShape::attach(CollisionShape* shape)
[2374]95    {
[8706]96        // If either the input shape is NULL or we try to attach the CollisionShape to itself.
[2423]97        if (!shape || static_cast<CollisionShape*>(this) == shape)
98            return;
[8706]99
[2527]100        if (this->attachedShapes_.find(shape) != this->attachedShapes_.end())
[2423]101        {
[8858]102            orxout(internal_warning) << "Attaching a CollisionShape twice is not yet supported." << endl;
[2423]103            return;
104        }
[2562]105
[8706]106        // Notify the CollisionShape that it is being attached to the CompoundCollisionShape.
[2562]107        if (!shape->notifyBeingAttached(this))
108            return;
109
[8706]110        // Attach it.
[2527]111        this->attachedShapes_[shape] = shape->getCollisionShape();
[2423]112
[8706]113        // Only actually attach if we didn't pick a CompoundCollisionShape with no content.
[2423]114        if (shape->getCollisionShape())
115        {
[3196]116            btTransform transf(multi_cast<btQuaternion>(shape->getOrientation()), multi_cast<btVector3>(shape->getPosition()));
[8706]117            // Add the btCollisionShape of the CollisionShape as a child shape to the btCompoundShape of the CompoundCollisionShape.
[2423]118            this->compoundShape_->addChildShape(transf, shape->getCollisionShape());
119
120            this->updatePublicShape();
121        }
[10073]122
123        // If the shape to be attached is not a CompoundCollisionShape (thus an actual physical shape) & this is a WorldEntity's CollisionShape,
124        // set it's userPointer to the WorldEntity this CompoundCollisionShape belongs to.
125        if (!orxonox_cast<CompoundCollisionShape*>(shape) && orxonox_cast<WorldEntityCollisionShape*>(this))
126            shape->getCollisionShape()->setUserPointer(orxonox_cast<WorldEntityCollisionShape*>(this)->getWorldEntityOwner());
[2374]127    }
128
[8706]129    /**
130    @brief
131        Detach the input CollisionShape form the CompoundCollisionShape.
132    @param shape
133        A pointer to the CollisionShape to be detached.
134    */
[2527]135    void CompoundCollisionShape::detach(CollisionShape* shape)
[2303]136    {
[8706]137        // If the input CollisionShape is actually attached.
[2527]138        if (this->attachedShapes_.find(shape) != this->attachedShapes_.end())
[2423]139        {
[2527]140            this->attachedShapes_.erase(shape);
[2423]141            if (shape->getCollisionShape())
[8706]142                this->compoundShape_->removeChildShape(shape->getCollisionShape()); // TODO: Apparently this is broken?
[2562]143            shape->notifyDetached();
[2423]144
145            this->updatePublicShape();
146        }
[2527]147        else
[8858]148            orxout(internal_warning) << "Cannot detach non child collision shape" << endl;
[2423]149    }
150
[8706]151    /**
152    @brief
153        Detach all attached CollisionShapes.
154    */
[2527]155    void CompoundCollisionShape::detachAll()
[2423]156    {
[2527]157        while (this->attachedShapes_.size() > 0)
158            this->detach(this->attachedShapes_.begin()->first);
[2423]159    }
160
[8706]161    /**
162    @brief
163        Update the input CollisionShape that is attached to the CompoundCollisionShape.
164        This is called when the input shape's internal collision shape (a btCollisionShape) has changed.
165    @param shape
166        A pointer to the CollisionShape to be updated.
167    */
[2527]168    void CompoundCollisionShape::updateAttachedShape(CollisionShape* shape)
[2423]169    {
[2403]170        if (!shape)
[2374]171            return;
[8706]172
[2527]173        std::map<CollisionShape*, btCollisionShape*>::iterator it = this->attachedShapes_.find(shape);
[8706]174        // Check whether the input shape belongs to this CompoundCollisionShape.
[2527]175        if (it == this->attachedShapes_.end())
[2423]176        {
[8858]177            orxout(internal_warning) << "Cannot update child shape: Instance not a child." << endl;
[2423]178            return;
179        }
[2374]180
[2423]181        // Remove old btCollisionShape, stored in the children map
182        if (it->second)
[8706]183            this->compoundShape_->removeChildShape(it->second); // TODO: Apparently this is broken?
184
185        // Only actually attach if we didn't pick a CompoundCollisionShape with no content
[2403]186        if (shape->getCollisionShape())
[2374]187        {
[3196]188            btTransform transf(multi_cast<btQuaternion>(shape->getOrientation()), multi_cast<btVector3>(shape->getPosition()));
[2403]189            this->compoundShape_->addChildShape(transf, shape->getCollisionShape());
[2423]190            it->second = shape->getCollisionShape();
191        }
[2403]192
[2423]193        this->updatePublicShape();
194    }
195
[8706]196    /**
197    @brief
198        Updates the public shape, the collision shape this CompoundCollisionShape has to the outside.
199    */
[2423]200    void CompoundCollisionShape::updatePublicShape()
201    {
[8706]202        btCollisionShape* primitive = 0; // The primitive shape, if there is one.
203        bool bPrimitive = true; // Whether the CompoundCollisionShape has just one non-empty CollisionShape. And that shape also has no transformation.
204        bool bEmpty = true; // Whether the CompoundCollisionShape is empty.
205        // Iterate over all CollisionShapes that belong to this CompoundCollisionShape.
[2527]206        for (std::map<CollisionShape*, btCollisionShape*>::const_iterator it = this->attachedShapes_.begin(); it != this->attachedShapes_.end(); ++it)
[2423]207        {
[8706]208            // TODO: Make sure this is correct.
[2423]209            if (it->second)
[2403]210            {
[2423]211                bEmpty = false;
[8706]212                if (!it->first->hasTransform() && bPrimitive)
[2423]213                    primitive = it->second;
214                else
[8706]215                {
[2423]216                    bPrimitive = false;
[8706]217                    break;
218                }
[2403]219            }
[2374]220        }
[8706]221
222        // If there is no non-empty CollisionShape.
[2423]223        if (bEmpty)
[2463]224        {
[8706]225            // If there was none all along, nothing needs to be changed.
[2463]226            if (this->collisionShape_ == 0)
227                return;
[2423]228            this->collisionShape_ = 0;
[2463]229        }
[8706]230        // If the CompoundCollisionShape is just a primitive.
231        // Only one shape to be added, no transform; return it directly.
[2423]232        else if (bPrimitive)
233            this->collisionShape_ = primitive;
[8706]234        // Make sure we use the compound shape when returning a btCollisionShape.
[2423]235        else
236            this->collisionShape_ = this->compoundShape_;
[8706]237
[2423]238        this->updateParent();
239    }
[2374]240
[8706]241    /**
242    @brief
243        Get the attached CollisionShape at the given index.
244    @param index
245        The index of the desired CollisionShape.
246    @return
247        Returns a pointer to the attached CollisionShape at the given index.
248    */
[2527]249    CollisionShape* CompoundCollisionShape::getAttachedShape(unsigned int index) const
[2303]250    {
[2423]251        unsigned int i = 0;
[2527]252        for (std::map<CollisionShape*, btCollisionShape*>::const_iterator it = this->attachedShapes_.begin(); it != this->attachedShapes_.end(); ++it)
[2423]253        {
254            if (i == index)
255                return it->first;
256            ++i;
257        }
258        return 0;
[2303]259    }
[8706]260
261    /**
262    @brief
263        Is called when the scale of the CompoundCollisionShape has changed.
264        Iterates over all attached CollisionShapes and scales them, then recomputes their compound shape.
265    */
266    void CompoundCollisionShape::changedScale()
267    {
268        CollisionShape::changedScale();
269
270        std::vector<CollisionShape*> shapes;
271        // Iterate through all attached CollisionShapes and add them to the list of shapes.
272        for(std::map<CollisionShape*, btCollisionShape*>::iterator it = this->attachedShapes_.begin(); it != this->attachedShapes_.end(); it++)
273            shapes.push_back(it->first);
274
275        // Delete the compound shape and create a new one.
276        delete this->compoundShape_;
277        this->compoundShape_ = new btCompoundShape();
278
279        // Re-attach all CollisionShapes.
280        for(std::vector<CollisionShape*>::iterator it = shapes.begin(); it != shapes.end(); it++)
281        {
282            CollisionShape* shape = *it;
283            shape->setScale3D(this->getScale3D());
284            // Only actually attach if we didn't pick a CompoundCollisionShape with no content.
285            if (shape->getCollisionShape())
286            {
287                btTransform transf(multi_cast<btQuaternion>(shape->getOrientation()), multi_cast<btVector3>(shape->getPosition()));
288                // Add the btCollisionShape of the CollisionShape as a child shape to the btCompoundShape of the CompoundCollisionShape.
289                this->compoundShape_->addChildShape(transf, shape->getCollisionShape());
290            }
291        }
292
293        this->updatePublicShape();
294
295        /*
296        // Iterate through all attached CollisionShapes
297        for(std::map<CollisionShape*, btCollisionShape*>::const_iterator it = this->attachedShapes_.begin(); it != this->attachedShapes_.end(); it++)
298        {
299            // Rescale the CollisionShape.
300            it->first->setScale3D(this->getScale3D());
301            this->updateAttachedShape(it->first);
302        }
303
304        this->updatePublicShape();*/
305    }
[10073]306
307    int CompoundCollisionShape::getNumChildShapes()
308    {
309        return this->compoundShape_->getNumChildShapes();
310    }
[2303]311}
Note: See TracBrowser for help on using the repository browser.