Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 2926 was 2662, checked in by rgrieder, 16 years ago

Merged presentation branch back to trunk.

  • Property svn:eol-style set to native
File size: 6.5 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
29#include "OrxonoxStableHeaders.h"
30#include "CompoundCollisionShape.h"
31
32#include "BulletCollision/CollisionShapes/btCompoundShape.h"
33
[2423]34#include "util/Exception.h"
[2303]35#include "core/CoreIncludes.h"
36#include "core/XMLPort.h"
37#include "tools/BulletConversions.h"
38
39namespace orxonox
40{
41    CreateFactory(CompoundCollisionShape);
42
43    CompoundCollisionShape::CompoundCollisionShape(BaseObject* creator) : CollisionShape(creator)
44    {
45        RegisterObject(CompoundCollisionShape);
46
47        this->compoundShape_  = new btCompoundShape();
48    }
49
50    CompoundCollisionShape::~CompoundCollisionShape()
51    {
52        if (this->isInitialized())
[2423]53        {
[2514]54            // Delete all children
[2527]55            for (std::map<CollisionShape*, btCollisionShape*>::iterator it = this->attachedShapes_.begin();
56                it != this->attachedShapes_.end(); ++it)
[2477]57            {
[2514]58                // make sure that the child doesn't want to detach itself --> speedup because of the missing update
[2562]59                it->first->notifyDetached();
[2514]60                delete it->first;
[2477]61            }
62
[2303]63            delete this->compoundShape_;
[2423]64        }
[2303]65    }
66
67    void CompoundCollisionShape::XMLPort(Element& xmlelement, XMLPort::Mode mode)
68    {
69        SUPER(CompoundCollisionShape, XMLPort, xmlelement, mode);
[2374]70        // Attached collision shapes
[2527]71        XMLPortObject(CompoundCollisionShape, CollisionShape, "", attach, detach, xmlelement, mode);
[2303]72    }
73
[2527]74    void CompoundCollisionShape::attach(CollisionShape* shape)
[2374]75    {
[2423]76        if (!shape || static_cast<CollisionShape*>(this) == shape)
77            return;
[2527]78        if (this->attachedShapes_.find(shape) != this->attachedShapes_.end())
[2423]79        {
[2433]80            CCOUT(2) << "Warning: Attaching a CollisionShape twice is not yet supported." << std::endl;
[2423]81            return;
82        }
[2562]83
84        if (!shape->notifyBeingAttached(this))
85            return;
86
[2527]87        this->attachedShapes_[shape] = shape->getCollisionShape();
[2423]88
89        if (shape->getCollisionShape())
90        {
91            // Only actually attach if we didn't pick a CompoundCollisionShape with no content
92            btTransform transf(omni_cast<btQuaternion>(shape->getOrientation()), omni_cast<btVector3>(shape->getPosition()));
93            this->compoundShape_->addChildShape(transf, shape->getCollisionShape());
94
95            this->updatePublicShape();
96        }
[2374]97    }
98
[2527]99    void CompoundCollisionShape::detach(CollisionShape* shape)
[2303]100    {
[2527]101        if (this->attachedShapes_.find(shape) != this->attachedShapes_.end())
[2423]102        {
[2527]103            this->attachedShapes_.erase(shape);
[2423]104            if (shape->getCollisionShape())
105                this->compoundShape_->removeChildShape(shape->getCollisionShape());
[2562]106            shape->notifyDetached();
[2423]107
108            this->updatePublicShape();
109        }
[2527]110        else
111            CCOUT(2) << "Warning: Cannot detach non child collision shape" << std::endl;
[2423]112    }
113
[2527]114    void CompoundCollisionShape::detachAll()
[2423]115    {
[2527]116        while (this->attachedShapes_.size() > 0)
117            this->detach(this->attachedShapes_.begin()->first);
[2423]118    }
119
[2527]120    void CompoundCollisionShape::updateAttachedShape(CollisionShape* shape)
[2423]121    {
[2403]122        if (!shape)
[2374]123            return;
[2527]124        std::map<CollisionShape*, btCollisionShape*>::iterator it = this->attachedShapes_.find(shape);
125        if (it == this->attachedShapes_.end())
[2423]126        {
127            CCOUT(2) << "Warning: Cannot update child shape: Instance not a child." << std::endl;
128            return;
129        }
[2374]130
[2423]131        // Remove old btCollisionShape, stored in the children map
132        if (it->second)
133            this->compoundShape_->removeChildShape(it->second);
[2403]134        if (shape->getCollisionShape())
[2374]135        {
[2403]136            // Only actually attach if we didn't pick a CompoundCollisionShape with no content
137            btTransform transf(omni_cast<btQuaternion>(shape->getOrientation()), omni_cast<btVector3>(shape->getPosition()));
138            this->compoundShape_->addChildShape(transf, shape->getCollisionShape());
[2423]139            it->second = shape->getCollisionShape();
140        }
[2403]141
[2423]142        this->updatePublicShape();
143    }
144
145    void CompoundCollisionShape::updatePublicShape()
146    {
147        btCollisionShape* primitive = 0;
148        bool bPrimitive = true;
149        bool bEmpty = true;
[2527]150        for (std::map<CollisionShape*, btCollisionShape*>::const_iterator it = this->attachedShapes_.begin(); it != this->attachedShapes_.end(); ++it)
[2423]151        {
152            if (it->second)
[2403]153            {
[2423]154                bEmpty = false;
[2484]155                if (!it->first->hasTransform() && !bPrimitive)
[2423]156                    primitive = it->second;
157                else
158                    bPrimitive = false;
[2403]159            }
[2374]160        }
[2423]161        if (bEmpty)
[2463]162        {
163            if (this->collisionShape_ == 0)
164            {
165                this->collisionShape_ = 0;
166                return;
167            }
[2423]168            this->collisionShape_ = 0;
[2463]169        }
[2423]170        else if (bPrimitive)
171        {
172            // --> Only one shape to be added, no transform; return it directly
173            this->collisionShape_ = primitive;
174        }
175        else
176        {
177            // Make sure we use the compound shape when returning a btCollisionShape
178            this->collisionShape_ = this->compoundShape_;
179        }
180        this->updateParent();
181    }
[2374]182
[2527]183    CollisionShape* CompoundCollisionShape::getAttachedShape(unsigned int index) const
[2303]184    {
[2423]185        unsigned int i = 0;
[2527]186        for (std::map<CollisionShape*, btCollisionShape*>::const_iterator it = this->attachedShapes_.begin(); it != this->attachedShapes_.end(); ++it)
[2423]187        {
188            if (i == index)
189                return it->first;
190            ++i;
191        }
192        return 0;
[2303]193    }
194}
Note: See TracBrowser for help on using the repository browser.