Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/physics/src/orxonox/objects/worldentities/WorldEntity.cc @ 2276

Last change on this file since 2276 was 2201, checked in by martisty, 16 years ago

loading objects and creating shapes…

  • Property svn:eol-style set to native
File size: 8.2 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 *      Fabian 'x3n' Landau
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include "OrxonoxStableHeaders.h"
30#include "WorldEntity.h"
31
32#include <cassert>
33#include <OgreSceneManager.h>
34#include "BulletCollision/CollisionShapes/btSphereShape.h"
35
36#include "core/CoreIncludes.h"
37#include "core/XMLPort.h"
38#include "util/Convert.h"
39
40#include "objects/Scene.h"
41
42namespace orxonox
43{
44    const Vector3 WorldEntity::FRONT = Vector3::NEGATIVE_UNIT_Z;
45    const Vector3 WorldEntity::BACK  = Vector3::UNIT_Z;
46    const Vector3 WorldEntity::LEFT  = Vector3::NEGATIVE_UNIT_X;
47    const Vector3 WorldEntity::RIGHT = Vector3::UNIT_X;
48    const Vector3 WorldEntity::DOWN  = Vector3::NEGATIVE_UNIT_Y;
49    const Vector3 WorldEntity::UP    = Vector3::UNIT_Y;
50
51    WorldEntity::WorldEntity(BaseObject* creator) : BaseObject(creator), network::Synchronisable(creator)
52    {
53        RegisterObject(WorldEntity);
54
55        assert(this->getScene());
56        assert(this->getScene()->getRootSceneNode());
57
58        this->node_ = this->getScene()->getRootSceneNode()->createChildSceneNode();
59
60        this->parent_ = 0;
61        this->parentID_ = (unsigned int)-1;
62
63        this->node_->setPosition(Vector3::ZERO);
64        this->node_->setOrientation(Quaternion::IDENTITY);
65
66        // Default behaviour does not include physics
67        this->bAddedToPhysicalWorld_ = false;
68        this->physicalBody_ = 0;
69
70        this->registerVariables();
71    }
72
73    WorldEntity::~WorldEntity()
74    {
75        if (this->isInitialized())
76        {
77            this->node_->detachAllObjects();
78            if (this->getScene()->getSceneManager())
79                this->getScene()->getSceneManager()->destroySceneNode(this->node_->getName());
80           
81            // Physics is not guaranteed, so check first
82            if (this->physicalBody_)
83            {
84                if (this->bAddedToPhysicalWorld_)
85                    this->getScene()->getPhysicalWorld()->removeRigidBody(this->physicalBody_);
86                if (this->physicalBody_->getCollisionShape())
87                    delete this->physicalBody_->getCollisionShape();
88                delete this->physicalBody_;
89            }
90        }
91    }
92
93    void WorldEntity::XMLPort(Element& xmlelement, XMLPort::Mode mode)
94    {
95        SUPER(WorldEntity, XMLPort, xmlelement, mode);
96
97        XMLPortParamTemplate(WorldEntity, "position", setPosition, getPosition, xmlelement, mode, const Vector3&);
98        XMLPortParamTemplate(WorldEntity, "orientation", setOrientation, getOrientation, xmlelement, mode, const Quaternion&);
99        XMLPortParamLoadOnly(WorldEntity, "lookat", lookAt_xmlport, xmlelement, mode);
100        XMLPortParamLoadOnly(WorldEntity, "direction", setDirection_xmlport, xmlelement, mode);
101        XMLPortParamLoadOnly(WorldEntity, "yaw", yaw_xmlport, xmlelement, mode);
102        XMLPortParamLoadOnly(WorldEntity, "pitch", pitch_xmlport, xmlelement, mode);
103        XMLPortParamLoadOnly(WorldEntity, "roll", roll_xmlport, xmlelement, mode);
104        XMLPortParamTemplate(WorldEntity, "scale3D", setScale3D, getScale3D, xmlelement, mode, const Vector3&);
105        XMLPortParam(WorldEntity, "scale", setScale, getScale, xmlelement, mode);
106        XMLPortParam(WorldEntity, "collisionRadius", setcollisionRadius, getcollisionRadius, xmlelement, mode);
107
108        XMLPortObject(WorldEntity, WorldEntity, "attached", attach, getAttachedObject, xmlelement, mode);
109    }
110
111    void WorldEntity::registerVariables()
112    {
113        REGISTERDATA(this->bActive_,  network::direction::toclient, new network::NetworkCallback<WorldEntity>(this, &WorldEntity::changedActivity));
114        REGISTERDATA(this->bVisible_, network::direction::toclient, new network::NetworkCallback<WorldEntity>(this, &WorldEntity::changedVisibility));
115
116        REGISTERDATA(this->getScale3D().x, network::direction::toclient);
117        REGISTERDATA(this->getScale3D().y, network::direction::toclient);
118        REGISTERDATA(this->getScale3D().z, network::direction::toclient);
119
120        REGISTERDATA(this->parentID_, network::direction::toclient, new network::NetworkCallback<WorldEntity>(this, &WorldEntity::updateParent));
121    }
122
123    void WorldEntity::updateParent()
124    {
125        WorldEntity* parent = dynamic_cast<WorldEntity*>(Synchronisable::getSynchronisable(this->parentID_));
126        if (parent)
127            this->attachToParent(parent);
128    }
129
130    void WorldEntity::attach(WorldEntity* object)
131    {
132        if (object->getParent())
133            object->detachFromParent();
134        else
135        {
136            Ogre::Node* parent = object->node_->getParent();
137            if (parent)
138                parent->removeChild(object->node_);
139        }
140
141        this->node_->addChild(object->node_);
142        this->children_.insert(object);
143        object->parent_ = this;
144        object->parentID_ = this->getObjectID();
145
146        // Do the physical connection if required
147        this->attachPhysicalObject(object);
148    }
149
150    void WorldEntitiy::attachPhysicalObject(WorldEntity* object){
151    //function attachhysicalObject
152        StaticEntity* staticObject = dynamic_cast<WorldEntity*>(object);
153        if (staticObject != 0 && hasPhysics()){
154           btCompountShape* compoundShape = dynamic_cast<btCompoundShape*>(physicalBody_->getCollisionShape());
155           if(compoundShape == 0){
156                //NEW
157                btCompoundShape* newShape = new btCompoundShape();
158                newShape->addChildShape(this->physcialBody_->getCollisionShape());
159                newShape->addChildShape(staticObject->getCollisionShape());
160                this->physicalBody_->setCollisionShape();
161           }
162           else{
163               compoundShape -> addChildShape(staticObject->getCollisionShape());
164           }
165        }
166    }
167
168    void WorldEntity::detach(WorldEntity* object)
169    {
170        this->node_->removeChild(object->node_);
171        this->children_.erase(object);
172        object->parent_ = 0;
173        object->parentID_ = (unsigned int)-1;
174
175//        this->getScene()->getRootSceneNode()->addChild(object->node_);
176    }
177
178    WorldEntity* WorldEntity::getAttachedObject(unsigned int index) const
179    {
180        unsigned int i = 0;
181        for (std::set<WorldEntity*>::const_iterator it = this->children_.begin(); it != this->children_.end(); ++it)
182        {
183            if (i == index)
184                return (*it);
185            ++i;
186        }
187        return 0;
188    }
189
190    void WorldEntity::createPhysicalBody()
191    {
192        // Note: The motion state will be configured in a derived class.
193        btRigidBody::btRigidBodyConstructionInfo bodyConstructionInfo(0, this, 0, btVector3(0,0,0));
194        this->physicalBody_ = new btRigidBody(bodyConstructionInfo);
195        this->getScene()->getPhysicalWorld()->addRigidBody(this->physicalBody_);
196        this->bAddedToPhysicalWorld_ = true;
197    }
198
199    void WorldEntity::setcollisionRadius(float radius)
200    {
201        if (!this->physicalBody_)
202            createPhysicalBody();
203
204        // destroy old onw first
205        btCollisionShape* oldShape = this->physicalBody_->getCollisionShape();
206        if (oldShape)
207            delete oldShape;
208
209        this->physicalBody_->setCollisionShape(new btSphereShape(btScalar(radius)));
210    }
211
212    float WorldEntity::getcollisionRadius()
213    {
214        if (this->physicalBody_)
215        {
216            btSphereShape* sphere = dynamic_cast<btSphereShape*>(this->physicalBody_->getCollisionShape());
217            if (sphere)
218                return (float)sphere->getRadius();
219        }
220        return 0.0f;
221    }
222}
Note: See TracBrowser for help on using the repository browser.