Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation/src/orxonox/objects/collisionshapes/CompoundCollisionShape.cc @ 2469

Last change on this file since 2469 was 2469, checked in by rgrieder, 17 years ago

Resolved the issue with the collision shape synchronisation.

  • Property svn:eol-style set to native
File size: 6.9 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"
[2423]38#include "objects/worldentities/WorldEntity.h"
[2303]39
40namespace orxonox
41{
42    CreateFactory(CompoundCollisionShape);
43
44    CompoundCollisionShape::CompoundCollisionShape(BaseObject* creator) : CollisionShape(creator)
45    {
46        RegisterObject(CompoundCollisionShape);
47
48        this->compoundShape_  = new btCompoundShape();
[2469]49        this->worldEntityParent_ = 0;
[2303]50    }
51
52    CompoundCollisionShape::~CompoundCollisionShape()
53    {
54        if (this->isInitialized())
[2423]55        {
56            // Detatch all children first
57            this->removeAllChildShapes();
[2303]58            delete this->compoundShape_;
[2423]59        }
[2303]60    }
61
62    void CompoundCollisionShape::XMLPort(Element& xmlelement, XMLPort::Mode mode)
63    {
64        SUPER(CompoundCollisionShape, XMLPort, xmlelement, mode);
[2374]65        // Attached collision shapes
[2303]66        XMLPortObject(CompoundCollisionShape, CollisionShape, "", addChildShape, getChildShape, xmlelement, mode);
67    }
68
[2469]69    void CompoundCollisionShape::setWorldEntityParent(WorldEntity* parent)
70    {
71        // suppress synchronisation
72        this->setObjectMode(0x0);
73
74        this->worldEntityParent_ = parent;
75    }
76
[2423]77    void CompoundCollisionShape::addChildShape(CollisionShape* shape)
[2374]78    {
[2423]79        if (!shape || static_cast<CollisionShape*>(this) == shape)
80            return;
81        if (this->childShapes_.find(shape) != this->childShapes_.end())
82        {
[2433]83            CCOUT(2) << "Warning: Attaching a CollisionShape twice is not yet supported." << std::endl;
[2423]84            return;
85        }
86        this->childShapes_[shape] = shape->getCollisionShape();
87
88        if (shape->getCollisionShape())
89        {
90            // Only actually attach if we didn't pick a CompoundCollisionShape with no content
91            btTransform transf(omni_cast<btQuaternion>(shape->getOrientation()), omni_cast<btVector3>(shape->getPosition()));
92            this->compoundShape_->addChildShape(transf, shape->getCollisionShape());
93
94            this->updatePublicShape();
95        }
96
97        // network synchro
[2469]98        if (this->worldEntityParent_)
99        {
100            // This compound collision shape belongs to a WE and doesn't get synchronised
101            // So set our parent to be the WE
102            shape->setParent(this, this->worldEntityParent_->getObjectID());
103        }
104        else
105            shape->setParent(this, this->getObjectID());
[2374]106    }
107
[2423]108    void CompoundCollisionShape::removeChildShape(CollisionShape* shape)
[2303]109    {
[2423]110        if (this->childShapes_.find(shape) != this->childShapes_.end())
111        {
[2445]112            shape->setParent(0, OBJECTID_UNKNOWN);
[2423]113            this->childShapes_.erase(shape);
114            if (shape->getCollisionShape())
115                this->compoundShape_->removeChildShape(shape->getCollisionShape());
116
117            this->updatePublicShape();
118        }
119    }
120
121    void CompoundCollisionShape::removeAllChildShapes()
122    {
123        while (this->childShapes_.size() > 0)
124            this->removeChildShape(this->childShapes_.begin()->first);
125    }
126
127    void CompoundCollisionShape::updateChildShape(CollisionShape* shape)
128    {
[2403]129        if (!shape)
[2374]130            return;
[2423]131        std::map<CollisionShape*, btCollisionShape*>::iterator it = this->childShapes_.find(shape);
132        if (it == this->childShapes_.end())
133        {
134            CCOUT(2) << "Warning: Cannot update child shape: Instance not a child." << std::endl;
135            return;
136        }
[2374]137
[2423]138        // Remove old btCollisionShape, stored in the children map
139        if (it->second)
140            this->compoundShape_->removeChildShape(it->second);
[2403]141        if (shape->getCollisionShape())
[2374]142        {
[2403]143            // Only actually attach if we didn't pick a CompoundCollisionShape with no content
144            btTransform transf(omni_cast<btQuaternion>(shape->getOrientation()), omni_cast<btVector3>(shape->getPosition()));
145            this->compoundShape_->addChildShape(transf, shape->getCollisionShape());
[2423]146            it->second = shape->getCollisionShape();
147        }
[2403]148
[2423]149        this->updatePublicShape();
150    }
151
152    void CompoundCollisionShape::updatePublicShape()
153    {
154        btCollisionShape* primitive = 0;
155        bool bPrimitive = true;
156        bool bEmpty = true;
157        for (std::map<CollisionShape*, btCollisionShape*>::const_iterator it = this->childShapes_.begin(); it != this->childShapes_.end(); ++it)
158        {
159            if (it->second)
[2403]160            {
[2423]161                bEmpty = false;
162                if (!it->first->hasTransform())
163                    primitive = it->second;
164                else
165                    bPrimitive = false;
[2403]166            }
[2374]167        }
[2423]168        if (bEmpty)
[2463]169        {
170            if (this->collisionShape_ == 0)
171            {
172                this->collisionShape_ = 0;
173                return;
174            }
[2423]175            this->collisionShape_ = 0;
[2463]176        }
[2423]177        else if (bPrimitive)
178        {
179            // --> Only one shape to be added, no transform; return it directly
180            this->collisionShape_ = primitive;
181        }
182        else
183        {
184            // Make sure we use the compound shape when returning a btCollisionShape
185            this->collisionShape_ = this->compoundShape_;
186        }
187        this->updateParent();
188    }
[2374]189
[2423]190    void CompoundCollisionShape::updateParent()
191    {
192        if (this->parent_)
193            this->parent_->updateChildShape(this);
[2469]194        else if (this->worldEntityParent_)
195            this->worldEntityParent_->notifyCollisionShapeChanged();
[2303]196    }
197
198    CollisionShape* CompoundCollisionShape::getChildShape(unsigned int index) const
199    {
[2423]200        unsigned int i = 0;
201        for (std::map<CollisionShape*, btCollisionShape*>::const_iterator it = this->childShapes_.begin(); it != this->childShapes_.end(); ++it)
202        {
203            if (i == index)
204                return it->first;
205            ++i;
206        }
207        return 0;
[2303]208    }
209}
Note: See TracBrowser for help on using the repository browser.