Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/physics/src/orxonox/objects/PhysicsTest.cc @ 1924

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

Added little physics test using OgreODE.

  • Property svn:eol-style set to native
File size: 5.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 *      Reto Grieder
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include "OrxonoxStableHeaders.h"
30#include "PhysicsTest.h"
31
32#include <OgreStaticGeometry.h>
33#include <OgreSceneManager.h>
34#include "OgreOde/OgreOde_Core.h"
35#include "util/Convert.h"
36#include "core/CoreIncludes.h"
37#include "core/ConfigValueIncludes.h"
38#include "core/XMLPort.h"
39#include "GraphicsEngine.h"
40
41namespace orxonox
42{
43    CreateFactory(PhysicsTest);
44
45    PhysicsTest::PhysicsTest()
46        : odeWorld_(0)
47        , odeSpace_(0)
48        , odeStepper_(0)
49        , odeGround_(0)   
50        , odeBody_(0)
51        , odeGeom_(0)
52        , sceneNode_(0)
53        , entity_(0)
54        , bRunning_(false)
55    {
56        RegisterObject(PhysicsTest);
57        setConfigValues();
58        ModifyConfigValue(bRunning_, tset, false);
59    }
60
61    PhysicsTest::~PhysicsTest()
62    {
63    }
64
65    void PhysicsTest::setConfigValues()
66    {
67        SetConfigValue(bRunning_, false);
68    }
69
70    /**
71    @brief
72        XML loading and saving.
73    @param
74        xmlelement The XML-element
75    @param
76        loading Loading (true) or saving (false)
77    @return
78        The XML-element
79    */
80    void PhysicsTest::XMLPort(Element& xmlelement, XMLPort::Mode mode)
81    {
82        SUPER(PhysicsTest, XMLPort, xmlelement, mode);
83
84        Ogre::SceneManager* sceneMgr = GraphicsEngine::getInstance().getLevelSceneManager();
85
86        // set up OgreOde
87
88        odeWorld_ = new OgreOde::World(sceneMgr);
89        odeWorld_->setGravity(Vector3(0,-9.80665,0));
90        odeWorld_->setCFM(10e-5);
91        odeWorld_->setERP(0.8);
92        odeWorld_->setAutoSleep(true);
93        odeWorld_->setAutoSleepAverageSamplesCount(10);
94        odeWorld_->setContactCorrectionVelocity(1.0);
95        odeSpace_ = odeWorld_->getDefaultSpace();
96
97
98        // set up stepper
99
100        const Ogre::Real _time_step = 0.5;
101        const Ogre::Real time_scale = Ogre::Real(1.7);
102        const Ogre::Real max_frame_time = Ogre::Real(1.0 / 4);
103        odeStepper_ = new OgreOde::StepHandler(odeWorld_, OgreOde::StepHandler::QuickStep,_time_step,
104            max_frame_time, time_scale);
105
106
107        // Create a hanging crate
108
109        odeGround_ = new OgreOde::InfinitePlaneGeometry(Ogre::Plane(Ogre::Vector3(0,1,0),0),
110            odeWorld_, odeWorld_->getDefaultSpace());
111        // Use a load of meshes to represent the floor
112        int i = 0;
113        Ogre::StaticGeometry* floor;
114        floor = sceneMgr->createStaticGeometry("StaticFloor");
115        floor->setRegionDimensions(Ogre::Vector3(160.0, 100.0, 160.0));
116        // Set the region origin so the center is at 0 world
117        floor->setOrigin(Ogre::Vector3::ZERO);
118        for (Real z = -80.0; z <= 80.0; z += 20.0)
119        {
120            for (Real x = -80.0; x <= 80.0; x += 20.0)
121            {
122                std::string name = std::string("Ground") + convertToString(i++);
123                Ogre::Entity* entity = sceneMgr->createEntity(name, "plane.mesh");
124                entity->setQueryFlags (1<<4);
125                entity->setUserObject(odeGround_);
126                entity->setCastShadows(false);
127                floor->addEntity(entity, Ogre::Vector3(x,0,z));
128            }
129        }
130        floor->build();
131
132
133        // create a plane in x-z dimensions.
134
135        entity_ = sceneMgr->createEntity("crate","crate.mesh");
136        entity_->setQueryFlags (1<<2);
137        sceneNode_ = sceneMgr->getRootSceneNode()->createChildSceneNode("crate");
138        sceneNode_->attachObject(entity_);
139        entity_->setNormaliseNormals(true);
140        entity_->setCastShadows(true);
141
142
143        odeBody_ = new OgreOde::Body(odeWorld_);
144        sceneNode_->attachObject(odeBody_);
145
146       
147        // set size and mass of the crate
148
149        Vector3 size(10.0, 10.0, 10.0);
150        odeMass_ = OgreOde::BoxMass(0.5, size);
151        odeMass_.setDensity(5.0, size);
152        odeGeom_ = new OgreOde::BoxGeometry(size, odeWorld_, odeSpace_);
153        sceneNode_->setScale(size.x * 0.1, size.y * 0.1, size.z * 0.1);
154        odeBody_->setMass(odeMass_);
155        odeGeom_->setBody(odeBody_);
156        entity_->setUserObject(odeGeom_);
157
158        odeBody_->setOrientation(Quaternion(Degree(30.0), Vector3(0,0,0)));
159        odeBody_->setPosition(Vector3(0,120,-20));
160
161    }
162
163    void PhysicsTest::tick(float dt)
164    {
165        // only update physics in a certain interval
166        if (this->bRunning_ && odeStepper_->step(dt))
167            odeWorld_->synchronise();
168    }
169}
Note: See TracBrowser for help on using the repository browser.