Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/objects/collisionshapes/CompoundCollisionShape.cc @ 3172

Last change on this file since 3172 was 3110, checked in by rgrieder, 16 years ago

Removed old msvc specific support for precompiled header files.

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