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/CollisionShape.cc

    r7401 r8706  
    2626 *
    2727 */
     28
     29/**
     30    @file CollisionShape.cc
     31    @brief Implementation of the CollisionShape class.
     32*/
    2833
    2934#include "CollisionShape.h"
     
    3944namespace orxonox
    4045{
     46
     47    /**
     48    @brief
     49        Constructor. Registers and initializes the object.
     50    */
    4151    CollisionShape::CollisionShape(BaseObject* creator)
    4252        : BaseObject(creator)
     
    5161        this->orientation_ = Quaternion::IDENTITY;
    5262        this->scale_ = Vector3::UNIT_SCALE;
     63        this->uniformScale_ = true;
    5364
    5465        this->registerVariables();
    5566    }
    5667
     68    /**
     69    @brief
     70        Destructor. Detaches the CollisionShape from its parent.
     71    */
    5772    CollisionShape::~CollisionShape()
    5873    {
    59         // Detach from parent
     74        // Detach from parent CompoundCollisionShape.
    6075        if (this->isInitialized() && this->parent_)
    6176            this->parent_->detach(this);
     
    7590    }
    7691
     92    /**
     93    @brief
     94        Register variables that need synchronizing over the network.
     95    */
    7796    void CollisionShape::registerVariables()
    7897    {
     98        // Keep the shape's parent (can be either a CompoundCollisionShape or a WorldEntity) consistent over the network.
    7999        registerVariable(this->parentID_, VariableDirection::ToClient, new NetworkCallback<CollisionShape>(this, &CollisionShape::parentChanged));
    80100    }
    81101
     102    /**
     103    @brief
     104        Notifies the CollisionShape of being attached to a CompoundCollisionShape.
     105    @param newParent
     106        A pointer to the CompoundCollisionShape the CollisionShape was attached to.
     107    @return
     108        Returns
     109    */
     110    bool CollisionShape::notifyBeingAttached(CompoundCollisionShape* newParent)
     111    {
     112        // If the CollisionShape is attached to a CompoundCollisionShapes, detach it.
     113        if (this->parent_)
     114            this->parent_->detach(this);
     115
     116        this->parent_ = newParent;
     117
     118        // If the new parent is a WorldEntityCollisionShape, the parentID is set to the objectID of the WorldEntity that is its owner.
     119        // TODO: Why?
     120        WorldEntityCollisionShape* parentWECCS = orxonox_cast<WorldEntityCollisionShape*>(newParent);
     121        if (parentWECCS)
     122            this->parentID_ = parentWECCS->getWorldEntityOwner()->getObjectID();
     123        // Else it is set to the objectID of the CompoundCollisionShape.
     124        else
     125            this->parentID_ = newParent->getObjectID();
     126
     127        return true;
     128    }
     129
     130    /**
     131    @brief
     132        Notifies the CollisionShape of being detached from a CompoundCollisionShape.
     133    */
     134    void CollisionShape::notifyDetached()
     135    {
     136        this->parent_ = 0;
     137        this->parentID_ = OBJECTID_UNKNOWN;
     138    }
     139
     140    /**
     141    @brief
     142        Updates the CompoundCollisionShape the CollisionShape belongs to (if it belongs to one), after the CollisionShape has changed.
     143    */
     144    void CollisionShape::updateParent()
     145    {
     146        if (this->parent_)
     147            this->parent_->updateAttachedShape(this);
     148    }
     149
     150    /**
     151    @brief
     152        Is called when the parentID of the CollisionShape has changed.
     153        Attaches it to the object with the changed parentID, which can either be a CompoundCollisionShape or a WorldEntity.
     154    */
    82155    void CollisionShape::parentChanged()
    83156    {
     157        // Get the parent object from the network.
    84158        Synchronisable* parent = Synchronisable::getSynchronisable(this->parentID_);
     159
    85160        // Parent can either be a WorldEntity or a CompoundCollisionShape. The reason is that the
    86161        // internal collision shape (which is compound) of a WE doesn't get synchronised.
    87162        CompoundCollisionShape* parentCCS = orxonox_cast<CompoundCollisionShape*>(parent);
     163
     164        // If the parent is a CompoundCollisionShape, attach the CollisionShape to it.
    88165        if (parentCCS)
    89166            parentCCS->attach(this);
    90167        else
    91168        {
     169            // If the parent is a WorldEntity, attach the CollisionShape to its collision shapes.
    92170            WorldEntity* parentWE = orxonox_cast<WorldEntity*>(parent);
    93171            if (parentWE)
     
    96174    }
    97175
    98     bool CollisionShape::notifyBeingAttached(CompoundCollisionShape* newParent)
    99     {
    100         if (this->parent_)
    101             this->parent_->detach(this);
    102 
    103         this->parent_ = newParent;
    104 
    105         WorldEntityCollisionShape* parentWECCS = orxonox_cast<WorldEntityCollisionShape*>(newParent);
    106         if (parentWECCS)
    107             this->parentID_ = parentWECCS->getWorldEntityOwner()->getObjectID();
    108         else
    109             this->parentID_ = newParent->getObjectID();
    110 
    111         return true;
    112     }
    113 
    114     void CollisionShape::notifyDetached()
    115     {
    116         this->parent_ = 0;
    117         this->parentID_ = OBJECTID_UNKNOWN;
    118     }
    119 
    120     void CollisionShape::updateParent()
    121     {
    122         if (this->parent_)
    123             this->parent_->updateAttachedShape(this);
    124     }
    125 
     176    /**
     177    @brief
     178        Check whether the CollisionShape has been either moved or rotated or both. (i.e. it doesn't have position zero and identity orientation any more)
     179    @return
     180        Returns true if it has been moved.
     181    */
    126182    bool CollisionShape::hasTransform() const
    127183    {
     
    130186    }
    131187
     188    /**
     189    @brief
     190        Set the scale of the CollisionShape.
     191        Since the scale is a vector the CollisionShape can be scaled independently in each direction, allowing for linear distortions.
     192        If the scale changes, this causes the parent CompoundCollisionShape (if there is one) to be updated.
     193        Beware, non-uniform scaling (i.e. distortions) might not be supported by all CollisionShapes.
     194    @param scale
     195        The new scale to be set. Vector3::UNIT_SCALE is the initial scale.
     196    */
    132197    void CollisionShape::setScale3D(const Vector3& scale)
    133198    {
    134         CCOUT(2) << "Warning: Cannot set the scale of a collision shape: Not yet implemented." << std::endl;
    135     }
    136 
     199        if(this->scale_ == scale)
     200            return;
     201
     202        // If the vectors are not in the same direction, then this is no longer a uniform scaling.
     203        if(scale_.crossProduct(scale).squaredLength() != 0.0f)
     204        {
     205            CCOUT(2) << "Warning: Non-uniform scaling is not yet supported." << endl;
     206            return;
     207        }
     208
     209        this->scale_ = scale;
     210
     211        this->changedScale();
     212        this->updateParent();
     213    }
     214
     215    /**
     216    @brief
     217        Set the (uniform) scale of the CollisionShape.
     218        If the scale changes, this causes the parent CompoundCollisionShape (if there is one) to be updated.
     219    @param scale
     220        The scale to scale the CollisionShape with. 1.0f is the initial scale.
     221    */
    137222    void CollisionShape::setScale(float scale)
    138223    {
    139         CCOUT(2) << "Warning: Cannot set the scale of a collision shape: Not yet implemented." << std::endl;
    140     }
    141 
     224        if(this->scale_.length() == scale)
     225            return;
     226
     227        this->scale_ = Vector3::UNIT_SCALE*scale;
     228
     229        this->changedScale();
     230        this->updateParent();
     231    }
     232
     233    /**
     234    @brief
     235        Is called when the scale of the CollisionShape has changed.
     236    */
     237    void CollisionShape::changedScale()
     238    {
     239        // Adjust the position of the CollisionShape.
     240        this->position_ *= this->getScale3D();
     241    }
     242
     243    /**
     244    @brief
     245        Updates the shape.
     246        Is called when the internal parameters of the shape have changed such that a new shape needs to be created.
     247    */
    142248    void CollisionShape::updateShape()
    143249    {
    144250        btCollisionShape* oldShape = this->collisionShape_;
    145251        this->collisionShape_ = this->createNewShape();
     252        // If the CollisionShape has been rescaled, scale the shape to fit the current scale.
     253        if(this->scale_ != Vector3::UNIT_SCALE)
     254            this->changedScale();
    146255        // When we recreate the shape, we have to inform the parent about this to update the shape
    147256        this->updateParent();
     
    150259    }
    151260
     261    /**
     262    @brief
     263        Calculates the local inertia of the collision shape.
     264    @todo
     265        Document.
     266    */
    152267    void CollisionShape::calculateLocalInertia(float mass, btVector3& inertia) const
    153268    {
Note: See TracChangeset for help on using the changeset viewer.