Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10917 was 10624, checked in by landauf, 9 years ago

merged branch core7 back to trunk

  • Property svn:eol-style set to native
File size: 11.0 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/**
30    @file CompoundCollisionShape.cc
31    @brief Implementation of the CompoundCollisionShape class.
32*/
33
34#include "CompoundCollisionShape.h"
35
36#include <BulletCollision/CollisionShapes/btCompoundShape.h>
37
38#include "core/CoreIncludes.h"
39#include "core/XMLPort.h"
40#include "tools/BulletConversions.h"
41
42namespace orxonox
43{
44    RegisterClass(CompoundCollisionShape);
45
46    /**
47    @brief
48        Constructor. Registers and initializes the object.
49    */
50    CompoundCollisionShape::CompoundCollisionShape(Context* context) : CollisionShape(context)
51    {
52        RegisterObject(CompoundCollisionShape);
53
54        this->compoundShape_  = new btCompoundShape();
55    }
56
57    /**
58    @brief
59        Destructor.
60        Deletes all its children.
61    */
62    CompoundCollisionShape::~CompoundCollisionShape()
63    {
64        if (this->isInitialized())
65        {
66            // Delete all children
67            for (std::map<CollisionShape*, btCollisionShape*>::iterator it = this->attachedShapes_.begin();
68                it != this->attachedShapes_.end(); ++it)
69            {
70                // make sure that the child doesn't want to detach itself --> speedup because of the missing update
71                it->first->notifyDetached();
72                it->first->destroy();
73                if (this->collisionShape_ == it->second)
74                    this->collisionShape_ = NULL; // don't destroy it twice
75            }
76
77            delete this->compoundShape_;
78            if (this->collisionShape_ == this->compoundShape_)
79                this->collisionShape_ = NULL; // don't destroy it twice
80        }
81    }
82
83    void CompoundCollisionShape::XMLPort(Element& xmlelement, XMLPort::Mode mode)
84    {
85        SUPER(CompoundCollisionShape, XMLPort, xmlelement, mode);
86        // Attached collision shapes
87        XMLPortObject(CompoundCollisionShape, CollisionShape, "", attach, detach, xmlelement, mode);
88    }
89
90    /**
91    @brief
92        Attach the input CollisionShape to the CompoundCollisionShape.
93    @param shape
94        A pointer to the CollisionShape that is to be attached.
95    */
96    void CompoundCollisionShape::attach(CollisionShape* shape)
97    {
98        // If either the input shape is NULL or we try to attach the CollisionShape to itself.
99        if (!shape || static_cast<CollisionShape*>(this) == shape)
100            return;
101
102        if (this->attachedShapes_.find(shape) != this->attachedShapes_.end())
103        {
104            orxout(internal_warning) << "Attaching a CollisionShape twice is not yet supported." << endl;
105            return;
106        }
107
108        // Notify the CollisionShape that it is being attached to the CompoundCollisionShape.
109        if (!shape->notifyBeingAttached(this))
110            return;
111
112        // Attach it.
113        this->attachedShapes_[shape] = shape->getCollisionShape();
114
115        // Only actually attach if we didn't pick a CompoundCollisionShape with no content.
116        if (shape->getCollisionShape())
117        {
118            btTransform transf(multi_cast<btQuaternion>(shape->getOrientation()), multi_cast<btVector3>(shape->getPosition()));
119            // Add the btCollisionShape of the CollisionShape as a child shape to the btCompoundShape of the CompoundCollisionShape.
120            this->compoundShape_->addChildShape(transf, shape->getCollisionShape());
121
122            this->updatePublicShape();
123        }
124    }
125
126    /**
127    @brief
128        Detach the input CollisionShape form the CompoundCollisionShape.
129    @param shape
130        A pointer to the CollisionShape to be detached.
131    */
132    void CompoundCollisionShape::detach(CollisionShape* shape)
133    {
134        // If the input CollisionShape is actually attached.
135        if (this->attachedShapes_.find(shape) != this->attachedShapes_.end())
136        {
137            this->attachedShapes_.erase(shape);
138            if (shape->getCollisionShape())
139                this->compoundShape_->removeChildShape(shape->getCollisionShape()); // TODO: Apparently this is broken?
140            shape->notifyDetached();
141
142            this->updatePublicShape();
143        }
144        else
145            orxout(internal_warning) << "Cannot detach non child collision shape" << endl;
146    }
147
148    /**
149    @brief
150        Detach all attached CollisionShapes.
151    */
152    void CompoundCollisionShape::detachAll()
153    {
154        while (this->attachedShapes_.size() > 0)
155            this->detach(this->attachedShapes_.begin()->first);
156    }
157
158    /**
159    @brief
160        Update the input CollisionShape that is attached to the CompoundCollisionShape.
161        This is called when the input shape's internal collision shape (a btCollisionShape) has changed.
162    @param shape
163        A pointer to the CollisionShape to be updated.
164    */
165    void CompoundCollisionShape::updateAttachedShape(CollisionShape* shape)
166    {
167        if (!shape)
168            return;
169
170        std::map<CollisionShape*, btCollisionShape*>::iterator it = this->attachedShapes_.find(shape);
171        // Check whether the input shape belongs to this CompoundCollisionShape.
172        if (it == this->attachedShapes_.end())
173        {
174            orxout(internal_warning) << "Cannot update child shape: Instance not a child." << endl;
175            return;
176        }
177
178        // Remove old btCollisionShape, stored in the children map
179        if (it->second)
180            this->compoundShape_->removeChildShape(it->second); // TODO: Apparently this is broken?
181
182        // Only actually attach if we didn't pick a CompoundCollisionShape with no content
183        if (shape->getCollisionShape())
184        {
185            btTransform transf(multi_cast<btQuaternion>(shape->getOrientation()), multi_cast<btVector3>(shape->getPosition()));
186            this->compoundShape_->addChildShape(transf, shape->getCollisionShape());
187            it->second = shape->getCollisionShape();
188        }
189
190        this->updatePublicShape();
191    }
192
193    /**
194    @brief
195        Updates the public shape, the collision shape this CompoundCollisionShape has to the outside.
196    */
197    void CompoundCollisionShape::updatePublicShape()
198    {
199        btCollisionShape* primitive = 0; // The primitive shape, if there is one.
200        bool bPrimitive = true; // Whether the CompoundCollisionShape has just one non-empty CollisionShape. And that shape also has no transformation.
201        bool bEmpty = true; // Whether the CompoundCollisionShape is empty.
202        // Iterate over all CollisionShapes that belong to this CompoundCollisionShape.
203        for (std::map<CollisionShape*, btCollisionShape*>::const_iterator it = this->attachedShapes_.begin(); it != this->attachedShapes_.end(); ++it)
204        {
205            // TODO: Make sure this is correct.
206            if (it->second)
207            {
208                bEmpty = false;
209                if (!it->first->hasTransform() && bPrimitive)
210                    primitive = it->second;
211                else
212                {
213                    bPrimitive = false;
214                    break;
215                }
216            }
217        }
218
219        // If there is no non-empty CollisionShape.
220        if (bEmpty)
221        {
222            // If there was none all along, nothing needs to be changed.
223            if (this->collisionShape_ == 0)
224                return;
225            this->collisionShape_ = 0;
226        }
227        // If the CompoundCollisionShape is just a primitive.
228        // Only one shape to be added, no transform; return it directly.
229        else if (bPrimitive)
230            this->collisionShape_ = primitive;
231        // Make sure we use the compound shape when returning a btCollisionShape.
232        else
233            this->collisionShape_ = this->compoundShape_;
234
235        this->updateParent();
236    }
237
238    /**
239    @brief
240        Get the attached CollisionShape at the given index.
241    @param index
242        The index of the desired CollisionShape.
243    @return
244        Returns a pointer to the attached CollisionShape at the given index.
245    */
246    CollisionShape* CompoundCollisionShape::getAttachedShape(unsigned int index) const
247    {
248        unsigned int i = 0;
249        for (std::map<CollisionShape*, btCollisionShape*>::const_iterator it = this->attachedShapes_.begin(); it != this->attachedShapes_.end(); ++it)
250        {
251            if (i == index)
252                return it->first;
253            ++i;
254        }
255        return 0;
256    }
257
258    /**
259    @brief
260        Is called when the scale of the CompoundCollisionShape has changed.
261        Iterates over all attached CollisionShapes and scales them, then recomputes their compound shape.
262    */
263    void CompoundCollisionShape::changedScale()
264    {
265        CollisionShape::changedScale();
266
267        std::vector<CollisionShape*> shapes;
268        // Iterate through all attached CollisionShapes and add them to the list of shapes.
269        for(std::map<CollisionShape*, btCollisionShape*>::iterator it = this->attachedShapes_.begin(); it != this->attachedShapes_.end(); it++)
270            shapes.push_back(it->first);
271
272        // Delete the compound shape and create a new one.
273        delete this->compoundShape_;
274        this->compoundShape_ = new btCompoundShape();
275
276        // Re-attach all CollisionShapes.
277        for(std::vector<CollisionShape*>::iterator it = shapes.begin(); it != shapes.end(); it++)
278        {
279            CollisionShape* shape = *it;
280            shape->setScale3D(this->getScale3D());
281            // Only actually attach if we didn't pick a CompoundCollisionShape with no content.
282            if (shape->getCollisionShape())
283            {
284                btTransform transf(multi_cast<btQuaternion>(shape->getOrientation()), multi_cast<btVector3>(shape->getPosition()));
285                // Add the btCollisionShape of the CollisionShape as a child shape to the btCompoundShape of the CompoundCollisionShape.
286                this->compoundShape_->addChildShape(transf, shape->getCollisionShape());
287            }
288        }
289
290        this->updatePublicShape();
291
292        /*
293        // Iterate through all attached CollisionShapes
294        for(std::map<CollisionShape*, btCollisionShape*>::const_iterator it = this->attachedShapes_.begin(); it != this->attachedShapes_.end(); it++)
295        {
296            // Rescale the CollisionShape.
297            it->first->setScale3D(this->getScale3D());
298            this->updateAttachedShape(it->first);
299        }
300
301        this->updatePublicShape();*/
302    }
303}
Note: See TracBrowser for help on using the repository browser.