Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/physics/src/orxonox/objects/Scene.cc @ 2296

Last change on this file since 2296 was 2292, checked in by rgrieder, 16 years ago

Finally managed to work out some physics. According to my tests, collisions with simple spheres should work with dynamic/kinematic/static objects. There are currently only a limited number of XML parameters, but we're surely going to extend that. Furthermore there is some more thinking to be done concerning changes of btRigidBody properties when it's already added to the world.

  • Property svn:eol-style set to native
File size: 7.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 "Scene.h"
31
32#include <OgreRoot.h>
33#include <OgreSceneManagerEnumerator.h>
34#include <OgreSceneNode.h>
35#include <OgreLight.h>
36
37#include "core/CoreIncludes.h"
38#include "core/Core.h"
39#include "core/XMLPort.h"
40
41#include "BulletCollision/BroadphaseCollision/btAxisSweep3.h"
42#include "BulletDynamics/ConstraintSolver/btSequentialImpulseConstraintSolver.h"
43#include "BulletCollision/CollisionDispatch/btDefaultCollisionConfiguration.h"
44
45namespace orxonox
46{
47    CreateFactory(Scene);
48
49    Scene::Scene(BaseObject* creator) : BaseObject(creator), network::Synchronisable(creator)
50    {
51        RegisterObject(Scene);
52
53        this->setScene(this);
54        this->bShadows_ = false;
55
56        if (Core::showsGraphics())
57        {
58            if (Ogre::Root::getSingletonPtr())
59            {
60                this->sceneManager_ = Ogre::Root::getSingleton().createSceneManager(Ogre::ST_GENERIC);
61                this->rootSceneNode_ = this->sceneManager_->getRootSceneNode();
62            }
63            else
64            {
65                this->sceneManager_ = 0;
66                this->rootSceneNode_ = 0;
67            }
68        }
69        else
70        {
71            // create a dummy SceneManager of our own since we don't have Ogre::Root.
72            this->sceneManager_ = new Ogre::DefaultSceneManager("");
73            this->rootSceneNode_ = this->sceneManager_->getRootSceneNode();
74        }
75
76        // No physics for default
77        this->physicalWorld_ = 0;
78
79        // test test test
80        if (Core::showsGraphics() && this->sceneManager_)
81        {
82            Ogre::Light* light;
83            light = this->sceneManager_->createLight("Light-1");
84            light->setType(Ogre::Light::LT_DIRECTIONAL);
85            light->setDiffuseColour(ColourValue(1.0, 0.9, 0.6, 1.0));
86            light->setSpecularColour(ColourValue(1.0, 0.9, 0.6, 1.0));
87            light->setDirection(1, -0.3, 0.3);
88        }
89        // test test test
90
91        this->registerVariables();
92    }
93
94    Scene::~Scene()
95    {
96        if (this->isInitialized())
97        {
98            if (Ogre::Root::getSingletonPtr())
99            {
100                Ogre::Root::getSingleton().destroySceneManager(this->sceneManager_);
101            }
102            else if (!Core::showsGraphics())
103            {
104                delete this->sceneManager_;
105            }
106        }
107    }
108
109    void Scene::XMLPort(Element& xmlelement, XMLPort::Mode mode)
110    {
111        SUPER(Scene, XMLPort, xmlelement, mode);
112
113        XMLPortParam(Scene, "skybox", setSkybox, getSkybox, xmlelement, mode);
114        XMLPortParam(Scene, "ambientlight", setAmbientLight, getAmbientLight, xmlelement, mode).defaultValues(ColourValue(0.2, 0.2, 0.2, 1));
115        XMLPortParam(Scene, "shadow", setShadow, getShadow, xmlelement, mode).defaultValues(true);
116
117        const int defaultMaxWorldSize = 100000;
118        Vector3 worldAabbMin(-defaultMaxWorldSize, -defaultMaxWorldSize, -defaultMaxWorldSize);
119        Vector3 worldAabbMax( defaultMaxWorldSize,  defaultMaxWorldSize,  defaultMaxWorldSize);
120        XMLPortParamVariable(Scene, "negativeWorldRange", worldAabbMin, xmlelement, mode);
121        XMLPortParamVariable(Scene, "positiveWorldRange", worldAabbMax, xmlelement, mode);
122        XMLPortParam(Scene, "hasPhysics", setPhysicalWorld, hasPhysics, xmlelement, mode).defaultValue(0, true).defaultValue(1, worldAabbMin).defaultValue(2, worldAabbMax);
123
124        XMLPortObjectExtended(Scene, BaseObject, "", addObject, getObject, xmlelement, mode, true, false);
125    }
126
127    void Scene::registerVariables()
128    {
129        REGISTERSTRING(this->skybox_,     network::direction::toclient, new network::NetworkCallback<Scene>(this, &Scene::networkcallback_applySkybox));
130        REGISTERDATA(this->ambientLight_, network::direction::toclient, new network::NetworkCallback<Scene>(this, &Scene::networkcallback_applyAmbientLight));
131    }
132
133    void Scene::setPhysicalWorld(bool wantPhysics, const Vector3& worldAabbMin, const Vector3& worldAabbMax)
134    {
135        if (wantPhysics && !hasPhysics())
136        {
137            btVector3 worldAabbMin(worldAabbMin.x, worldAabbMin.y, worldAabbMin.z);
138            btVector3 worldAabbMax(worldAabbMax.x, worldAabbMax.y, worldAabbMax.z);
139
140            btDefaultCollisionConfiguration*     collisionConfig = new btDefaultCollisionConfiguration();
141            btCollisionDispatcher*               dispatcher      = new btCollisionDispatcher(collisionConfig);
142            bt32BitAxisSweep3*                   broadphase      = new bt32BitAxisSweep3(worldAabbMin,worldAabbMax);
143            btSequentialImpulseConstraintSolver* solver          = new btSequentialImpulseConstraintSolver;
144
145            this->physicalWorld_ =  new btDiscreteDynamicsWorld(dispatcher, broadphase, solver, collisionConfig);
146
147            // test test test
148            this->physicalWorld_->setGravity(btVector3(0,0,0));
149            // test test test
150        }
151        else
152        {
153            // TODO: Destroy Bullet physics
154        }
155    }
156
157    void Scene::tick(float dt)
158    {
159        if (physicalWorld_)
160            physicalWorld_->stepSimulation(dt,10);
161    }
162
163    void Scene::setSkybox(const std::string& skybox)
164    {
165        if (Core::showsGraphics() && this->sceneManager_)
166            this->sceneManager_->setSkyBox(true, skybox);
167
168        this->skybox_ = skybox;
169    }
170
171    void Scene::setAmbientLight(const ColourValue& colour)
172    {
173        if (Core::showsGraphics() && this->sceneManager_)
174            this->sceneManager_->setAmbientLight(colour);
175
176        this->ambientLight_ = colour;
177    }
178
179    void Scene::setShadow(bool bShadow)
180    {
181        if (Core::showsGraphics() && this->sceneManager_)
182        {
183            if (bShadow)
184                this->sceneManager_->setShadowTechnique(Ogre::SHADOWTYPE_STENCIL_ADDITIVE);
185            else
186                this->sceneManager_->setShadowTechnique(Ogre::SHADOWTYPE_NONE);
187        }
188
189        this->bShadows_ = bShadow;
190    }
191
192    void Scene::addObject(BaseObject* object)
193    {
194        this->objects_.push_back(object);
195        object->setScene(this);
196    }
197
198    BaseObject* Scene::getObject(unsigned int index) const
199    {
200        unsigned int i = 0;
201        for (std::list<BaseObject*>::const_iterator it = this->objects_.begin(); it != this->objects_.end(); ++it)
202        {
203            if (i == index)
204                return (*it);
205            ++i;
206        }
207        return 0;
208    }
209}
Note: See TracBrowser for help on using the repository browser.