Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentationFS14/src/orxonox/collisionshapes/CompoundCollisionShape.cc @ 10179

Last change on this file since 10179 was 10073, checked in by smerkli, 11 years ago

Merged modularships branch

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