Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Jun 14, 2011, 8:53:28 PM (13 years ago)
Author:
dafrick
Message:

Merging presentation branch back into trunk.
There are many new features and also a lot of other changes and bugfixes, if you want to know, digg through the svn log.
Not everything is yet working as it should, but it should be fairly stable. If you habe any bug reports, just send me an email.

Location:
code/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/trunk

  • code/trunk/src/orxonox/collisionshapes/CompoundCollisionShape.cc

    r5929 r8706  
    2727 */
    2828
     29/**
     30    @file CompoundCollisionShape.cc
     31    @brief Implementation of the CompoundCollisionShape class.
     32*/
     33
    2934#include "CompoundCollisionShape.h"
    3035
     
    3944    CreateFactory(CompoundCollisionShape);
    4045
     46    /**
     47    @brief
     48        Constructor. Registers and initializes the object.
     49    */
    4150    CompoundCollisionShape::CompoundCollisionShape(BaseObject* creator) : CollisionShape(creator)
    4251    {
     
    4655    }
    4756
     57    /**
     58    @brief
     59        Destructor.
     60        Deletes all its children.
     61    */
    4862    CompoundCollisionShape::~CompoundCollisionShape()
    4963    {
     
    7084    }
    7185
     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    */
    7292    void CompoundCollisionShape::attach(CollisionShape* shape)
    7393    {
     94        // If either the input shape is NULL or we try to attach the CollisionShape to itself.
    7495        if (!shape || static_cast<CollisionShape*>(this) == shape)
    7596            return;
     97
    7698        if (this->attachedShapes_.find(shape) != this->attachedShapes_.end())
    7799        {
     
    80102        }
    81103
     104        // Notify the CollisionShape that it is being attached to the CompoundCollisionShape.
    82105        if (!shape->notifyBeingAttached(this))
    83106            return;
    84107
     108        // Attach it.
    85109        this->attachedShapes_[shape] = shape->getCollisionShape();
    86110
     111        // Only actually attach if we didn't pick a CompoundCollisionShape with no content.
    87112        if (shape->getCollisionShape())
    88113        {
    89             // Only actually attach if we didn't pick a CompoundCollisionShape with no content
    90114            btTransform transf(multi_cast<btQuaternion>(shape->getOrientation()), multi_cast<btVector3>(shape->getPosition()));
     115            // Add the btCollisionShape of the CollisionShape as a child shape to the btCompoundShape of the CompoundCollisionShape.
    91116            this->compoundShape_->addChildShape(transf, shape->getCollisionShape());
    92117
     
    95120    }
    96121
     122    /**
     123    @brief
     124        Detach the input CollisionShape form the CompoundCollisionShape.
     125    @param shape
     126        A pointer to the CollisionShape to be detached.
     127    */
    97128    void CompoundCollisionShape::detach(CollisionShape* shape)
    98129    {
     130        // If the input CollisionShape is actually attached.
    99131        if (this->attachedShapes_.find(shape) != this->attachedShapes_.end())
    100132        {
    101133            this->attachedShapes_.erase(shape);
    102134            if (shape->getCollisionShape())
    103                 this->compoundShape_->removeChildShape(shape->getCollisionShape());
     135                this->compoundShape_->removeChildShape(shape->getCollisionShape()); // TODO: Apparently this is broken?
    104136            shape->notifyDetached();
    105137
     
    110142    }
    111143
     144    /**
     145    @brief
     146        Detach all attached CollisionShapes.
     147    */
    112148    void CompoundCollisionShape::detachAll()
    113149    {
     
    116152    }
    117153
     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    */
    118161    void CompoundCollisionShape::updateAttachedShape(CollisionShape* shape)
    119162    {
    120163        if (!shape)
    121164            return;
     165
    122166        std::map<CollisionShape*, btCollisionShape*>::iterator it = this->attachedShapes_.find(shape);
     167        // Check whether the input shape belongs to this CompoundCollisionShape.
    123168        if (it == this->attachedShapes_.end())
    124169        {
     
    129174        // Remove old btCollisionShape, stored in the children map
    130175        if (it->second)
    131             this->compoundShape_->removeChildShape(it->second);
     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
    132179        if (shape->getCollisionShape())
    133180        {
    134             // Only actually attach if we didn't pick a CompoundCollisionShape with no content
    135181            btTransform transf(multi_cast<btQuaternion>(shape->getOrientation()), multi_cast<btVector3>(shape->getPosition()));
    136182            this->compoundShape_->addChildShape(transf, shape->getCollisionShape());
     
    141187    }
    142188
     189    /**
     190    @brief
     191        Updates the public shape, the collision shape this CompoundCollisionShape has to the outside.
     192    */
    143193    void CompoundCollisionShape::updatePublicShape()
    144194    {
    145         btCollisionShape* primitive = 0;
    146         bool bPrimitive = true;
    147         bool bEmpty = true;
     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.
    148199        for (std::map<CollisionShape*, btCollisionShape*>::const_iterator it = this->attachedShapes_.begin(); it != this->attachedShapes_.end(); ++it)
    149200        {
     201            // TODO: Make sure this is correct.
    150202            if (it->second)
    151203            {
    152204                bEmpty = false;
    153                 if (!it->first->hasTransform() && !bPrimitive)
     205                if (!it->first->hasTransform() && bPrimitive)
    154206                    primitive = it->second;
    155207                else
     208                {
    156209                    bPrimitive = false;
     210                    break;
     211                }
    157212            }
    158213        }
     214
     215        // If there is no non-empty CollisionShape.
    159216        if (bEmpty)
    160217        {
     218            // If there was none all along, nothing needs to be changed.
    161219            if (this->collisionShape_ == 0)
    162             {
    163                 this->collisionShape_ = 0;
    164220                return;
    165             }
    166221            this->collisionShape_ = 0;
    167222        }
     223        // If the CompoundCollisionShape is just a primitive.
     224        // Only one shape to be added, no transform; return it directly.
    168225        else if (bPrimitive)
    169         {
    170             // --> Only one shape to be added, no transform; return it directly
    171226            this->collisionShape_ = primitive;
    172         }
     227        // Make sure we use the compound shape when returning a btCollisionShape.
    173228        else
    174         {
    175             // Make sure we use the compound shape when returning a btCollisionShape
    176229            this->collisionShape_ = this->compoundShape_;
    177         }
     230
    178231        this->updateParent();
    179232    }
    180233
     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    */
    181242    CollisionShape* CompoundCollisionShape::getAttachedShape(unsigned int index) const
    182243    {
     
    190251        return 0;
    191252    }
     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    }
    192299}
Note: See TracChangeset for help on using the changeset viewer.