Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 2527 was 2527, checked in by rgrieder, 15 years ago
  • Forgot to account for collision shape position and orientation when attaching WEs
  • addChildShape —> attach in CompoundCollisionShape
  • Fixed an issue which allowed WE's to detach themselves from non-parents.
  • Fixed an issue that occurred when physics was not active before attaching
  • Added some forgotten const in WE (physics)
  • When attaching WE's the child doesn't get modified anymore. Alternative: notifyDetached() and notifyBeingAttached(this) —> the child does the work
  • Property svn:eol-style set to native
File size: 7.4 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            // Delete all children
57            for (std::map<CollisionShape*, btCollisionShape*>::iterator it = this->attachedShapes_.begin();
58                it != this->attachedShapes_.end(); ++it)
59            {
60                // make sure that the child doesn't want to detach itself --> speedup because of the missing update
61                it->first->setParent(0, OBJECTID_UNKNOWN);
62                delete it->first;
63            }
64
65            delete this->compoundShape_;
66        }
67    }
68
69    void CompoundCollisionShape::XMLPort(Element& xmlelement, XMLPort::Mode mode)
70    {
71        SUPER(CompoundCollisionShape, XMLPort, xmlelement, mode);
72        // Attached collision shapes
73        XMLPortObject(CompoundCollisionShape, CollisionShape, "", attach, detach, xmlelement, mode);
74    }
75
76    void CompoundCollisionShape::setWorldEntityParent(WorldEntity* parent)
77    {
78        // suppress synchronisation
79        this->setObjectMode(0x0);
80
81        this->worldEntityParent_ = parent;
82    }
83
84    void CompoundCollisionShape::attach(CollisionShape* shape)
85    {
86        if (!shape || static_cast<CollisionShape*>(this) == shape)
87            return;
88        if (this->attachedShapes_.find(shape) != this->attachedShapes_.end())
89        {
90            CCOUT(2) << "Warning: Attaching a CollisionShape twice is not yet supported." << std::endl;
91            return;
92        }
93        this->attachedShapes_[shape] = shape->getCollisionShape();
94
95        if (shape->getCollisionShape())
96        {
97            // Only actually attach if we didn't pick a CompoundCollisionShape with no content
98            btTransform transf(omni_cast<btQuaternion>(shape->getOrientation()), omni_cast<btVector3>(shape->getPosition()));
99            this->compoundShape_->addChildShape(transf, shape->getCollisionShape());
100
101            this->updatePublicShape();
102        }
103
104        // network synchro
105        if (this->worldEntityParent_)
106        {
107            // This compound collision shape belongs to a WE and doesn't get synchronised
108            // So set our parent to be the WE
109            shape->setParent(this, this->worldEntityParent_->getObjectID());
110        }
111        else
112            shape->setParent(this, this->getObjectID());
113    }
114
115    void CompoundCollisionShape::detach(CollisionShape* shape)
116    {
117        if (this->attachedShapes_.find(shape) != this->attachedShapes_.end())
118        {
119            shape->setParent(0, OBJECTID_UNKNOWN);
120            this->attachedShapes_.erase(shape);
121            if (shape->getCollisionShape())
122                this->compoundShape_->removeChildShape(shape->getCollisionShape());
123
124            this->updatePublicShape();
125        }
126        else
127            CCOUT(2) << "Warning: Cannot detach non child collision shape" << std::endl;
128    }
129
130    void CompoundCollisionShape::detachAll()
131    {
132        while (this->attachedShapes_.size() > 0)
133            this->detach(this->attachedShapes_.begin()->first);
134    }
135
136    void CompoundCollisionShape::updateAttachedShape(CollisionShape* shape)
137    {
138        if (!shape)
139            return;
140        std::map<CollisionShape*, btCollisionShape*>::iterator it = this->attachedShapes_.find(shape);
141        if (it == this->attachedShapes_.end())
142        {
143            CCOUT(2) << "Warning: Cannot update child shape: Instance not a child." << std::endl;
144            return;
145        }
146
147        // Remove old btCollisionShape, stored in the children map
148        if (it->second)
149            this->compoundShape_->removeChildShape(it->second);
150        if (shape->getCollisionShape())
151        {
152            // Only actually attach if we didn't pick a CompoundCollisionShape with no content
153            btTransform transf(omni_cast<btQuaternion>(shape->getOrientation()), omni_cast<btVector3>(shape->getPosition()));
154            this->compoundShape_->addChildShape(transf, shape->getCollisionShape());
155            it->second = shape->getCollisionShape();
156        }
157
158        this->updatePublicShape();
159    }
160
161    void CompoundCollisionShape::updatePublicShape()
162    {
163        btCollisionShape* primitive = 0;
164        bool bPrimitive = true;
165        bool bEmpty = true;
166        for (std::map<CollisionShape*, btCollisionShape*>::const_iterator it = this->attachedShapes_.begin(); it != this->attachedShapes_.end(); ++it)
167        {
168            if (it->second)
169            {
170                bEmpty = false;
171                if (!it->first->hasTransform() && !bPrimitive)
172                    primitive = it->second;
173                else
174                    bPrimitive = false;
175            }
176        }
177        if (bEmpty)
178        {
179            if (this->collisionShape_ == 0)
180            {
181                this->collisionShape_ = 0;
182                return;
183            }
184            this->collisionShape_ = 0;
185        }
186        else if (bPrimitive)
187        {
188            // --> Only one shape to be added, no transform; return it directly
189            this->collisionShape_ = primitive;
190        }
191        else
192        {
193            // Make sure we use the compound shape when returning a btCollisionShape
194            this->collisionShape_ = this->compoundShape_;
195        }
196        this->updateParent();
197    }
198
199    void CompoundCollisionShape::updateParent()
200    {
201        if (this->parent_)
202            this->parent_->updateAttachedShape(this);
203        if (this->worldEntityParent_)
204            this->worldEntityParent_->notifyCollisionShapeChanged();
205    }
206
207    void CompoundCollisionShape::parentChanged()
208    {
209        if (!this->worldEntityParent_)
210            CollisionShape::parentChanged();
211    }
212
213    CollisionShape* CompoundCollisionShape::getAttachedShape(unsigned int index) const
214    {
215        unsigned int i = 0;
216        for (std::map<CollisionShape*, btCollisionShape*>::const_iterator it = this->attachedShapes_.begin(); it != this->attachedShapes_.end(); ++it)
217        {
218            if (i == index)
219                return it->first;
220            ++i;
221        }
222        return 0;
223    }
224}
Note: See TracBrowser for help on using the repository browser.