Changeset 2459 for code/branches/presentation/src/orxonox/objects/Scene.cc
- Timestamp:
- Dec 15, 2008, 12:53:05 AM (16 years ago)
- Location:
- code/branches/presentation
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/presentation
- Property svn:mergeinfo changed
-
code/branches/presentation/src/orxonox/objects/Scene.cc
r2371 r2459 22 22 * Author: 23 23 * Fabian 'x3n' Landau 24 * Reto Grieder (physics) 24 25 * Co-authors: 25 26 * ... … … 35 36 #include <OgreLight.h> 36 37 38 #include "BulletCollision/BroadphaseCollision/btAxisSweep3.h" 39 #include "BulletCollision/CollisionDispatch/btDefaultCollisionConfiguration.h" 40 #include "BulletDynamics/ConstraintSolver/btSequentialImpulseConstraintSolver.h" 41 #include "BulletDynamics/Dynamics/btDiscreteDynamicsWorld.h" 42 37 43 #include "core/CoreIncludes.h" 38 44 #include "core/Core.h" 39 45 #include "core/XMLPort.h" 46 #include "tools/BulletConversions.h" 47 #include "objects/worldentities/WorldEntity.h" 40 48 41 49 namespace orxonox … … 69 77 this->rootSceneNode_ = this->sceneManager_->getRootSceneNode(); 70 78 } 79 80 // No physics yet, XMLPort will do that. 81 const int defaultMaxWorldSize = 100000; 82 this->negativeWorldRange_ = Vector3::UNIT_SCALE * -defaultMaxWorldSize; 83 this->positiveWorldRange_ = Vector3::UNIT_SCALE * defaultMaxWorldSize; 84 this->gravity_ = Vector3::ZERO; 85 this->physicalWorld_ = 0; 86 this->solver_ = 0; 87 this->dispatcher_ = 0; 88 this->collisionConfig_ = 0; 89 this->broadphase_ = 0; 71 90 72 91 // test test test … … 91 110 if (Ogre::Root::getSingletonPtr()) 92 111 { 93 // this->sceneManager_->destroySceneNode(this->rootSceneNode_->getName()); // TODO: remove getName() for newer versions of Ogre94 112 Ogre::Root::getSingleton().destroySceneManager(this->sceneManager_); 95 113 } … … 98 116 delete this->sceneManager_; 99 117 } 118 119 this->setPhysicalWorld(false); 100 120 } 101 121 } … … 109 129 XMLPortParam(Scene, "shadow", setShadow, getShadow, xmlelement, mode).defaultValues(true); 110 130 131 XMLPortParam(Scene, "negativeWorldRange", setNegativeWorldRange, getNegativeWorldRange, xmlelement, mode); 132 XMLPortParam(Scene, "positiveWorldRange", setPositiveWorldRange, getPositiveWorldRange, xmlelement, mode); 133 XMLPortParam(Scene, "hasPhysics", setPhysicalWorld, hasPhysics, xmlelement, mode).defaultValues(true); 134 111 135 XMLPortObjectExtended(Scene, BaseObject, "", addObject, getObject, xmlelement, mode, true, false); 112 136 } … … 114 138 void Scene::registerVariables() 115 139 { 116 registerVariable(this->skybox_, variableDirection::toclient, new NetworkCallback<Scene>(this, &Scene::networkcallback_applySkybox)); 117 registerVariable(this->ambientLight_, variableDirection::toclient, new NetworkCallback<Scene>(this, &Scene::networkcallback_applyAmbientLight)); 140 registerVariable(this->skybox_, variableDirection::toclient, new NetworkCallback<Scene>(this, &Scene::networkcallback_applySkybox)); 141 registerVariable(this->ambientLight_, variableDirection::toclient, new NetworkCallback<Scene>(this, &Scene::networkcallback_applyAmbientLight)); 142 registerVariable(this->negativeWorldRange_, variableDirection::toclient, new NetworkCallback<Scene>(this, &Scene::networkcallback_negativeWorldRange)); 143 registerVariable(this->positiveWorldRange_, variableDirection::toclient, new NetworkCallback<Scene>(this, &Scene::networkcallback_positiveWorldRange)); 144 registerVariable(this->gravity_, variableDirection::toclient, new NetworkCallback<Scene>(this, &Scene::networkcallback_gravity)); 145 registerVariable(this->bHasPhysics_, variableDirection::toclient, new NetworkCallback<Scene>(this, &Scene::networkcallback_hasPhysics)); 146 } 147 148 void Scene::setNegativeWorldRange(const Vector3& range) 149 { 150 if (range.length() < 10.0f) 151 { 152 CCOUT(2) << "Warning: Setting the negative world range to a very small value: " 153 << omni_cast<std::string>(range) << std::endl; 154 } 155 if (this->hasPhysics()) 156 { 157 CCOUT(2) << "Warning: Attempting to set the physical world range at run time. " 158 << "This causes a complete physical reload which might take some time." << std::endl; 159 this->setPhysicalWorld(false); 160 this->negativeWorldRange_ = range; 161 this->setPhysicalWorld(true); 162 } 163 else 164 this->negativeWorldRange_ = range; 165 } 166 167 void Scene::setPositiveWorldRange(const Vector3& range) 168 { 169 if (range.length() < 10.0f) 170 { 171 CCOUT(2) << "Warning: Setting the positive world range to a very small value: " 172 << omni_cast<std::string>(range) << std::endl; 173 } 174 if (this->hasPhysics()) 175 { 176 CCOUT(2) << "Warning: Attempting to set the physical world range at run time. " 177 << "This causes a complete physical reload which might take some time." << std::endl; 178 this->setPhysicalWorld(false); 179 this->positiveWorldRange_ = range; 180 this->setPhysicalWorld(true); 181 } 182 else 183 this->positiveWorldRange_ = range; 184 } 185 186 void Scene::setGravity(const Vector3& gravity) 187 { 188 this->gravity_ = gravity; 189 if (this->hasPhysics()) 190 this->physicalWorld_->setGravity(omni_cast<btVector3>(this->gravity_)); 191 } 192 193 void Scene::setPhysicalWorld(bool wantPhysics) 194 { 195 this->bHasPhysics_ = wantPhysics; 196 if (wantPhysics && !hasPhysics()) 197 { 198 // Note: These are all little known default classes and values. 199 // It would require further investigation to properly dertermine the right choices. 200 this->broadphase_ = new bt32BitAxisSweep3( 201 omni_cast<btVector3>(this->negativeWorldRange_), omni_cast<btVector3>(this->positiveWorldRange_)); 202 this->collisionConfig_ = new btDefaultCollisionConfiguration(); 203 this->dispatcher_ = new btCollisionDispatcher(this->collisionConfig_); 204 this->solver_ = new btSequentialImpulseConstraintSolver; 205 206 this->physicalWorld_ = new btDiscreteDynamicsWorld(this->dispatcher_, this->broadphase_, this->solver_, this->collisionConfig_); 207 this->physicalWorld_->setGravity(omni_cast<btVector3>(this->gravity_)); 208 } 209 else if (!wantPhysics && hasPhysics()) 210 { 211 // Remove all WorldEntities and shove them to the queue since they would still like to be in a physical world. 212 for (std::set<WorldEntity*>::const_iterator it = this->physicalObjects_.begin(); 213 it != this->physicalObjects_.end(); ++it) 214 { 215 this->physicalWorld_->removeRigidBody((*it)->getPhysicalBody()); 216 this->physicalObjectQueue_.insert(*it); 217 } 218 this->physicalObjects_.clear(); 219 220 delete this->physicalWorld_; 221 delete this->solver_; 222 delete this->dispatcher_; 223 delete this->collisionConfig_; 224 delete this->broadphase_; 225 this->physicalWorld_ = 0; 226 this->solver_ = 0; 227 this->dispatcher_ = 0; 228 this->collisionConfig_ = 0; 229 this->broadphase_ = 0; 230 } 231 } 232 233 void Scene::tick(float dt) 234 { 235 if (!Core::showsGraphics()) 236 { 237 // We need to update the scene nodes if we don't render 238 this->rootSceneNode_->_update(true, false); 239 } 240 if (this->hasPhysics()) 241 { 242 // TODO: This here is bad practice! It will slow down the first tick() by ages. 243 // Rather have an initialise() method as well, called by the Level after everything has been loaded. 244 if (this->physicalObjectQueue_.size() > 0) 245 { 246 // Add all scheduled WorldEntities 247 for (std::set<WorldEntity*>::const_iterator it = this->physicalObjectQueue_.begin(); 248 it != this->physicalObjectQueue_.end(); ++it) 249 { 250 this->physicalWorld_->addRigidBody((*it)->getPhysicalBody()); 251 } 252 this->physicalObjectQueue_.clear(); 253 } 254 255 // Note: 60 means that Bullet will do physics correctly down to 1 frames per seconds. 256 // Under that mark, the simulation will "loose time" and get unusable. 257 physicalWorld_->stepSimulation(dt, 60); 258 } 118 259 } 119 260 … … 165 306 } 166 307 167 void Scene::tick(float dt) 168 { 169 if (!Core::showsGraphics()) 170 { 171 // We need to update the scene nodes if we don't render 172 this->rootSceneNode_->_update(true, false); 173 } 308 void Scene::addPhysicalObject(WorldEntity* object) 309 { 310 if (object) 311 { 312 std::set<WorldEntity*>::iterator it = this->physicalObjects_.find(object); 313 if (it != this->physicalObjects_.end()) 314 return; 315 316 this->physicalObjectQueue_.insert(object); 317 } 318 } 319 320 void Scene::removePhysicalObject(WorldEntity* object) 321 { 322 // check queue first 323 std::set<WorldEntity*>::iterator it = this->physicalObjectQueue_.find(object); 324 if (it != this->physicalObjectQueue_.end()) 325 { 326 this->physicalObjectQueue_.erase(it); 327 return; 328 } 329 330 it = this->physicalObjects_.find(object); 331 if (it == this->physicalObjects_.end()) 332 return; 333 this->physicalObjects_.erase(it); 334 335 if (this->hasPhysics()) 336 this->physicalWorld_->removeRigidBody(object->getPhysicalBody()); 174 337 } 175 338 }
Note: See TracChangeset
for help on using the changeset viewer.