Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 2515 was 2514, checked in by rgrieder, 16 years ago

Resolved four issues with the collision shapes:

  • NetworkCallback will of course not call functions virtually
  • CompoundCollisionShapes with a WorldEntity parent don't have to attach themselves when synchronised
  • Just in case: When changing a CollisionShape, the old gets destroyed AFTER everything has been updated
  • CompoundCollisionShapes with a WorldEntity parent now update both the WE and the CompoundCollisionShape when something changes

Other changes

  • Also replaced some redundant code while at it (updating the shapes was done individually)
  • Like in WE, the CompoundCollisionShape deletes all its children when being destroyed itself. This requires in WE that the children get detached before the CompoundCollisionShape gets deleted.
  • Property svn:eol-style set to native
File size: 7.3 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->childShapes_.begin();
58                it != this->childShapes_.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, "", addChildShape, getChildShape, 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::addChildShape(CollisionShape* shape)
85    {
86        if (!shape || static_cast<CollisionShape*>(this) == shape)
87            return;
88        if (this->childShapes_.find(shape) != this->childShapes_.end())
89        {
90            CCOUT(2) << "Warning: Attaching a CollisionShape twice is not yet supported." << std::endl;
91            return;
92        }
93        this->childShapes_[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::removeChildShape(CollisionShape* shape)
116    {
117        if (this->childShapes_.find(shape) != this->childShapes_.end())
118        {
119            shape->setParent(0, OBJECTID_UNKNOWN);
120            this->childShapes_.erase(shape);
121            if (shape->getCollisionShape())
122                this->compoundShape_->removeChildShape(shape->getCollisionShape());
123
124            this->updatePublicShape();
125        }
126    }
127
128    void CompoundCollisionShape::removeAllChildShapes()
129    {
130        while (this->childShapes_.size() > 0)
131            this->removeChildShape(this->childShapes_.begin()->first);
132    }
133
134    void CompoundCollisionShape::updateChildShape(CollisionShape* shape)
135    {
136        if (!shape)
137            return;
138        std::map<CollisionShape*, btCollisionShape*>::iterator it = this->childShapes_.find(shape);
139        if (it == this->childShapes_.end())
140        {
141            CCOUT(2) << "Warning: Cannot update child shape: Instance not a child." << std::endl;
142            return;
143        }
144
145        // Remove old btCollisionShape, stored in the children map
146        if (it->second)
147            this->compoundShape_->removeChildShape(it->second);
148        if (shape->getCollisionShape())
149        {
150            // Only actually attach if we didn't pick a CompoundCollisionShape with no content
151            btTransform transf(omni_cast<btQuaternion>(shape->getOrientation()), omni_cast<btVector3>(shape->getPosition()));
152            this->compoundShape_->addChildShape(transf, shape->getCollisionShape());
153            it->second = shape->getCollisionShape();
154        }
155
156        this->updatePublicShape();
157    }
158
159    void CompoundCollisionShape::updatePublicShape()
160    {
161        btCollisionShape* primitive = 0;
162        bool bPrimitive = true;
163        bool bEmpty = true;
164        for (std::map<CollisionShape*, btCollisionShape*>::const_iterator it = this->childShapes_.begin(); it != this->childShapes_.end(); ++it)
165        {
166            if (it->second)
167            {
168                bEmpty = false;
169                if (!it->first->hasTransform() && !bPrimitive)
170                    primitive = it->second;
171                else
172                    bPrimitive = false;
173            }
174        }
175        if (bEmpty)
176        {
177            if (this->collisionShape_ == 0)
178            {
179                this->collisionShape_ = 0;
180                return;
181            }
182            this->collisionShape_ = 0;
183        }
184        else if (bPrimitive)
185        {
186            // --> Only one shape to be added, no transform; return it directly
187            this->collisionShape_ = primitive;
188        }
189        else
190        {
191            // Make sure we use the compound shape when returning a btCollisionShape
192            this->collisionShape_ = this->compoundShape_;
193        }
194        this->updateParent();
195    }
196
197    void CompoundCollisionShape::updateParent()
198    {
199        if (this->parent_)
200            this->parent_->updateChildShape(this);
201        if (this->worldEntityParent_)
202            this->worldEntityParent_->notifyCollisionShapeChanged();
203    }
204
205    void CompoundCollisionShape::parentChanged()
206    {
207        if (!this->worldEntityParent_)
208            CollisionShape::parentChanged();
209    }
210
211    CollisionShape* CompoundCollisionShape::getChildShape(unsigned int index) const
212    {
213        unsigned int i = 0;
214        for (std::map<CollisionShape*, btCollisionShape*>::const_iterator it = this->childShapes_.begin(); it != this->childShapes_.end(); ++it)
215        {
216            if (i == index)
217                return it->first;
218            ++i;
219        }
220        return 0;
221    }
222}
Note: See TracBrowser for help on using the repository browser.