Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/pch/src/orxonox/objects/collisionshapes/CollisionShape.cc @ 3149

Last change on this file since 3149 was 3149, checked in by rgrieder, 15 years ago

Extracted OrxAssert from Exception.h to OrxAssert.h since it doesn't really have anything to do with exceptions.

  • Property svn:eol-style set to native
File size: 5.4 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#include "CollisionShape.h"
30
31#include "BulletCollision/CollisionShapes/btCollisionShape.h"
32
33#include "core/CoreIncludes.h"
34#include "core/XMLPort.h"
35#include "tools/BulletConversions.h"
36
37#include "objects/worldentities/WorldEntity.h"
38#include "CompoundCollisionShape.h"
39#include "WorldEntityCollisionShape.h"
40
41namespace orxonox
42{
43    CollisionShape::CollisionShape(BaseObject* creator)
44        : BaseObject(creator)
45        , Synchronisable(creator)
46    {
47        RegisterObject(CollisionShape);
48
49        this->parent_ = 0;
50        this->parentID_ = OBJECTID_UNKNOWN;
51        this->collisionShape_ = 0;
52        this->position_ = Vector3::ZERO;
53        this->orientation_ = Quaternion::IDENTITY;
54        this->scale_ = Vector3::UNIT_SCALE;
55
56        this->registerVariables();
57    }
58
59    CollisionShape::~CollisionShape()
60    {
61        // Detach from parent
62        if (this->isInitialized() && this->parent_)
63            this->parent_->detach(this);
64    }
65
66    void CollisionShape::XMLPort(Element& xmlelement, XMLPort::Mode mode)
67    {
68        SUPER(CollisionShape, XMLPort, xmlelement, mode);
69
70        XMLPortParamTemplate(CollisionShape, "position", setPosition, getPosition, xmlelement, mode, const Vector3&);
71        XMLPortParamTemplate(CollisionShape, "orientation", setOrientation, getOrientation, xmlelement, mode, const Quaternion&);
72        XMLPortParamTemplate(CollisionShape, "scale3D", setScale3D, getScale3D, xmlelement, mode, const Vector3&);
73        XMLPortParamLoadOnly(CollisionShape, "scale", setScale, xmlelement, mode);
74        XMLPortParamLoadOnly(CollisionShape, "yaw",   yaw,   xmlelement, mode);
75        XMLPortParamLoadOnly(CollisionShape, "pitch", pitch, xmlelement, mode);
76        XMLPortParamLoadOnly(CollisionShape, "roll",  roll,  xmlelement, mode);
77    }
78
79    void CollisionShape::registerVariables()
80    {
81        registerVariable(this->parentID_, variableDirection::toclient, new NetworkCallback<CollisionShape>(this, &CollisionShape::parentChanged));
82    }
83
84    void CollisionShape::parentChanged()
85    {
86        Synchronisable* parent = Synchronisable::getSynchronisable(this->parentID_);
87        // Parent can either be a WorldEntity or a CompoundCollisionShape. The reason is that the
88        // internal collision shape (which is compound) of a WE doesn't get synchronised.
89        CompoundCollisionShape* parentCCS = dynamic_cast<CompoundCollisionShape*>(parent);
90        if (parentCCS)
91            parentCCS->attach(this);
92        else
93        {
94            WorldEntity* parentWE = dynamic_cast<WorldEntity*>(parent);
95            if (parentWE)
96                parentWE->attachCollisionShape(this);
97        }
98    }
99
100    bool CollisionShape::notifyBeingAttached(CompoundCollisionShape* newParent)
101    {
102        if (this->parent_)
103            this->parent_->detach(this);
104
105        this->parent_ = newParent;
106
107        WorldEntityCollisionShape* parentWECCS = dynamic_cast<WorldEntityCollisionShape*>(newParent);
108        if (parentWECCS)
109            this->parentID_ = parentWECCS->getWorldEntityOwner()->getObjectID();
110        else
111            this->parentID_ = newParent->getObjectID();
112
113        return true;
114    }
115
116    void CollisionShape::notifyDetached()
117    {
118        this->parent_ = 0;
119        this->parentID_ = OBJECTID_UNKNOWN;
120    }
121
122    void CollisionShape::updateParent()
123    {
124        if (this->parent_)
125            this->parent_->updateAttachedShape(this);
126    }
127
128    bool CollisionShape::hasTransform() const
129    {
130        return (!this->position_.positionEquals(Vector3(0, 0, 0), 0.001) ||
131                !this->orientation_.equals(Quaternion(1,0,0,0), Degree(0.1)));
132    }
133
134    void CollisionShape::setScale3D(const Vector3& scale)
135    {
136        CCOUT(2) << "Warning: Cannot set the scale of a collision shape: Not yet implemented." << std::endl;
137    }
138
139    void CollisionShape::setScale(float scale)
140    {
141        CCOUT(2) << "Warning: Cannot set the scale of a collision shape: Not yet implemented." << std::endl;
142    }
143
144    void CollisionShape::updateShape()
145    {
146        btCollisionShape* oldShape = this->collisionShape_;
147        this->collisionShape_ = this->createNewShape();
148        // When we recreate the shape, we have to inform the parent about this to update the shape
149        this->updateParent();
150        if (oldShape)
151            delete oldShape;
152    }
153
154    void CollisionShape::calculateLocalInertia(btScalar mass, btVector3& inertia) const
155    {
156        if (this->collisionShape_)
157            this->collisionShape_->calculateLocalInertia(mass, inertia);
158        else
159            inertia.setValue(0, 0, 0);
160    }
161}
Note: See TracBrowser for help on using the repository browser.