Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 9592 was 8858, checked in by landauf, 13 years ago

merged output branch back to trunk.

Changes:

  • you have to include util/Output.h instead of util/Debug.h
  • COUT(x) is now called orxout(level)
  • output levels are now defined by an enum instead of numbers. see util/Output.h for the definition
  • it's possible to use output contexts with orxout(level, context). see util/Output.h for some common contexts. you can define more contexts
  • you must use 'endl' at the end of an output message, '\n' does not flush the message

Output levels:

  • instead of COUT(0) use orxout()
  • instead of COUT(1) use orxout(user_error) or orxout(internal_error)
  • instead of COUT(2) use orxout(user_warning) or orxout(internal_warning)
  • instead of COUT(3) use orxout(user_status/user_info) or orxout(internal_status/internal_info)
  • instead of COUT(4) use orxout(verbose)
  • instead of COUT(5) use orxout(verbose_more)
  • instead of COUT(6) use orxout(verbose_ultra)

Guidelines:

  • user_* levels are for the user, visible in the console and the log-file
  • internal_* levels are for developers, visible in the log-file
  • verbose_* levels are for debugging, only visible if the context of the output is activated

Usage in C++:

  • orxout() << "message" << endl;
  • orxout(level) << "message" << endl;
  • orxout(level, context) << "message" << endl;

Usage in Lua:

  • orxout("message")
  • orxout(orxonox.level.levelname, "message")
  • orxout(orxonox.level.levelname, "context", "message")

Usage in Tcl (and in the in-game-console):

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