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