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, 15 years ago

Resolved the issue with the collision shape synchronisation.

  • Property svn:eol-style set to native
File size: 6.9 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            // Detatch all children first
57            this->removeAllChildShapes();
58            delete this->compoundShape_;
59        }
60    }
61
62    void CompoundCollisionShape::XMLPort(Element& xmlelement, XMLPort::Mode mode)
63    {
64        SUPER(CompoundCollisionShape, XMLPort, xmlelement, mode);
65        // Attached collision shapes
66        XMLPortObject(CompoundCollisionShape, CollisionShape, "", addChildShape, getChildShape, xmlelement, mode);
67    }
68
69    void CompoundCollisionShape::setWorldEntityParent(WorldEntity* parent)
70    {
71        // suppress synchronisation
72        this->setObjectMode(0x0);
73
74        this->worldEntityParent_ = parent;
75    }
76
77    void CompoundCollisionShape::addChildShape(CollisionShape* shape)
78    {
79        if (!shape || static_cast<CollisionShape*>(this) == shape)
80            return;
81        if (this->childShapes_.find(shape) != this->childShapes_.end())
82        {
83            CCOUT(2) << "Warning: Attaching a CollisionShape twice is not yet supported." << std::endl;
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
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());
106    }
107
108    void CompoundCollisionShape::removeChildShape(CollisionShape* shape)
109    {
110        if (this->childShapes_.find(shape) != this->childShapes_.end())
111        {
112            shape->setParent(0, OBJECTID_UNKNOWN);
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    {
129        if (!shape)
130            return;
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        }
137
138        // Remove old btCollisionShape, stored in the children map
139        if (it->second)
140            this->compoundShape_->removeChildShape(it->second);
141        if (shape->getCollisionShape())
142        {
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());
146            it->second = shape->getCollisionShape();
147        }
148
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)
160            {
161                bEmpty = false;
162                if (!it->first->hasTransform())
163                    primitive = it->second;
164                else
165                    bPrimitive = false;
166            }
167        }
168        if (bEmpty)
169        {
170            if (this->collisionShape_ == 0)
171            {
172                this->collisionShape_ = 0;
173                return;
174            }
175            this->collisionShape_ = 0;
176        }
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    }
189
190    void CompoundCollisionShape::updateParent()
191    {
192        if (this->parent_)
193            this->parent_->updateChildShape(this);
194        else if (this->worldEntityParent_)
195            this->worldEntityParent_->notifyCollisionShapeChanged();
196    }
197
198    CollisionShape* CompoundCollisionShape::getChildShape(unsigned int index) const
199    {
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;
208    }
209}
Note: See TracBrowser for help on using the repository browser.