Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/ggz/src/orxonox/objects/collisionshapes/CompoundCollisionShape.cc @ 3374

Last change on this file since 3374 was 2662, checked in by rgrieder, 16 years ago

Merged presentation branch back to trunk.

  • Property svn:eol-style set to native
File size: 6.5 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
39namespace orxonox
40{
41    CreateFactory(CompoundCollisionShape);
42
43    CompoundCollisionShape::CompoundCollisionShape(BaseObject* creator) : CollisionShape(creator)
44    {
45        RegisterObject(CompoundCollisionShape);
46
47        this->compoundShape_  = new btCompoundShape();
48    }
49
50    CompoundCollisionShape::~CompoundCollisionShape()
51    {
52        if (this->isInitialized())
53        {
54            // Delete all children
55            for (std::map<CollisionShape*, btCollisionShape*>::iterator it = this->attachedShapes_.begin();
56                it != this->attachedShapes_.end(); ++it)
57            {
58                // make sure that the child doesn't want to detach itself --> speedup because of the missing update
59                it->first->notifyDetached();
60                delete it->first;
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, "", attach, detach, xmlelement, mode);
72    }
73
74    void CompoundCollisionShape::attach(CollisionShape* shape)
75    {
76        if (!shape || static_cast<CollisionShape*>(this) == shape)
77            return;
78        if (this->attachedShapes_.find(shape) != this->attachedShapes_.end())
79        {
80            CCOUT(2) << "Warning: Attaching a CollisionShape twice is not yet supported." << std::endl;
81            return;
82        }
83
84        if (!shape->notifyBeingAttached(this))
85            return;
86
87        this->attachedShapes_[shape] = shape->getCollisionShape();
88
89        if (shape->getCollisionShape())
90        {
91            // Only actually attach if we didn't pick a CompoundCollisionShape with no content
92            btTransform transf(omni_cast<btQuaternion>(shape->getOrientation()), omni_cast<btVector3>(shape->getPosition()));
93            this->compoundShape_->addChildShape(transf, shape->getCollisionShape());
94
95            this->updatePublicShape();
96        }
97    }
98
99    void CompoundCollisionShape::detach(CollisionShape* shape)
100    {
101        if (this->attachedShapes_.find(shape) != this->attachedShapes_.end())
102        {
103            this->attachedShapes_.erase(shape);
104            if (shape->getCollisionShape())
105                this->compoundShape_->removeChildShape(shape->getCollisionShape());
106            shape->notifyDetached();
107
108            this->updatePublicShape();
109        }
110        else
111            CCOUT(2) << "Warning: Cannot detach non child collision shape" << std::endl;
112    }
113
114    void CompoundCollisionShape::detachAll()
115    {
116        while (this->attachedShapes_.size() > 0)
117            this->detach(this->attachedShapes_.begin()->first);
118    }
119
120    void CompoundCollisionShape::updateAttachedShape(CollisionShape* shape)
121    {
122        if (!shape)
123            return;
124        std::map<CollisionShape*, btCollisionShape*>::iterator it = this->attachedShapes_.find(shape);
125        if (it == this->attachedShapes_.end())
126        {
127            CCOUT(2) << "Warning: Cannot update child shape: Instance not a child." << std::endl;
128            return;
129        }
130
131        // Remove old btCollisionShape, stored in the children map
132        if (it->second)
133            this->compoundShape_->removeChildShape(it->second);
134        if (shape->getCollisionShape())
135        {
136            // Only actually attach if we didn't pick a CompoundCollisionShape with no content
137            btTransform transf(omni_cast<btQuaternion>(shape->getOrientation()), omni_cast<btVector3>(shape->getPosition()));
138            this->compoundShape_->addChildShape(transf, shape->getCollisionShape());
139            it->second = shape->getCollisionShape();
140        }
141
142        this->updatePublicShape();
143    }
144
145    void CompoundCollisionShape::updatePublicShape()
146    {
147        btCollisionShape* primitive = 0;
148        bool bPrimitive = true;
149        bool bEmpty = true;
150        for (std::map<CollisionShape*, btCollisionShape*>::const_iterator it = this->attachedShapes_.begin(); it != this->attachedShapes_.end(); ++it)
151        {
152            if (it->second)
153            {
154                bEmpty = false;
155                if (!it->first->hasTransform() && !bPrimitive)
156                    primitive = it->second;
157                else
158                    bPrimitive = false;
159            }
160        }
161        if (bEmpty)
162        {
163            if (this->collisionShape_ == 0)
164            {
165                this->collisionShape_ = 0;
166                return;
167            }
168            this->collisionShape_ = 0;
169        }
170        else if (bPrimitive)
171        {
172            // --> Only one shape to be added, no transform; return it directly
173            this->collisionShape_ = primitive;
174        }
175        else
176        {
177            // Make sure we use the compound shape when returning a btCollisionShape
178            this->collisionShape_ = this->compoundShape_;
179        }
180        this->updateParent();
181    }
182
183    CollisionShape* CompoundCollisionShape::getAttachedShape(unsigned int index) const
184    {
185        unsigned int i = 0;
186        for (std::map<CollisionShape*, btCollisionShape*>::const_iterator it = this->attachedShapes_.begin(); it != this->attachedShapes_.end(); ++it)
187        {
188            if (i == index)
189                return it->first;
190            ++i;
191        }
192        return 0;
193    }
194}
Note: See TracBrowser for help on using the repository browser.