Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 2484 was 2484, checked in by rgrieder, 15 years ago

Bugfix within CompoundCollisionShape.

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